您需要的是一个有状态的词法分析器。你的正则表达式不会的原因
工作是因为组不连续。
int a=3,b=5,c=4;
在这里,您希望字符 0..2 为 Type、3..3 Space、4..7 Name、Equal
数字和逗号,然后是名称、相等、数字和逗号。那不是
好的。
解决方案是记住何时看到了类型声明,
输入一个新的词法分析器模式,该模式一直持续到下一个分号。看
Changing states 在 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 */
另一个状态被压入堆栈。在 cmets 令牌中
否则有;之类的意义就被忽略了。
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())