Search in sources :

Example 6 with RuleFunction

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

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) LeftFactoredRuleFunction(org.antlr.v4.codegen.model.LeftFactoredRuleFunction) LeftUnfactoredRuleFunction(org.antlr.v4.codegen.model.LeftUnfactoredRuleFunction) 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 7 with RuleFunction

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

the class ActionTranslator method translateAction.

public static List<ActionChunk> translateAction(OutputModelFactory factory, RuleFunction rf, Token tokenWithinAction, ActionAST node) {
    String action = tokenWithinAction.getText();
    if (action != null && action.length() > 0 && action.charAt(0) == '{') {
        int firstCurly = action.indexOf('{');
        int lastCurly = action.lastIndexOf('}');
        if (firstCurly >= 0 && lastCurly >= 0) {
            // trim {...}
            action = action.substring(firstCurly + 1, lastCurly);
        }
    }
    return translateActionChunk(factory, rf, action, node);
}
Also used : TokenPropertyRef_int(org.antlr.v4.codegen.model.chunk.TokenPropertyRef_int)

Example 8 with RuleFunction

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

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

the class Target method getRuleFunctionContextStructName.

/**
 * If we know which actual function, we can provide the actual ctx type.
 *  This will contain implicit labels etc...  From outside, though, we
 *  see only ParserRuleContext unless there are externally visible stuff
 *  like args, locals, explicit labels, etc...
 */
public String getRuleFunctionContextStructName(RuleFunction function) {
    Rule r = function.rule;
    if (r.g.isLexer()) {
        return getTemplates().getInstanceOf("LexerRuleContext").render();
    }
    String baseName = r.getBaseContext();
    return Utils.capitalize(baseName) + getTemplates().getInstanceOf("RuleContextNameSuffix").render();
}
Also used : Rule(org.antlr.v4.tool.Rule)

Example 10 with RuleFunction

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

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)

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