mirror of https://github.com/lark-parser/lark.git
Change namespace naming and aliasing for imports
The `.` character is invalid as a group name in regex, replacing by `__`. The bug arose in `lexer._build_mres`.
This commit is contained in:
parent
0485a8a5b1
commit
9b22d41e49
|
@ -586,7 +586,7 @@ def import_from_grammar_into_namespace(grammar, namespace, aliases):
|
|||
try:
|
||||
return aliases[name].value
|
||||
except KeyError:
|
||||
return '%s.%s' % (namespace, name)
|
||||
return '%s__%s' % (namespace, name)
|
||||
|
||||
to_import = list(bfs(aliases, rule_dependencies))
|
||||
for symbol in to_import:
|
||||
|
@ -745,7 +745,7 @@ class GrammarLoader:
|
|||
g = import_grammar(grammar_path, base_paths=[base_path])
|
||||
|
||||
aliases_dict = dict(zip(names, aliases))
|
||||
new_td, new_rd = import_from_grammar_into_namespace(g, '.'.join(dotted_path), aliases_dict)
|
||||
new_td, new_rd = import_from_grammar_into_namespace(g, '__'.join(dotted_path), aliases_dict)
|
||||
|
||||
term_defs += new_td
|
||||
rule_defs += new_rd
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
startab: expr
|
||||
|
||||
expr: A B
|
||||
| A expr B
|
||||
|
||||
A: "a"
|
||||
B: "b"
|
||||
|
||||
%import common.WS
|
||||
%ignore WS
|
|
@ -1025,6 +1025,37 @@ def _make_parser_test(LEXER, PARSER):
|
|||
self.assertEqual(x.children, ['12', 'lions'])
|
||||
|
||||
|
||||
def test_relative_rule_import(self):
|
||||
l = _Lark_open('test_relative_rule_import.lark', rel_to=__file__)
|
||||
x = l.parse('xaabby')
|
||||
self.assertEqual(x.children, [
|
||||
'x',
|
||||
Tree('expr', ['a', Tree('expr', ['a', 'b']), 'b']),
|
||||
'y'])
|
||||
|
||||
|
||||
def test_relative_rule_import_drop_ignore(self):
|
||||
# %ignore rules are dropped on import
|
||||
l = _Lark_open('test_relative_rule_import_drop_ignore.lark',
|
||||
rel_to=__file__)
|
||||
self.assertRaises((ParseError, UnexpectedInput),
|
||||
l.parse, 'xa abby')
|
||||
|
||||
|
||||
def test_relative_rule_import_subrule(self):
|
||||
l = _Lark_open('test_relative_rule_import_subrule.lark',
|
||||
rel_to=__file__)
|
||||
x = l.parse('xaabby')
|
||||
self.assertEqual(x.children, [
|
||||
'x',
|
||||
Tree('startab', [
|
||||
Tree('grammars__ab__expr', [
|
||||
'a', Tree('grammars__ab__expr', ['a', 'b']), 'b',
|
||||
]),
|
||||
]),
|
||||
'y'])
|
||||
|
||||
|
||||
def test_multi_import(self):
|
||||
grammar = """
|
||||
start: NUMBER WORD
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
start: X expr Y
|
||||
|
||||
X: "x"
|
||||
Y: "y"
|
||||
|
||||
%import .grammars.ab.expr
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
start: X expr Y
|
||||
|
||||
X: "x"
|
||||
Y: "y"
|
||||
|
||||
%import .grammars.ab.expr
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
start: X startab Y
|
||||
|
||||
X: "x"
|
||||
Y: "y"
|
||||
|
||||
%import .grammars.ab.startab
|
||||
|
Loading…
Reference in New Issue