Search in sources :

Example 1 with RuleActionFunction

use of org.antlr.v4.codegen.model.RuleActionFunction in project antlr4 by antlr.

the class OutputModelController method buildLexerRuleActions.

public void buildLexerRuleActions(Lexer lexer, final Rule r) {
    if (r.actions.isEmpty()) {
        return;
    }
    CodeGenerator gen = delegate.getGenerator();
    Grammar g = delegate.getGrammar();
    String ctxType = gen.getTarget().getRuleFunctionContextStructName(r);
    RuleActionFunction raf = lexer.actionFuncs.get(r);
    if (raf == null) {
        raf = new RuleActionFunction(delegate, r, ctxType);
    }
    for (ActionAST a : r.actions) {
        if (a instanceof PredAST) {
            PredAST p = (PredAST) a;
            RuleSempredFunction rsf = lexer.sempredFuncs.get(r);
            if (rsf == null) {
                rsf = new RuleSempredFunction(delegate, r, ctxType);
                lexer.sempredFuncs.put(r, rsf);
            }
            rsf.actions.put(g.sempreds.get(p), new Action(delegate, p));
        } else if (a.getType() == ANTLRParser.ACTION) {
            raf.actions.put(g.lexerActions.get(a), new Action(delegate, a));
        }
    }
    if (!raf.actions.isEmpty() && !lexer.actionFuncs.containsKey(r)) {
        // only add to lexer if the function actually contains actions
        lexer.actionFuncs.put(r, raf);
    }
}
Also used : RuleActionFunction(org.antlr.v4.codegen.model.RuleActionFunction) Action(org.antlr.v4.codegen.model.Action) PredAST(org.antlr.v4.tool.ast.PredAST) RuleSempredFunction(org.antlr.v4.codegen.model.RuleSempredFunction) Grammar(org.antlr.v4.tool.Grammar) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Aggregations

Action (org.antlr.v4.codegen.model.Action)1 RuleActionFunction (org.antlr.v4.codegen.model.RuleActionFunction)1 RuleSempredFunction (org.antlr.v4.codegen.model.RuleSempredFunction)1 Grammar (org.antlr.v4.tool.Grammar)1 ActionAST (org.antlr.v4.tool.ast.ActionAST)1 PredAST (org.antlr.v4.tool.ast.PredAST)1