Python3 Antlr4 AttributeError: 'CommonToken' object has no attribute 'getLine'
Python3 Antlr4 AttributeError: 'CommonToken' object has no attribute 'getLine'
我在 Python3.
中使用 Antlr4
我有一个 getTerminalPos
方法,它 returns 给定终端的行号和列号的元组。为此,我首先使用 .getSymbol()
获取令牌,然后使用 .getLine()
和 .getCharPositionInLine()
方法获取位置。
def getTerminalPos(self, terminal):
token = terminal.getSymbol()
return (token.getLine(), token.getCharPositionInLine())
在 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()))
当我 运行 代码时,我收到以下错误消息:
File ".../py-antlr4-lmaspl/AntlrVisitor.py", line 55, in getTerminalPos
return (token.getLine(), token.getCharPositionInLine())
AttributeError: 'CommonToken' object has no attribute 'getLine'
根据 Antlr4 Java 运行时,存在这些方法:https://www.antlr.org/api/Java/org/antlr/v4/runtime/CommonToken.html
根据 Antlr3 Python 运行时,存在这些方法:https://www.antlr3.org/api/Python/classantlr3_1_1_common_token.html
那么,对于 Antlr4 Python 运行时它们也应该存在吗?
如何修复此错误?我应该使用一组不同的方法来获取行号和列号吗?
编辑:我的意思是我在这里发现了一个类似的问题:https://github.com/antlr/antlr4/issues/1529。它被标记为错误,但暂时关闭...
如果我查看 Python 3 运行时的源代码,我会看到 CommonToken
like this:
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)
我在 Python3.
中使用 Antlr4我有一个 getTerminalPos
方法,它 returns 给定终端的行号和列号的元组。为此,我首先使用 .getSymbol()
获取令牌,然后使用 .getLine()
和 .getCharPositionInLine()
方法获取位置。
def getTerminalPos(self, terminal):
token = terminal.getSymbol()
return (token.getLine(), token.getCharPositionInLine())
在 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()))
当我 运行 代码时,我收到以下错误消息:
File ".../py-antlr4-lmaspl/AntlrVisitor.py", line 55, in getTerminalPos
return (token.getLine(), token.getCharPositionInLine())
AttributeError: 'CommonToken' object has no attribute 'getLine'
根据 Antlr4 Java 运行时,存在这些方法:https://www.antlr.org/api/Java/org/antlr/v4/runtime/CommonToken.html
根据 Antlr3 Python 运行时,存在这些方法:https://www.antlr3.org/api/Python/classantlr3_1_1_common_token.html
那么,对于 Antlr4 Python 运行时它们也应该存在吗?
如何修复此错误?我应该使用一组不同的方法来获取行号和列号吗?
编辑:我的意思是我在这里发现了一个类似的问题:https://github.com/antlr/antlr4/issues/1529。它被标记为错误,但暂时关闭...
如果我查看 Python 3 运行时的源代码,我会看到 CommonToken
like this:
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)