Search in sources :

Example 11 with RuleFunction

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

the class OutputModelController method buildNormalRuleFunction.

public void buildNormalRuleFunction(Rule r, RuleFunction function) {
    CodeGenerator gen = delegate.getGenerator();
    // TRIGGER factory functions for rule alts, elements
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(r.ast.token.getInputStream());
    GrammarAST blk = (GrammarAST) r.ast.getFirstChildWithType(ANTLRParser.BLOCK);
    CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, blk);
    walker = new SourceGenTriggers(nodes, this);
    try {
        // walk AST of rule alts/elements
        function.code = DefaultOutputModelFactory.list(walker.block(null, null));
        function.hasLookaheadBlock = walker.hasLookaheadBlock;
    } catch (org.antlr.runtime.RecognitionException e) {
        e.printStackTrace(System.err);
    }
    function.ctxType = gen.getTarget().getRuleFunctionContextStructName(function);
    function.postamble = rulePostamble(function, r);
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 12 with RuleFunction

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

the class DefaultOutputModelFactory method rulePostamble.

@Override
public List<SrcOp> rulePostamble(RuleFunction function, Rule r) {
    if (r.namedActions.containsKey("after") || r.namedActions.containsKey("finally")) {
        // See OutputModelController.buildLeftRecursiveRuleFunction
        // and Parser.exitRule for other places which set stop.
        CodeGenerator gen = getGenerator();
        STGroup codegenTemplates = gen.getTemplates();
        ST setStopTokenAST = codegenTemplates.getInstanceOf("recRuleSetStopToken");
        Action setStopTokenAction = new Action(this, function.ruleCtx, setStopTokenAST);
        List<SrcOp> ops = new ArrayList<SrcOp>(1);
        ops.add(setStopTokenAction);
        return ops;
    }
    return super.rulePostamble(function, r);
}
Also used : ST(org.stringtemplate.v4.ST) Action(org.antlr.v4.codegen.model.Action) SrcOp(org.antlr.v4.codegen.model.SrcOp) STGroup(org.stringtemplate.v4.STGroup) ArrayList(java.util.ArrayList)

Example 13 with RuleFunction

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

the class OutputModelController method buildRuleFunction.

/**
 * Create RuleFunction per rule and update sempreds,actions of parser
 *  output object with stuff found in r.
 */
public void buildRuleFunction(Parser parser, Rule r) {
    RuleFunction function = rule(r);
    parser.funcs.add(function);
    pushCurrentRule(function);
    function.fillNamedActions(delegate, r);
    if (r instanceof LeftRecursiveRule) {
        buildLeftRecursiveRuleFunction((LeftRecursiveRule) r, (LeftRecursiveRuleFunction) function);
    } else {
        buildNormalRuleFunction(r, function);
    }
    Grammar g = getGrammar();
    for (ActionAST a : r.actions) {
        if (a instanceof PredAST) {
            PredAST p = (PredAST) a;
            RuleSempredFunction rsf = parser.sempredFuncs.get(r);
            if (rsf == null) {
                rsf = new RuleSempredFunction(delegate, r, function.ctxType);
                parser.sempredFuncs.put(r, rsf);
            }
            rsf.actions.put(g.sempreds.get(p), new Action(delegate, p));
        }
    }
    popCurrentRule();
}
Also used : LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) Action(org.antlr.v4.codegen.model.Action) RuleFunction(org.antlr.v4.codegen.model.RuleFunction) LeftRecursiveRuleFunction(org.antlr.v4.codegen.model.LeftRecursiveRuleFunction) 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)

Example 14 with RuleFunction

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

the class ParserFactory method set.

