雷克瑟一代
lexit的Python项目详细描述
Lexit
LExchange是使用Python 3.6编写的开源词表生成器,使用了新的特性,如^ {CD1>}、类型提示和^ {CD2>}挂钩。
json lexer示例
fromlexitimportLexerclassJsonLexer(Lexer):NUMBER=r'-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?'STRING=r'"(\\\"|\\\\|[^"\n])*?"i?'L_BRACE=r'{'R_BRACE=r'}'L_BRACKET=r'\['R_BRACKET=r'\]'TRUE=r'true'FALSE=r'false'NULL=r'null'COMMA=r','COLON=r':'ignore=r'\s+'tokens=list(JsonLexer.lex('{"hello": "world"}'))
要求
- 唯一的要求是python3.6+
- 用于测试
pytest
库
安装
pip install lexit
错误处理
try:
tokens = list(JsonLexer.lex('${"hello": "world"}'))
except LexerError as e:
print(e.pretty())
exit(1)
# Will produce the following output:
No match for character '$' in line 1 column 1
${"hello": "world"}
^
设计决策
- 应该易于使用
- 最长匹配优先级(
++
总是胜过+
,尽管在lexer类中定义令牌的顺序不同) - 对人类的自我描述错误(应该很明显发生了什么以及何时发生)