LexerATNSimulator and ParserATNSimulator switch on result of getSerializationType() instead of performing multiple type checks
This commit is contained in:
parent
ff4eb0f744
commit
54a3759412
|
@ -452,7 +452,8 @@ public class LexerATNSimulator extends ATNSimulator {
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public ATNState getReachableTarget(Transition trans, int t) {
|
public ATNState getReachableTarget(Transition trans, int t) {
|
||||||
if ( trans instanceof AtomTransition ) {
|
switch (trans.getSerializationType()) {
|
||||||
|
case Transition.ATOM:
|
||||||
AtomTransition at = (AtomTransition)trans;
|
AtomTransition at = (AtomTransition)trans;
|
||||||
if ( at.label == t ) {
|
if ( at.label == t ) {
|
||||||
if ( debug ) {
|
if ( debug ) {
|
||||||
|
@ -461,8 +462,10 @@ public class LexerATNSimulator extends ATNSimulator {
|
||||||
|
|
||||||
return at.target;
|
return at.target;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else if ( trans.getClass() == RangeTransition.class ) {
|
return null;
|
||||||
|
|
||||||
|
case Transition.RANGE:
|
||||||
RangeTransition rt = (RangeTransition)trans;
|
RangeTransition rt = (RangeTransition)trans;
|
||||||
if ( t>=rt.from && t<=rt.to ) {
|
if ( t>=rt.from && t<=rt.to ) {
|
||||||
if ( debug ) {
|
if ( debug ) {
|
||||||
|
@ -471,24 +474,44 @@ public class LexerATNSimulator extends ATNSimulator {
|
||||||
|
|
||||||
return rt.target;
|
return rt.target;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else if ( trans instanceof SetTransition ) {
|
return null;
|
||||||
|
|
||||||
|
case Transition.SET:
|
||||||
SetTransition st = (SetTransition)trans;
|
SetTransition st = (SetTransition)trans;
|
||||||
boolean not = trans instanceof NotSetTransition;
|
if ( st.set.contains(t) ) {
|
||||||
if ( (!not && st.set.contains(t)) ||
|
|
||||||
(not && !st.set.contains(t) && t!=CharStream.EOF) ) // ~set doesn't not match EOF
|
|
||||||
{
|
|
||||||
if ( debug ) {
|
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 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) {
|
public void deleteWildcardConfigsForAlt(@NotNull ATNConfigSet closure, int ci, int alt) {
|
||||||
|
@ -572,13 +595,16 @@ public class LexerATNSimulator extends ATNSimulator {
|
||||||
@NotNull ATNConfigSet configs)
|
@NotNull ATNConfigSet configs)
|
||||||
{
|
{
|
||||||
ATNState p = config.state;
|
ATNState p = config.state;
|
||||||
ATNConfig c = null;
|
ATNConfig c;
|
||||||
if ( t.getClass() == RuleTransition.class ) {
|
|
||||||
|
switch (t.getSerializationType()) {
|
||||||
|
case Transition.RULE:
|
||||||
RuleContext newContext =
|
RuleContext newContext =
|
||||||
new RuleContext(config.context, p.stateNumber);
|
new RuleContext(config.context, p.stateNumber);
|
||||||
c = new ATNConfig(config, t.target, newContext);
|
c = new ATNConfig(config, t.target, newContext);
|
||||||
}
|
break;
|
||||||
else if ( t.getClass() == PredicateTransition.class ) {
|
|
||||||
|
case Transition.PREDICATE:
|
||||||
if (recog == null) {
|
if (recog == null) {
|
||||||
System.out.format("Predicates cannot be evaluated without a recognizer; assuming true.\n");
|
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) ) {
|
if ( recog == null || recog.sempred(null, pt.ruleIndex, pt.predIndex) ) {
|
||||||
c = new ATNConfig(config, t.target, pt.getPredicate());
|
c = new ATNConfig(config, t.target, pt.getPredicate());
|
||||||
}
|
}
|
||||||
}
|
else {
|
||||||
// ignore actions; just exec one per rule upon accept
|
c = null;
|
||||||
else if ( t.getClass() == ActionTransition.class ) {
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Transition.ACTION:
|
||||||
|
// ignore actions; just exec one per rule upon accept
|
||||||
c = new ATNConfig(config, t.target);
|
c = new ATNConfig(config, t.target);
|
||||||
c.lexerActionIndex = ((ActionTransition)t).actionIndex;
|
c.lexerActionIndex = ((ActionTransition)t).actionIndex;
|
||||||
}
|
break;
|
||||||
else if ( t.isEpsilon() ) {
|
|
||||||
|
case Transition.EPSILON:
|
||||||
c = new ATNConfig(config, t.target);
|
c = new ATNConfig(config, t.target);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
c = null;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -737,27 +737,49 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public ATNState getReachableTarget(@NotNull Transition trans, int ttype) {
|
public ATNState getReachableTarget(@NotNull Transition trans, int ttype) {
|
||||||
if ( trans instanceof AtomTransition ) {
|
switch (trans.getSerializationType()) {
|
||||||
|
case Transition.ATOM:
|
||||||
AtomTransition at = (AtomTransition)trans;
|
AtomTransition at = (AtomTransition)trans;
|
||||||
if ( at.label == ttype ) {
|
if ( at.label == ttype ) {
|
||||||
return at.target;
|
return at.target;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else if ( trans instanceof SetTransition ) {
|
return null;
|
||||||
|
|
||||||
|
case Transition.SET:
|
||||||
SetTransition st = (SetTransition)trans;
|
SetTransition st = (SetTransition)trans;
|
||||||
boolean not = trans instanceof NotSetTransition;
|
if ( st.set.contains(ttype) ) {
|
||||||
if ( !not && st.set.contains(ttype) || not && !st.set.contains(ttype) ) {
|
|
||||||
return st.target;
|
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;
|
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 */
|
/** collect and set D's semantic context */
|
||||||
|
@ -1023,19 +1045,22 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public ATNConfig getEpsilonTarget(@NotNull ATNConfig config, @NotNull Transition t, boolean collectPredicates, boolean inContext) {
|
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);
|
return ruleTransition(config, t);
|
||||||
}
|
|
||||||
else if ( t instanceof PredicateTransition ) {
|
case Transition.PREDICATE:
|
||||||
return predTransition(config, (PredicateTransition)t, collectPredicates, inContext);
|
return predTransition(config, (PredicateTransition)t, collectPredicates, inContext);
|
||||||
}
|
|
||||||
else if ( t instanceof ActionTransition ) {
|
case Transition.ACTION:
|
||||||
return actionTransition(config, (ActionTransition)t);
|
return actionTransition(config, (ActionTransition)t);
|
||||||
}
|
|
||||||
else if ( t.isEpsilon() ) {
|
case Transition.EPSILON:
|
||||||
return new ATNConfig(config, t.target);
|
return new ATNConfig(config, t.target);
|
||||||
|
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|
Loading…
Reference in New Issue