From 84e9984e218272dd9a05532022192f18f84febd8 Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Sun, 1 Sep 2024 11:14:38 +0200 Subject: [PATCH] Better error in Lark.parse when using on_error when parser!=lalr (issue #1311) --- lark/lark.py | 2 ++ tests/test_parser.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/lark/lark.py b/lark/lark.py index 7ae1f240..0bec71bb 100644 --- a/lark/lark.py +++ b/lark/lark.py @@ -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) diff --git a/tests/test_parser.py b/tests/test_parser.py index 98290710..36a9cc63 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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"*'