From b055bc6399de13d8dcc00992e22945ac9de3da32 Mon Sep 17 00:00:00 2001 From: PJCampi Date: Wed, 10 Apr 2019 10:36:25 +0200 Subject: [PATCH] import_grammar now include base_path in recursive call to load_grammar --- lark/load_grammar.py | 5 +++-- tests/grammars/test_relative_import_of_nested_grammar.lark | 4 ++++ ...relative_import_of_nested_grammar__grammar_to_import.lark | 4 ++++ ...st_relative_import_of_nested_grammar__nested_grammar.lark | 1 + tests/test_parser.py | 5 +++++ 5 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 tests/grammars/test_relative_import_of_nested_grammar.lark create mode 100644 tests/grammars/test_relative_import_of_nested_grammar__grammar_to_import.lark create mode 100644 tests/grammars/test_relative_import_of_nested_grammar__nested_grammar.lark diff --git a/lark/load_grammar.py b/lark/load_grammar.py index 3608fcb..2d34e4d 100644 --- a/lark/load_grammar.py +++ b/lark/load_grammar.py @@ -562,9 +562,10 @@ def import_grammar(grammar_path, base_paths=[]): import_paths = base_paths + IMPORT_PATHS for import_path in import_paths: with suppress(IOError): - with open(os.path.join(import_path, grammar_path)) as f: + joined_path = os.path.join(import_path, grammar_path) + with open(joined_path) as f: text = f.read() - grammar = load_grammar(text, grammar_path) + grammar = load_grammar(text, joined_path) _imported_grammars[grammar_path] = grammar break else: diff --git a/tests/grammars/test_relative_import_of_nested_grammar.lark b/tests/grammars/test_relative_import_of_nested_grammar.lark new file mode 100644 index 0000000..1af5953 --- /dev/null +++ b/tests/grammars/test_relative_import_of_nested_grammar.lark @@ -0,0 +1,4 @@ + +start: rule_to_import + +%import .test_relative_import_of_nested_grammar__grammar_to_import.rule_to_import \ No newline at end of file diff --git a/tests/grammars/test_relative_import_of_nested_grammar__grammar_to_import.lark b/tests/grammars/test_relative_import_of_nested_grammar__grammar_to_import.lark new file mode 100644 index 0000000..6a40e2b --- /dev/null +++ b/tests/grammars/test_relative_import_of_nested_grammar__grammar_to_import.lark @@ -0,0 +1,4 @@ + +rule_to_import: NESTED_TERMINAL + +%import .test_relative_import_of_nested_grammar__nested_grammar.NESTED_TERMINAL diff --git a/tests/grammars/test_relative_import_of_nested_grammar__nested_grammar.lark b/tests/grammars/test_relative_import_of_nested_grammar__nested_grammar.lark new file mode 100644 index 0000000..2d4a2a8 --- /dev/null +++ b/tests/grammars/test_relative_import_of_nested_grammar__nested_grammar.lark @@ -0,0 +1 @@ +NESTED_TERMINAL: "N" diff --git a/tests/test_parser.py b/tests/test_parser.py index 92f2f06..c5c9027 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -1104,6 +1104,11 @@ def _make_parser_test(LEXER, PARSER): x = l.parse('Ax') self.assertEqual(next(x.find_data('c')).children, ['A']) + def test_relative_import_of_nested_grammar(self): + l = _Lark_open("grammars/test_relative_import_of_nested_grammar.lark", rel_to=__file__) + x = l.parse('N') + self.assertEqual(next(x.find_data('rule_to_import')).children, ['N']) + def test_import_errors(self): grammar = """ start: NUMBER WORD