Search in sources :

Example 16 with Transition

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

the class ATNVisitor method visit_.

public void visit_(ATNState s, 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 17 with Transition

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

the class LexerATNFactory method set.

@Override
public Handle set(GrammarAST associatedAST, List<GrammarAST> alts, boolean invert) {
    ATNState left = newState(associatedAST);
    ATNState right = newState(associatedAST);
    IntervalSet set = new IntervalSet();
    for (GrammarAST t : alts) {
        if (t.getType() == ANTLRParser.RANGE) {
            int a = CharSupport.getCharValueFromGrammarCharLiteral(t.getChild(0).getText());
            int b = CharSupport.getCharValueFromGrammarCharLiteral(t.getChild(1).getText());
            if (checkRange((GrammarAST) t.getChild(0), (GrammarAST) t.getChild(1), a, b)) {
                checkSetCollision(associatedAST, set, a, b);
                set.add(a, b);
            }
        } else if (t.getType() == ANTLRParser.LEXER_CHAR_SET) {
            set.addAll(getSetFromCharSetLiteral(t));
        } else if (t.getType() == ANTLRParser.STRING_LITERAL) {
            int c = CharSupport.getCharValueFromGrammarCharLiteral(t.getText());
            if (c != -1) {
                checkSetCollision(associatedAST, set, c);
                set.add(c);
            } else {
                g.tool.errMgr.grammarError(ErrorType.INVALID_LITERAL_IN_LEXER_SET, g.fileName, t.getToken(), t.getText());
            }
        } else if (t.getType() == ANTLRParser.TOKEN_REF) {
            g.tool.errMgr.grammarError(ErrorType.UNSUPPORTED_REFERENCE_IN_LEXER_SET, g.fileName, t.getToken(), t.getText());
        }
    }
    if (invert) {
        left.addTransition(new NotSetTransition(right, set));
    } else {
        Transition transition;
        if (set.getIntervals().size() == 1) {
            Interval interval = set.getIntervals().get(0);
            transition = CodePointTransitions.createWithCodePointRange(right, interval.a, interval.b);
        } else {
            transition = new SetTransition(right, set);
        }
        left.addTransition(transition);
    }
    associatedAST.atnState = left;
    return new Handle(left, right);
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) 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) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) SetTransition(org.antlr.v4.runtime.atn.SetTransition) ATNState(org.antlr.v4.runtime.atn.ATNState) Interval(org.antlr.v4.runtime.misc.Interval)

Example 18 with Transition

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

the class ParserATNFactory method block.

/**
	 * From {@code A|B|..|Z} alternative block build
	 *
	 * <pre>
	 *  o-&gt;o-A-&gt;o-&gt;o (last ATNState is BlockEndState pointed to by all alts)
	 *  |          ^
	 *  |-&gt;o-B-&gt;o--|
	 *  |          |
	 *  ...        |
	 *  |          |
	 *  |-&gt;o-Z-&gt;o--|
	 * </pre>
	 *
	 * So start node points at every alternative with epsilon transition and
	 * every alt right side points at a block end ATNState.
	 * <p>
	 * Special case: only one alternative: don't make a block with alt
	 * begin/end.
	 * <p>
	 * Special case: if just a list of tokens/chars/sets, then collapse to a
	 * single edged o-set-&gt;o graph.
	 * <p>
	 * TODO: Set alt number (1..n) in the states?
	 */
@Override
public Handle block(BlockAST blkAST, GrammarAST ebnfRoot, List<Handle> alts) {
    if (ebnfRoot == null) {
        if (alts.size() == 1) {
            Handle h = alts.get(0);
            blkAST.atnState = h.left;
            return h;
        }
        BlockStartState start = newState(BasicBlockStartState.class, blkAST);
        if (alts.size() > 1)
            atn.defineDecisionState(start);
        return makeBlock(start, blkAST, alts);
    }
    switch(ebnfRoot.getType()) {
        case ANTLRParser.OPTIONAL:
            BlockStartState start = newState(BasicBlockStartState.class, blkAST);
            atn.defineDecisionState(start);
            Handle h = makeBlock(start, blkAST, alts);
            return optional(ebnfRoot, h);
        case ANTLRParser.CLOSURE:
            BlockStartState star = newState(StarBlockStartState.class, ebnfRoot);
            if (alts.size() > 1)
                atn.defineDecisionState(star);
            h = makeBlock(star, blkAST, alts);
            return star(ebnfRoot, h);
        case ANTLRParser.POSITIVE_CLOSURE:
            PlusBlockStartState plus = newState(PlusBlockStartState.class, ebnfRoot);
            if (alts.size() > 1)
                atn.defineDecisionState(plus);
            h = makeBlock(plus, blkAST, alts);
            return plus(ebnfRoot, h);
    }
    return null;
}
Also used : PlusBlockStartState(org.antlr.v4.runtime.atn.PlusBlockStartState) PlusBlockStartState(org.antlr.v4.runtime.atn.PlusBlockStartState) StarBlockStartState(org.antlr.v4.runtime.atn.StarBlockStartState) BasicBlockStartState(org.antlr.v4.runtime.atn.BasicBlockStartState) BlockStartState(org.antlr.v4.runtime.atn.BlockStartState)

Example 19 with Transition

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

the class TestParserProfiler method test3xLL2.

@Test
public void test3xLL2() throws Exception {
    Grammar g = new Grammar("parser grammar T;\n" + "s : ID ';'{}\n" + "  | ID '.'\n" + "  ;\n", lg);
    // The '.' vs ';' causes another ATN transition
    DecisionInfo[] info = interpAndGetDecisionInfo(lg, g, "s", "xyz;", "abc;", "z.");
    assertEquals(1, info.length);
    String expecting = "{decision=0, contextSensitivities=0, errors=0, ambiguities=0, SLL_lookahead=6, " + "SLL_ATNTransitions=3, SLL_DFATransitions=3, LL_Fallback=0, LL_lookahead=0, LL_ATNTransitions=0}";
    assertEquals(expecting, info[0].toString());
}
Also used : DecisionInfo(org.antlr.v4.runtime.atn.DecisionInfo) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 20 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)

Aggregations

ATNState (org.antlr.v4.runtime.atn.ATNState)12 Transition (org.antlr.v4.runtime.atn.Transition)11 AtomTransition (org.antlr.v4.runtime.atn.AtomTransition)9 ActionTransition (org.antlr.v4.runtime.atn.ActionTransition)8 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)8 SetTransition (org.antlr.v4.runtime.atn.SetTransition)7 NotSetTransition (org.antlr.v4.runtime.atn.NotSetTransition)6 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)6 PrecedencePredicateTransition (org.antlr.v4.runtime.atn.PrecedencePredicateTransition)5 PredicateTransition (org.antlr.v4.runtime.atn.PredicateTransition)5 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)5 AbstractPredicateTransition (org.antlr.v4.runtime.atn.AbstractPredicateTransition)4 EpsilonTransition (org.antlr.v4.runtime.atn.EpsilonTransition)4 ArrayList (java.util.ArrayList)3 DecisionState (org.antlr.v4.runtime.atn.DecisionState)3 RangeTransition (org.antlr.v4.runtime.atn.RangeTransition)3 RuleStopState (org.antlr.v4.runtime.atn.RuleStopState)3 Interval (org.antlr.v4.runtime.misc.Interval)3 Rule (org.antlr.v4.tool.Rule)3 HashMap (java.util.HashMap)2