diff --git a/lark/lark.py b/lark/lark.py index 071c9d3..05ad9b1 100644 --- a/lark/lark.py +++ b/lark/lark.py @@ -176,6 +176,9 @@ class LarkOptions(Serialize): # These option are only used outside of `load_grammar`. _LOAD_ALLOWED_OPTIONS = {'postlex', 'transformer', 'use_bytes', 'debug', 'g_regex_flags', 'regex', 'propagate_positions', 'tree_class'} +_VALID_PRIORITY_OPTIONS = ('auto', 'normal', 'invert', None) +_VALID_AMBIGUITY_OPTIONS = ('auto', 'resolve', 'explicit', 'forest') + class Lark(Serialize): """Main interface for the library. @@ -274,9 +277,11 @@ class Lark(Serialize): if self.options.priority == 'auto': self.options.priority = 'normal' - assert self.options.priority in ('auto', None, 'normal', 'invert'), 'invalid priority option specified: {}. options are auto, none, normal, invert.'.format(self.options.priority) + if self.options.priority not in _VALID_PRIORITY_OPTIONS: + raise ValueError("invalid priority option: %r. Must be one of %r" % (self.options.priority, _VALID_PRIORITY_OPTIONS)) assert self.options.ambiguity not in ('resolve__antiscore_sum', ), 'resolve__antiscore_sum has been replaced with the option priority="invert"' - assert self.options.ambiguity in ('resolve', 'explicit', 'forest', 'auto', ) + if self.options.ambiguity not in _VALID_AMBIGUITY_OPTIONS: + raise ValueError("invalid ambiguity option: %r. Must be one of %r" % (self.options.ambiguity, _VALID_AMBIGUITY_OPTIONS)) # Parse the grammar file and compose the grammars (TODO) self.grammar = load_grammar(grammar, self.source, re_module, self.options.keep_all_tokens) diff --git a/tests/test_parser.py b/tests/test_parser.py index f31ac00..7a0a181 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -1829,6 +1829,20 @@ def _make_parser_test(LEXER, PARSER): res = l.parse("a") self.assertEqual(res.children[0].data, 'a') + grammar = """ + start: a | b + a.2: "A"+ + b.1: "A"+ "B"? + """ + + l = _Lark(grammar) + res = l.parse("AAAA") + self.assertEqual(res.children[0].data, 'a') + + l = _Lark(grammar, priority="invert") + res = l.parse("AAAA") + self.assertEqual(res.children[0].data, 'b') + @unittest.skipIf(PARSER != 'earley' or LEXER == 'standard', "Currently only Earley supports priority sum in rules")