-
Quick question before I try to boil this down to a minimal reproduction. I am trying to gracefully handle certain errors when parsing my language, and I hit a case where
Are there any cases where |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I don't think there's a good reason that get_context should fail like that. If I had to guess, it's probably that the postlexer isn't assigning the pos_in_stream to the dedent tokens.Maybe it should instead assign the positions of the previous token. |
Beta Was this translation helpful? Give feedback.
-
Here's a reproduction derived from the indented tree example: from lark import Lark
from lark.indenter import Indenter
from lark.exceptions import UnexpectedInput
tree_grammar = r"""
%import common.CNAME -> NAME
%import common.WS_INLINE
%import common.SH_COMMENT
%import common.ESCAPED_STRING
%ignore WS_INLINE
%ignore SH_COMMENT
%declare _INDENT _DEDENT
?start: _NL* tree
tree: NAME _LPAR ESCAPED_STRING _RPAR _NL [_INDENT tree+ _DEDENT]
_NL: (/\r?\n[\t ]*/ | SH_COMMENT)+
_LPAR: "("
_RPAR: ")"
"""
class TreeIndenter(Indenter):
NL_type = '_NL'
OPEN_PAREN_types = ['_LPAR']
CLOSE_PAREN_types = ['_RPAR']
INDENT_type = '_INDENT'
DEDENT_type = '_DEDENT'
tab_len = 8
parser = Lark(tree_grammar, parser='lalr', postlex=TreeIndenter())
test_tree = """
a("hello")
b("poop"
"""
def test():
try:
print(parser.parse(test_tree).pretty())
except UnexpectedInput as e:
print(e.get_context(test_tree))
raise
if __name__ == '__main__':
test() Running this yields:
Assuming this is a bug, should we somehow convert this discussion into an issue? |
Beta Was this translation helpful? Give feedback.
Yes, it makes sense.
But anyway I just created a PR that fixes it: #1547