소스 검색

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 3 년 전
committed by GitHub
부모
커밋
066fe8f325
No known key found for this signature in database GPG 키 ID: 4AEE18F83AFDEB23
2개의 변경된 파일5개의 추가작업 그리고 3개의 파일을 삭제
  1. +4
    -0
      lark/load_grammar.py
  2. +1
    -3
      tests/test_grammar.py

+ 4
- 0
lark/load_grammar.py 파일 보기

@@ -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 파일 보기

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


불러오는 중...
취소
저장