Browse Source

Added error message for the alias syntax in terminals (Issue #97)

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.5.6
Erez Shinan 6 years ago
parent
commit
255ef0d973
2 changed files with 12 additions and 1 deletions
  1. +6
    -1
      lark/load_grammar.py
  2. +6
    -0
      tests/test_parser.py

+ 6
- 1
lark/load_grammar.py View File

@@ -402,6 +402,8 @@ class TokenTreeToPattern(Transformer):
assert len(args) == 2
return PatternRE('(?:%s)%s' % (inner.to_regexp(), op), inner.flags)

def alias(self, t):
raise GrammarError("Aliasing not allowed in terminals (You used -> in the wrong place)")

def _interleave(l, item):
for e in l:
@@ -455,6 +457,9 @@ class Grammar:
exp.children[i] = Token(sym.type, new_terminal_names[sym])

for name, (tree, priority) in term_defs: # TODO transfer priority to rule?
if any(tree.find_data('alias')):
raise GrammarError("Aliasing not allowed in terminals (You used -> in the wrong place)")

if name.startswith('_'):
options = RuleOptions(filter_out=True, priority=-priority)
else:
@@ -516,7 +521,7 @@ class Grammar:

for expansion, alias in expansions:
if alias and name.startswith('_'):
raise Exception("Rule %s is marked for expansion (it starts with an underscore) and isn't allowed to have aliases (alias=%s)" % (name, alias))
raise GrammarError("Rule %s is marked for expansion (it starts with an underscore) and isn't allowed to have aliases (alias=%s)" % (name, alias))

rule = Rule(name, expansion, alias, options)
compiled_rules.append(rule)


+ 6
- 0
tests/test_parser.py View File

@@ -822,6 +822,12 @@ def _make_parser_test(LEXER, PARSER):
"""
self.assertRaises( GrammarError, _Lark, g)

def test_alias_in_terminal(self):
g = """start: TERM
TERM: "a" -> alias
"""
self.assertRaises( GrammarError, _Lark, g)

@unittest.skipIf(LEXER==None, "TODO: Fix scanless parsing or get rid of it") # TODO
def test_line_and_column(self):
g = r"""!start: "A" bc "D"


Loading…
Cancel
Save