From 149f7cec1f828358ad74d91bde7ddef63a118159 Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Tue, 6 Nov 2018 16:07:27 +0200 Subject: [PATCH] BUGFIX: Importing the same grammar twice could lead to unexpected behavior (Issue #268) --- lark/load_grammar.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lark/load_grammar.py b/lark/load_grammar.py index 2f48cb1..1966117 100644 --- a/lark/load_grammar.py +++ b/lark/load_grammar.py @@ -526,8 +526,12 @@ def import_grammar(grammar_path, base_paths=[]): return _imported_grammars[grammar_path] def import_from_grammar_into_namespace(grammar, namespace, aliases): + """Returns all rules and terminals of grammar, prepended + with a 'namespace' prefix, except for those which are aliased. + """ + imported_terms = dict(grammar.term_defs) - imported_rules = {n:(n,t,o) for n,t,o in grammar.rule_defs} + imported_rules = {n:(n,deepcopy(t),o) for n,t,o in grammar.rule_defs} term_defs = [] rule_defs = [] @@ -535,7 +539,10 @@ def import_from_grammar_into_namespace(grammar, namespace, aliases): def rule_dependencies(symbol): if symbol.type != 'RULE': return [] - _, tree, _ = imported_rules[symbol] + try: + _, tree, _ = imported_rules[symbol] + except KeyError: + raise GrammarError("Missing symbol '%s' in grammar %s" % (symbol, namespace)) return tree.scan_values(lambda x: x.type in ('RULE', 'TERMINAL')) def get_namespace_name(name):