Search in sources :

Example 31 with Action

use of org.antlr.v4.codegen.model.Action 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 32 with Action

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

the class OutputModelController method buildLeftRecursiveRuleFunction.

public void buildLeftRecursiveRuleFunction(LeftRecursiveRule r, LeftRecursiveRuleFunction function) {
    buildNormalRuleFunction(r, function);
    // now inject code to start alts
    CodeGenerator gen = delegate.getGenerator();
    STGroup codegenTemplates = gen.getTemplates();
    // pick out alt(s) for primaries
    CodeBlockForOuterMostAlt outerAlt = (CodeBlockForOuterMostAlt) function.code.get(0);
    List<CodeBlockForAlt> primaryAltsCode = new ArrayList<CodeBlockForAlt>();
    SrcOp primaryStuff = outerAlt.ops.get(0);
    if (primaryStuff instanceof Choice) {
        Choice primaryAltBlock = (Choice) primaryStuff;
        primaryAltsCode.addAll(primaryAltBlock.alts);
    } else {
        // just a single alt I guess; no block
        primaryAltsCode.add((CodeBlockForAlt) primaryStuff);
    }
    // pick out alt(s) for op alts
    StarBlock opAltStarBlock = (StarBlock) outerAlt.ops.get(1);
    CodeBlockForAlt altForOpAltBlock = opAltStarBlock.alts.get(0);
    List<CodeBlockForAlt> opAltsCode = new ArrayList<CodeBlockForAlt>();
    SrcOp opStuff = altForOpAltBlock.ops.get(0);
    if (opStuff instanceof AltBlock) {
        AltBlock opAltBlock = (AltBlock) opStuff;
        opAltsCode.addAll(opAltBlock.alts);
    } else {
        // just a single alt I guess; no block
        opAltsCode.add((CodeBlockForAlt) opStuff);
    }
    // Insert code in front of each primary alt to create specialized ctx if there was a label
    for (int i = 0; i < primaryAltsCode.size(); i++) {
        LeftRecursiveRuleAltInfo altInfo = r.recPrimaryAlts.get(i);
        if (altInfo.altLabel == null)
            continue;
        ST altActionST = codegenTemplates.getInstanceOf("recRuleReplaceContext");
        altActionST.add("ctxName", Utils.capitalize(altInfo.altLabel));
        Action altAction = new Action(delegate, function.altLabelCtxs.get(altInfo.altLabel), altActionST);
        CodeBlockForAlt alt = primaryAltsCode.get(i);
        alt.insertOp(0, altAction);
    }
    // Insert code to set ctx.stop after primary block and before op * loop
    ST setStopTokenAST = codegenTemplates.getInstanceOf("recRuleSetStopToken");
    Action setStopTokenAction = new Action(delegate, function.ruleCtx, setStopTokenAST);
    outerAlt.insertOp(1, setStopTokenAction);
    // Insert code to set _prevctx at start of * loop
    ST setPrevCtx = codegenTemplates.getInstanceOf("recRuleSetPrevCtx");
    Action setPrevCtxAction = new Action(delegate, function.ruleCtx, setPrevCtx);
    opAltStarBlock.addIterationOp(setPrevCtxAction);
    // Insert code in front of each op alt to create specialized ctx if there was an alt label
    for (int i = 0; i < opAltsCode.size(); i++) {
        ST altActionST;
        LeftRecursiveRuleAltInfo altInfo = r.recOpAlts.getElement(i);
        String templateName;
        if (altInfo.altLabel != null) {
            templateName = "recRuleLabeledAltStartAction";
            altActionST = codegenTemplates.getInstanceOf(templateName);
            altActionST.add("currentAltLabel", altInfo.altLabel);
        } else {
            templateName = "recRuleAltStartAction";
            altActionST = codegenTemplates.getInstanceOf(templateName);
            altActionST.add("ctxName", Utils.capitalize(r.name));
        }
        altActionST.add("ruleName", r.name);
        // add label of any lr ref we deleted
        altActionST.add("label", altInfo.leftRecursiveRuleRefLabel);
        if (altActionST.impl.formalArguments.containsKey("isListLabel")) {
            altActionST.add("isListLabel", altInfo.isListLabel);
        } else if (altInfo.isListLabel) {
            delegate.getGenerator().tool.errMgr.toolError(ErrorType.CODE_TEMPLATE_ARG_ISSUE, templateName, "isListLabel");
        }
        Action altAction = new Action(delegate, function.altLabelCtxs.get(altInfo.altLabel), altActionST);
        CodeBlockForAlt alt = opAltsCode.get(i);
        alt.insertOp(0, altAction);
    }
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ActionAST(org.antlr.v4.tool.ast.ActionAST) BlockAST(org.antlr.v4.tool.ast.BlockAST) ST(org.stringtemplate.v4.ST) PredAST(org.antlr.v4.tool.ast.PredAST) Action(org.antlr.v4.codegen.model.Action) SrcOp(org.antlr.v4.codegen.model.SrcOp) Choice(org.antlr.v4.codegen.model.Choice) STGroup(org.stringtemplate.v4.STGroup) ArrayList(java.util.ArrayList) StarBlock(org.antlr.v4.codegen.model.StarBlock) CodeBlockForOuterMostAlt(org.antlr.v4.codegen.model.CodeBlockForOuterMostAlt) CodeBlockForAlt(org.antlr.v4.codegen.model.CodeBlockForAlt) AltBlock(org.antlr.v4.codegen.model.AltBlock) LeftRecursiveRuleAltInfo(org.antlr.v4.analysis.LeftRecursiveRuleAltInfo)

Example 33 with Action

use of org.antlr.v4.codegen.model.Action 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)

