Browse Source

Sort Options inside a TerminalTree

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.12.0
MegaIng 3 years ago
parent
commit
e0889a3cf3
2 changed files with 5 additions and 3 deletions
  1. +4
    -0
      lark/load_grammar.py
  2. +1
    -3
      tests/test_grammar.py

+ 4
- 0
lark/load_grammar.py View File

@@ -614,6 +614,10 @@ class TerminalTreeToPattern(Transformer_NonRecursive):
if len(exps) == 1:
return exps[0]

# Do a bit of sorting to make sure that the longest option is returned
# (Python's re module otherwise prefers just 'l' when given (l|ll) and both could match)
exps.sort(key=lambda x: (-x.max_width, -x.min_width, -len(x.value)))

pattern = '(?:%s)' % ('|'.join(i.to_regexp() for i in exps))
return _make_joined_pattern(pattern, {i.flags for i in exps})



+ 1
- 3
tests/test_grammar.py View File

@@ -247,10 +247,8 @@ class TestGrammar(TestCase):
self.assertRaises(UnexpectedInput, l.parse, u'A' * 8192)

def test_large_terminal(self):
# TODO: The `reversed` below is required because otherwise the regex engine is happy
# with just parsing 9 from the string 999 instead of consuming the longest
g = "start: NUMBERS\n"
g += "NUMBERS: " + '|'.join('"%s"' % i for i in reversed(range(0, 1000)))
g += "NUMBERS: " + '|'.join('"%s"' % i for i in range(0, 1000))

l = Lark(g, parser='lalr')
for i in (0, 9, 99, 999):


Loading…
Cancel
Save