你需要的是一个有状态的词法分析器。您的正则表达式不起作用的原因是这些组不连续。
int a=3,b=5,c=4;
在这里,您希望字符 0..2 为 Type、3..3 Space、4..7 Name、Equal Number 和 Comma,然后是 Name、Equal、Number 和 Comma。那不好。
解决方案是记住何时看到类型声明,进入一个新的词法分析器模式,该模式一直持续到下一个分号。请参阅
pygments 文档中的更改状态。
下面是一个使用 CFamilyLexer 并添加三个新词法分析器状态的解决方案。因此,当它在function
状态下看到这样的行时:
int m = 3 * a + b, x = /* comments ; everywhere */ a * a;
首先它消耗:
int
它匹配我添加的新规则,因此进入vardecl
状态:
m
哦,一个变量的名称!由于词法分析器处于vardecl
状态,这是一个新定义的变量。将其作为NameDecl
令牌发出。然后进入varvalue
状态。
3
只是一个数字。
*
只是一个运营商。
a
哦,一个变量的名称!但是现在我们处于varvalue
状态所以它不是一个变量声明,只是一个常规的变量引用。
+ b
一个运算符和另一个变量引用。
,
m
完全声明的变量值。回到vardecl
国家。
x =
新变量声明。
/* comments ; everywhere */
另一个状态被压入堆栈。在注释中,原本有意义的标记会;
被忽略。
a * a
变量的值x
。
;
返回function
状态。特殊变量声明规则完成。
from pygments import highlight
from pygments.formatters import HtmlFormatter, TerminalFormatter
from pygments.formatters.terminal import TERMINAL_COLORS
from pygments.lexer import inherit
from pygments.lexers.compiled import CFamilyLexer
from pygments.token import *
# New token type for variable declarations. Red makes them stand out
# on the console.
NameDecl = Token.NameDecl
STANDARD_TYPES[NameDecl] = 'ndec'
TERMINAL_COLORS[NameDecl] = ('red', 'red')
class CDeclLexer(CFamilyLexer):
tokens = {
# Only touch variables declared inside functions.
'function': [
# The obvious fault that is hard to get around is that
# user-defined types won't be cathed by this regexp.
(r'(?<=\s)(bool|int|long|float|short|double|char|unsigned|signed|void|'
r'[a-z_][a-z0-9_]*_t)\b',
Keyword.Type, 'vardecl'),
inherit
],
'vardecl' : [
(r'\s+', Text),
# Comments
(r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
(r';', Punctuation, '#pop'),
(r'[~!%^&*+=|?:<>/-]', Operator),
# After the name of the variable has been tokenized enter
# a new mode for the value.
(r'[a-zA-Z_][a-zA-Z0-9_]*', NameDecl, 'varvalue'),
],
'varvalue' : [
(r'\s+', Text),
(r',', Punctuation, '#pop'),
(r';', Punctuation, '#pop:2'),
# Comments
(r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
(r'[~!%^&*+=|?:<>/-\[\]]', Operator),
(r'\d+[LlUu]*', Number.Integer),
# Rules for strings and chars.
(r'L?"', String, 'string'),
(r"L?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", String.Char),
(r'[a-zA-Z_][a-zA-Z0-9_]*', Name),
# Getting arrays right is tricky.
(r'{', Punctuation, 'arrvalue'),
],
'arrvalue' : [
(r'\s+', Text),
(r'\d+[LlUu]*', Number.Integer),
(r'}', Punctuation, '#pop'),
(r'[~!%^&*+=|?:<>/-\[\]]', Operator),
(r',', Punctuation),
(r'[a-zA-Z_][a-zA-Z0-9_]*', Name),
(r'{', Punctuation, '#push'),
]
}
code = '''
#include <stdio.h>
void main(int argc, char *argv[])
{
int vec_a, vec_b;
int a = 3, /* Mo;yo */ b=5, c=7;
int m = 3 * a + b, x = /* comments everywhere */ a * a;
char *myst = "hi;there";
char semi = ';';
time_t now = /* Null; */ NULL;
int arr[10] = {1, 2, 9 / c};
int foo[][2] = {{1, 2}};
a = b * 9;
c = 77;
d = (int) 99;
}
'''
for formatter in [TerminalFormatter, HtmlFormatter]:
print highlight(code, CDeclLexer(), formatter())