Search in sources :

Example 1 with PredAST

use of org.antlr.v4.tool.ast.PredAST in project antlr4 by antlr.

the class Grammar method getIndexToPredicateMap.

public LinkedHashMap<Integer, PredAST> getIndexToPredicateMap() {
    LinkedHashMap<Integer, PredAST> indexToPredMap = new LinkedHashMap<Integer, PredAST>();
    for (Rule r : rules.values()) {
        for (ActionAST a : r.actions) {
            if (a instanceof PredAST) {
                PredAST p = (PredAST) a;
                indexToPredMap.put(sempreds.get(p), p);
            }
        }
    }
    return indexToPredMap;
}
Also used : PredAST(org.antlr.v4.tool.ast.PredAST) LinkedHashMap(java.util.LinkedHashMap) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Example 2 with PredAST

use of org.antlr.v4.tool.ast.PredAST 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 3 with PredAST

use of org.antlr.v4.tool.ast.PredAST 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 4 with PredAST

use of org.antlr.v4.tool.ast.PredAST in project antlr4 by antlr.

the class ParserATNFactory method sempred.

/** Build what amounts to an epsilon transition with a semantic
	 *  predicate action.  The {@code pred} is a pointer into the AST of
	 *  the {@link ANTLRParser#SEMPRED} token.
	 */
@Override
public Handle sempred(PredAST pred) {
    //System.out.println("sempred: "+ pred);
    ATNState left = newState(pred);
    ATNState right = newState(pred);
    AbstractPredicateTransition p;
    if (pred.getOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME) != null) {
        int precedence = Integer.parseInt(pred.getOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME));
        p = new PrecedencePredicateTransition(right, precedence);
    } else {
        boolean isCtxDependent = UseDefAnalyzer.actionIsContextDependent(pred);
        p = new PredicateTransition(right, currentRule.index, g.sempreds.get(pred), isCtxDependent);
    }
    left.addTransition(p);
    pred.atnState = left;
    return new Handle(left, right);
}
Also used : PrecedencePredicateTransition(org.antlr.v4.runtime.atn.PrecedencePredicateTransition) PrecedencePredicateTransition(org.antlr.v4.runtime.atn.PrecedencePredicateTransition) AbstractPredicateTransition(org.antlr.v4.runtime.atn.AbstractPredicateTransition) PredicateTransition(org.antlr.v4.runtime.atn.PredicateTransition) AbstractPredicateTransition(org.antlr.v4.runtime.atn.AbstractPredicateTransition) ATNState(org.antlr.v4.runtime.atn.ATNState)

Aggregations

ActionAST (org.antlr.v4.tool.ast.ActionAST)3 PredAST (org.antlr.v4.tool.ast.PredAST)3 Action (org.antlr.v4.codegen.model.Action)2 RuleSempredFunction (org.antlr.v4.codegen.model.RuleSempredFunction)2 Grammar (org.antlr.v4.tool.Grammar)2 LinkedHashMap (java.util.LinkedHashMap)1 LeftRecursiveRuleFunction (org.antlr.v4.codegen.model.LeftRecursiveRuleFunction)1 RuleActionFunction (org.antlr.v4.codegen.model.RuleActionFunction)1 RuleFunction (org.antlr.v4.codegen.model.RuleFunction)1 ATNState (org.antlr.v4.runtime.atn.ATNState)1 AbstractPredicateTransition (org.antlr.v4.runtime.atn.AbstractPredicateTransition)1 PrecedencePredicateTransition (org.antlr.v4.runtime.atn.PrecedencePredicateTransition)1 PredicateTransition (org.antlr.v4.runtime.atn.PredicateTransition)1 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)1