Search in sources :

Example 46 with Transition

use of org.antlr.v4.runtime.atn.Transition 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)

Example 47 with Transition

use of org.antlr.v4.runtime.atn.Transition in project antlr4 by antlr.

the class ParserATNFactory method elemList.

public Handle elemList(List<Handle> els) {
    int n = els.size();
    for (int i = 0; i < n - 1; i++) {
        // hook up elements (visit all but last)
        Handle el = els.get(i);
        // if el is of form o-x->o for x in {rule, action, pred, token, ...}
        // and not last in alt
        Transition tr = null;
        if (el.left.getNumberOfTransitions() == 1)
            tr = el.left.transition(0);
        boolean isRuleTrans = tr instanceof RuleTransition;
        if (el.left.getStateType() == ATNState.BASIC && el.right.getStateType() == ATNState.BASIC && tr != null && (isRuleTrans && ((RuleTransition) tr).followState == el.right || tr.target == el.right)) {
            // we can avoid epsilon edge to next el
            if (isRuleTrans)
                ((RuleTransition) tr).followState = els.get(i + 1).left;
            else
                tr.target = els.get(i + 1).left;
            // we skipped over this state
            atn.removeState(el.right);
        } else {
            // need epsilon if previous block's right end node is complicated
            epsilon(el.right, els.get(i + 1).left);
        }
    }
    Handle first = els.get(0);
    Handle last = els.get(n - 1);
    if (first == null || last == null) {
        g.tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, "element list has first|last == null");
    }
    return new Handle(first.left, last.right);
}
Also used : RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) WildcardTransition(org.antlr.v4.runtime.atn.WildcardTransition) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) PrecedencePredicateTransition(org.antlr.v4.runtime.atn.PrecedencePredicateTransition) AbstractPredicateTransition(org.antlr.v4.runtime.atn.AbstractPredicateTransition) PredicateTransition(org.antlr.v4.runtime.atn.PredicateTransition) EpsilonTransition(org.antlr.v4.runtime.atn.EpsilonTransition) ActionTransition(org.antlr.v4.runtime.atn.ActionTransition) Transition(org.antlr.v4.runtime.atn.Transition) AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) SetTransition(org.antlr.v4.runtime.atn.SetTransition)

Example 48 with Transition

use of org.antlr.v4.runtime.atn.Transition in project antlr4 by antlr.

the class ParserATNFactory method action.

/**
 * Build what amounts to an epsilon transition with an action.
 *  The action goes into ATN though it is ignored during prediction
 *  if {@link ActionTransition#actionIndex actionIndex}{@code <0}.
 */
@Override
public Handle action(ActionAST action) {
    // System.out.println("action: "+action);
    ATNState left = newState(action);
    ATNState right = newState(action);
    ActionTransition a = new ActionTransition(right, currentRule.index);
    left.addTransition(a);
    action.atnState = left;
    return new Handle(left, right);
}
Also used : ActionTransition(org.antlr.v4.runtime.atn.ActionTransition) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 49 with Transition

use of org.antlr.v4.runtime.atn.Transition in project antlr4 by antlr.

the class ProfilingATNSimulator method getExistingTargetState.

@Override
protected DFAState getExistingTargetState(DFAState previousD, int t) {
    // this method is called after each time the input position advances
    // during SLL prediction
    _sllStopIndex = _input.index();
    DFAState existingTargetState = super.getExistingTargetState(previousD, t);
    if (existingTargetState != null) {
        // count only if we transition over a DFA state
        decisions[currentDecision].SLL_DFATransitions++;
        if (existingTargetState == ERROR) {
            decisions[currentDecision].errors.add(new ErrorInfo(currentDecision, previousD.configs, _input, _startIndex, _sllStopIndex, false));
        }
    }
    currentState = existingTargetState;
    return existingTargetState;
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState)

Aggregations

ATNState (org.antlr.v4.runtime.atn.ATNState)25 Transition (org.antlr.v4.runtime.atn.Transition)23 AtomTransition (org.antlr.v4.runtime.atn.AtomTransition)18 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)17 ActionTransition (org.antlr.v4.runtime.atn.ActionTransition)16 SetTransition (org.antlr.v4.runtime.atn.SetTransition)14 NotSetTransition (org.antlr.v4.runtime.atn.NotSetTransition)13 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)13 PrecedencePredicateTransition (org.antlr.v4.runtime.atn.PrecedencePredicateTransition)10 PredicateTransition (org.antlr.v4.runtime.atn.PredicateTransition)10 AbstractPredicateTransition (org.antlr.v4.runtime.atn.AbstractPredicateTransition)9 EpsilonTransition (org.antlr.v4.runtime.atn.EpsilonTransition)9 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)9 ArrayList (java.util.ArrayList)7 NotNull (org.antlr.v4.runtime.misc.NotNull)7 Rule (org.antlr.v4.tool.Rule)7 RuleStopState (org.antlr.v4.runtime.atn.RuleStopState)6 Interval (org.antlr.v4.runtime.misc.Interval)6 DecisionState (org.antlr.v4.runtime.atn.DecisionState)5 HashSet (java.util.HashSet)4