use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by antlr.
the class ATNOptimizer method optimizeStates.
private static void optimizeStates(ATN atn) {
// System.out.println(atn.states);
List<ATNState> compressed = new ArrayList<ATNState>();
// new state number
int i = 0;
for (ATNState s : atn.states) {
if (s != null) {
compressed.add(s);
// reset state number as we shift to new position
s.stateNumber = i;
i++;
}
}
// System.out.println(compressed);
// System.out.println("ATN optimizer removed " + (atn.states.size() - compressed.size()) + " null states.");
atn.states.clear();
atn.states.addAll(compressed);
}
use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by antlr.
the class LexerATNFactory method charSetLiteral.
/**
* [Aa\t \u1234a-z\]\p{Letter}\-] char sets
*/
@Override
public Handle charSetLiteral(GrammarAST charSetAST) {
ATNState left = newState(charSetAST);
ATNState right = newState(charSetAST);
IntervalSet set = getSetFromCharSetLiteral(charSetAST);
if (set.isNil()) {
g.tool.errMgr.grammarError(ErrorType.EMPTY_STRINGS_AND_SETS_NOT_ALLOWED, g.fileName, charSetAST.getToken(), "[]");
}
left.addTransition(new SetTransition(right, set));
charSetAST.atnState = left;
return new Handle(left, right);
}
use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by antlr.
the class TestATNLexerInterpreter method checkLexerMatches.
protected void checkLexerMatches(LexerGrammar lg, String inputString, String expecting) {
ATN atn = createATN(lg, true);
CharStream input = CharStreams.fromString(inputString);
ATNState startState = atn.modeNameToStartState.get("DEFAULT_MODE");
DOTGenerator dot = new DOTGenerator(lg);
// System.out.println(dot.getDOT(startState, true));
List<String> tokenTypes = RuntimeTestUtils.getTokenTypes(lg, atn, input);
String result = Utils.join(tokenTypes.iterator(), ", ");
// System.out.println(tokenTypes);
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by antlr.
the class ParserInterpreter method visitState.
protected void visitState(ATNState p) {
// System.out.println("visitState "+p.stateNumber);
int predictedAlt = 1;
if (p instanceof DecisionState) {
predictedAlt = visitDecisionState((DecisionState) p);
}
Transition transition = p.transition(predictedAlt - 1);
switch(transition.getSerializationType()) {
case Transition.EPSILON:
if (p.getStateType() == ATNState.STAR_LOOP_ENTRY && ((StarLoopEntryState) p).isPrecedenceDecision && !(transition.target instanceof LoopEndState)) {
// We are at the start of a left recursive rule's (...)* loop
// and we're not taking the exit branch of loop.
InterpreterRuleContext localctx = createInterpreterRuleContext(_parentContextStack.peek().a, _parentContextStack.peek().b, _ctx.getRuleIndex());
pushNewRecursionContext(localctx, atn.ruleToStartState[p.ruleIndex].stateNumber, _ctx.getRuleIndex());
}
break;
case Transition.ATOM:
match(((AtomTransition) transition).label);
break;
case Transition.RANGE:
case Transition.SET:
case Transition.NOT_SET:
if (!transition.matches(_input.LA(1), Token.MIN_USER_TOKEN_TYPE, 65535)) {
recoverInline();
}
matchWildcard();
break;
case Transition.WILDCARD:
matchWildcard();
break;
case Transition.RULE:
RuleStartState ruleStartState = (RuleStartState) transition.target;
int ruleIndex = ruleStartState.ruleIndex;
InterpreterRuleContext newctx = createInterpreterRuleContext(_ctx, p.stateNumber, ruleIndex);
if (ruleStartState.isLeftRecursiveRule) {
enterRecursionRule(newctx, ruleStartState.stateNumber, ruleIndex, ((RuleTransition) transition).precedence);
} else {
enterRule(newctx, transition.target.stateNumber, ruleIndex);
}
break;
case Transition.PREDICATE:
PredicateTransition predicateTransition = (PredicateTransition) transition;
if (!sempred(_ctx, predicateTransition.ruleIndex, predicateTransition.predIndex)) {
throw new FailedPredicateException(this);
}
break;
case Transition.ACTION:
ActionTransition actionTransition = (ActionTransition) transition;
action(_ctx, actionTransition.ruleIndex, actionTransition.actionIndex);
break;
case Transition.PRECEDENCE:
if (!precpred(_ctx, ((PrecedencePredicateTransition) transition).precedence)) {
throw new FailedPredicateException(this, String.format("precpred(_ctx, %d)", ((PrecedencePredicateTransition) transition).precedence));
}
break;
default:
throw new UnsupportedOperationException("Unrecognized ATN transition type.");
}
setState(transition.target.stateNumber);
}
use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by antlr.
the class ParserInterpreter method visitRuleStopState.
protected void visitRuleStopState(ATNState p) {
RuleStartState ruleStartState = atn.ruleToStartState[p.ruleIndex];
if (ruleStartState.isLeftRecursiveRule) {
Pair<ParserRuleContext, Integer> parentContext = _parentContextStack.pop();
unrollRecursionContexts(parentContext.a);
setState(parentContext.b);
} else {
exitRule();
}
RuleTransition ruleTransition = (RuleTransition) atn.states.get(getState()).transition(0);
setState(ruleTransition.followState.stateNumber);
}
Aggregations