From d5c9006f01b3fa2fa0bb05b403b9ee641c3535da Mon Sep 17 00:00:00 2001 From: Kyungdahm Yun Date: Sat, 23 Feb 2019 15:32:31 -0800 Subject: [PATCH] Fix literal range escape assertion --- lark/load_grammar.py | 2 +- tests/test_parser.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lark/load_grammar.py b/lark/load_grammar.py index 2d5d547..f95d0fa 100644 --- a/lark/load_grammar.py +++ b/lark/load_grammar.py @@ -389,7 +389,7 @@ class PrepareLiterals(Transformer_InPlace): assert start.type == end.type == 'STRING' start = start.value[1:-1] end = end.value[1:-1] - assert len(start) == len(end) == 1, (start, end, len(start), len(end)) + assert len(_fix_escaping(start)) == len(_fix_escaping(end)) == 1, (start, end, len(_fix_escaping(start)), len(_fix_escaping(end))) regexp = '[%s-%s]' % (start, end) return ST('pattern', [PatternRE(regexp)]) diff --git a/tests/test_parser.py b/tests/test_parser.py index 68514a1..a61fc37 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -456,6 +456,18 @@ def _make_parser_test(LEXER, PARSER): """) g.parse('\x01\x02\xABCD') + def test_unicode_literal_range_escape(self): + g = _Lark(r"""start: A+ + A: "\u0061".."\u0063" + """) + g.parse('abc') + + def test_hex_literal_range_escape(self): + g = _Lark(r"""start: A+ + A: "\x01".."\x03" + """) + g.parse('\x01\x02\x03') + @unittest.skipIf(PARSER == 'cyk', "Takes forever") def test_stack_for_ebnf(self): """Verify that stack depth isn't an issue for EBNF grammars"""