带有提示工具包的自定义 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 (将#修改为@)