Search in sources :

Example 31 with Transition

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

the class ATNPrinter method asString.

public String asString() {
    if (start == null)
        return null;
    marked = new HashSet<ATNState>();
    work = new ArrayList<ATNState>();
    work.add(start);
    StringBuilder buf = new StringBuilder();
    ATNState s;
    while (!work.isEmpty()) {
        s = work.remove(0);
        if (marked.contains(s))
            continue;
        int n = s.getNumberOfTransitions();
        // System.out.println("visit "+s+"; edges="+n);
        marked.add(s);
        for (int i = 0; i < n; i++) {
            Transition t = s.transition(i);
            if (!(s instanceof RuleStopState)) {
                // don't add follow states to work
                if (t instanceof RuleTransition)
                    work.add(((RuleTransition) t).followState);
                else
                    work.add(t.target);
            }
            buf.append(getStateString(s));
            if (t instanceof EpsilonTransition) {
                buf.append("->").append(getStateString(t.target)).append('\n');
            } else if (t instanceof RuleTransition) {
                buf.append("-").append(g.getRule(((RuleTransition) t).ruleIndex).name).append("->").append(getStateString(t.target)).append('\n');
            } else if (t instanceof ActionTransition) {
                ActionTransition a = (ActionTransition) t;
                buf.append("-").append(a.toString()).append("->").append(getStateString(t.target)).append('\n');
            } else if (t instanceof SetTransition) {
                SetTransition st = (SetTransition) t;
                boolean not = st instanceof NotSetTransition;
                if (g.isLexer()) {
                    buf.append("-").append(not ? "~" : "").append(st.toString()).append("->").append(getStateString(t.target)).append('\n');
                } else {
                    buf.append("-").append(not ? "~" : "").append(st.label().toString(g.getVocabulary())).append("->").append(getStateString(t.target)).append('\n');
                }
            } else if (t instanceof AtomTransition) {
                AtomTransition a = (AtomTransition) t;
                String label = g.getTokenDisplayName(a.label);
                buf.append("-").append(label).append("->").append(getStateString(t.target)).append('\n');
            } else {
                buf.append("-").append(t.toString()).append("->").append(getStateString(t.target)).append('\n');
            }
        }
    }
    return buf.toString();
}
Also used : RuleStopState(org.antlr.v4.runtime.atn.RuleStopState) AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) SetTransition(org.antlr.v4.runtime.atn.SetTransition) ATNState(org.antlr.v4.runtime.atn.ATNState) ActionTransition(org.antlr.v4.runtime.atn.ActionTransition) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) EpsilonTransition(org.antlr.v4.runtime.atn.EpsilonTransition) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) ActionTransition(org.antlr.v4.runtime.atn.ActionTransition) SetTransition(org.antlr.v4.runtime.atn.SetTransition) Transition(org.antlr.v4.runtime.atn.Transition) EpsilonTransition(org.antlr.v4.runtime.atn.EpsilonTransition)

Example 32 with Transition

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

the class ATNVisitor method visit_.

public void visit_(@NotNull ATNState s, @NotNull Set<Integer> visited) {
    if (!visited.add(s.stateNumber))
        return;
    visited.add(s.stateNumber);
    visitState(s);
    int n = s.getNumberOfTransitions();
    for (int i = 0; i < n; i++) {
        Transition t = s.transition(i);
        visit_(t.target, visited);
    }
}
Also used : Transition(org.antlr.v4.runtime.atn.Transition)

Example 33 with Transition

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

the class ParserATNFactory method epsilon.

protected void epsilon(ATNState a, @NotNull ATNState b, boolean prepend) {
    for (Transition t : a.getTransitions()) {
        if (t.getSerializationType() != Transition.EPSILON) {
            continue;
        }
        if (t.target == b && ((EpsilonTransition) t).outermostPrecedenceReturn() == -1) {
            // This transition was already added
            return;
        }
    }
    if (a != null) {
        int index = prepend ? 0 : a.getNumberOfTransitions();
        a.addTransition(index, new EpsilonTransition(b));
    }
}
Also used : 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) EpsilonTransition(org.antlr.v4.runtime.atn.EpsilonTransition)

Example 34 with Transition

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

the class ParserATNFactory method addEOFTransitionToStartRules.

/**
 * Add an EOF transition to any rule end ATNState that points to nothing
 *  (i.e., for all those rules not invoked by another rule).  These
 *  are start symbols then.
 *
 *  Return the number of grammar entry points; i.e., how many rules are
 *  not invoked by another rule (they can only be invoked from outside).
 *  These are the start rules.
 */
public int addEOFTransitionToStartRules() {
    int n = 0;
    // one unique EOF target for all rules
    ATNState eofTarget = newState(null);
    for (Rule r : g.rules.values()) {
        ATNState stop = atn.ruleToStopState[r.index];
        if (stop.getNumberOfTransitions() > 0)
            continue;
        n++;
        Transition t = new AtomTransition(eofTarget, Token.EOF);
        stop.addTransition(t);
    }
    return n;
}
Also used : AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) 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) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 35 with Transition

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

the class ParserATNFactory method elemList.

@NotNull
public Handle elemList(@NotNull 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) NotNull(org.antlr.v4.runtime.misc.NotNull)

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