Skip to content

Commit 5faea92

Browse files
authored
Merge pull request #1460 from lark-parser/dev
Better error in Lark.parse when using on_error when parser!=lalr (issue #1311)
2 parents 5e07379 + 84e9984 commit 5faea92

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

lark/lark.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,8 @@ def parse(self, text: str, start: Optional[str]=None, on_error: 'Optional[Callab
652652
For convenience, these sub-exceptions also inherit from ``ParserError`` and ``LexerError``.
653653
654654
"""
655+
if on_error is not None and self.options.parser != 'lalr':
656+
raise NotImplementedError("The on_error option is only implemented for the LALR(1) parser.")
655657
return self.parser.parse(text, start=start, on_error=on_error)
656658

657659

tests/test_parser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,6 +2584,11 @@ def ignore_errors(e):
25842584
s = "[0 1, 2,@, 3,,, 4, 5 6 ]$"
25852585
tree = g.parse(s, on_error=ignore_errors)
25862586

2587+
@unittest.skipIf(PARSER == 'lalr', "test on_error only works with lalr")
2588+
def test_on_error_without_lalr(self):
2589+
p = _Lark(r"""start: "A" """)
2590+
self.assertRaises(NotImplementedError, p.parse, "", on_error=print)
2591+
25872592
@unittest.skipIf(PARSER != 'lalr', "interactive_parser error handling only works with LALR for now")
25882593
def test_iter_parse(self):
25892594
ab_grammar = '!start: "a"* "b"*'

0 commit comments

Comments
 (0)