diff --git a/lark/load_grammar.py b/lark/load_grammar.py index 1f13b5d7..d4f553c5 100644 --- a/lark/load_grammar.py +++ b/lark/load_grammar.py @@ -1050,6 +1050,8 @@ def _make_rule_tuple(modifiers_tree, name, params, priority_tree, expansions): if modifiers_tree.children: m ,= modifiers_tree.children expand1 = '?' in m + if expand1 and name.startswith('_'): + raise GrammarError("Inlined rules (_rule) cannot use the ?rule modifier.") keep_all_tokens = '!' in m else: keep_all_tokens = False diff --git a/tests/test_grammar.py b/tests/test_grammar.py index 78161899..e388cb75 100644 --- a/tests/test_grammar.py +++ b/tests/test_grammar.py @@ -281,6 +281,14 @@ def test_list_grammar_imports(self): imports = list_grammar_imports('%import common.WS', []) assert len(imports) == 1 and imports[0].pkg_name == 'lark' + def test_inline_with_expand_single(self): + grammar = r""" + start: _a + !?_a: "A" + """ + self.assertRaises(GrammarError, Lark, grammar) + + def test_line_breaks(self): p = Lark(r"""start: "a" \ "b"