v4: @NotNull and @Nullable annotations for fields and methods

mark fields final
no behavior changes

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9389]
This commit is contained in:
sharwell 2011-11-18 11:29:58 -08:00
parent 909ea67d6b
commit ca141afc28
29 changed files with 413 additions and 286 deletions

View File

@ -28,6 +28,7 @@
*/
package org.antlr.v4.runtime;
import com.sun.istack.internal.NotNull;
import org.antlr.v4.runtime.atn.ATN;
import org.antlr.v4.runtime.atn.ATNState;
import org.antlr.v4.runtime.misc.Interval;
@ -49,6 +50,7 @@ import java.util.List;
* ATN simulation to record invoking states.
*/
public class RuleContext implements ParseTree.RuleNode {
@NotNull
public static final RuleContext EMPTY = new RuleContext();
/** What context invoked this rule? */

View File

@ -29,6 +29,7 @@
package org.antlr.v4.runtime.atn;
import com.sun.istack.internal.NotNull;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.misc.IntervalSet;
@ -43,18 +44,21 @@ public class ATN {
public static final int LEXER = 2;
public static final int TREE_PARSER = 3;
public List<ATNState> states = new ArrayList<ATNState>();
@NotNull
public final List<ATNState> states = new ArrayList<ATNState>();
/** Each subrule/rule is a decision point and we must track them so we
* can go back later and build DFA predictors for them. This includes
* all the rules, subrules, optional blocks, ()+, ()* etc...
*/
public List<DecisionState> decisionToState = new ArrayList<DecisionState>();
@NotNull
public final List<DecisionState> decisionToState = new ArrayList<DecisionState>();
public RuleStartState[] ruleToStartState;
public RuleStopState[] ruleToStopState;
public Map<String, TokensStartState> modeNameToStartState =
@NotNull
public final Map<String, TokensStartState> modeNameToStartState =
new LinkedHashMap<String, TokensStartState>();
// runtime for parsers, lexers
@ -64,7 +68,8 @@ public class ATN {
// runtime for lexer only
public int[] ruleToTokenType;
public int[] ruleToActionIndex;
public List<TokensStartState> modeToStartState = new ArrayList<TokensStartState>();
@NotNull
public final List<TokensStartState> modeToStartState = new ArrayList<TokensStartState>();
/** used during construction from grammar AST */
int stateNumber = 0;
@ -92,13 +97,13 @@ public class ATN {
return next;
}
public void addState(ATNState state) {
public void addState(@NotNull ATNState state) {
state.atn = this;
states.add(state);
state.stateNumber = stateNumber++;
}
public int defineDecisionState(DecisionState s) {
public int defineDecisionState(@NotNull DecisionState s) {
decisionToState.add(s);
s.decision = decisionToState.size()-1;
return s.decision;

View File

@ -29,6 +29,8 @@
package org.antlr.v4.runtime.atn;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import org.antlr.v4.runtime.*;
/** An ATN state, predicted alt, and syntactic/semantic context.
@ -39,16 +41,18 @@ import org.antlr.v4.runtime.*;
*/
public class ATNConfig {
/** The ATN state associated with this configuration */
public ATNState state;
@NotNull
public final ATNState state;
/** What alt (or lexer rule) is predicted by this configuration */
public int alt;
public final int alt;
/** The stack of invoking states leading to the rule/states associated
* with this config. We track only those contexts pushed during
* execution of the ATN simulator.
*/
public RuleContext context;
@Nullable
public final RuleContext context;
/**
* Indicates that we have reached this ATN configuration after
@ -81,68 +85,67 @@ public class ATNConfig {
*/
public int reachesIntoOuterContext;
public ATNConfig(ATNState state,
public ATNConfig(@NotNull ATNState state,
int alt,
RuleContext context)
@Nullable RuleContext context)
{
this.state = state;
this.alt = alt;
this.context = context;
}
public ATNConfig(ATNConfig c) {
this.state = c.state;
public ATNConfig(@NotNull ATNConfig c) {
this(c, c.state, c.context);
}
public ATNConfig(@NotNull ATNConfig c, @NotNull ATNState state) {
this(c, state, c.context);
}
public ATNConfig(@NotNull ATNConfig c, @NotNull ATNState state, @Nullable RuleContext context) {
this.state = state;
this.alt = c.alt;
this.context = c.context;
this.context = context;
this.traversedPredicate = c.traversedPredicate;
this.traversedAction = c.traversedAction;
this.reachesIntoOuterContext = c.reachesIntoOuterContext;
}
public ATNConfig(ATNConfig c, ATNState state) {
this(c);
this.state = state;
}
public ATNConfig(ATNConfig c, ATNState state, RuleContext context) {
this(c);
this.state = state;
this.context = context;
}
public ATNConfig(ATNConfig c, RuleContext context) {
this(c);
this.context = context;
public ATNConfig(@NotNull ATNConfig c, @Nullable RuleContext context) {
this(c, c.state, context);
}
/** An ATN configuration is equal to another if both have
* the same state, they predict the same alternative, and
* syntactic/semantic contexts are the same.
*/
@Override
public boolean equals(Object o) {
if ( o==null ) return false;
if ( this==o ) return true;
if (!(o instanceof ATNConfig)) {
return false;
}
ATNConfig other = (ATNConfig)o;
return this.state.stateNumber==other.state.stateNumber &&
this.alt==other.alt &&
(this.context==other.context ||
this.context.equals(other.context));
(this.context==other.context || (this.context != null && this.context.equals(other.context)));
}
@Override
public int hashCode() {
if ( state==null ) {
System.out.println("eh?");
}
int h = state.stateNumber + alt;
if ( context!=null ) h += context.hashCode();
return h;
}
@Override
public String toString() {
return toString(null, true);
}
public String toString(Recognizer<?, ?> recog, boolean showAlt) {
public String toString(@Nullable Recognizer<?, ?> recog, boolean showAlt) {
StringBuilder buf = new StringBuilder();
// if ( state.ruleIndex>=0 ) {
// if ( recog!=null ) buf.append(recog.getRuleNames()[state.ruleIndex]+":");

View File

@ -29,6 +29,7 @@
package org.antlr.v4.runtime.atn;
import com.sun.istack.internal.NotNull;
import org.antlr.v4.runtime.dfa.DFAState;
import org.antlr.v4.runtime.misc.*;
@ -36,19 +37,21 @@ import java.util.*;
public abstract class ATNSimulator {
/** Must distinguish between missing edge and edge we know leads nowhere */
public static DFAState ERROR;
public ATN atn;
@NotNull
public static final DFAState ERROR;
@NotNull
public final ATN atn;
static {
ERROR = new DFAState(new OrderedHashSet<ATNConfig>());
ERROR.stateNumber = Integer.MAX_VALUE;
}
public ATNSimulator(ATN atn) {
public ATNSimulator(@NotNull ATN atn) {
this.atn = atn;
}
public static ATN deserialize(char[] data) {
public static ATN deserialize(@NotNull char[] data) {
ATN atn = new ATN();
List<IntervalSet> sets = new ArrayList<IntervalSet>();
int p = 0;
@ -131,7 +134,7 @@ public abstract class ATNSimulator {
return c==65535 ? -1 : c;
}
public static Transition edgeFactory(ATN atn,
public static Transition edgeFactory(@NotNull ATN atn,
int type, int src, int trg,
int arg1, int arg2, int arg3,
List<IntervalSet> sets)
@ -139,21 +142,19 @@ public abstract class ATNSimulator {
ATNState target = atn.states.get(trg);
switch (type) {
case Transition.EPSILON : return new EpsilonTransition(target);
case Transition.RANGE : return new RangeTransition(arg1, arg2, target);
case Transition.RANGE : return new RangeTransition(target, arg1, arg2);
case Transition.RULE :
RuleTransition rt = new RuleTransition(arg2, atn.states.get(arg1), target);
RuleTransition rt = new RuleTransition((RuleStartState)atn.states.get(arg1), arg2, target);
return rt;
case Transition.PREDICATE :
PredicateTransition pt = new PredicateTransition(target, arg1, arg2);
pt.isCtxDependent = arg3==1;
PredicateTransition pt = new PredicateTransition(target, arg1, arg2, arg3 != 0);
return pt;
case Transition.ATOM : return new AtomTransition(arg1, target);
case Transition.ATOM : return new AtomTransition(target, arg1);
case Transition.ACTION :
ActionTransition a = new ActionTransition(target, arg1, arg2);
a.isCtxDependent = arg3==1;
ActionTransition a = new ActionTransition(target, arg1, arg2, arg3 != 0);
return a;
case Transition.SET : return new SetTransition(sets.get(arg1), target);
case Transition.NOT_SET : return new NotSetTransition(sets.get(arg1), null, target);
case Transition.SET : return new SetTransition(target, sets.get(arg1));
case Transition.NOT_SET : return new NotSetTransition(target, sets.get(arg1), null);
case Transition.WILDCARD : return new WildcardTransition(target);
}
return null;

View File

@ -55,23 +55,24 @@ public class ATNState {
public static final int STAR_LOOP_ENTRY = 10;
public static final int PLUS_LOOP_BACK = 11;
public static String[] serializationNames = {
"INVALID",
"BASIC",
"RULE_START",
"BLOCK_START",
"PLUS_BLOCK_START",
"STAR_BLOCK_START",
"TOKEN_START",
"RULE_STOP",
"BLOCK_END",
"STAR_LOOP_BACK",
"STAR_LOOP_ENTRY",
"PLUS_LOOP_BACK",
};
public static final List<String> serializationNames =
Collections.unmodifiableList(Arrays.asList(
"INVALID",
"BASIC",
"RULE_START",
"BLOCK_START",
"PLUS_BLOCK_START",
"STAR_BLOCK_START",
"TOKEN_START",
"RULE_STOP",
"BLOCK_END",
"STAR_LOOP_BACK",
"STAR_LOOP_ENTRY",
"PLUS_LOOP_BACK"
));
public static Map<Class<? extends ATNState>, Integer> serializationTypes =
new HashMap<Class<? extends ATNState>, Integer>() {{
public static final Map<Class<? extends ATNState>, Integer> serializationTypes =
Collections.unmodifiableMap(new HashMap<Class<? extends ATNState>, Integer>() {{
put(ATNState.class, BASIC);
put(RuleStartState.class, RULE_START);
put(BlockStartState.class, BLOCK_START);
@ -83,7 +84,7 @@ public class ATNState {
put(PlusLoopbackState.class, PLUS_LOOP_BACK);
put(StarLoopbackState.class, STAR_LOOP_BACK);
put(StarLoopEntryState.class, STAR_LOOP_ENTRY);
}};
}});
public static final int INVALID_STATE_NUMBER = -1;
@ -99,7 +100,7 @@ public class ATNState {
//public Transition transition;
/** Track the transitions emanating from this ATN state. */
protected List<Transition> transitions =
protected final List<Transition> transitions =
new ArrayList<Transition>(INITIAL_NUM_TRANSITIONS);
/** For o-A->o type ATN tranitions, record the label that leads to this

View File

@ -29,19 +29,22 @@
package org.antlr.v4.runtime.atn;
public class ActionTransition extends Transition {
public int ruleIndex;
public int actionIndex = -1;
public boolean isCtxDependent; // e.g., $i ref in action
import com.sun.istack.internal.NotNull;
public ActionTransition(ATNState target) {
super(target);
public class ActionTransition extends Transition {
public final int ruleIndex;
public final int actionIndex;
public final boolean isCtxDependent; // e.g., $i ref in action
public ActionTransition(@NotNull ATNState target, int ruleIndex) {
this(target, ruleIndex, -1, false);
}
public ActionTransition(ATNState target, int ruleIndex, int actionIndex) {
public ActionTransition(@NotNull ATNState target, int ruleIndex, int actionIndex, boolean isCtxDependent) {
super(target);
this.ruleIndex = ruleIndex;
this.actionIndex = actionIndex;
this.isCtxDependent = isCtxDependent;
}
@Override

View File

@ -29,25 +29,26 @@
package org.antlr.v4.runtime.atn;
import com.sun.istack.internal.NotNull;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.misc.IntervalSet;
/** TODO: make all transitions sets? no, should remove set edges */
public class AtomTransition extends Transition {
/** The token type or character value; or, signifies special label. */
public int label;
public final int label;
public AtomTransition(int label, ATNState target) {
this.label = label;
this.target = target;
}
public AtomTransition(ATNState target) {
public AtomTransition(@NotNull ATNState target, int label) {
super(target);
this.label = label;
}
@Override
@NotNull
public IntervalSet label() { return IntervalSet.of(label); }
@Override
@NotNull
public String toString() {
return String.valueOf(label);
}

View File

@ -29,12 +29,16 @@
package org.antlr.v4.runtime.atn;
import com.sun.istack.internal.NotNull;
public class EpsilonTransition extends Transition {
public EpsilonTransition(ATNState target) { super(target); }
public EpsilonTransition(@NotNull ATNState target) { super(target); }
@Override
public boolean isEpsilon() { return true; }
@Override
@NotNull
public String toString() {
return "epsilon";
}

View File

@ -29,8 +29,10 @@
package org.antlr.v4.runtime.atn;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.misc.IntervalSet;
import java.util.*;
@ -41,14 +43,16 @@ public class LL1Analyzer {
* loop.
*/
public ATN atn;
@NotNull
public final ATN atn;
public LL1Analyzer(ATN atn) { this.atn = atn; }
public LL1Analyzer(@NotNull ATN atn) { this.atn = atn; }
/** From an ATN state, s, find the set of all labels reachable from s at
* depth k. Only for DecisionStates.
*/
public IntervalSet[] getDecisionLookahead(ATNState s) {
@Nullable
public IntervalSet[] getDecisionLookahead(@Nullable ATNState s) {
// System.out.println("LOOK("+s.stateNumber+")");
if ( s==null ) return null;
IntervalSet[] look = new IntervalSet[s.getNumberOfTransitions()+1];
@ -60,14 +64,15 @@ public class LL1Analyzer {
return look;
}
public IntervalSet LOOK(ATNState s, @Nullable RuleContext ctx) {
@NotNull
public IntervalSet LOOK(@NotNull ATNState s, @Nullable RuleContext ctx) {
IntervalSet r = new IntervalSet();
_LOOK(s, ctx, r, new HashSet<ATNConfig>());
return r;
}
protected void _LOOK(ATNState s, @Nullable RuleContext ctx, IntervalSet look,
Set<ATNConfig> lookBusy) {
protected void _LOOK(@NotNull ATNState s, @Nullable RuleContext ctx, @NotNull IntervalSet look,
@NotNull Set<ATNConfig> lookBusy) {
// System.out.println("_LOOK("+s.stateNumber+", ctx="+ctx);
ATNConfig c = new ATNConfig(s, 0, ctx);
if ( lookBusy.contains(c) ) return;

View File

@ -29,6 +29,7 @@
package org.antlr.v4.runtime.atn;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.dfa.DFA;
@ -69,12 +70,14 @@ public class LexerATNSimulator extends ATNSimulator {
ATNConfig config = null;
}
protected Lexer recog;
@Nullable
protected final Lexer recog;
/** In case the stream is not offering characters, we need to track
* at minimum the text for the current token. This is what
* getText() returns.
*/
@NotNull
protected char[] text = new char[100];
protected int textIndex = -1;
@ -91,21 +94,24 @@ public class LexerATNSimulator extends ATNSimulator {
/** The index of the character relative to the beginning of the line 0..n-1 */
protected int charPositionInLine = 0;
@NotNull
protected DFA[] dfa;
protected int mode = Lexer.DEFAULT_MODE;
/** Used during DFA/ATN exec to record the most recent accept configuration info */
protected DFAExecState dfaPrevAccept = new DFAExecState();
protected ATNExecState atnPrevAccept = new ATNExecState();
@NotNull
protected final DFAExecState dfaPrevAccept = new DFAExecState();
@NotNull
protected final ATNExecState atnPrevAccept = new ATNExecState();
public static int ATN_failover = 0;
public static int match_calls = 0;
public LexerATNSimulator(ATN atn) {
public LexerATNSimulator(@NotNull ATN atn) {
this(null, atn);
}
public LexerATNSimulator(@Nullable Lexer recog, ATN atn) {
public LexerATNSimulator(@Nullable Lexer recog, @NotNull ATN atn) {
super(atn);
dfa = new DFA[atn.modeToStartState.size()];
for (int i=0; i<atn.modeToStartState.size(); i++) {
@ -114,7 +120,7 @@ public class LexerATNSimulator extends ATNSimulator {
this.recog = recog;
}
public int match(CharStream input, int mode) {
public int match(@NotNull CharStream input, int mode) {
match_calls++;
this.mode = mode;
if ( dfa[mode].s0==null ) {
@ -126,7 +132,7 @@ public class LexerATNSimulator extends ATNSimulator {
}
// only called from test code from outside
public int matchATN(CharStream input) {
public int matchATN(@NotNull CharStream input) {
textIndex = -1;
startIndex = input.index();
ATNState startState = atn.modeToStartState.get(mode);
@ -139,7 +145,7 @@ public class LexerATNSimulator extends ATNSimulator {
return predict;
}
protected int exec(CharStream input, DFAState s0) {
protected int exec(@NotNull CharStream input, @NotNull DFAState s0) {
if ( dfa_debug ) System.out.println("DFA[mode "+(recog==null?0:recog.mode)+"] exec LA(1)=="+
(char)input.LA(1));
//System.out.println("DFA start of execDFA: "+dfa[mode].toLexerString());
@ -198,12 +204,14 @@ public class LexerATNSimulator extends ATNSimulator {
return dfaPrevAccept.state.prediction;
}
protected int exec(CharStream input, OrderedHashSet<ATNConfig> s0) {
protected int exec(@NotNull CharStream input, @NotNull OrderedHashSet<ATNConfig> s0) {
//System.out.println("enter exec index "+input.index()+" from "+s0);
@NotNull
OrderedHashSet<ATNConfig> closure = new OrderedHashSet<ATNConfig>();
closure.addAll(s0);
if ( debug ) System.out.println("start state closure="+closure);
@NotNull
OrderedHashSet<ATNConfig> reach = new OrderedHashSet<ATNConfig>();
resetPrevAccept(atnPrevAccept);
atnPrevAccept.config = null;
@ -246,6 +254,7 @@ public class LexerATNSimulator extends ATNSimulator {
// swap to avoid reallocating space
// TODO: faster to reallocate?
@NotNull
OrderedHashSet<ATNConfig> tmp = reach;
reach = closure;
closure = tmp;
@ -266,7 +275,7 @@ public class LexerATNSimulator extends ATNSimulator {
return atn.ruleToTokenType[ruleIndex];
}
protected void processAcceptStates(CharStream input, OrderedHashSet<ATNConfig> reach) {
protected void processAcceptStates(@NotNull CharStream input, @NotNull OrderedHashSet<ATNConfig> reach) {
for (int ci=0; ci<reach.size(); ci++) {
ATNConfig c = reach.get(ci);
if ( c.state instanceof RuleStopState) {
@ -298,7 +307,7 @@ public class LexerATNSimulator extends ATNSimulator {
}
}
protected void accept(CharStream input, int ruleIndex, ExecState prevAccept) {
protected void accept(@NotNull CharStream input, int ruleIndex, @NotNull ExecState prevAccept) {
if ( debug ) {
if ( recog!=null ) System.out.println("ACTION "+
recog.getRuleNames()[ruleIndex]+
@ -316,6 +325,7 @@ public class LexerATNSimulator extends ATNSimulator {
consume(input);
}
@Nullable
public ATNState getReachableTarget(Transition trans, int t) {
if ( trans instanceof AtomTransition ) {
AtomTransition at = (AtomTransition)trans;
@ -349,7 +359,7 @@ public class LexerATNSimulator extends ATNSimulator {
return null;
}
public void deleteWildcardConfigsForAlt(OrderedHashSet<ATNConfig> closure, int ci, int alt) {
public void deleteWildcardConfigsForAlt(@NotNull OrderedHashSet<ATNConfig> closure, int ci, int alt) {
int j=ci+1;
while ( j<closure.size() ) {
ATNConfig c = closure.get(j);
@ -363,8 +373,9 @@ public class LexerATNSimulator extends ATNSimulator {
}
}
protected OrderedHashSet<ATNConfig> computeStartState(IntStream input,
ATNState p)
@NotNull
protected OrderedHashSet<ATNConfig> computeStartState(@NotNull IntStream input,
@NotNull ATNState p)
{
RuleContext initialContext = RuleContext.EMPTY;
OrderedHashSet<ATNConfig> configs = new OrderedHashSet<ATNConfig>();
@ -376,7 +387,7 @@ public class LexerATNSimulator extends ATNSimulator {
return configs;
}
protected void closure(ATNConfig config, OrderedHashSet<ATNConfig> configs) {
protected void closure(@NotNull ATNConfig config, @NotNull OrderedHashSet<ATNConfig> configs) {
if ( debug ) {
System.out.println("closure("+config.toString(recog, true)+")");
}
@ -416,7 +427,8 @@ public class LexerATNSimulator extends ATNSimulator {
}
}
public ATNConfig getEpsilonTarget(ATNConfig config, Transition t) {
@Nullable
public ATNConfig getEpsilonTarget(@NotNull ATNConfig config, @NotNull Transition t) {
ATNState p = config.state;
ATNConfig c = null;
if ( t.getClass() == RuleTransition.class ) {
@ -441,7 +453,7 @@ public class LexerATNSimulator extends ATNSimulator {
return c;
}
int failOverToATN(CharStream input, DFAState s) {
int failOverToATN(@NotNull CharStream input, @NotNull DFAState s) {
if ( dfa_debug ) System.out.println("no edge for "+(char)input.LA(1));
if ( dfa_debug ) {
System.out.println("ATN exec upon "+
@ -459,23 +471,23 @@ public class LexerATNSimulator extends ATNSimulator {
return ttype;
}
protected void markAcceptState(ExecState state, CharStream input) {
protected void markAcceptState(@NotNull ExecState state, @NotNull CharStream input) {
state.marker = input.mark();
state.index = input.index();
state.line = line;
state.charPos = charPositionInLine;
}
protected void resetPrevAccept(ExecState prevAccept) {
protected void resetPrevAccept(@NotNull ExecState prevAccept) {
prevAccept.marker = -1;
prevAccept.index = -1;
prevAccept.line = 0;
prevAccept.charPos = -1;
}
protected void addDFAEdge(OrderedHashSet<ATNConfig> p,
protected void addDFAEdge(@NotNull OrderedHashSet<ATNConfig> p,
int t,
OrderedHashSet<ATNConfig> q)
@NotNull OrderedHashSet<ATNConfig> q)
{
// System.out.println("MOVE "+p+" -> "+q+" upon "+getTokenName(t));
DFAState from = addDFAState(p);
@ -483,8 +495,9 @@ public class LexerATNSimulator extends ATNSimulator {
addDFAEdge(from, t, to);
}
protected void addDFAEdge(DFAState p, int t, DFAState q) {
if ( p==null || t==CharStream.EOF ) return; // Don't track EOF edges from stop states
// TODO (sam): should we allow q==null?
protected void addDFAEdge(@Nullable DFAState p, int t, DFAState q) {
if ( p==null || t==CharStream.EOF || q==null ) return; // Don't track EOF edges from stop states
if ( p.edges==null ) {
// make room for tokens 1..n and -1 masquerading as index 0
p.edges = new DFAState[NUM_EDGES+1]; // TODO: make adaptive
@ -518,7 +531,8 @@ public class LexerATNSimulator extends ATNSimulator {
states reached by traversing predicates. Since this is when we
test them, we cannot cash the DFA state target of ID.
*/
protected DFAState addDFAState(OrderedHashSet<ATNConfig> configs) {
@Nullable
protected DFAState addDFAState(@NotNull OrderedHashSet<ATNConfig> configs) {
DFAState proposed = new DFAState(configs);
DFAState existing = dfa[mode].states.get(proposed);
if ( existing!=null ) return existing;
@ -551,11 +565,13 @@ public class LexerATNSimulator extends ATNSimulator {
return newState;
}
@Nullable
public DFA getDFA(int mode) {
return dfa[mode];
}
/** Get the text of the current token */
@NotNull
public String getText() {
if ( textIndex<0 ) return "";
return new String(text, 0, textIndex+1);
@ -569,7 +585,7 @@ public class LexerATNSimulator extends ATNSimulator {
return charPositionInLine;
}
public void consume(CharStream input) {
public void consume(@NotNull CharStream input) {
int curChar = input.LA(1);
if ( curChar!=CharStream.EOF ) {
if ( (textIndex+1)>=text.length ) {
@ -587,6 +603,7 @@ public class LexerATNSimulator extends ATNSimulator {
input.consume();
}
@NotNull
public String getTokenName(int t) {
if ( t==-1 ) return "EOF";
//if ( atn.g!=null ) return atn.g.getTokenDisplayName(t);

View File

@ -29,22 +29,21 @@
package org.antlr.v4.runtime.atn;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import org.antlr.v4.runtime.misc.IntervalSet;
public class NotSetTransition extends SetTransition {
// keep both set, notSet; we can only compute at construction time
// since only then do we have grammar, which knows token set for complement.
public IntervalSet notSet;
@Nullable
public final IntervalSet notSet;
public NotSetTransition(IntervalSet set, IntervalSet notSet, ATNState target) {
super(set, target);
public NotSetTransition(@NotNull ATNState target, @Nullable IntervalSet set, @Nullable IntervalSet notSet) {
super(target, set);
this.notSet = notSet;
}
public NotSetTransition(ATNState target) {
super(target);
}
@Override
public String toString() {
return '~'+super.toString();

View File

@ -29,6 +29,8 @@
package org.antlr.v4.runtime.atn;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.dfa.*;
import org.antlr.v4.runtime.misc.OrderedHashSet;
@ -46,12 +48,14 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
public static int retry_with_context = 0;
public static int retry_with_context_indicates_no_conflict = 0;
@Nullable
protected final BaseRecognizer<TSymbol> parser;
protected BaseRecognizer<TSymbol> parser;
public Map<RuleContext, DFA[]> ctxToDFAs;
@NotNull
public final Map<RuleContext, DFA[]> ctxToDFAs;
public Map<RuleContext, DFA>[] decisionToDFAPerCtx; // TODO: USE THIS ONE
public DFA[] decisionToDFA;
@NotNull
public final DFA[] decisionToDFA;
protected boolean userWantsCtxSensitive = false;
/** This is the original context upon entry to the ATN simulator.
@ -63,27 +67,28 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
*
* The full stack at any moment is [config.outerContext + config.context].
*/
protected RuleContext outerContext;
@NotNull
protected RuleContext outerContext = RuleContext.EMPTY;
@Nullable
protected ATNConfig prevAccept; // TODO Move down? used to avoid passing int down and back up in method calls
protected int prevAcceptIndex = -1;
public ParserATNSimulator(ATN atn) {
super(atn);
ctxToDFAs = new HashMap<RuleContext, DFA[]>();
decisionToDFA = new DFA[atn.getNumberOfDecisions()];
public ParserATNSimulator(@NotNull ATN atn) {
this(null, atn);
}
public ParserATNSimulator(BaseRecognizer<TSymbol> parser, ATN atn) {
public ParserATNSimulator(@Nullable BaseRecognizer<TSymbol> parser, @NotNull ATN atn) {
super(atn);
this.parser = parser;
ctxToDFAs = new HashMap<RuleContext, DFA[]>();
decisionToDFA = new DFA[atn.getNumberOfDecisions()+1];
// TODO (sam): why distinguish on parser != null?
decisionToDFA = new DFA[atn.getNumberOfDecisions() + (parser != null ? 1 : 0)];
// DOTGenerator dot = new DOTGenerator(null);
// System.out.println(dot.getDOT(atn.rules.get(0), parser.getRuleNames()));
// System.out.println(dot.getDOT(atn.rules.get(1), parser.getRuleNames()));
}
public int adaptivePredict(SymbolStream<TSymbol> input, int decision, RuleContext outerContext) {
public int adaptivePredict(@NotNull SymbolStream<TSymbol> input, int decision, @Nullable RuleContext outerContext) {
predict_calls++;
DFA dfa = decisionToDFA[decision];
if ( dfa==null || dfa.s0==null ) {
@ -108,8 +113,8 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
}
}
public int predictATN(DFA dfa, SymbolStream<TSymbol> input,
RuleContext outerContext,
public int predictATN(@NotNull DFA dfa, @NotNull SymbolStream<TSymbol> input,
@Nullable RuleContext outerContext,
boolean useContext)
{
if ( outerContext==null ) outerContext = RuleContext.EMPTY;
@ -146,15 +151,14 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
}
// doesn't create DFA when matching
public int matchATN(SymbolStream<TSymbol> input, ATNState startState) {
public int matchATN(@NotNull SymbolStream<TSymbol> input, @NotNull ATNState startState) {
DFA dfa = new DFA(startState);
if ( outerContext==null ) outerContext = RuleContext.EMPTY;
RuleContext ctx = RuleContext.EMPTY;
OrderedHashSet<ATNConfig> s0_closure = computeStartState(dfa.decision, startState, ctx);
return execATN(input, dfa, input.index(), s0_closure, false);
}
public int execDFA(SymbolStream<TSymbol> input, DFA dfa, DFAState s0, RuleContext outerContext) {
public int execDFA(@NotNull SymbolStream<TSymbol> input, @NotNull DFA dfa, @NotNull DFAState s0, @Nullable RuleContext outerContext) {
// dump(dfa);
if ( outerContext==null ) outerContext = RuleContext.EMPTY;
this.outerContext = outerContext;
@ -226,7 +230,7 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
}
DFAState target = s.edges[t+1];
if ( target == ERROR ) {
return throwNoViableAlt(input, outerContext, s.configs, startIndex);
throw noViableAlt(input, outerContext, s.configs, startIndex);
}
s = target;
input.consume();
@ -241,7 +245,7 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return prevAcceptState.prediction;
}
public String getInputString(SymbolStream<TSymbol> input, int start) {
public String getInputString(@NotNull SymbolStream<TSymbol> input, int start) {
if ( input instanceof TokenStream ) {
return ((TokenStream)input).toString(start,input.index());
}
@ -251,10 +255,10 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return "n/a";
}
public int execATN(SymbolStream<TSymbol> input,
DFA dfa,
public int execATN(@NotNull SymbolStream<TSymbol> input,
@NotNull DFA dfa,
int startIndex,
OrderedHashSet<ATNConfig> s0,
@NotNull OrderedHashSet<ATNConfig> s0,
boolean useContext)
{
if ( debug ) System.out.println("execATN decision "+dfa.decision+" exec LA(1)=="+ getLookaheadName(input));
@ -377,14 +381,14 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
if ( prevAccept==null ) {
// System.out.println("no viable token at input "+ getLookaheadName(input) +", index "+input.index());
throwNoViableAlt(input, outerContext, closure, startIndex);
throw noViableAlt(input, outerContext, closure, startIndex);
}
if ( debug ) System.out.println("PREDICT " + prevAccept + " index " + prevAccept.alt);
return prevAccept.alt;
}
protected int resolveToMinAlt(OrderedHashSet<ATNConfig> reach, Set<Integer> ambigAlts) {
protected int resolveToMinAlt(@NotNull OrderedHashSet<ATNConfig> reach, @NotNull Set<Integer> ambigAlts) {
int min = getMinAlt(ambigAlts);
// create DFA accept state for resolved alt
ambigAlts.remove(min);
@ -394,7 +398,7 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return min;
}
protected int resolveNongreedyToExitBranch(OrderedHashSet<ATNConfig> reach, Set<Integer> ambigAlts) {
protected int resolveNongreedyToExitBranch(@NotNull OrderedHashSet<ATNConfig> reach, @NotNull Set<Integer> ambigAlts) {
// exit branch is alt 2 always; alt 1 is entry or loopback branch
// since we're predicting, create DFA accept state for exit alt
int exitAlt = 2;
@ -405,14 +409,14 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return exitAlt;
}
public int retryWithContext(SymbolStream<TSymbol> input,
DFA dfa,
public int retryWithContext(@NotNull SymbolStream<TSymbol> input,
@NotNull DFA dfa,
int startIndex,
RuleContext originalContext,
OrderedHashSet<ATNConfig> closure,
@NotNull RuleContext originalContext,
@NotNull OrderedHashSet<ATNConfig> closure,
int t,
OrderedHashSet<ATNConfig> reach,
Set<Integer> ambigAlts)
@NotNull OrderedHashSet<ATNConfig> reach,
@NotNull Set<Integer> ambigAlts)
{
// ASSUMES PREDICT ONLY
retry_with_context++;
@ -466,7 +470,8 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return predictedAlt;
}
public OrderedHashSet<ATNConfig> computeStartState(int decision, ATNState p, RuleContext ctx) {
@NotNull
public OrderedHashSet<ATNConfig> computeStartState(int decision, @NotNull ATNState p, @Nullable RuleContext ctx) {
RuleContext initialContext = ctx; // always at least the implicit call to start rule
OrderedHashSet<ATNConfig> configs = new OrderedHashSet<ATNConfig>();
prevAccept = null; // might reach end rule; track
@ -485,7 +490,8 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return configs;
}
public ATNState getReachableTarget(Transition trans, int ttype) {
@Nullable
public ATNState getReachableTarget(@NotNull Transition trans, int ttype) {
if ( trans instanceof AtomTransition ) {
AtomTransition at = (AtomTransition)trans;
if ( at.label == ttype ) {
@ -516,10 +522,10 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
ambig detection thought :(
*/
protected void closure(ATNConfig config,
OrderedHashSet<ATNConfig> configs,
DecisionState decState,
Set<ATNConfig> closureBusy)
protected void closure(@NotNull ATNConfig config,
@NotNull OrderedHashSet<ATNConfig> configs,
@Nullable DecisionState decState,
@NotNull Set<ATNConfig> closureBusy)
{
if ( debug ) System.out.println("closure("+config+")");
@ -577,12 +583,14 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
}
}
@NotNull
private String getRuleName(int index) {
if ( parser!=null && index>=0 ) return parser.getRuleNames()[index];
return "<rule "+index+">";
}
public ATNConfig getEpsilonTarget(ATNConfig config, Transition t, boolean ignorePreds) {
@Nullable
public ATNConfig getEpsilonTarget(@NotNull ATNConfig config, @NotNull Transition t, boolean ignorePreds) {
if ( t instanceof RuleTransition ) {
return ruleTransition(config, t);
}
@ -590,7 +598,7 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return predTransition(config, (PredicateTransition)t, ignorePreds);
}
else if ( t instanceof ActionTransition ) {
return actionTransition(config, t);
return actionTransition(config, (ActionTransition)t);
}
else if ( t.isEpsilon() ) {
return new ATNConfig(config, t.target);
@ -598,9 +606,9 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return null;
}
public ATNConfig actionTransition(ATNConfig config, Transition t) {
ActionTransition at = (ActionTransition)t;
if ( debug ) System.out.println("ACTION edge "+at.ruleIndex+":"+at.actionIndex);
@NotNull
public ATNConfig actionTransition(@NotNull ATNConfig config, @NotNull ActionTransition t) {
if ( debug ) System.out.println("ACTION edge "+t.ruleIndex+":"+t.actionIndex);
if ( debug && !config.traversedAction ) {
System.out.println("pruning future pred eval derived from s"+
config.state.stateNumber);
@ -611,7 +619,8 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return c;
}
public ATNConfig predTransition(ATNConfig config, PredicateTransition pt,
@Nullable
public ATNConfig predTransition(@NotNull ATNConfig config, @NotNull PredicateTransition pt,
boolean ignorePreds)
{
if ( debug ) {
@ -647,7 +656,8 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return c;
}
public ATNConfig ruleTransition(ATNConfig config, Transition t) {
@NotNull
public ATNConfig ruleTransition(@NotNull ATNConfig config, @NotNull Transition t) {
if ( debug ) {
System.out.println("CALL rule "+getRuleName(t.target.ruleIndex)+
", ctx="+config.context);
@ -658,20 +668,20 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return new ATNConfig(config, t.target, newContext);
}
public void reportConflict(int startIndex, int stopIndex, Set<Integer> alts, OrderedHashSet<ATNConfig> configs) {
public void reportConflict(int startIndex, int stopIndex, @NotNull Set<Integer> alts, @NotNull OrderedHashSet<ATNConfig> configs) {
if ( parser!=null ) parser.reportConflict(startIndex, stopIndex, alts, configs);
}
public void reportContextSensitivity(int startIndex, int stopIndex, Set<Integer> alts, OrderedHashSet<ATNConfig> configs) {
public void reportContextSensitivity(int startIndex, int stopIndex, @NotNull Set<Integer> alts, @NotNull OrderedHashSet<ATNConfig> configs) {
if ( parser!=null ) parser.reportContextSensitivity(startIndex, stopIndex, alts, configs);
}
/** If context sensitive parsing, we know it's ambiguity not conflict */
public void reportAmbiguity(int startIndex, int stopIndex, Set<Integer> alts, OrderedHashSet<ATNConfig> configs) {
public void reportAmbiguity(int startIndex, int stopIndex, @NotNull Set<Integer> alts, @NotNull OrderedHashSet<ATNConfig> configs) {
if ( parser!=null ) parser.reportAmbiguity(startIndex, stopIndex, alts, configs);
}
public static int getUniqueAlt(Collection<ATNConfig> configs) {
public static int getUniqueAlt(@NotNull Collection<ATNConfig> configs) {
int alt = ATN.INVALID_ALT_NUMBER;
for (ATNConfig c : configs) {
if ( alt == ATN.INVALID_ALT_NUMBER ) {
@ -684,7 +694,8 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return alt;
}
public ATNConfig configWithAltAtStopState(Collection<ATNConfig> configs, int alt) {
@Nullable
public ATNConfig configWithAltAtStopState(@NotNull Collection<ATNConfig> configs, int alt) {
for (ATNConfig c : configs) {
if ( c.alt == alt ) {
if ( c.state.getClass() == RuleStopState.class ) {
@ -695,7 +706,8 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return null;
}
public Set<Integer> getAmbiguousAlts(OrderedHashSet<ATNConfig> configs) {
@Nullable
public Set<Integer> getAmbiguousAlts(@NotNull OrderedHashSet<ATNConfig> configs) {
// System.err.println("check ambiguous "+configs);
Set<Integer> ambigAlts = null;
int numConfigs = configs.size();
@ -739,7 +751,7 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return ambigAlts;
}
public static int getMinAlt(Set<Integer> ambigAlts) {
public static int getMinAlt(@NotNull Set<Integer> ambigAlts) {
int min = Integer.MAX_VALUE;
for (int alt : ambigAlts) {
if ( alt < min ) min = alt;
@ -747,7 +759,7 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return min;
}
public static void killAlts(Set<Integer> alts, OrderedHashSet<ATNConfig> configs) {
public static void killAlts(@NotNull Set<Integer> alts, @NotNull OrderedHashSet<ATNConfig> configs) {
int i = 0;
while ( i<configs.size() ) {
ATNConfig c = configs.get(i);
@ -758,10 +770,10 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
}
}
protected DFAState addDFAEdge(DFA dfa,
OrderedHashSet<ATNConfig> p,
protected DFAState addDFAEdge(@NotNull DFA dfa,
@NotNull OrderedHashSet<ATNConfig> p,
int t,
OrderedHashSet<ATNConfig> q)
@NotNull OrderedHashSet<ATNConfig> q)
{
// System.out.println("MOVE "+p+" -> "+q+" upon "+getTokenName(t));
DFAState from = addDFAState(dfa, p);
@ -770,8 +782,8 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return to;
}
protected void addDFAEdge(DFAState p, int t, DFAState q) {
if ( p==null || t < -1 ) return;
protected void addDFAEdge(@Nullable DFAState p, int t, @Nullable DFAState q) {
if ( p==null || t < -1 || q == null ) return;
if ( p.edges==null ) {
p.edges = new DFAState[atn.maxTokenType+1+1]; // TODO: make adaptive
}
@ -779,7 +791,8 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
}
/** See comment on LexerInterpreter.addDFAState. */
protected DFAState addDFAState(DFA dfa, OrderedHashSet<ATNConfig> configs) {
@Nullable
protected DFAState addDFAState(@NotNull DFA dfa, @NotNull OrderedHashSet<ATNConfig> configs) {
DFAState proposed = new DFAState(configs);
DFAState existing = dfa.states.get(proposed);
if ( existing!=null ) return existing;
@ -800,7 +813,7 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
return newState;
}
public void makeAcceptState(DFA dfa, OrderedHashSet<ATNConfig> reach, int uniqueAlt) {
public void makeAcceptState(@NotNull DFA dfa, @NotNull OrderedHashSet<ATNConfig> reach, int uniqueAlt) {
DFAState accept = dfa.states.get(new DFAState(reach));
if ( accept==null ) return;
accept.isAcceptState = true;
@ -808,6 +821,7 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
accept.complete = true;
}
@NotNull
public String getTokenName(int t) {
if ( t==-1 ) return "EOF";
if ( parser!=null && parser.getTokenNames()!=null ) {
@ -831,7 +845,7 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
this.userWantsCtxSensitive = ctxSensitive;
}
public void dumpDeadEndConfigs(NoViableAltException nvae) {
public void dumpDeadEndConfigs(@NotNull NoViableAltException nvae) {
System.err.println("dead end configs: ");
for (ATNConfig c : nvae.deadEndConfigs) {
Transition t = c.state.transition(0);
@ -849,22 +863,23 @@ public class ParserATNSimulator<TSymbol> extends ATNSimulator {
}
}
public int throwNoViableAlt(SymbolStream<TSymbol> input, RuleContext outerContext,
OrderedHashSet<ATNConfig> configs, int startIndex)
@NotNull
public NoViableAltException noViableAlt(@NotNull SymbolStream<TSymbol> input, @NotNull RuleContext outerContext,
@NotNull OrderedHashSet<ATNConfig> configs, int startIndex)
{
if ( parser instanceof TreeParser) {
TSymbol startNode = null;
if ( input instanceof BufferedASTNodeStream ) {
startNode = input.get(startIndex);
}
throw new NoViableTreeGrammarAltException(parser,
return new NoViableTreeGrammarAltException(parser,
(ASTNodeStream<TSymbol>)input,
startNode,
input.LT(1),
configs, outerContext);
}
else {
throw new NoViableAltException(parser, input,
return new NoViableAltException(parser, input,
(Token)input.get(startIndex),
(Token)input.LT(1),
input.LT(1),

View File

@ -29,6 +29,8 @@
package org.antlr.v4.runtime.atn;
import com.sun.istack.internal.NotNull;
/** TODO: this is old comment:
* A tree of semantic predicates from the grammar AST if label==SEMPRED.
* In the ATN, labels will always be exactly one predicate, but the DFA
@ -36,23 +38,22 @@ package org.antlr.v4.runtime.atn;
* multiple ATN configurations into a single DFA state.
*/
public class PredicateTransition extends Transition {
public int ruleIndex;
public int predIndex;
public boolean isCtxDependent; // e.g., $i ref in pred
public final int ruleIndex;
public final int predIndex;
public final boolean isCtxDependent; // e.g., $i ref in pred
public PredicateTransition(ATNState target) {
super(target);
}
public PredicateTransition(ATNState target, int ruleIndex, int predIndex) {
public PredicateTransition(@NotNull ATNState target, int ruleIndex, int predIndex, boolean isCtxDependent) {
super(target);
this.ruleIndex = ruleIndex;
this.predIndex = predIndex;
this.isCtxDependent = isCtxDependent;
}
@Override
public boolean isEpsilon() { return true; }
@Override
@NotNull
public String toString() {
return "pred_"+ruleIndex+":"+predIndex;
}

View File

@ -29,24 +29,25 @@
package org.antlr.v4.runtime.atn;
import com.sun.istack.internal.NotNull;
import org.antlr.v4.runtime.misc.IntervalSet;
public class RangeTransition extends Transition {
public int from;
public int to;
public RangeTransition(int from, int to, ATNState target) {
public final int from;
public final int to;
public RangeTransition(@NotNull ATNState target, int from, int to) {
super(target);
this.from = from;
this.to = to;
}
public RangeTransition(ATNState target) {
super(target);
}
@Override
@NotNull
public IntervalSet label() { return IntervalSet.of(from, to); }
@Override
@NotNull
public String toString() {
return "'"+(char)from+"'..'"+(char)to+"'";
}

View File

@ -29,27 +29,26 @@
package org.antlr.v4.runtime.atn;
import com.sun.istack.internal.NotNull;
/** */
public class RuleTransition extends Transition {
/** Ptr to the rule definition object for this rule ref */
public int ruleIndex; // no Rule object at runtime
public final int ruleIndex; // no Rule object at runtime
/** What node to begin computations following ref to rule */
public ATNState followState;
@NotNull
public final ATNState followState;
public RuleTransition(int ruleIndex,
ATNState ruleStart,
ATNState followState)
public RuleTransition(@NotNull RuleStartState ruleStart,
int ruleIndex,
@NotNull ATNState followState)
{
super(ruleStart);
this.ruleIndex = ruleIndex;
this.followState = followState;
}
public RuleTransition(ATNState ruleStart) {
super(ruleStart);
}
@Override
public boolean isEpsilon() { return true; }
}

View File

@ -29,26 +29,29 @@
package org.antlr.v4.runtime.atn;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.misc.IntervalSet;
/** A transition containing a set of values */
public class SetTransition extends Transition {
public IntervalSet set;
@NotNull
public final IntervalSet set;
public SetTransition(IntervalSet set, ATNState target) {
// TODO (sam): should we really allow null here?
public SetTransition(@NotNull ATNState target, @Nullable IntervalSet set) {
super(target);
if ( set == null ) set = IntervalSet.of(Token.INVALID_TYPE);
this.set = set;
}
public SetTransition(ATNState target) {
super(target);
}
@Override
@NotNull
public IntervalSet label() { return set; }
@Override
@NotNull
public String toString() {
return set.toString();
}

View File

@ -29,6 +29,8 @@
package org.antlr.v4.runtime.atn;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import org.antlr.v4.runtime.misc.IntervalSet;
import java.util.*;
@ -58,21 +60,22 @@ public abstract class Transition {
public static final int WILDCARD = 9;
public static String[] serializationNames = {
"INVALID",
"EPSILON",
"RANGE",
"RULE",
"PREDICATE",
"ATOM",
"ACTION",
"SET",
"NOT_SET",
"WILDCARD",
};
public static final List<String> serializationNames =
Collections.unmodifiableList(Arrays.asList(
"INVALID",
"EPSILON",
"RANGE",
"RULE",
"PREDICATE",
"ATOM",
"ACTION",
"SET",
"NOT_SET",
"WILDCARD"
));
public static Map<Class<? extends Transition>, Integer> serializationTypes =
new HashMap<Class<? extends Transition>, Integer>() {{
public static final Map<Class<? extends Transition>, Integer> serializationTypes =
Collections.unmodifiableMap(new HashMap<Class<? extends Transition>, Integer>() {{
put(EpsilonTransition.class, EPSILON);
put(RangeTransition.class, RANGE);
put(RuleTransition.class, RULE);
@ -82,19 +85,19 @@ public abstract class Transition {
put(SetTransition.class, SET);
put(NotSetTransition.class, NOT_SET);
put(WildcardTransition.class, WILDCARD);
}};
}});
/** The target of this transition */
public ATNState target;
@NotNull
public final ATNState target;
public Transition() { }
public Transition(ATNState target) { this.target = target; }
protected Transition(@NotNull ATNState target) { this.target = target; }
public int getSerializationType() { return 0; }
/** Are we epsilon, action, sempred? */
public boolean isEpsilon() { return false; }
@Nullable
public IntervalSet label() { return null; }
}

View File

@ -29,9 +29,13 @@
package org.antlr.v4.runtime.atn;
public class WildcardTransition extends Transition {
public WildcardTransition(ATNState target) { super(target); }
import com.sun.istack.internal.NotNull;
public class WildcardTransition extends Transition {
public WildcardTransition(@NotNull ATNState target) { super(target); }
@Override
@NotNull
public String toString() {
return ".";
}

View File

@ -28,6 +28,7 @@
*/
package org.antlr.v4.runtime.dfa;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import org.antlr.v4.runtime.atn.ATNState;
@ -37,20 +38,24 @@ public class DFA {
/** A set of all DFA states. Use Map so we can get old state back
* (Set only allows you to see if it's there).
*/
public Map<DFAState, DFAState> states = new LinkedHashMap<DFAState, DFAState>();
@NotNull
public final Map<DFAState, DFAState> states = new LinkedHashMap<DFAState, DFAState>();
@Nullable
public DFAState s0;
public int decision;
/** From which ATN state did we create this DFA? */
public ATNState atnStartState;
@NotNull
public final ATNState atnStartState;
/** Does at least one state have a conflict? Mainly used as return value
* from predictATN()
*/
public boolean conflict;
public DFA(ATNState atnStartState) { this.atnStartState = atnStartState; }
public DFA(@NotNull ATNState atnStartState) { this.atnStartState = atnStartState; }
@Override
public String toString() { return toString(null); }
public String toString(@Nullable String[] tokenNames) {

View File

@ -29,18 +29,22 @@
package org.antlr.v4.runtime.dfa;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
/** A DFA walker that knows how to dump them to serialized strings. */
public class DFASerializer {
String[] tokenNames;
DFA dfa;
@NotNull
final DFA dfa;
@Nullable
final String[] tokenNames;
public DFASerializer(DFA dfa, @Nullable String[] tokenNames) {
public DFASerializer(@NotNull DFA dfa, @Nullable String[] tokenNames) {
this.dfa = dfa;
this.tokenNames = tokenNames;
}
@Override
public String toString() {
if ( dfa.s0==null ) return null;
StringBuilder buf = new StringBuilder();

View File

@ -29,6 +29,7 @@
package org.antlr.v4.runtime.dfa;
import com.sun.istack.internal.Nullable;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.atn.ATNConfig;
import org.antlr.v4.runtime.misc.OrderedHashSet;
@ -64,9 +65,11 @@ public class DFAState {
public int stateNumber = -1;
/** The set of ATN configurations (state,alt,context) for this DFA state */
@Nullable
public OrderedHashSet<ATNConfig> configs = new OrderedHashSet<ATNConfig>();
/** edges[symbol] points to target of symbol */
@Nullable
public DFAState[] edges;
// public IntervalSet viableChars;
@ -80,6 +83,7 @@ public class DFAState {
public boolean complete; // all alts predict "prediction"
public boolean isCtxSensitive;
@Nullable
public Map<RuleContext, Integer> ctxToPrediction;
public DFAState() { }
@ -92,6 +96,7 @@ public class DFAState {
* DFA state.
*/
public Set<Integer> getAltSet() {
// TODO (sam): what to do when configs==null?
Set<Integer> alts = new HashSet<Integer>();
for (ATNConfig c : configs) {
alts.add(c.alt);
@ -111,7 +116,9 @@ public class DFAState {
*/
/** A decent hash for a DFA state is the sum of the ATN state/alt pairs. */
@Override
public int hashCode() {
// TODO (sam): what to do when configs==null?
int h = 0;
for (ATNConfig c : configs) {
h += c.alt;
@ -130,16 +137,18 @@ public class DFAState {
* to know if any other state exists that has this exact set of ATN
* configurations. The DFAState state number is irrelevant.
*/
@Override
public boolean equals(Object o) {
// compare set of ATN configurations in this set with other
if ( this==o ) return true;
DFAState other = (DFAState)o;
// TODO (sam): what to do when configs==null?
boolean sameSet = this.configs.equals(other.configs);
// System.out.println("DFAState.equals: "+configs+(sameSet?"==":"!=")+other.configs);
return sameSet;
}
@Override
public String toString() {
return stateNumber+":"+configs+(isAcceptState?("=>"+prediction):"");
}

View File

@ -29,12 +29,15 @@
package org.antlr.v4.runtime.dfa;
import com.sun.istack.internal.NotNull;
public class LexerDFASerializer extends DFASerializer {
public LexerDFASerializer(DFA dfa) {
public LexerDFASerializer(@NotNull DFA dfa) {
super(dfa, null);
}
@Override
@NotNull
protected String getEdgeLabel(int i) {
return "'"+(char)i+"'";
}

View File

@ -28,6 +28,7 @@
*/
package org.antlr.v4.runtime.misc;
import com.sun.istack.internal.NotNull;
import org.antlr.v4.runtime.*;
import java.util.*;
@ -68,6 +69,7 @@ public class IntervalSet implements IntSet {
}
/** Create a set with a single element, el. */
@NotNull
public static IntervalSet of(int a) {
IntervalSet s = new IntervalSet();
s.add(a);

View File

@ -197,7 +197,7 @@ public class ATNSerializer {
int ruleIndex = ATNSimulator.toInt(data[p++]);
if ( stype==0 ) continue; // ignore bad type of states
buf.append((i - 1) + ":" +
ATNState.serializationNames[stype] + " "+
ATNState.serializationNames.get(stype) + " "+
ruleIndex + "\n");
}
int nrules = ATNSimulator.toInt(data[p++]);
@ -232,7 +232,7 @@ public class ATNSerializer {
int arg2 = ATNSimulator.toInt(data[p + 4]);
int arg3 = ATNSimulator.toInt(data[p + 5]);
buf.append(src+"->"+trg+
" "+Transition.serializationNames[ttype]+
" "+Transition.serializationNames.get(ttype)+
" "+arg1+","+arg2+","+arg3+
"\n");
p += 6;

View File

@ -103,7 +103,7 @@ public class LexerATNFactory extends ParserATNFactory {
ATNState right = newState(b);
int t1 = CharSupport.getCharValueFromGrammarCharLiteral(a.getText());
int t2 = CharSupport.getCharValueFromGrammarCharLiteral(b.getText());
left.addTransition(new RangeTransition(t1, t2, right));
left.addTransition(new RangeTransition(right, t1, t2));
a.atnState = left;
b.atnState = left;
return new Handle(left, right);
@ -128,10 +128,10 @@ public class LexerATNFactory extends ParserATNFactory {
if ( invert ) {
// TODO: what? should be chars not token types
IntervalSet notSet = (IntervalSet)set.complement(Token.MIN_TOKEN_TYPE, g.getMaxTokenType());
left.addTransition(new NotSetTransition(set, notSet, right));
left.addTransition(new NotSetTransition(right, set, notSet));
}
else {
left.addTransition(new SetTransition(set, right));
left.addTransition(new SetTransition(right, set));
}
right.incidentTransition = left.transition(0);
associatedAST.atnState = left;
@ -153,7 +153,7 @@ public class LexerATNFactory extends ParserATNFactory {
ATNState right = null;
for (int i=0; i<n; i++) {
right = newState(stringLiteralAST);
prev.addTransition(new AtomTransition(chars.charAt(i), right));
prev.addTransition(new AtomTransition(right, chars.charAt(i)));
prev = right;
}
stringLiteralAST.atnState = left;
@ -166,7 +166,7 @@ public class LexerATNFactory extends ParserATNFactory {
if ( node.getText().equals("EOF") ) {
ATNState left = newState(node);
ATNState right = newState(node);
left.addTransition(new AtomTransition(CharStream.EOF, right));
left.addTransition(new AtomTransition(right, CharStream.EOF));
return new Handle(left, right);
}
return _ruleRef(node);

View File

@ -30,6 +30,8 @@
package org.antlr.v4.automata;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.v4.misc.CharSupport;
@ -48,11 +50,15 @@ import java.util.*;
* No side-effects. It builds an ATN object and returns it.
*/
public class ParserATNFactory implements ATNFactory {
public Grammar g;
public Rule currentRule;
public ATN atn;
@NotNull
public final Grammar g;
public ParserATNFactory(Grammar g) { this.g = g; atn = new ATN(); }
@NotNull
public final ATN atn;
public Rule currentRule;
public ParserATNFactory(@NotNull Grammar g) { this.g = g; atn = new ATN(); }
public ATN createATN() {
_createATN(g.rules.values());
@ -104,7 +110,7 @@ public class ParserATNFactory implements ATNFactory {
ATNState left = newState(node);
ATNState right = newState(node);
int ttype = g.getTokenType(node.getText());
left.addTransition(new AtomTransition(ttype, right));
left.addTransition(new AtomTransition(right, ttype));
right.incidentTransition = left.transition(0);
node.atnState = left;
return new Handle(left, right);
@ -124,10 +130,10 @@ public class ParserATNFactory implements ATNFactory {
}
if ( invert ) {
IntervalSet notSet = (IntervalSet)set.complement(Token.MIN_TOKEN_TYPE, g.getMaxTokenType());
left.addTransition(new NotSetTransition(set, notSet, right));
left.addTransition(new NotSetTransition(right, set, notSet));
}
else {
left.addTransition(new SetTransition(set, right));
left.addTransition(new SetTransition(right, set));
}
right.incidentTransition = left.transition(0);
associatedAST.atnState = left;
@ -178,7 +184,7 @@ public class ParserATNFactory implements ATNFactory {
RuleStartState start = atn.ruleToStartState[r.index];
ATNState left = newState(node);
ATNState right = newState(node);
RuleTransition call = new RuleTransition(r.index, start, right);
RuleTransition call = new RuleTransition(start, r.index, right);
left.addTransition(call);
node.atnState = left;
@ -208,10 +214,8 @@ public class ParserATNFactory implements ATNFactory {
//System.out.println("sempred: "+ pred);
ATNState left = newState(pred);
ATNState right = newState(pred);
PredicateTransition p = new PredicateTransition(right);
p.ruleIndex = currentRule.index;
p.predIndex = g.sempreds.get(pred);
p.isCtxDependent = UseDefAnalyzer.actionIsContextDependent(pred);
boolean isCtxDependent = UseDefAnalyzer.actionIsContextDependent(pred);
PredicateTransition p = new PredicateTransition(right, currentRule.index, g.sempreds.get(pred), isCtxDependent);
left.addTransition(p);
pred.atnState = left;
return new Handle(left, right);
@ -225,8 +229,7 @@ public class ParserATNFactory implements ATNFactory {
//System.out.println("action: "+action);
ATNState left = newState(action);
ATNState right = newState(action);
ActionTransition a = new ActionTransition(right);
a.ruleIndex = currentRule.index;
ActionTransition a = new ActionTransition(right, currentRule.index);
left.addTransition(a);
action.atnState = left;
return new Handle(left, right);
@ -306,11 +309,13 @@ public class ParserATNFactory implements ATNFactory {
// return set;
// }
public Handle alt(List<Handle> els) {
@NotNull
public Handle alt(@NotNull List<Handle> els) {
return elemList(els);
}
public Handle elemList(List<Handle> els) {
@NotNull
public Handle elemList(@NotNull List<Handle> els) {
Handle prev = null;
for (Handle el : els) { // hook up elements
if ( prev!=null ) epsilon(prev.right, el.left);
@ -332,7 +337,8 @@ public class ParserATNFactory implements ATNFactory {
*
* or, if A is a block, just add an empty alt to the end of the block
*/
public Handle optional(GrammarAST optAST, Handle blk) {
@NotNull
public Handle optional(@NotNull GrammarAST optAST, @NotNull Handle blk) {
// TODO: no such thing as nongreedy ()? so give error
BlockStartState blkStart = (BlockStartState)blk.left;
epsilon(blkStart, blk.right);
@ -349,7 +355,8 @@ public class ParserATNFactory implements ATNFactory {
* We add a decision for loop back node to the existing one at
* blk start.
*/
public Handle plus(GrammarAST plusAST, Handle blk) {
@NotNull
public Handle plus(@NotNull GrammarAST plusAST, @NotNull Handle blk) {
PlusBlockStartState blkStart = (PlusBlockStartState)blk.left;
BlockEndState blkEnd = (BlockEndState)blk.right;
@ -388,7 +395,8 @@ public class ParserATNFactory implements ATNFactory {
* Note that the optional bypass must jump outside the loop as (A|B)* is
* not the same thing as (A|B|)+.
*/
public Handle star(GrammarAST starAST, Handle elem) {
@NotNull
public Handle star(@NotNull GrammarAST starAST, @NotNull Handle elem) {
StarBlockStartState blkStart = (StarBlockStartState)elem.left;
BlockEndState blkEnd = (BlockEndState)elem.right;
@ -417,6 +425,7 @@ public class ParserATNFactory implements ATNFactory {
}
/** Build an atom with all possible values in its label */
@NotNull
public Handle wildcard(GrammarAST node) {
ATNState left = newState(node);
ATNState right = newState(node);
@ -431,7 +440,7 @@ public class ParserATNFactory implements ATNFactory {
*/
public Handle wildcardTree(GrammarAST associatedAST) { throw new UnsupportedOperationException(); }
void epsilon(ATNState a, ATNState b) {
void epsilon(ATNState a, @NotNull ATNState b) {
if ( a!=null ) a.addTransition(new EpsilonTransition(b));
}
@ -442,8 +451,8 @@ public class ParserATNFactory implements ATNFactory {
atn.ruleToStartState = new RuleStartState[g.rules.size()];
atn.ruleToStopState = new RuleStopState[g.rules.size()];
for (Rule r : g.rules.values()) {
RuleStartState start = (RuleStartState)newState(RuleStartState.class, r.ast);
RuleStopState stop = (RuleStopState)newState(RuleStopState.class, r.ast);
RuleStartState start = newState(RuleStartState.class, r.ast);
RuleStopState stop = newState(RuleStopState.class, r.ast);
start.stopState = stop;
start.setRuleIndex(r.index);
stop.setRuleIndex(r.index);
@ -467,7 +476,7 @@ public class ParserATNFactory implements ATNFactory {
ATNState stop = atn.ruleToStopState[r.index];
if ( stop.getNumberOfTransitions()>0 ) continue;
n++;
Transition t = new AtomTransition(Token.EOF, eofTarget);
Transition t = new AtomTransition(eofTarget, Token.EOF);
stop.addTransition(t);
}
return n;
@ -481,10 +490,12 @@ public class ParserATNFactory implements ATNFactory {
return t;
}
public ATNState newState(Class nodeType, GrammarAST node) {
// TODO (sam): should we allow this to throw an exception instead of returning null?
@Nullable
public <T extends ATNState> T newState(@NotNull Class<T> nodeType, GrammarAST node) {
try {
Constructor ctor = nodeType.getConstructor();
ATNState s = (ATNState)ctor.newInstance();
Constructor<T> ctor = nodeType.getConstructor();
T s = ctor.newInstance();
if ( currentRule==null ) s.setRuleIndex(-1);
else s.setRuleIndex(currentRule.index);
atn.addState(s);
@ -496,16 +507,18 @@ public class ParserATNFactory implements ATNFactory {
return null;
}
public ATNState newState(GrammarAST node) {
@NotNull
public ATNState newState(@Nullable GrammarAST node) {
ATNState n = new ATNState();
n.setRuleIndex(currentRule.index);
atn.addState(n);
return n;
}
@NotNull
public ATNState newState() { return newState(null); }
public boolean isGreedy(BlockAST blkAST) {
public boolean isGreedy(@NotNull BlockAST blkAST) {
boolean greedy = true;
String greedyOption = blkAST.getOptionString("greedy");
if ( blockHasWildcardAlt(blkAST) || greedyOption!=null&&greedyOption.equals("false") ) {
@ -515,7 +528,7 @@ public class ParserATNFactory implements ATNFactory {
}
// (BLOCK (ALT .)) or (BLOCK (ALT 'a') (ALT .))
public static boolean blockHasWildcardAlt(GrammarAST block) {
public static boolean blockHasWildcardAlt(@NotNull GrammarAST block) {
for (Object alt : block.getChildren()) {
if ( !(alt instanceof AltAST) ) continue;
AltAST altAST = (AltAST)alt;

View File

@ -29,6 +29,7 @@
package org.antlr.v4.codegen;
import com.sun.istack.internal.NotNull;
import org.antlr.v4.Tool;
import org.antlr.v4.codegen.model.OutputModelObject;
import org.antlr.v4.runtime.Token;
@ -48,18 +49,21 @@ public class CodeGenerator {
"<tokens.keys:{t | <t>=<tokens.(t)>\n}>" +
"<literals.keys:{t | <t>=<literals.(t)>\n}>";
public Grammar g;
public Tool tool;
@NotNull
public final Grammar g;
@NotNull
public final Tool tool;
public Target target;
public STGroup templates;
public int lineWidth = 72;
public CodeGenerator(Grammar g) {
public CodeGenerator(@NotNull Grammar g) {
this(g.tool, g, g.getOptionString("language", "Java"));
}
public CodeGenerator(Tool tool, Grammar g, String language) {
public CodeGenerator(@NotNull Tool tool, @NotNull Grammar g, String language) {
this.g = g;
this.tool = tool;
loadLanguageTarget(language);

View File

@ -29,6 +29,8 @@
package org.antlr.v4.codegen;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import org.antlr.v4.codegen.model.*;
import org.antlr.v4.codegen.model.decl.CodeBlock;
import org.antlr.v4.codegen.model.decl.Decl;
@ -46,37 +48,50 @@ import java.util.List;
*/
public abstract class DefaultOutputModelFactory extends BlankOutputModelFactory {
// Interface to outside world
public Grammar g;
public CodeGenerator gen;
@NotNull
public final Grammar g;
@NotNull
public final CodeGenerator gen;
public OutputModelController controller;
protected DefaultOutputModelFactory(CodeGenerator gen) {
protected DefaultOutputModelFactory(@NotNull CodeGenerator gen) {
this.gen = gen;
this.g = gen.g;
}
@Override
public void setController(OutputModelController controller) {
this.controller = controller;
}
// Convenience methods
@NotNull
public Grammar getGrammar() { return g; }
@Override
public CodeGenerator getGenerator() { return gen; }
@Override
public OutputModelObject getRoot() { return controller.getRoot(); }
@Override
public RuleFunction getCurrentRuleFunction() { return controller.getCurrentRuleFunction(); }
@Override
public Alternative getCurrentOuterMostAlt() { return controller.getCurrentOuterMostAlt(); }
@Override
public CodeBlock getCurrentBlock() { return controller.getCurrentBlock(); }
@Override
public CodeBlock getCurrentOuterMostAlternativeBlock() { return controller.getCurrentOuterMostAlternativeBlock(); }
@Override
public int getCodeBlockLevel() { return controller.codeBlockLevel; }
@Override
public int getTreeLevel() { return controller.treeLevel; }
@Override
@ -96,6 +111,7 @@ public abstract class DefaultOutputModelFactory extends BlankOutputModelFactory
// MISC
@NotNull
public static List<SrcOp> list(Object... values) {
List<SrcOp> x = new ArrayList<SrcOp>(values.length);
for (Object v : values) {
@ -107,6 +123,7 @@ public abstract class DefaultOutputModelFactory extends BlankOutputModelFactory
return x;
}
@Nullable
public Decl getCurrentDeclForName(String name) {
if ( getCurrentBlock().locals==null ) return null;
for (Decl d : getCurrentBlock().locals.elements()) {

View File

@ -29,6 +29,8 @@
package org.antlr.v4.tool;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import org.antlr.runtime.tree.*;
import org.antlr.v4.Tool;
import org.antlr.v4.misc.*;
@ -99,7 +101,8 @@ public class Grammar implements AttributeResolver {
public Vector<IntervalSet[]> decisionLOOK;
public Tool tool;
@NotNull
public final Tool tool;
/** Token names and literal tokens like "void" are uniquely indexed.
* with -1 implying EOF. Characters are different; they go from
@ -175,7 +178,7 @@ public class Grammar implements AttributeResolver {
}
/** For testing; builds trees, does sem anal */
public Grammar(String fileName, String grammarText, ANTLRToolListener listener)
public Grammar(String fileName, String grammarText, @Nullable ANTLRToolListener listener)
throws org.antlr.runtime.RecognitionException
{
this.text = grammarText;