Browse Source

Added support of expansions

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.10.0
MegaIng1 4 years ago
parent
commit
3d3bf69403
2 changed files with 18 additions and 3 deletions
  1. +6
    -3
      lark/load_grammar.py
  2. +12
    -0
      tests/test_parser.py

+ 6
- 3
lark/load_grammar.py View File

@@ -451,9 +451,12 @@ class TerminalTreeToPattern(Transformer):
def expansions(self, exps):
if len(exps) == 1:
return exps[0]
if len({i.flags for i in exps}) > 1:
raise GrammarError("Lark doesn't support joining terminals with conflicting flags!")
return PatternRE('(?:%s)' % ('|'.join(i.to_regexp() for i in exps)), exps[0].flags)
if not Py36:
if len({i.flags for i in exps}) > 1:
raise GrammarError("Lark doesn't support joining terminals with conflicting flags!")
return PatternRE('(?:%s)' % ('|'.join(i.to_regexp() for i in exps)), exps[0].flags)
else:
return PatternRE('(?:%s)' % ('|'.join(i.to_regexp() for i in exps)), ())

def expr(self, args):
inner, op = args[:2]


+ 12
- 0
tests/test_parser.py View File

@@ -1078,6 +1078,18 @@ def _make_parser_test(LEXER, PARSER):
self.assertEqual(g.parse(" ").children,[" "])
self.assertEqual(g.parse("\n ").children,["\n "])
self.assertRaises(UnexpectedCharacters, g.parse, "\n\n")
g = r"""
start: A
A: B | C
B: "b"i
C: "c"
"""
g = _Lark(g)
self.assertEqual(g.parse("b").children,["b"])
self.assertEqual(g.parse("B").children,["B"])
self.assertEqual(g.parse("c").children,["c"])
self.assertRaises(UnexpectedCharacters, g.parse, "C")


def test_lexer_token_limit(self):


Loading…
Cancel
Save