Search in sources :

Example 1 with SrcOp

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

the class ParserFactory method tokenRef.

@Override
public List<SrcOp> tokenRef(GrammarAST ID, GrammarAST labelAST, GrammarAST args) {
    MatchToken matchOp = new MatchToken(this, (TerminalAST) ID);
    if (labelAST != null) {
        String label = labelAST.getText();
        RuleFunction rf = getCurrentRuleFunction();
        if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
            // add Token _X and List<Token> X decls
            // adds _X
            defineImplicitLabel(ID, matchOp);
            TokenListDecl l = getTokenListLabelDecl(label);
            rf.addContextDecl(ID.getAltLabel(), l);
        } else {
            Decl d = getTokenLabelDecl(label);
            matchOp.labels.add(d);
            rf.addContextDecl(ID.getAltLabel(), d);
        }
    //			Decl d = getTokenLabelDecl(label);
    //			((MatchToken)matchOp).labels.add(d);
    //			getCurrentRuleFunction().addContextDecl(ID.getAltLabel(), d);
    //			if ( labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN ) {
    //				TokenListDecl l = getTokenListLabelDecl(label);
    //				getCurrentRuleFunction().addContextDecl(ID.getAltLabel(), l);
    //			}
    }
    if (controller.needsImplicitLabel(ID, matchOp))
        defineImplicitLabel(ID, matchOp);
    AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(matchOp, labelAST);
    return list(matchOp, listLabelOp);
}
Also used : TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) MatchToken(org.antlr.v4.codegen.model.MatchToken) 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) AddToLabelList(org.antlr.v4.codegen.model.AddToLabelList)

Example 2 with SrcOp

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

the class ParserFactory method ruleRef.

@Override
public List<SrcOp> ruleRef(GrammarAST ID, GrammarAST label, GrammarAST args) {
    InvokeRule invokeOp = new InvokeRule(this, ID, label);
    // If no manual label and action refs as token/rule not label, we need to define implicit label
    if (controller.needsImplicitLabel(ID, invokeOp))
        defineImplicitLabel(ID, invokeOp);
    AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(invokeOp, label);
    return list(invokeOp, listLabelOp);
}
Also used : InvokeRule(org.antlr.v4.codegen.model.InvokeRule) AddToLabelList(org.antlr.v4.codegen.model.AddToLabelList)

Example 3 with SrcOp

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

the class Choice method addCodeForLookaheadTempVar.

public TestSetInline addCodeForLookaheadTempVar(IntervalSet look) {
    List<SrcOp> testOps = factory.getLL1Test(look, ast);
    TestSetInline expr = Utils.find(testOps, TestSetInline.class);
    if (expr != null) {
        Decl d = new TokenTypeDecl(factory, expr.varName);
        factory.getCurrentRuleFunction().addLocalDecl(d);
        CaptureNextTokenType nextType = new CaptureNextTokenType(factory, expr.varName);
        addPreambleOp(nextType);
    }
    return expr;
}
Also used : TokenTypeDecl(org.antlr.v4.codegen.model.decl.TokenTypeDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) TokenTypeDecl(org.antlr.v4.codegen.model.decl.TokenTypeDecl)

Example 4 with SrcOp

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

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

Aggregations

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)3 TokenDecl (org.antlr.v4.codegen.model.decl.TokenDecl)3 TokenListDecl (org.antlr.v4.codegen.model.decl.TokenListDecl)3 ArrayList (java.util.ArrayList)2 Action (org.antlr.v4.codegen.model.Action)2 LeftRecursiveRuleFunction (org.antlr.v4.codegen.model.LeftRecursiveRuleFunction)2 RuleFunction (org.antlr.v4.codegen.model.RuleFunction)2 SrcOp (org.antlr.v4.codegen.model.SrcOp)2 ST (org.stringtemplate.v4.ST)2 STGroup (org.stringtemplate.v4.STGroup)2 LeftRecursiveRuleAltInfo (org.antlr.v4.analysis.LeftRecursiveRuleAltInfo)1 AltBlock (org.antlr.v4.codegen.model.AltBlock)1 Choice (org.antlr.v4.codegen.model.Choice)1 CodeBlockForAlt (org.antlr.v4.codegen.model.CodeBlockForAlt)1 CodeBlockForOuterMostAlt (org.antlr.v4.codegen.model.CodeBlockForOuterMostAlt)1 InvokeRule (org.antlr.v4.codegen.model.InvokeRule)1 MatchNotSet (org.antlr.v4.codegen.model.MatchNotSet)1 MatchSet (org.antlr.v4.codegen.model.MatchSet)1