Browse Source

Allow renaming relative import rule

%import .local.foo -> bar
tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.6.6
Rogdham 6 years ago
parent
commit
2f6b97cfd3
4 changed files with 42 additions and 1 deletions
  1. +1
    -1
      lark/load_grammar.py
  2. +25
    -0
      tests/test_parser.py
  3. +7
    -0
      tests/test_relative_rule_import_rename.lark
  4. +9
    -0
      tests/test_relative_rule_import_subrule_no_conflict.lark

+ 1
- 1
lark/load_grammar.py View File

@@ -139,7 +139,7 @@ RULES = {
'declare': ['_DECLARE _declare_args _NL'],
'import': ['_IMPORT _import_path _NL',
'_IMPORT _import_path _LPAR name_list _RPAR _NL',
'_IMPORT _import_path _TO TERMINAL _NL'],
'_IMPORT _import_path _TO name _NL'],

'_import_path': ['import_lib', 'import_rel'],
'import_lib': ['_import_args'],


+ 25
- 0
tests/test_parser.py View File

@@ -1056,6 +1056,31 @@ def _make_parser_test(LEXER, PARSER):
'y'])


def test_relative_rule_import_subrule_no_conflict(self):
l = _Lark_open(
'test_relative_rule_import_subrule_no_conflict.lark',
rel_to=__file__)
x = l.parse('xaby')
self.assertEqual(x.children, [Tree('expr', [
'x',
Tree('startab', [
Tree('grammars__ab__expr', ['a', 'b']),
]),
'y'])])
self.assertRaises((ParseError, UnexpectedInput),
l.parse, 'xaxabyby')


def test_relative_rule_import_rename(self):
l = _Lark_open('test_relative_rule_import_rename.lark',
rel_to=__file__)
x = l.parse('xaabby')
self.assertEqual(x.children, [
'x',
Tree('ab', ['a', Tree('ab', ['a', 'b']), 'b']),
'y'])


def test_multi_import(self):
grammar = """
start: NUMBER WORD


+ 7
- 0
tests/test_relative_rule_import_rename.lark View File

@@ -0,0 +1,7 @@
start: X ab Y

X: "x"
Y: "y"

%import .grammars.ab.expr -> ab


+ 9
- 0
tests/test_relative_rule_import_subrule_no_conflict.lark View File

@@ -0,0 +1,9 @@
start: expr

expr: X startab Y

X: "x"
Y: "y"

%import .grammars.ab.startab


Loading…
Cancel
Save