From c44bb3e0cda6c4c45c1df68fe70ae513a4d8de67 Mon Sep 17 00:00:00 2001 From: MegaIng Date: Tue, 22 Jun 2021 22:21:34 +0200 Subject: [PATCH] Raise a GrammarError on empty string literal. --- lark/load_grammar.py | 3 +++ tests/test_grammar.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/lark/load_grammar.py b/lark/load_grammar.py index dcb4c81..7b38a74 100644 --- a/lark/load_grammar.py +++ b/lark/load_grammar.py @@ -448,6 +448,9 @@ def _literal_to_pattern(literal): s = eval_escaping(x) + if s == "": + raise GrammarError("Can't have empty terminals (offending literal: %s)" % literal.value) + if literal.type == 'STRING': s = s.replace('\\\\', '\\') return PatternStr(s, flags, raw=literal.value) diff --git a/tests/test_grammar.py b/tests/test_grammar.py index 6a1aefa..a643117 100644 --- a/tests/test_grammar.py +++ b/tests/test_grammar.py @@ -22,6 +22,10 @@ class TestGrammar(TestCase): else: assert False, "example did not raise an error" + def test_empty_literal(self): + # Issues #888 + self.assertRaises(GrammarError, Lark, "start: \"\"") + def test_override_rule(self): # Overrides the 'sep' template in existing grammar to add an optional terminating delimiter # Thus extending it beyond its original capacity