How do I do error handling with Grako?
EBNF (MyGrammar.ebnf):
pattern = { tag | function }* ;
tag = tag:( "%" name:id "%" );
function = function:("$" name:id "()" );
id = ?/([^\\%$,()=])+/? ;
I'm generating the parser with
python -m grako --whitespace '' MyGrammar.ebnf > my_parser.py
Parsing an empty string and a "bad" string (that could not be matched by the grammar) both results to an empty AST Closure.
parser = MyGrammarParser()
ast = parser.parse(u"%test%", rule_name='pattern') #ast contains something
ast = parser.parse(u"", rule_name='pattern') #ast = []
ast = parser.parse(u"$bad $test", rule_name='pattern') #ast = []
And additionally: Is there any error message like 'expected foo at position 123'?