Skip to content

Commit

Permalink
Better error in Lark.parse when using on_error when parser!=lalr (issue
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Sep 1, 2024
1 parent 5e07379 commit 84e9984
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lark/lark.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,8 @@ def parse(self, text: str, start: Optional[str]=None, on_error: 'Optional[Callab
For convenience, these sub-exceptions also inherit from ``ParserError`` and ``LexerError``.
"""
if on_error is not None and self.options.parser != 'lalr':
raise NotImplementedError("The on_error option is only implemented for the LALR(1) parser.")
return self.parser.parse(text, start=start, on_error=on_error)


Expand Down
5 changes: 5 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2584,6 +2584,11 @@ def ignore_errors(e):
s = "[0 1, 2,@, 3,,, 4, 5 6 ]$"
tree = g.parse(s, on_error=ignore_errors)

@unittest.skipIf(PARSER == 'lalr', "test on_error only works with lalr")
def test_on_error_without_lalr(self):
p = _Lark(r"""start: "A" """)
self.assertRaises(NotImplementedError, p.parse, "", on_error=print)

@unittest.skipIf(PARSER != 'lalr', "interactive_parser error handling only works with LALR for now")
def test_iter_parse(self):
ab_grammar = '!start: "a"* "b"*'
Expand Down

0 comments on commit 84e9984

Please sign in to comment.