Merge remote-tracking branch 'sharwell/serialization-type'

This commit is contained in:
Terence Parr 2012-08-04 12:03:24 -07:00
commit f8aeb04eda
12 changed files with 160 additions and 52 deletions

View File

@ -31,7 +31,7 @@ package org.antlr.v4.runtime.atn;
import org.antlr.v4.runtime.misc.NotNull;
public class ActionTransition extends Transition {
public final class ActionTransition extends Transition {
public final int ruleIndex;
public final int actionIndex;
public final boolean isCtxDependent; // e.g., $i ref in action
@ -47,6 +47,11 @@ public class ActionTransition extends Transition {
this.isCtxDependent = isCtxDependent;
}
@Override
public int getSerializationType() {
return ACTION;
}
@Override
public boolean isEpsilon() {
return true; // we are to be ignored by analysis 'cept for predicates

View File

@ -33,7 +33,7 @@ import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.IntervalSet;
/** TODO: make all transitions sets? no, should remove set edges */
public class AtomTransition extends Transition {
public final class AtomTransition extends Transition {
/** The token type or character value; or, signifies special label. */
public final int label;
@ -42,6 +42,11 @@ public class AtomTransition extends Transition {
this.label = label;
}
@Override
public int getSerializationType() {
return ATOM;
}
@Override
@NotNull
public IntervalSet label() { return IntervalSet.of(label); }

View File

@ -31,9 +31,14 @@ package org.antlr.v4.runtime.atn;
import org.antlr.v4.runtime.misc.NotNull;
public class EpsilonTransition extends Transition {
public final class EpsilonTransition extends Transition {
public EpsilonTransition(@NotNull ATNState target) { super(target); }
@Override
public int getSerializationType() {
return EPSILON;
}
@Override
public boolean isEpsilon() { return true; }

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

@ -33,11 +33,16 @@ import org.antlr.v4.runtime.misc.IntervalSet;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable;
public class NotSetTransition extends SetTransition {
public final class NotSetTransition extends SetTransition {
public NotSetTransition(@NotNull ATNState target, @Nullable IntervalSet set) {
super(target, set);
}
@Override
public int getSerializationType() {
return NOT_SET;
}
@Override
public String toString() {
return '~'+super.toString();

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

View File

@ -37,7 +37,7 @@ import org.antlr.v4.runtime.misc.NotNull;
* may have to combine a bunch of them as it collects predicates from
* multiple ATN configurations into a single DFA state.
*/
public class PredicateTransition extends Transition {
public final class PredicateTransition extends Transition {
public final int ruleIndex;
public final int predIndex;
public final boolean isCtxDependent; // e.g., $i ref in pred
@ -49,6 +49,11 @@ public class PredicateTransition extends Transition {
this.isCtxDependent = isCtxDependent;
}
@Override
public int getSerializationType() {
return PREDICATE;
}
@Override
public boolean isEpsilon() { return true; }

View File

@ -32,7 +32,7 @@ package org.antlr.v4.runtime.atn;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.IntervalSet;
public class RangeTransition extends Transition {
public final class RangeTransition extends Transition {
public final int from;
public final int to;
@ -42,6 +42,11 @@ public class RangeTransition extends Transition {
this.to = to;
}
@Override
public int getSerializationType() {
return RANGE;
}
@Override
@NotNull
public IntervalSet label() { return IntervalSet.of(from, to); }

View File

@ -32,7 +32,7 @@ package org.antlr.v4.runtime.atn;
import org.antlr.v4.runtime.misc.NotNull;
/** */
public class RuleTransition extends Transition {
public final class RuleTransition extends Transition {
/** Ptr to the rule definition object for this rule ref */
public final int ruleIndex; // no Rule object at runtime
@ -49,6 +49,11 @@ public class RuleTransition extends Transition {
this.followState = followState;
}
@Override
public int getSerializationType() {
return RULE;
}
@Override
public boolean isEpsilon() { return true; }
}

View File

@ -46,6 +46,11 @@ public class SetTransition extends Transition {
this.set = set;
}
@Override
public int getSerializationType() {
return SET;
}
@Override
@NotNull
public IntervalSet label() { return set; }

View File

@ -99,7 +99,7 @@ public abstract class Transition {
this.target = target;
}
public int getSerializationType() { return 0; }
public abstract int getSerializationType();
/** Are we epsilon, action, sempred? */
public boolean isEpsilon() { return false; }

View File

@ -31,9 +31,14 @@ package org.antlr.v4.runtime.atn;
import org.antlr.v4.runtime.misc.NotNull;
public class WildcardTransition extends Transition {
public final class WildcardTransition extends Transition {
public WildcardTransition(@NotNull ATNState target) { super(target); }
@Override
public int getSerializationType() {
return WILDCARD;
}
@Override
@NotNull
public String toString() {