在创建了 JavaLexer 的“复制、粘贴和修改”解决方案后,因为我真的不会说 Python,我设法破解了另一个 quick'n'dirty 解决方案,它继承了 JavaLexer 的子类并将词法分析委托给它大部分。例外是
- AspectJ 特定的关键字,
- 处理类型间声明后跟不带空格的冒号不是作为 Java 标签,而是作为 AspectJ 关键字加上“:”运算符和
- 将类型间注释声明作为 AspectJ 关键字处理,而不是作为 Java 名称装饰器。
我确信我的小启发式解决方案遗漏了一些细节,但正如 Andrew Eisenberg 所说:一个不完美但可行的解决方案比一个不存在的完美解决方案要好:
class AspectJLexer(JavaLexer):
"""
For `AspectJ <http://www.eclipse.org/aspectj/>`_ source code.
"""
name = 'AspectJ'
aliases = ['aspectj']
filenames = ['*.aj']
mimetypes = ['text/x-aspectj']
aj_keywords = [
'aspect', 'pointcut', 'privileged', 'call', 'execution',
'initialization', 'preinitialization', 'handler', 'get', 'set',
'staticinitialization', 'target', 'args', 'within', 'withincode',
'cflow', 'cflowbelow', 'annotation', 'before', 'after', 'around',
'proceed', 'throwing', 'returning', 'adviceexecution', 'declare',
'parents', 'warning', 'error', 'soft', 'precedence', 'thisJoinPoint',
'thisJoinPointStaticPart', 'thisEnclosingJoinPointStaticPart',
'issingleton', 'perthis', 'pertarget', 'percflow', 'percflowbelow',
'pertypewithin', 'lock', 'unlock', 'thisAspectInstance'
]
aj_inter_type = ['parents:', 'warning:', 'error:', 'soft:', 'precedence:']
aj_inter_type_annotation = ['@type', '@method', '@constructor', '@field']
def get_tokens_unprocessed(self, text):
for index, token, value in JavaLexer.get_tokens_unprocessed(self, text):
if token is Name and value in self.aj_keywords:
yield index, Keyword, value
elif token is Name.Label and value in self.aj_inter_type:
yield index, Keyword, value[:-1]
yield index, Operator, value[-1]
elif token is Name.Decorator and value in self.aj_inter_type_annotation:
yield index, Keyword, value
else:
yield index, token, value