pythontatsu

Is it possible to continue parsing with other rules after an exception is raised in semantics?


Is it possible to fallback to other rules after an exception is raised in a semantic action? Like the following (contrived) scenario:

var = /[a-zA-Z]+/;
keyword = 'for' | 'in';
a = var:var | keyword:keyword;

def a(ast):
    if (ast.var not in symbolTable):
        raise Exception()

and when the exception is raised, parsing continues with the 'keyword' rule. I am aware of the @@keyword feature, but I want to declare keywords at runtime (my parser is for a programming language with user-defined operators).


Solution

  • If the semantics code raises tatsu.exceptions.FailedSemantics, the exception will be treated exactly as a ParseException, so the normal parsing control-flow will be resumed.