LexerATNSimulator and ParserATNSimulator switch on result of getSerializationType() instead of performing multiple type checks

This commit is contained in:
Sam Harwell 2012-08-02 12:20:44 -05:00
parent ff4eb0f744
commit 54a3759412
2 changed files with 106 additions and 43 deletions

View File

@ -452,7 +452,8 @@ public class LexerATNSimulator extends ATNSimulator {
@Nullable
public ATNState getReachableTarget(Transition trans, int t) {
if ( trans instanceof AtomTransition ) {
switch (trans.getSerializationType()) {
case Transition.ATOM:
AtomTransition at = (AtomTransition)trans;
if ( at.label == t ) {
if ( debug ) {
@ -461,8 +462,10 @@ public class LexerATNSimulator extends ATNSimulator {
return at.target;
}
}
else if ( trans.getClass() == RangeTransition.class ) {
return null;
case Transition.RANGE:
RangeTransition rt = (RangeTransition)trans;
if ( t>=rt.from && t<=rt.to ) {
if ( debug ) {
@ -471,24 +474,44 @@ public class LexerATNSimulator extends ATNSimulator {
return rt.target;
}
}
else if ( trans instanceof SetTransition ) {
return null;
case Transition.SET:
SetTransition st = (SetTransition)trans;
boolean not = trans instanceof NotSetTransition;
if ( (!not && st.set.contains(t)) ||
(not && !st.set.contains(t) && t!=CharStream.EOF) ) // ~set doesn't not match EOF
{
if ( st.set.contains(t) ) {
if ( debug ) {
System.out.format("match %sset %s\n", not ? "~" : "", st.set.toString(true));
System.out.format("match set %s\n", st.set.toString(true));
}
return st.target;
}
return null;
case Transition.NOT_SET:
NotSetTransition nst = (NotSetTransition)trans;
if (!nst.set.contains(t) && t!=CharStream.EOF) // ~set doesn't not match EOF
{
if ( debug ) {
System.out.format("match ~set %s\n", nst.set.toString(true));
}
return nst.target;
}
return null;
case Transition.WILDCARD:
if (t != CharStream.EOF) {
return trans.target;
}
return null;
default:
return null;
}
else if ( trans instanceof WildcardTransition && t!=CharStream.EOF ) {
return trans.target;
}
return null;
}
public void deleteWildcardConfigsForAlt(@NotNull ATNConfigSet closure, int ci, int alt) {
@ -572,13 +595,16 @@ public class LexerATNSimulator extends ATNSimulator {
@NotNull ATNConfigSet configs)
{
ATNState p = config.state;
ATNConfig c = null;
if ( t.getClass() == RuleTransition.class ) {
ATNConfig c;
switch (t.getSerializationType()) {
case Transition.RULE:
RuleContext newContext =
new RuleContext(config.context, p.stateNumber);
c = new ATNConfig(config, t.target, newContext);
}
else if ( t.getClass() == PredicateTransition.class ) {
break;
case Transition.PREDICATE:
if (recog == null) {
System.out.format("Predicates cannot be evaluated without a recognizer; assuming true.\n");
}
@ -609,15 +635,27 @@ public class LexerATNSimulator extends ATNSimulator {
if ( recog == null || recog.sempred(null, pt.ruleIndex, pt.predIndex) ) {
c = new ATNConfig(config, t.target, pt.getPredicate());
}
}
// ignore actions; just exec one per rule upon accept
else if ( t.getClass() == ActionTransition.class ) {
else {
c = null;
}
break;
case Transition.ACTION:
// ignore actions; just exec one per rule upon accept
c = new ATNConfig(config, t.target);
c.lexerActionIndex = ((ActionTransition)t).actionIndex;
}
else if ( t.isEpsilon() ) {
break;
case Transition.EPSILON:
c = new ATNConfig(config, t.target);
break;
default:
c = null;
break;
}
return c;
}

View File

@ -737,27 +737,49 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
@Nullable
public ATNState getReachableTarget(@NotNull Transition trans, int ttype) {
if ( trans instanceof AtomTransition ) {
switch (trans.getSerializationType()) {
case Transition.ATOM:
AtomTransition at = (AtomTransition)trans;
if ( at.label == ttype ) {
return at.target;
}
}
else if ( trans instanceof SetTransition ) {
return null;
case Transition.SET:
SetTransition st = (SetTransition)trans;
boolean not = trans instanceof NotSetTransition;
if ( !not && st.set.contains(ttype) || not && !st.set.contains(ttype) ) {
if ( st.set.contains(ttype) ) {
return st.target;
}
}
else if ( trans instanceof RangeTransition ) { // TODO: can't happen in parser, right? remove
return null;
case Transition.NOT_SET:
NotSetTransition nst = (NotSetTransition)trans;
if ( !nst.set.contains(ttype) ) {
return nst.target;
}
return null;
case Transition.RANGE:
RangeTransition rt = (RangeTransition)trans;
if ( ttype>=rt.from && ttype<=rt.to ) return rt.target;
if ( ttype>=rt.from && ttype<=rt.to ) {
return rt.target;
}
return null;
case Transition.WILDCARD:
if (ttype != Token.EOF) {
return trans.target;
}
return null;
default:
return null;
}
else if ( trans instanceof WildcardTransition && ttype!=Token.EOF ) {
return trans.target;
}
return null;
}
/** collect and set D's semantic context */
@ -1023,19 +1045,22 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
@Nullable
public ATNConfig getEpsilonTarget(@NotNull ATNConfig config, @NotNull Transition t, boolean collectPredicates, boolean inContext) {
if ( t instanceof RuleTransition ) {
switch (t.getSerializationType()) {
case Transition.RULE:
return ruleTransition(config, t);
}
else if ( t instanceof PredicateTransition ) {
case Transition.PREDICATE:
return predTransition(config, (PredicateTransition)t, collectPredicates, inContext);
}
else if ( t instanceof ActionTransition ) {
case Transition.ACTION:
return actionTransition(config, (ActionTransition)t);
}
else if ( t.isEpsilon() ) {
case Transition.EPSILON:
return new ATNConfig(config, t.target);
default:
return null;
}
return null;
}
@NotNull