[英]Python3 Antlr4 AttributeError: 'CommonToken' object has no attribute 'getLine'
I'm using Antlr4 with Python3.我在 Python3 中使用 Antlr4。
I have a getTerminalPos
method which returns a tuple of the line and column number of a given terminal.我有一个
getTerminalPos
方法,它返回给定终端的行号和列号的元组。 To do this, I first get the token using .getSymbol()
and I then use the .getLine()
and .getCharPositionInLine()
methods to get the positions.为此,我首先使用
.getSymbol()
获取令牌,然后使用.getLine()
和.getCharPositionInLine()
方法获取位置。
def getTerminalPos(self, terminal):
token = terminal.getSymbol()
return (token.getLine(), token.getCharPositionInLine())
An example of calling getTerminalPos
inside an antlr visitor:在 antlr 访问者中调用
getTerminalPos
的示例:
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()))
When I run the code, I get the following error message:当我运行代码时,我收到以下错误消息:
File ".../py-antlr4-lmaspl/AntlrVisitor.py", line 55, in getTerminalPos
return (token.getLine(), token.getCharPositionInLine())
AttributeError: 'CommonToken' object has no attribute 'getLine'
According to the Antlr4 Java Runtime, these methods exist: https://www.antlr.org/api/Java/org/antlr/v4/runtime/CommonToken.html根据 Antlr4 Java Runtime,这些方法存在: https ://www.antlr.org/api/Java/org/antlr/v4/runtime/CommonToken.html
According to the Antlr3 Python Runtime, these methods exist: https://www.antlr3.org/api/Python/classantlr3_1_1_common_token.html根据 Antlr3 Python Runtime,这些方法存在: https : //www.antlr3.org/api/Python/classantlr3_1_1_common_token.html
So, they should exist for the Antlr4 Python Runtime too?那么,它们也应该存在于 Antlr4 Python 运行时吗?
How do I fix this error?我该如何解决这个错误? Is there a different set of methods I should use instead to get the line and column numbers?
我应该使用一组不同的方法来获取行号和列号吗?
Edit : I meant to say that I found a similar issue here: https://github.com/antlr/antlr4/issues/1529 .编辑:我的意思是说我在这里发现了一个类似的问题: https : //github.com/antlr/antlr4/issues/1529 。 It is marked as a bug, but closed for now...
它被标记为错误,但暂时关闭...
If I look at the source of the Python 3 runtime, I see CommonToken
like this :如果我查看 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
...
and Token
like this:和
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.
...
So, my guess is this should do it for you:所以,我猜这应该为你做:
return (token.line, token.column)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.