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);
}
}
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);
}
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->o-A->o->o (last ATNState is BlockEndState pointed to by all alts)
* | ^
* |->o-B->o--|
* | |
* ... |
* | |
* |->o-Z->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->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;
}
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());
}
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);
}
Aggregations