python - Python3 Antlr4 AttributeError:“CommonToken”对象没有属性“getLine”
问题描述
我将 Antlr4 与 Python3 一起使用。
我有一个getTerminalPos
方法,它返回给定终端的行号和列号的元组。为此,我首先使用获取令牌.getSymbol()
,然后使用.getLine()
and.getCharPositionInLine()
方法获取位置。
def getTerminalPos(self, terminal):
token = terminal.getSymbol()
return (token.getLine(), token.getCharPositionInLine())
getTerminalPos
在 antlr 访问者内部调用的示例:
def visitAtom(self, ctx):
if ctx.LPAREN():
return self.visitExpr(ctx.expr())
if ctx.INT():
return nodes.Number(int(ctx.INT().getText()), getTerminalPos(ctx.INT()))
if ctx.FLOAT():
return nodes.Number(float(ctx.FLOAT().getText()), getTerminalPos(ctx.FLOAT()))
当我运行代码时,我收到以下错误消息:
File ".../py-antlr4-lmaspl/AntlrVisitor.py", line 55, in getTerminalPos
return (token.getLine(), token.getCharPositionInLine())
AttributeError: 'CommonToken' object has no attribute 'getLine'
根据 Antlr4 Java Runtime,这些方法存在:https ://www.antlr.org/api/Java/org/antlr/v4/runtime/CommonToken.html
根据 Antlr3 Python Runtime,这些方法存在:https ://www.antlr3.org/api/Python/classantlr3_1_1_common_token.html
那么,它们也应该存在于 Antlr4 Python Runtime 中吗?
如何修复此错误?我应该使用一组不同的方法来获取行号和列号吗?
编辑:我的意思是说我在这里发现了一个类似的问题:https ://github.com/antlr/antlr4/issues/1529 。它被标记为一个错误,但现在关闭......
解决方案
如果我查看 Python 3 运行时的源代码,我会看到CommonToken
如下所示:
class CommonToken(Token):
# An empty {@link Pair} which is used as the default value of
# {@link #source} for tokens that do not have a source.
EMPTY_SOURCE = (None, None)
def __init__(self, source = EMPTY_SOURCE, type = None, channel=Token.DEFAULT_CHANNEL, start=-1, stop=-1):
super(CommonToken, self).__init__()
self.source = source
self.type = type
self.channel = channel
self.start = start
self.stop = stop
self.tokenIndex = -1
if source[0] is not None:
self.line = source[0].line
self.column = source[0].column
else:
self.column = -1
...
像这样Token
:
class Token (object):
...
def __init__(self):
self.source = None
self.type = None # token type of the token
self.channel = None # The parser ignores everything not on DEFAULT_CHANNEL
self.start = None # optional; return -1 if not implemented.
self.stop = None # optional; return -1 if not implemented.
self.tokenIndex = None # from 0..n-1 of the token object in the input stream
self.line = None # line=1..n of the 1st character
self.column = None # beginning of the line at which it occurs, 0..n-1
self._text = None # text of the token.
...
所以,我的猜测是这应该为你做:
return (token.line, token.column)
推荐阅读
- delphi - Delphi TJvDBGrid 保存布局
- performance - 如何在图像上并行化相同且独立应用的代码(每个核心均匀分布图像)(目标:加速)
- spring-data-jdbc - 使用 Spring Data JDBC 的嵌套数据结构
- elasticsearch - 从查询中删除停用词
- sql - 如何使用旧列的别名对新创建的列进行分组?
- sql-server - Sql Studio (v17) 有哪些新的登录选项,例如“Active Directory - 集成”
- c# - 如何在有关系的两个类之间添加在下拉列表中获取数据的可能性?
- javascript - 环回查询mongodb文档
- javascript - 为什么进度条不可见?
- c# - 在 EF 查询中使用字符串变量而不是文字时出现问题