From e6fc3c9b00306e3a8661210fcc93bf50479ee229 Mon Sep 17 00:00:00 2001 From: MegaIng1 Date: Thu, 16 Jul 2020 18:04:26 +0200 Subject: [PATCH] Added possibility for terminals with different flags to be joined in python3.6+ --- lark/load_grammar.py | 11 +++++++---- tests/test_parser.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lark/load_grammar.py b/lark/load_grammar.py index ee0f1c0..f20feb6 100644 --- a/lark/load_grammar.py +++ b/lark/load_grammar.py @@ -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: diff --git a/tests/test_parser.py b/tests/test_parser.py index def4eca..762c979 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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):