【发布时间】:2022-12-14 05:35:41
【问题描述】:
我整理了一个应该理想地用于伪代码的词法分析器,但是当我使用python3 -m pygments -x -l ./psuedo.py:PseudoLexer test.pseudo 测试它时,我不断收到以下错误:“在 ./psuedo.py 中找不到有效的伪词法分析器类”。
我查看了我的词法分析器,几乎只是从 C++ 的注释词法分析器和 Python 的关键字词法分析器的示例中获取了很多内容,所以我不明白为什么会出现此错误。
这是代码,如果需要的话:
from pygments.lexer import RegexLexer, bygroups, words
from pygments.token import *
__all__ = ['PsuedoLexer']
class PsuedoLexer(RegexLexer):
"""
Lexer for minted highlighting in psuedocode
"""
name = 'Pseudo'
aliases = ['psuedo']
filenames = ['*.pseudo']
tokens = {
'root' : [
# comments from cpp
(r'[^/]+', Text),
(r'/\*', Comment.Multiline, 'comment'),
(r'//.*?$', Comment.Singleline),
(r'/', Text),
# operators from python
(r'!=|==|<<|>>|:=|[-~+/*%=<>&^|.]', Operator),
(r'[]{}:(),;[]', Punctuation),
(r'(in|is|and|or|not)\b', Operator.Word),
# keywords from python (modified)
(words((
'assert', 'break', 'continue', 'del', 'elif',
'else', 'except', 'finally', 'for', 'if', 'lambda',
'pass', 'return', 'try', 'while', 'as', 'with',
'end', 'repeat', 'do', 'then'), suffix=r'\b'),
Keyword),
(words(('True', 'False', 'None'), suffix=r'\b'), Keyword.Constant)
],
'comment': [
(r'[^*/]+', Comment.Multiline),
(r'/\*', Comment.Multiline, '#push'),
(r'\*/', Comment.Multiline, '#pop'),
(r'[*/]', Comment.Multiline)
]
}
此外,一旦我让这个词法分析器开始工作,我如何在 LaTeX 中全局/在 minted 环境中使用它?
【问题讨论】: