为clang添加中文关键字

简介: 通过修改clang编译器源码, 使之支持中文关键词. Add Chinese keywords to clang compiler.

知乎原文地址 作者:@狗屎咖啡

以clang为基础做了一个与C++、C兼容中英文关键字的编译器

swizl/cnlang

原理就是加与英文关键字等效的中文关键字

在/include/clang/Basic/TokenKinds.def 中

用ALIAS添加中文关键字

例如

KEYWORD(char, KEYALL)
ALIAS("字", char, KEYALL)

在 /lib/Basic/IdentifierTable.cpp 中

加个头文件 #include "llvm/ADT/StringSwitch.h"

在 tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const 里

用 llvm::StringSwitch 添加 中文宏

例如

//default: return tok::pp_not_keyword;
default: return llvm::StringSwitch<tok::PPKeywordKind>(Name)
.Case("如", tok::pp_if)
.Case("定义", tok::pp_define)
.Default(tok::pp_not_keyword);

用上面的方式将中文关键字,编译完成后,就可以编译中文源代码了。

llvm/clang的编译方法参照官网。

2017-11-17

相关文章
|
2月前
|
存储 程序员 C语言
Python变量命名规则
Python变量命名规则
35 0
|
3月前
|
PHP Ruby Python
醒醒!Python已经支持中文变量名啦!
醒醒!Python已经支持中文变量名啦!
29 2
|
4月前
|
Python
Python删除文件中的中文及中文符号
Python删除文件中的中文及中文符号
|
8月前
|
存储 Python
Python的变量命名规则是什么?
Python的变量命名规则是什么?
121 0
|
6月前
|
程序员 开发者 Python
05 python - 标示符和关键字
05 python - 标示符和关键字
25 0
|
8月前
|
Python
Python中的标识符和保留字
Python中的标识符和保留字
21 0
|
6月前
|
Python
Python中的保留字与标识符
Python中的保留字与标识符
35 1
|
12月前
|
C语言
C语言正则匹配库(regex.h)
C语言正则匹配库(regex.h)
278 0
|
Python
python中的标识符与关键字
python中的标识符与关键字,区分不同的字符,在自定义变量时不能使用Python中含有的关键字