Example 34 with Action

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

the class ParserFactory method defineImplicitLabel.

// support
public void defineImplicitLabel(GrammarAST ast, LabeledOp op) {
    Decl d;
    if (ast.getType() == ANTLRParser.SET || ast.getType() == ANTLRParser.WILDCARD) {
        String implLabel = gen.getTarget().getImplicitSetLabel(String.valueOf(ast.token.getTokenIndex()));
        d = getTokenLabelDecl(implLabel);
        ((TokenDecl) d).isImplicit = true;
    } else if (ast.getType() == ANTLRParser.RULE_REF) {
        // a rule reference?
        Rule r = g.getRule(ast.getText());
        String implLabel = gen.getTarget().getImplicitRuleLabel(ast.getText());
        String ctxName = gen.getTarget().getRuleFunctionContextStructName(r);
        d = new RuleContextDecl(this, implLabel, ctxName);
        ((RuleContextDecl) d).isImplicit = true;
    } else {
        String implLabel = gen.getTarget().getImplicitTokenLabel(ast.getText());
        d = getTokenLabelDecl(implLabel);
        ((TokenDecl) d).isImplicit = true;
    }
    op.getLabels().add(d);
    // all labels must be in scope struct in case we exec action out of context
    getCurrentRuleFunction().addContextDecl(ast.getAltLabel(), d);
}
Also used : 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) TokenDecl(org.antlr.v4.codegen.model.decl.TokenDecl) InvokeRule(org.antlr.v4.codegen.model.InvokeRule) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) RuleContextDecl(org.antlr.v4.codegen.model.decl.RuleContextDecl)

Example 35 with Action

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

the class ScopeParser method parse.

public static AttributeDict parse(ActionAST action, String s, char separator, Grammar g) {
    AttributeDict dict = new AttributeDict();
    List<Pair<String, Integer>> decls = splitDecls(s, separator);
    for (Pair<String, Integer> decl : decls) {
        if (decl.a.trim().length() > 0) {
            Attribute a = parseAttributeDef(action, decl, g);
            dict.add(a);
        }
    }
    return dict;
}
Also used : Attribute(org.antlr.v4.tool.Attribute) AttributeDict(org.antlr.v4.tool.AttributeDict) Pair(org.antlr.v4.runtime.misc.Pair)

Aggregations

ST (org.stringtemplate.v4.ST)11 ActionAST (org.antlr.v4.tool.ast.ActionAST)9 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)9 Grammar (org.antlr.v4.tool.Grammar)8 LexerGrammar (org.antlr.v4.tool.LexerGrammar)8 STGroup (org.stringtemplate.v4.STGroup)8 ArrayList (java.util.ArrayList)7 ATNFactory (org.antlr.v4.automata.ATNFactory)6 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)6 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)6 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)6 ATNState (org.antlr.v4.runtime.atn.ATNState)6 ActionTransition (org.antlr.v4.runtime.atn.ActionTransition)6 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)6 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)6 STGroupString (org.stringtemplate.v4.STGroupString)6 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)5 Rule (org.antlr.v4.tool.Rule)5 HashMap (java.util.HashMap)4 Action (org.antlr.v4.codegen.model.Action)4