From ca141afc287a63842ccb24cb559bb0c3fccfb042 Mon Sep 17 00:00:00 2001 From: sharwell Date: Fri, 18 Nov 2011 11:29:58 -0800 Subject: [PATCH] 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] --- .../src/org/antlr/v4/runtime/RuleContext.java | 2 + .../src/org/antlr/v4/runtime/atn/ATN.java | 17 ++- .../org/antlr/v4/runtime/atn/ATNConfig.java | 59 ++++---- .../antlr/v4/runtime/atn/ATNSimulator.java | 29 ++-- .../org/antlr/v4/runtime/atn/ATNState.java | 37 ++--- .../v4/runtime/atn/ActionTransition.java | 17 ++- .../antlr/v4/runtime/atn/AtomTransition.java | 15 +- .../v4/runtime/atn/EpsilonTransition.java | 6 +- .../org/antlr/v4/runtime/atn/LL1Analyzer.java | 17 ++- .../v4/runtime/atn/LexerATNSimulator.java | 67 +++++--- .../v4/runtime/atn/NotSetTransition.java | 13 +- .../v4/runtime/atn/ParserATNSimulator.java | 143 ++++++++++-------- .../v4/runtime/atn/PredicateTransition.java | 17 ++- .../antlr/v4/runtime/atn/RangeTransition.java | 13 +- .../antlr/v4/runtime/atn/RuleTransition.java | 17 +-- .../antlr/v4/runtime/atn/SetTransition.java | 15 +- .../org/antlr/v4/runtime/atn/Transition.java | 41 ++--- .../v4/runtime/atn/WildcardTransition.java | 8 +- .../src/org/antlr/v4/runtime/dfa/DFA.java | 11 +- .../antlr/v4/runtime/dfa/DFASerializer.java | 10 +- .../org/antlr/v4/runtime/dfa/DFAState.java | 11 +- .../v4/runtime/dfa/LexerDFASerializer.java | 5 +- .../antlr/v4/runtime/misc/IntervalSet.java | 2 + .../org/antlr/v4/automata/ATNSerializer.java | 4 +- .../antlr/v4/automata/LexerATNFactory.java | 10 +- .../antlr/v4/automata/ParserATNFactory.java | 71 +++++---- .../org/antlr/v4/codegen/CodeGenerator.java | 12 +- .../v4/codegen/DefaultOutputModelFactory.java | 23 ++- tool/src/org/antlr/v4/tool/Grammar.java | 7 +- 29 files changed, 413 insertions(+), 286 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java index e76d795ee..018b87be6 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java @@ -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? */ diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ATN.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ATN.java index 7aee37632..3d42e0561 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ATN.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ATN.java @@ -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 states = new ArrayList(); + @NotNull + public final List states = new ArrayList(); /** 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 decisionToState = new ArrayList(); + @NotNull + public final List decisionToState = new ArrayList(); public RuleStartState[] ruleToStartState; public RuleStopState[] ruleToStopState; - public Map modeNameToStartState = + @NotNull + public final Map modeNameToStartState = new LinkedHashMap(); // runtime for parsers, lexers @@ -64,7 +68,8 @@ public class ATN { // runtime for lexer only public int[] ruleToTokenType; public int[] ruleToActionIndex; - public List modeToStartState = new ArrayList(); + @NotNull + public final List modeToStartState = new ArrayList(); /** 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; diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNConfig.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNConfig.java index 7fd0753eb..54ffa9d1c 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNConfig.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNConfig.java @@ -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]+":"); diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java index 7bfb0fd09..40eaf8609 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java @@ -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()); 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 sets = new ArrayList(); 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 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; diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNState.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNState.java index ded024924..6b0007e4c 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNState.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNState.java @@ -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 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, Integer> serializationTypes = - new HashMap, Integer>() {{ + public static final Map, Integer> serializationTypes = + Collections.unmodifiableMap(new HashMap, 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 transitions = + protected final List transitions = new ArrayList(INITIAL_NUM_TRANSITIONS); /** For o-A->o type ATN tranitions, record the label that leads to this diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ActionTransition.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ActionTransition.java index cb285e1a2..3976cd95d 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ActionTransition.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ActionTransition.java @@ -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 diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/AtomTransition.java b/runtime/Java/src/org/antlr/v4/runtime/atn/AtomTransition.java index 26df22eb0..69709247d 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/AtomTransition.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/AtomTransition.java @@ -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); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/EpsilonTransition.java b/runtime/Java/src/org/antlr/v4/runtime/atn/EpsilonTransition.java index 75a2ba74d..a399e1195 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/EpsilonTransition.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/EpsilonTransition.java @@ -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"; } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java b/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java index 92f98a6e2..fd4516ddf 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java @@ -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()); return r; } - protected void _LOOK(ATNState s, @Nullable RuleContext ctx, IntervalSet look, - Set lookBusy) { + protected void _LOOK(@NotNull ATNState s, @Nullable RuleContext ctx, @NotNull IntervalSet look, + @NotNull Set lookBusy) { // System.out.println("_LOOK("+s.stateNumber+", ctx="+ctx); ATNConfig c = new ATNConfig(s, 0, ctx); if ( lookBusy.contains(c) ) return; diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java index de05d68a1..1fba95143 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java @@ -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 s0) { + protected int exec(@NotNull CharStream input, @NotNull OrderedHashSet s0) { //System.out.println("enter exec index "+input.index()+" from "+s0); + @NotNull OrderedHashSet closure = new OrderedHashSet(); closure.addAll(s0); if ( debug ) System.out.println("start state closure="+closure); + @NotNull OrderedHashSet reach = new OrderedHashSet(); 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 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 reach) { + protected void processAcceptStates(@NotNull CharStream input, @NotNull OrderedHashSet reach) { for (int ci=0; ci closure, int ci, int alt) { + public void deleteWildcardConfigsForAlt(@NotNull OrderedHashSet closure, int ci, int alt) { int j=ci+1; while ( j computeStartState(IntStream input, - ATNState p) + @NotNull + protected OrderedHashSet computeStartState(@NotNull IntStream input, + @NotNull ATNState p) { RuleContext initialContext = RuleContext.EMPTY; OrderedHashSet configs = new OrderedHashSet(); @@ -376,7 +387,7 @@ public class LexerATNSimulator extends ATNSimulator { return configs; } - protected void closure(ATNConfig config, OrderedHashSet configs) { + protected void closure(@NotNull ATNConfig config, @NotNull OrderedHashSet 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 p, + protected void addDFAEdge(@NotNull OrderedHashSet p, int t, - OrderedHashSet q) + @NotNull OrderedHashSet 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 configs) { + @Nullable + protected DFAState addDFAState(@NotNull OrderedHashSet 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); diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/NotSetTransition.java b/runtime/Java/src/org/antlr/v4/runtime/atn/NotSetTransition.java index 987f16b54..434789ff6 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/NotSetTransition.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/NotSetTransition.java @@ -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(); diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java index d37b51ad0..95f1ebb9f 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java @@ -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 extends ATNSimulator { public static int retry_with_context = 0; public static int retry_with_context_indicates_no_conflict = 0; + @Nullable + protected final BaseRecognizer parser; - protected BaseRecognizer parser; - - public Map ctxToDFAs; + @NotNull + public final Map ctxToDFAs; public Map[] 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 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(); - decisionToDFA = new DFA[atn.getNumberOfDecisions()]; + public ParserATNSimulator(@NotNull ATN atn) { + this(null, atn); } - public ParserATNSimulator(BaseRecognizer parser, ATN atn) { + public ParserATNSimulator(@Nullable BaseRecognizer parser, @NotNull ATN atn) { super(atn); this.parser = parser; ctxToDFAs = new HashMap(); - 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 input, int decision, RuleContext outerContext) { + public int adaptivePredict(@NotNull SymbolStream 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 extends ATNSimulator { } } - public int predictATN(DFA dfa, SymbolStream input, - RuleContext outerContext, + public int predictATN(@NotNull DFA dfa, @NotNull SymbolStream input, + @Nullable RuleContext outerContext, boolean useContext) { if ( outerContext==null ) outerContext = RuleContext.EMPTY; @@ -146,15 +151,14 @@ public class ParserATNSimulator extends ATNSimulator { } // doesn't create DFA when matching - public int matchATN(SymbolStream input, ATNState startState) { + public int matchATN(@NotNull SymbolStream input, @NotNull ATNState startState) { DFA dfa = new DFA(startState); - if ( outerContext==null ) outerContext = RuleContext.EMPTY; RuleContext ctx = RuleContext.EMPTY; OrderedHashSet s0_closure = computeStartState(dfa.decision, startState, ctx); return execATN(input, dfa, input.index(), s0_closure, false); } - public int execDFA(SymbolStream input, DFA dfa, DFAState s0, RuleContext outerContext) { + public int execDFA(@NotNull SymbolStream 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 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 extends ATNSimulator { return prevAcceptState.prediction; } - public String getInputString(SymbolStream input, int start) { + public String getInputString(@NotNull SymbolStream input, int start) { if ( input instanceof TokenStream ) { return ((TokenStream)input).toString(start,input.index()); } @@ -251,10 +255,10 @@ public class ParserATNSimulator extends ATNSimulator { return "n/a"; } - public int execATN(SymbolStream input, - DFA dfa, + public int execATN(@NotNull SymbolStream input, + @NotNull DFA dfa, int startIndex, - OrderedHashSet s0, + @NotNull OrderedHashSet s0, boolean useContext) { if ( debug ) System.out.println("execATN decision "+dfa.decision+" exec LA(1)=="+ getLookaheadName(input)); @@ -377,14 +381,14 @@ public class ParserATNSimulator 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 reach, Set ambigAlts) { + protected int resolveToMinAlt(@NotNull OrderedHashSet reach, @NotNull Set ambigAlts) { int min = getMinAlt(ambigAlts); // create DFA accept state for resolved alt ambigAlts.remove(min); @@ -394,7 +398,7 @@ public class ParserATNSimulator extends ATNSimulator { return min; } - protected int resolveNongreedyToExitBranch(OrderedHashSet reach, Set ambigAlts) { + protected int resolveNongreedyToExitBranch(@NotNull OrderedHashSet reach, @NotNull Set 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 extends ATNSimulator { return exitAlt; } - public int retryWithContext(SymbolStream input, - DFA dfa, + public int retryWithContext(@NotNull SymbolStream input, + @NotNull DFA dfa, int startIndex, - RuleContext originalContext, - OrderedHashSet closure, + @NotNull RuleContext originalContext, + @NotNull OrderedHashSet closure, int t, - OrderedHashSet reach, - Set ambigAlts) + @NotNull OrderedHashSet reach, + @NotNull Set ambigAlts) { // ASSUMES PREDICT ONLY retry_with_context++; @@ -466,7 +470,8 @@ public class ParserATNSimulator extends ATNSimulator { return predictedAlt; } - public OrderedHashSet computeStartState(int decision, ATNState p, RuleContext ctx) { + @NotNull + public OrderedHashSet computeStartState(int decision, @NotNull ATNState p, @Nullable RuleContext ctx) { RuleContext initialContext = ctx; // always at least the implicit call to start rule OrderedHashSet configs = new OrderedHashSet(); prevAccept = null; // might reach end rule; track @@ -485,7 +490,8 @@ public class ParserATNSimulator 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 extends ATNSimulator { ambig detection thought :( */ - protected void closure(ATNConfig config, - OrderedHashSet configs, - DecisionState decState, - Set closureBusy) + protected void closure(@NotNull ATNConfig config, + @NotNull OrderedHashSet configs, + @Nullable DecisionState decState, + @NotNull Set closureBusy) { if ( debug ) System.out.println("closure("+config+")"); @@ -577,12 +583,14 @@ public class ParserATNSimulator extends ATNSimulator { } } + @NotNull private String getRuleName(int index) { if ( parser!=null && index>=0 ) return parser.getRuleNames()[index]; return ""; } - 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 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 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 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 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 extends ATNSimulator { return new ATNConfig(config, t.target, newContext); } - public void reportConflict(int startIndex, int stopIndex, Set alts, OrderedHashSet configs) { + public void reportConflict(int startIndex, int stopIndex, @NotNull Set alts, @NotNull OrderedHashSet configs) { if ( parser!=null ) parser.reportConflict(startIndex, stopIndex, alts, configs); } - public void reportContextSensitivity(int startIndex, int stopIndex, Set alts, OrderedHashSet configs) { + public void reportContextSensitivity(int startIndex, int stopIndex, @NotNull Set alts, @NotNull OrderedHashSet 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 alts, OrderedHashSet configs) { + public void reportAmbiguity(int startIndex, int stopIndex, @NotNull Set alts, @NotNull OrderedHashSet configs) { if ( parser!=null ) parser.reportAmbiguity(startIndex, stopIndex, alts, configs); } - public static int getUniqueAlt(Collection configs) { + public static int getUniqueAlt(@NotNull Collection configs) { int alt = ATN.INVALID_ALT_NUMBER; for (ATNConfig c : configs) { if ( alt == ATN.INVALID_ALT_NUMBER ) { @@ -684,7 +694,8 @@ public class ParserATNSimulator extends ATNSimulator { return alt; } - public ATNConfig configWithAltAtStopState(Collection configs, int alt) { + @Nullable + public ATNConfig configWithAltAtStopState(@NotNull Collection configs, int alt) { for (ATNConfig c : configs) { if ( c.alt == alt ) { if ( c.state.getClass() == RuleStopState.class ) { @@ -695,7 +706,8 @@ public class ParserATNSimulator extends ATNSimulator { return null; } - public Set getAmbiguousAlts(OrderedHashSet configs) { + @Nullable + public Set getAmbiguousAlts(@NotNull OrderedHashSet configs) { // System.err.println("check ambiguous "+configs); Set ambigAlts = null; int numConfigs = configs.size(); @@ -739,7 +751,7 @@ public class ParserATNSimulator extends ATNSimulator { return ambigAlts; } - public static int getMinAlt(Set ambigAlts) { + public static int getMinAlt(@NotNull Set ambigAlts) { int min = Integer.MAX_VALUE; for (int alt : ambigAlts) { if ( alt < min ) min = alt; @@ -747,7 +759,7 @@ public class ParserATNSimulator extends ATNSimulator { return min; } - public static void killAlts(Set alts, OrderedHashSet configs) { + public static void killAlts(@NotNull Set alts, @NotNull OrderedHashSet configs) { int i = 0; while ( i extends ATNSimulator { } } - protected DFAState addDFAEdge(DFA dfa, - OrderedHashSet p, + protected DFAState addDFAEdge(@NotNull DFA dfa, + @NotNull OrderedHashSet p, int t, - OrderedHashSet q) + @NotNull OrderedHashSet q) { // System.out.println("MOVE "+p+" -> "+q+" upon "+getTokenName(t)); DFAState from = addDFAState(dfa, p); @@ -770,8 +782,8 @@ public class ParserATNSimulator 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 extends ATNSimulator { } /** See comment on LexerInterpreter.addDFAState. */ - protected DFAState addDFAState(DFA dfa, OrderedHashSet configs) { + @Nullable + protected DFAState addDFAState(@NotNull DFA dfa, @NotNull OrderedHashSet configs) { DFAState proposed = new DFAState(configs); DFAState existing = dfa.states.get(proposed); if ( existing!=null ) return existing; @@ -800,7 +813,7 @@ public class ParserATNSimulator extends ATNSimulator { return newState; } - public void makeAcceptState(DFA dfa, OrderedHashSet reach, int uniqueAlt) { + public void makeAcceptState(@NotNull DFA dfa, @NotNull OrderedHashSet 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 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 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 extends ATNSimulator { } } - public int throwNoViableAlt(SymbolStream input, RuleContext outerContext, - OrderedHashSet configs, int startIndex) + @NotNull + public NoViableAltException noViableAlt(@NotNull SymbolStream input, @NotNull RuleContext outerContext, + @NotNull OrderedHashSet 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)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), diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/PredicateTransition.java b/runtime/Java/src/org/antlr/v4/runtime/atn/PredicateTransition.java index da76cc3a9..bff32de86 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/PredicateTransition.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/PredicateTransition.java @@ -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; } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/RangeTransition.java b/runtime/Java/src/org/antlr/v4/runtime/atn/RangeTransition.java index 1f853ce08..d78dcaff1 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/RangeTransition.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/RangeTransition.java @@ -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+"'"; } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/RuleTransition.java b/runtime/Java/src/org/antlr/v4/runtime/atn/RuleTransition.java index dc70a1786..a136e6739 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/RuleTransition.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/RuleTransition.java @@ -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; } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/SetTransition.java b/runtime/Java/src/org/antlr/v4/runtime/atn/SetTransition.java index 8e15f87ac..78e997487 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/SetTransition.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/SetTransition.java @@ -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(); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/Transition.java b/runtime/Java/src/org/antlr/v4/runtime/atn/Transition.java index 1d65d0a44..eb94053f6 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/Transition.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/Transition.java @@ -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 serializationNames = + Collections.unmodifiableList(Arrays.asList( + "INVALID", + "EPSILON", + "RANGE", + "RULE", + "PREDICATE", + "ATOM", + "ACTION", + "SET", + "NOT_SET", + "WILDCARD" + )); - public static Map, Integer> serializationTypes = - new HashMap, Integer>() {{ + public static final Map, Integer> serializationTypes = + Collections.unmodifiableMap(new HashMap, 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; } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/WildcardTransition.java b/runtime/Java/src/org/antlr/v4/runtime/atn/WildcardTransition.java index ffc6a43f5..70be97a3b 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/WildcardTransition.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/WildcardTransition.java @@ -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 "."; } diff --git a/runtime/Java/src/org/antlr/v4/runtime/dfa/DFA.java b/runtime/Java/src/org/antlr/v4/runtime/dfa/DFA.java index de37f7240..7e8dc1e07 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/dfa/DFA.java +++ b/runtime/Java/src/org/antlr/v4/runtime/dfa/DFA.java @@ -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 states = new LinkedHashMap(); + @NotNull + public final Map states = new LinkedHashMap(); + @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) { diff --git a/runtime/Java/src/org/antlr/v4/runtime/dfa/DFASerializer.java b/runtime/Java/src/org/antlr/v4/runtime/dfa/DFASerializer.java index 98466d469..7792e416c 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/dfa/DFASerializer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/dfa/DFASerializer.java @@ -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(); diff --git a/runtime/Java/src/org/antlr/v4/runtime/dfa/DFAState.java b/runtime/Java/src/org/antlr/v4/runtime/dfa/DFAState.java index ab3d9efb8..aa597027f 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/dfa/DFAState.java +++ b/runtime/Java/src/org/antlr/v4/runtime/dfa/DFAState.java @@ -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 configs = new OrderedHashSet(); /** 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 ctxToPrediction; public DFAState() { } @@ -92,6 +96,7 @@ public class DFAState { * DFA state. */ public Set getAltSet() { + // TODO (sam): what to do when configs==null? Set alts = new HashSet(); 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):""); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/dfa/LexerDFASerializer.java b/runtime/Java/src/org/antlr/v4/runtime/dfa/LexerDFASerializer.java index bf1f7eb48..8d6ed4650 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/dfa/LexerDFASerializer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/dfa/LexerDFASerializer.java @@ -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+"'"; } diff --git a/runtime/Java/src/org/antlr/v4/runtime/misc/IntervalSet.java b/runtime/Java/src/org/antlr/v4/runtime/misc/IntervalSet.java index 6df10297e..8f73d9fef 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/IntervalSet.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/IntervalSet.java @@ -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); diff --git a/tool/src/org/antlr/v4/automata/ATNSerializer.java b/tool/src/org/antlr/v4/automata/ATNSerializer.java index 7b7de9a52..3da2bf067 100644 --- a/tool/src/org/antlr/v4/automata/ATNSerializer.java +++ b/tool/src/org/antlr/v4/automata/ATNSerializer.java @@ -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; diff --git a/tool/src/org/antlr/v4/automata/LexerATNFactory.java b/tool/src/org/antlr/v4/automata/LexerATNFactory.java index 14850ed27..a36a69e9f 100644 --- a/tool/src/org/antlr/v4/automata/LexerATNFactory.java +++ b/tool/src/org/antlr/v4/automata/LexerATNFactory.java @@ -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 els) { + @NotNull + public Handle alt(@NotNull List els) { return elemList(els); } - public Handle elemList(List els) { + @NotNull + public Handle elemList(@NotNull List 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 newState(@NotNull Class nodeType, GrammarAST node) { try { - Constructor ctor = nodeType.getConstructor(); - ATNState s = (ATNState)ctor.newInstance(); + Constructor 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; diff --git a/tool/src/org/antlr/v4/codegen/CodeGenerator.java b/tool/src/org/antlr/v4/codegen/CodeGenerator.java index c1ec15556..b8d0a929f 100644 --- a/tool/src/org/antlr/v4/codegen/CodeGenerator.java +++ b/tool/src/org/antlr/v4/codegen/CodeGenerator.java @@ -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 { "=\n}>" + "=\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); diff --git a/tool/src/org/antlr/v4/codegen/DefaultOutputModelFactory.java b/tool/src/org/antlr/v4/codegen/DefaultOutputModelFactory.java index 032cf300d..0e55a5289 100644 --- a/tool/src/org/antlr/v4/codegen/DefaultOutputModelFactory.java +++ b/tool/src/org/antlr/v4/codegen/DefaultOutputModelFactory.java @@ -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 list(Object... values) { List x = new ArrayList(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()) { diff --git a/tool/src/org/antlr/v4/tool/Grammar.java b/tool/src/org/antlr/v4/tool/Grammar.java index cbdce3f2c..c6ce0e69e 100644 --- a/tool/src/org/antlr/v4/tool/Grammar.java +++ b/tool/src/org/antlr/v4/tool/Grammar.java @@ -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 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;