Browse Source

import_grammar now include base_path in recursive call to load_grammar

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.7.1
PJCampi 5 years ago
parent
commit
b055bc6399
5 changed files with 17 additions and 2 deletions
  1. +3
    -2
      lark/load_grammar.py
  2. +4
    -0
      tests/grammars/test_relative_import_of_nested_grammar.lark
  3. +4
    -0
      tests/grammars/test_relative_import_of_nested_grammar__grammar_to_import.lark
  4. +1
    -0
      tests/grammars/test_relative_import_of_nested_grammar__nested_grammar.lark
  5. +5
    -0
      tests/test_parser.py

+ 3
- 2
lark/load_grammar.py View File

@@ -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:


+ 4
- 0
tests/grammars/test_relative_import_of_nested_grammar.lark View File

@@ -0,0 +1,4 @@

start: rule_to_import

%import .test_relative_import_of_nested_grammar__grammar_to_import.rule_to_import

+ 4
- 0
tests/grammars/test_relative_import_of_nested_grammar__grammar_to_import.lark View File

@@ -0,0 +1,4 @@

rule_to_import: NESTED_TERMINAL

%import .test_relative_import_of_nested_grammar__nested_grammar.NESTED_TERMINAL

+ 1
- 0
tests/grammars/test_relative_import_of_nested_grammar__nested_grammar.lark View File

@@ -0,0 +1 @@
NESTED_TERMINAL: "N"

+ 5
- 0
tests/test_parser.py View File

@@ -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


Loading…
Cancel
Save