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

This commit is contained in:
Erez Shinan 2018-04-01 00:06:31 +03:00
parent 1c57445fdc
commit 255ef0d973
2 changed files with 12 additions and 1 deletions

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)

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"