微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有提示工具包的自定义 Pygments Lexer 引发错误

如何解决带有提示工具包的自定义 Pygments Lexer 引发错误

我一直在尝试使用以下示例来使用提示工具包:

from pygments.lexer import RegexLexer
from pygments.token import *

class CalculatorLexer(RegexLexer):
    name = 'Calculator'

    tokens = {
        'root': [
            (r"[\+\-\/\*\(\)\%\^]",Operator),# All the single character operators
            (r"\d*\.\d*",Number.Float),# A Decimal
            (r"\d*",Number.Integer),# A Integer
            (r".*",Text)  # Any Other Thing
        ]
    }

from prompt_toolkit.shortcuts import prompt
from prompt_toolkit.lexers import PygmentsLexer

text = prompt('Enter HTML: ',lexer=PygmentsLexer(CalculatorLexer()))
print('You said: %s' % text)

但它总是引发此错误

Traceback (most recent call last):
  File "test.py",line 21,in <module>
    text = prompt('Enter HTML: ',lexer=PygmentsLexer(CalculatorLexer()))
  File "/home/xcodz/.local/lib/python3.8/site-packages/prompt_toolkit/lexers/pygments.py",line 197,in __init__
    self.pygments_lexer = pygments_lexer_cls(
TypeError: 'CalculatorLexer' object is not callable

起初我认为我不应该调用我创建的自定义词法分析器类。但是如果我不初始化我的自定义类,程序什么也不做,RAM 使用量立即开始上升。由于这种行为,我的电脑已经崩溃了 2 次。

这是我启动并终止程序进程后发生的情况:

System Monitor Image

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。