Merge pull request #435 from sharwell/annotations

Annotations
This commit is contained in:
Sam Harwell 2014-01-21 19:18:41 -08:00
commit 51b44f946b
7 changed files with 137 additions and 86 deletions

View File

@ -74,11 +74,11 @@ public interface ANTLRErrorListener {
* the parser was able to recover in line without exiting the
* surrounding rule.
*/
public void syntaxError(Recognizer<?, ?> recognizer,
public void syntaxError(@NotNull Recognizer<?, ?> recognizer,
@Nullable Object offendingSymbol,
int line,
int charPositionInLine,
String msg,
@NotNull String msg,
@Nullable RecognitionException e);
/**
@ -114,7 +114,7 @@ public interface ANTLRErrorListener {
int startIndex,
int stopIndex,
boolean exact,
@NotNull BitSet ambigAlts,
@Nullable BitSet ambigAlts,
@NotNull ATNConfigSet configs);
/**

View File

@ -31,6 +31,8 @@ package org.antlr.v4.runtime;
import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable;
import java.util.BitSet;
@ -39,43 +41,43 @@ import java.util.BitSet;
*/
public class BaseErrorListener implements ANTLRErrorListener {
@Override
public void syntaxError(Recognizer<?, ?> recognizer,
Object offendingSymbol,
public void syntaxError(@NotNull Recognizer<?, ?> recognizer,
@Nullable Object offendingSymbol,
int line,
int charPositionInLine,
String msg,
RecognitionException e)
@NotNull String msg,
@Nullable RecognitionException e)
{
}
@Override
public void reportAmbiguity(Parser recognizer,
DFA dfa,
public void reportAmbiguity(@NotNull Parser recognizer,
@NotNull DFA dfa,
int startIndex,
int stopIndex,
boolean exact,
BitSet ambigAlts,
ATNConfigSet configs)
@Nullable BitSet ambigAlts,
@NotNull ATNConfigSet configs)
{
}
@Override
public void reportAttemptingFullContext(Parser recognizer,
DFA dfa,
public void reportAttemptingFullContext(@NotNull Parser recognizer,
@NotNull DFA dfa,
int startIndex,
int stopIndex,
BitSet conflictingAlts,
ATNConfigSet configs)
@Nullable BitSet conflictingAlts,
@NotNull ATNConfigSet configs)
{
}
@Override
public void reportContextSensitivity(Parser recognizer,
DFA dfa,
public void reportContextSensitivity(@NotNull Parser recognizer,
@NotNull DFA dfa,
int startIndex,
int stopIndex,
int prediction,
ATNConfigSet configs)
@NotNull ATNConfigSet configs)
{
}
}

View File

@ -31,6 +31,8 @@ package org.antlr.v4.runtime;
import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable;
import java.util.BitSet;
import java.util.Collection;
@ -54,12 +56,12 @@ public class ProxyErrorListener implements ANTLRErrorListener {
}
@Override
public void syntaxError(Recognizer<?, ?> recognizer,
Object offendingSymbol,
public void syntaxError(@NotNull Recognizer<?, ?> recognizer,
@Nullable Object offendingSymbol,
int line,
int charPositionInLine,
String msg,
RecognitionException e)
@NotNull String msg,
@Nullable RecognitionException e)
{
for (ANTLRErrorListener listener : delegates) {
listener.syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);
@ -67,13 +69,13 @@ public class ProxyErrorListener implements ANTLRErrorListener {
}
@Override
public void reportAmbiguity(Parser recognizer,
DFA dfa,
public void reportAmbiguity(@NotNull Parser recognizer,
@NotNull DFA dfa,
int startIndex,
int stopIndex,
boolean exact,
BitSet ambigAlts,
ATNConfigSet configs)
@Nullable BitSet ambigAlts,
@NotNull ATNConfigSet configs)
{
for (ANTLRErrorListener listener : delegates) {
listener.reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs);
@ -81,12 +83,12 @@ public class ProxyErrorListener implements ANTLRErrorListener {
}
@Override
public void reportAttemptingFullContext(Parser recognizer,
DFA dfa,
public void reportAttemptingFullContext(@NotNull Parser recognizer,
@NotNull DFA dfa,
int startIndex,
int stopIndex,
BitSet conflictingAlts,
ATNConfigSet configs)
@Nullable BitSet conflictingAlts,
@NotNull ATNConfigSet configs)
{
for (ANTLRErrorListener listener : delegates) {
listener.reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs);
@ -94,12 +96,12 @@ public class ProxyErrorListener implements ANTLRErrorListener {
}
@Override
public void reportContextSensitivity(Parser recognizer,
DFA dfa,
public void reportContextSensitivity(@NotNull Parser recognizer,
@NotNull DFA dfa,
int startIndex,
int stopIndex,
int prediction,
ATNConfigSet configs)
@NotNull ATNConfigSet configs)
{
for (ANTLRErrorListener listener : delegates) {
listener.reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs);

View File

@ -139,7 +139,7 @@ public class ATNConfigSet implements Set<ATNConfig> {
}
@Override
public boolean add(@NotNull ATNConfig config) {
public boolean add(ATNConfig config) {
return add(config, null);
}

View File

@ -283,7 +283,7 @@ public class NullUsageProcessor extends AbstractProcessor {
String error = String.format("parameter %s annotated with %s cannot override or implement a parameter annotated with %s", overriderParameters.get(i).getSimpleName(), notNullType.getSimpleName(), nullableType.getSimpleName());
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, error, overriderParameters.get(i), getNotNullAnnotationMirror(overriderParameters.get(i)));
}
else if (isNotNull(overriderParameters.get(i)) && !(isNullable(overriddenParameters.get(i)) || isNotNull(overriddenParameters.get(i))) /*&& !errorElements.contains(overriderParameters.get(i)) && warnedElements.add(overriderParameters.get(i))*/) {
else if (isNotNull(overriderParameters.get(i)) && !(isNullable(overriddenParameters.get(i)) || isNotNull(overriddenParameters.get(i))) && !errorElements.contains(overriderParameters.get(i)) && warnedElements.add(overriderParameters.get(i))) {
String error = String.format("parameter %s annotated with %s overrides a parameter that is not annotated", overriderParameters.get(i).getSimpleName(), notNullType.getSimpleName());
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, error, overriderParameters.get(i), getNotNullAnnotationMirror(overriderParameters.get(i)));
}

View File

@ -32,6 +32,7 @@ package org.antlr.v4.automata;
import org.antlr.v4.runtime.atn.ATN;
import org.antlr.v4.runtime.atn.ATNState;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.tool.ast.ActionAST;
import org.antlr.v4.tool.ast.BlockAST;
import org.antlr.v4.tool.ast.GrammarAST;
@ -59,27 +60,36 @@ public interface ATNFactory {
}
}
@NotNull
ATN createATN();
void setCurrentRuleName(String name);
void setCurrentRuleName(@NotNull String name);
void setCurrentOuterAlt(int alt);
Handle rule(GrammarAST ruleAST, String name, Handle blk);
@NotNull
Handle rule(@NotNull GrammarAST ruleAST, @NotNull String name, @NotNull Handle blk);
@NotNull
ATNState newState();
Handle label(Handle t);
@NotNull
Handle label(@NotNull Handle t);
Handle listLabel(Handle t);
@NotNull
Handle listLabel(@NotNull Handle t);
Handle tokenRef(TerminalAST node);
@NotNull
Handle tokenRef(@NotNull TerminalAST node);
Handle set(GrammarAST associatedAST, List<GrammarAST> alts, boolean invert);
@NotNull
Handle set(@NotNull GrammarAST associatedAST, @NotNull List<GrammarAST> alts, boolean invert);
Handle charSetLiteral(GrammarAST charSetAST);
@NotNull
Handle charSetLiteral(@NotNull GrammarAST charSetAST);
Handle range(GrammarAST a, GrammarAST b);
@NotNull
Handle range(@NotNull GrammarAST a, @NotNull GrammarAST b);
/** For a non-lexer, just build a simple token reference atom.
* For a lexer, a string is a sequence of char to match. That is,
@ -87,7 +97,8 @@ public interface ATNFactory {
* the DFA. Machine== o-'f'->o-'o'->o-'g'->o and has n+1 states
* for n characters.
*/
Handle stringLiteral(TerminalAST stringLiteralAST);
@NotNull
Handle stringLiteral(@NotNull TerminalAST stringLiteralAST);
/** For reference to rule r, build
*
@ -105,25 +116,31 @@ public interface ATNFactory {
* TODO add to codegen: collapse alt blks that are sets into single matchSet
* @param node
*/
Handle ruleRef(GrammarAST node);
@NotNull
Handle ruleRef(@NotNull GrammarAST node);
/** From an empty alternative build Grip o-e->o */
Handle epsilon(GrammarAST node);
@NotNull
Handle epsilon(@NotNull GrammarAST node);
/** Build what amounts to an epsilon transition with a semantic
* predicate action. The pred is a pointer into the AST of
* the SEMPRED token.
*/
Handle sempred(PredAST pred);
@NotNull
Handle sempred(@NotNull PredAST pred);
/** Build what amounts to an epsilon transition with an action.
* The action goes into ATN though it is ignored during analysis.
*/
Handle action(ActionAST action);
@NotNull
Handle action(@NotNull ActionAST action);
Handle action(String action);
@NotNull
Handle action(@NotNull String action);
Handle alt(List<Handle> els);
@NotNull
Handle alt(@NotNull List<Handle> els);
/** From A|B|..|Z alternative block build
*
@ -148,7 +165,8 @@ public interface ATNFactory {
*
* Set alt number (1..n) in the left-Transition ATNState.
*/
Handle block(BlockAST blockAST, GrammarAST ebnfRoot, List<Handle> alternativeGrips);
@NotNull
Handle block(@NotNull BlockAST blockAST, @NotNull GrammarAST ebnfRoot, @NotNull List<Handle> alternativeGrips);
// Handle notBlock(GrammarAST blockAST, Handle set);
@ -160,7 +178,8 @@ public interface ATNFactory {
*
* or, if A is a block, just add an empty alt to the end of the block
*/
Handle optional(GrammarAST optAST, Handle blk);
@NotNull
Handle optional(@NotNull GrammarAST optAST, @NotNull Handle blk);
/** From (A)+ build
*
@ -175,7 +194,8 @@ public interface ATNFactory {
* During analysis we'll call the follow link (transition 1) alt n+1 for
* an n-alt A block.
*/
Handle plus(GrammarAST plusAST, Handle blk);
@NotNull
Handle plus(@NotNull GrammarAST plusAST, @NotNull Handle blk);
/** From (A)* build
*
@ -207,14 +227,19 @@ public interface ATNFactory {
* is sufficient to let me make an appropriate enter, exit, loop
* determination. See codegen.g
*/
Handle star(GrammarAST starAST, Handle blk);
@NotNull
Handle star(@NotNull GrammarAST starAST, @NotNull Handle blk);
/** Build an atom with all possible values in its label */
Handle wildcard(GrammarAST associatedAST);
@NotNull
Handle wildcard(@NotNull GrammarAST associatedAST);
Handle lexerAltCommands(ATNFactory.Handle alt, ATNFactory.Handle cmds);
@NotNull
Handle lexerAltCommands(@NotNull Handle alt, @NotNull Handle cmds);
Handle lexerCallCommand(GrammarAST ID, GrammarAST arg);
@NotNull
Handle lexerCallCommand(@NotNull GrammarAST ID, @NotNull GrammarAST arg);
Handle lexerCommand(GrammarAST ID);
@NotNull
Handle lexerCommand(@NotNull GrammarAST ID);
}

View File

@ -108,9 +108,11 @@ public class ParserATNFactory implements ATNFactory {
public int currentOuterAlt;
@NotNull
protected final List<Triple<Rule, ATNState, ATNState>> preventEpsilonClosureBlocks =
new ArrayList<Triple<Rule, ATNState, ATNState>>();
@NotNull
protected final List<Triple<Rule, ATNState, ATNState>> preventEpsilonOptionalBlocks =
new ArrayList<Triple<Rule, ATNState, ATNState>>();
@ -126,6 +128,7 @@ public class ParserATNFactory implements ATNFactory {
this.atn = new ATN(atnType, maxTokenType);
}
@NotNull
@Override
public ATN createATN() {
_createATN(g.rules.values());
@ -167,7 +170,7 @@ public class ParserATNFactory implements ATNFactory {
return atn;
}
protected void _createATN(Collection<Rule> rules) {
protected void _createATN(@NotNull Collection<Rule> rules) {
createRuleStartAndStopATNStates();
GrammarASTAdaptor adaptor = new GrammarASTAdaptor();
@ -188,7 +191,7 @@ public class ParserATNFactory implements ATNFactory {
}
@Override
public void setCurrentRuleName(String name) {
public void setCurrentRuleName(@NotNull String name) {
this.currentRule = g.getRule(name);
}
@ -198,8 +201,9 @@ public class ParserATNFactory implements ATNFactory {
}
/* start->ruleblock->end */
@NotNull
@Override
public Handle rule(GrammarAST ruleAST, String name, Handle blk) {
public Handle rule(@NotNull GrammarAST ruleAST, @NotNull String name, @NotNull Handle blk) {
Rule r = g.getRule(name);
RuleStartState start = atn.ruleToStartState[r.index];
epsilon(start, blk.left);
@ -213,8 +217,9 @@ public class ParserATNFactory implements ATNFactory {
}
/** From label {@code A} build graph {@code o-A->o}. */
@NotNull
@Override
public Handle tokenRef(TerminalAST node) {
public Handle tokenRef(@NotNull TerminalAST node) {
ATNState left = newState(node);
ATNState right = newState(node);
int ttype = g.getTokenType(node.getText());
@ -227,8 +232,9 @@ public class ParserATNFactory implements ATNFactory {
* what an alt block looks like, must have extra state on left.
* This also handles {@code ~A}, converted to {@code ~{A}} set.
*/
@NotNull
@Override
public Handle set(GrammarAST associatedAST, List<GrammarAST> terminals, boolean invert) {
public Handle set(@NotNull GrammarAST associatedAST, @NotNull List<GrammarAST> terminals, boolean invert) {
ATNState left = newState(associatedAST);
ATNState right = newState(associatedAST);
IntervalSet set = new IntervalSet();
@ -247,12 +253,13 @@ public class ParserATNFactory implements ATNFactory {
}
/** Not valid for non-lexers. */
@NotNull
@Override
public Handle range(GrammarAST a, GrammarAST b) {
throw new UnsupportedOperationException();
public Handle range(@NotNull GrammarAST a, @NotNull GrammarAST b) {
throw new UnsupportedOperationException("This construct is not valid in parsers.");
}
protected int getTokenType(GrammarAST atom) {
protected int getTokenType(@NotNull GrammarAST atom) {
int ttype;
if ( g.isLexer() ) {
ttype = CharSupport.getCharValueFromGrammarCharLiteral(atom.getText());
@ -264,14 +271,16 @@ public class ParserATNFactory implements ATNFactory {
}
/** For a non-lexer, just build a simple token reference atom. */
@NotNull
@Override
public Handle stringLiteral(TerminalAST stringLiteralAST) {
public Handle stringLiteral(@NotNull TerminalAST stringLiteralAST) {
return tokenRef(stringLiteralAST);
}
/** {@code [Aa]} char sets not allowed in parser */
@NotNull
@Override
public Handle charSetLiteral(GrammarAST charSetAST) {
public Handle charSetLiteral(@NotNull GrammarAST charSetAST) {
return null;
}
@ -286,13 +295,15 @@ public class ParserATNFactory implements ATNFactory {
* {@code o} is not linked to from rule ref state directly (uses
* {@link RuleTransition#followState}).
*/
@NotNull
@Override
public Handle ruleRef(GrammarAST node) {
public Handle ruleRef(@NotNull GrammarAST node) {
Handle h = _ruleRef(node);
return h;
}
public Handle _ruleRef(GrammarAST node) {
@NotNull
public Handle _ruleRef(@NotNull GrammarAST node) {
Rule r = g.getRule(node.getText());
if ( r==null ) {
g.tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, "Rule "+node.getText()+" undefined");
@ -320,8 +331,9 @@ public class ParserATNFactory implements ATNFactory {
}
/** From an empty alternative build {@code o-e->o}. */
@NotNull
@Override
public Handle epsilon(GrammarAST node) {
public Handle epsilon(@NotNull GrammarAST node) {
ATNState left = newState(node);
ATNState right = newState(node);
epsilon(left, right);
@ -333,8 +345,9 @@ public class ParserATNFactory implements ATNFactory {
* predicate action. The {@code pred} is a pointer into the AST of
* the {@link ANTLRParser#SEMPRED} token.
*/
@NotNull
@Override
public Handle sempred(PredAST pred) {
public Handle sempred(@NotNull PredAST pred) {
//System.out.println("sempred: "+ pred);
ATNState left = newState(pred);
ATNState right = newState(pred);
@ -358,8 +371,9 @@ public class ParserATNFactory implements ATNFactory {
* The action goes into ATN though it is ignored during prediction
* if {@link ActionTransition#actionIndex actionIndex}{@code <0}.
*/
@NotNull
@Override
public Handle action(ActionAST action) {
public Handle action(@NotNull ActionAST action) {
//System.out.println("action: "+action);
ATNState left = newState(action);
ATNState right = newState(action);
@ -369,9 +383,10 @@ public class ParserATNFactory implements ATNFactory {
return new Handle(left, right);
}
@NotNull
@Override
public Handle action(String action) {
return null;
public Handle action(@NotNull String action) {
throw new UnsupportedOperationException("This element is not valid in parsers.");
}
/**
@ -398,8 +413,9 @@ public class ParserATNFactory implements ATNFactory {
* <p/>
* TODO: Set alt number (1..n) in the states?
*/
@NotNull
@Override
public Handle block(BlockAST blkAST, GrammarAST ebnfRoot, List<Handle> alts) {
public Handle block(@NotNull BlockAST blkAST, @NotNull GrammarAST ebnfRoot, @NotNull List<Handle> alts) {
if ( ebnfRoot==null ) {
if ( alts.size()==1 ) {
Handle h = alts.get(0);
@ -430,7 +446,8 @@ public class ParserATNFactory implements ATNFactory {
return null;
}
protected Handle makeBlock(BlockStartState start, BlockAST blkAST, List<Handle> alts) {
@NotNull
protected Handle makeBlock(@NotNull BlockStartState start, @NotNull BlockAST blkAST, @NotNull List<Handle> alts) {
BlockEndState end = newState(BlockEndState.class, blkAST);
start.endState = end;
for (Handle alt : alts) {
@ -615,7 +632,7 @@ public class ParserATNFactory implements ATNFactory {
/** Build an atom with all possible values in its label. */
@NotNull
@Override
public Handle wildcard(GrammarAST node) {
public Handle wildcard(@NotNull GrammarAST node) {
ATNState left = newState(node);
ATNState right = newState(node);
left.addTransition(new WildcardTransition(right));
@ -685,13 +702,15 @@ public class ParserATNFactory implements ATNFactory {
return n;
}
@NotNull
@Override
public Handle label(Handle t) {
public Handle label(@NotNull Handle t) {
return t;
}
@NotNull
@Override
public Handle listLabel(Handle t) {
public Handle listLabel(@NotNull Handle t) {
return t;
}
@ -760,18 +779,21 @@ public class ParserATNFactory implements ATNFactory {
return false;
}
@NotNull
@Override
public Handle lexerAltCommands(Handle alt, Handle cmds) {
return null;
public Handle lexerAltCommands(@NotNull Handle alt, @NotNull Handle cmds) {
throw new UnsupportedOperationException("This element is not allowed in parsers.");
}
@NotNull
@Override
public Handle lexerCallCommand(GrammarAST ID, GrammarAST arg) {
return null;
public Handle lexerCallCommand(@NotNull GrammarAST ID, @NotNull GrammarAST arg) {
throw new UnsupportedOperationException("This element is not allowed in parsers.");
}
@NotNull
@Override
public Handle lexerCommand(GrammarAST ID) {
return null;
public Handle lexerCommand(@NotNull GrammarAST ID) {
throw new UnsupportedOperationException("This element is not allowed in parsers.");
}
}