Quellcode durchsuchen

Merge pull request #980 from MegaIng/sort-inside-terminals

Sort Options inside a TerminalTree
tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.12.0
Erez Shinan vor 3 Jahren
committed by GitHub
Ursprung
Commit
066fe8f325
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden GPG-Schlüssel-ID: 4AEE18F83AFDEB23
2 geänderte Dateien mit 5 neuen und 3 gelöschten Zeilen
  1. +4
    -0
      lark/load_grammar.py
  2. +1
    -3
      tests/test_grammar.py

+ 4
- 0
lark/load_grammar.py Datei anzeigen

@@ -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 Datei anzeigen

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


Laden…
Abbrechen
Speichern