Browse Source

Better error message for options. More tests for priority

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.11.0
Erez Sh 4 years ago
parent
commit
b5aeb812e0
2 changed files with 21 additions and 2 deletions
  1. +7
    -2
      lark/lark.py
  2. +14
    -0
      tests/test_parser.py

+ 7
- 2
lark/lark.py View File

@@ -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)


+ 14
- 0
tests/test_parser.py View File

@@ -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")


Loading…
Cancel
Save