Browse Source

Added possibility for terminals with different flags to be joined in python3.6+

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

+ 7
- 4
lark/load_grammar.py View File

@@ -5,7 +5,7 @@ import sys
from copy import copy, deepcopy
from io import open

from .utils import bfs, eval_escaping
from .utils import bfs, eval_escaping, Py36
from .lexer import Token, TerminalDef, PatternStr, PatternRE

from .parse_tree_builder import ParseTreeBuilder
@@ -441,9 +441,12 @@ class TerminalTreeToPattern(Transformer):
assert items
if len(items) == 1:
return items[0]
if len({i.flags for i in items}) > 1:
raise GrammarError("Lark doesn't support joining terminals with conflicting flags!")
return PatternRE(''.join(i.to_regexp() for i in items), items[0].flags if items else ())
if not Py36:
if len({i.flags for i in items}) > 1:
raise GrammarError("Lark doesn't support joining terminals with conflicting flags in python <3.6!")
return PatternRE(''.join(i.to_regexp() for i in items), items[0].flags if items else ())
else:
return PatternRE(''.join(i.to_regexp() for i in items), ())

def expansions(self, exps):
if len(exps) == 1:


+ 16
- 0
tests/test_parser.py View File

@@ -7,6 +7,9 @@ import logging
import os
import sys
from copy import copy, deepcopy

from lark.utils import Py36

try:
from cStringIO import StringIO as cStringIO
except ImportError:
@@ -1062,6 +1065,19 @@ def _make_parser_test(LEXER, PARSER):
g = _Lark(g)
self.assertEqual( g.parse('"hello"').children, ['"hello"'])
self.assertEqual( g.parse("'hello'").children, ["'hello'"])
@unittest.skipIf(not Py36, "Required re syntax only exists in python3.6+")
def test_join_regex_flags(self):
g = r"""
start: A
A: B C
B: /./s
C: /./
"""
g = _Lark(g)
self.assertEqual(g.parse(" ").children,[" "])
self.assertEqual(g.parse("\n ").children,["\n "])
self.assertRaises(UnexpectedCharacters, g.parse, "\n\n")


def test_lexer_token_limit(self):


Loading…
Cancel
Save