@Override
public List<SrcOp> set(GrammarAST setAST, GrammarAST labelAST, boolean invert) {
    MatchSet matchOp;
    if (invert)
        matchOp = new MatchNotSet(this, setAST);
    else
        matchOp = new MatchSet(this, setAST);
    if (labelAST != null) {
        String label = labelAST.getText();
        RuleFunction rf = getCurrentRuleFunction();
        if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
            defineImplicitLabel(setAST, matchOp);
            TokenListDecl l = getTokenListLabelDecl(label);
            rf.addContextDecl(setAST.getAltLabel(), l);
        } else {
            Decl d = getTokenLabelDecl(label);
            matchOp.labels.add(d);
            rf.addContextDecl(setAST.getAltLabel(), d);
        }
    }
    if (controller.needsImplicitLabel(setAST, matchOp))
        defineImplicitLabel(setAST, matchOp);
    AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(matchOp, labelAST);
    return list(matchOp, listLabelOp);
}
Also used : MatchSet(org.antlr.v4.codegen.model.MatchSet) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) RuleFunction(org.antlr.v4.codegen.model.RuleFunction) LeftRecursiveRuleFunction(org.antlr.v4.codegen.model.LeftRecursiveRuleFunction) TokenDecl(org.antlr.v4.codegen.model.decl.TokenDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) RuleContextDecl(org.antlr.v4.codegen.model.decl.RuleContextDecl) MatchNotSet(org.antlr.v4.codegen.model.MatchNotSet) AddToLabelList(org.antlr.v4.codegen.model.AddToLabelList)

Example 15 with RuleFunction

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

the class ActionTranslator method translateActionChunk.

public static List<ActionChunk> translateActionChunk(OutputModelFactory factory, RuleFunction rf, String action, ActionAST node) {
    Token tokenWithinAction = node.token;
    ActionTranslator translator = new ActionTranslator(factory, node);
    translator.rf = rf;
    factory.getGrammar().tool.log("action-translator", "translate " + action);
    String altLabel = node.getAltLabel();
    if (rf != null) {
        translator.nodeContext = rf.ruleCtx;
        if (altLabel != null)
            translator.nodeContext = rf.altLabelCtxs.get(altLabel);
    }
    ANTLRStringStream in = new ANTLRStringStream(action);
    in.setLine(tokenWithinAction.getLine());
    in.setCharPositionInLine(tokenWithinAction.getCharPositionInLine());
    ActionSplitter trigger = new ActionSplitter(in, translator);
    // forces eval, triggers listener methods
    trigger.getActionTokens();
    return translator.chunks;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) ActionSplitter(org.antlr.v4.parse.ActionSplitter) Token(org.antlr.runtime.Token)

Aggregations

LeftRecursiveRuleFunction (org.antlr.v4.codegen.model.LeftRecursiveRuleFunction)6 RuleFunction (org.antlr.v4.codegen.model.RuleFunction)6 Action (org.antlr.v4.codegen.model.Action)4 AddToLabelList (org.antlr.v4.codegen.model.AddToLabelList)4 Decl (org.antlr.v4.codegen.model.decl.Decl)4 RuleContextDecl (org.antlr.v4.codegen.model.decl.RuleContextDecl)4 TokenDecl (org.antlr.v4.codegen.model.decl.TokenDecl)4 TokenListDecl (org.antlr.v4.codegen.model.decl.TokenListDecl)4 ArrayList (java.util.ArrayList)2 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)2 Token (org.antlr.runtime.Token)2 CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)2 LeftFactoredRuleFunction (org.antlr.v4.codegen.model.LeftFactoredRuleFunction)2 LeftUnfactoredRuleFunction (org.antlr.v4.codegen.model.LeftUnfactoredRuleFunction)2 MatchNotSet (org.antlr.v4.codegen.model.MatchNotSet)2 MatchSet (org.antlr.v4.codegen.model.MatchSet)2 MatchToken (org.antlr.v4.codegen.model.MatchToken)2 RuleSempredFunction (org.antlr.v4.codegen.model.RuleSempredFunction)2 SrcOp (org.antlr.v4.codegen.model.SrcOp)2 TokenPropertyRef_int (org.antlr.v4.codegen.model.chunk.TokenPropertyRef_int)2