use of org.antlr.v4.runtime.misc.Nullable in project antlr4 by tunnelvisionlabs.
the class DefaultErrorStrategy method singleTokenDeletion.
/**
* This method implements the single-token deletion inline error recovery
* strategy. It is called by {@link #recoverInline} to attempt to recover
* from mismatched input. If this method returns null, the parser and error
* handler state will not have changed. If this method returns non-null,
* {@code recognizer} will <em>not</em> be in error recovery mode since the
* returned token was a successful match.
*
* <p>If the single-token deletion is successful, this method calls
* {@link #reportUnwantedToken} to report the error, followed by
* {@link Parser#consume} to actually "delete" the extraneous token. Then,
* before returning {@link #reportMatch} is called to signal a successful
* match.</p>
*
* @param recognizer the parser instance
* @return the successfully matched {@link Token} instance if single-token
* deletion successfully recovers from the mismatched input, otherwise
* {@code null}
*/
@Nullable
protected Token singleTokenDeletion(@NotNull Parser recognizer) {
int nextTokenType = recognizer.getInputStream().LA(2);
IntervalSet expecting = getExpectedTokens(recognizer);
if (expecting.contains(nextTokenType)) {
reportUnwantedToken(recognizer);
/*
System.err.println("recoverFromMismatchedToken deleting "+
((TokenStream)recognizer.getInputStream()).LT(1)+
" since "+((TokenStream)recognizer.getInputStream()).LT(2)+
" is what we want");
*/
// simply delete extra token
recognizer.consume();
// we want to return the token we're actually matching
Token matchedSymbol = recognizer.getCurrentToken();
// we know current token is correct
reportMatch(recognizer);
return matchedSymbol;
}
return null;
}
use of org.antlr.v4.runtime.misc.Nullable in project antlr4 by tunnelvisionlabs.
the class LL1Analyzer method getDecisionLookahead.
/**
* Calculates the SLL(1) expected lookahead set for each outgoing transition
* of an {@link ATNState}. The returned array has one element for each
* outgoing transition in {@code s}. If the closure from transition
* <em>i</em> leads to a semantic predicate before matching a symbol, the
* element at index <em>i</em> of the result will be {@code null}.
*
* @param s the ATN state
* @return the expected symbols for each outgoing transition of {@code 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()];
for (int alt = 0; alt < s.getNumberOfTransitions(); alt++) {
look[alt] = new IntervalSet();
Set<ATNConfig> lookBusy = new HashSet<ATNConfig>();
// fail to get lookahead upon pred
boolean seeThruPreds = false;
_LOOK(s.transition(alt).target, null, PredictionContext.EMPTY_LOCAL, look[alt], lookBusy, new BitSet(), seeThruPreds, false);
// or we had a predicate when we !seeThruPreds
if (look[alt].size() == 0 || look[alt].contains(HIT_PRED)) {
look[alt] = null;
}
}
return look;
}
use of org.antlr.v4.runtime.misc.Nullable in project kalang by kasonyang.
the class AstBuilder method parseClassType.
@Nullable
protected ObjectType parseClassType(KalangParser.ClassTypeContext ctx) {
NullableKind nullable = ctx.nullable == null ? NullableKind.NONNULL : NullableKind.NULLABLE;
Token rawTypeToken = ctx.rawClass;
List<String> classNameParts = new LinkedList();
for (Token p : ctx.paths) {
classNameParts.add(p.getText());
}
if (ctx.innerClass != null) {
classNameParts.add(rawTypeToken.getText() + "$" + ctx.innerClass.getText());
} else {
classNameParts.add(rawTypeToken.getText());
}
String rawType = String.join(".", classNameParts);
for (GenericType gt : thisClazz.getGenericTypes()) {
if (rawType.equals(gt.getName()))
return gt;
}
ObjectType clazzType = requireClassType(rawType, rawTypeToken);
if (clazzType == null)
return null;
ClassNode clazzNode = clazzType.getClassNode();
GenericType[] clzDeclaredGenericTypes = clazzNode.getGenericTypes();
List<KalangParser.ParameterizedElementTypeContext> parameterTypes = ctx.parameterTypes;
if (parameterTypes != null && !parameterTypes.isEmpty()) {
Type[] typeArguments = new Type[parameterTypes.size()];
if (parameterTypes != null && parameterTypes.size() > 0) {
if (clzDeclaredGenericTypes.length != parameterTypes.size()) {
diagnosisReporter.report(Diagnosis.Kind.ERROR, "wrong number of type arguments", ctx);
return null;
}
for (int i = 0; i < typeArguments.length; i++) {
typeArguments[i] = parseParameterizedElementType(parameterTypes.get(i));
// TODO should return null?
if (typeArguments[i] == null)
return null;
}
}
return Types.getClassType(clazzType.getClassNode(), typeArguments, nullable);
} else {
return Types.getClassType(clazzType.getClassNode(), nullable);
}
}
use of org.antlr.v4.runtime.misc.Nullable in project kalang by kasonyang.
the class AstBuilder method visitAnnotation.
@Override
@Nullable
public AnnotationNode visitAnnotation(KalangParser.AnnotationContext ctx) {
ClassNode anType = requireAst(ctx.annotationType);
if (anType == null)
return null;
List<Token> vk = ctx.annotationValueKey;
LiteralContext dv = ctx.annotationDefaultValue;
AnnotationNode anNode = new AnnotationNode(anType);
if (vk != null && vk.size() > 0) {
List<LiteralContext> anValues = ctx.annotationValue;
int ksize = vk.size();
for (int i = 0; i < ksize; i++) {
String kname = vk.get(i).getText();
ConstExpr value = visitLiteral(anValues.get(i));
anNode.values.put(kname, value);
}
} else if (dv != null) {
ConstExpr defaultValue = visitLiteral(dv);
anNode.values.put("value", defaultValue);
}
if (!semanticAnalyzer.validateAnnotation(anNode))
return null;
// TODO validate annotation's values
return anNode;
}
use of org.antlr.v4.runtime.misc.Nullable in project antlr4 by tunnelvisionlabs.
the class ScopeParser method parse.
public static AttributeDict parse(@Nullable ActionAST action, String s, char separator, Grammar g) {
AttributeDict dict = new AttributeDict();
List<Tuple2<String, Integer>> decls = splitDecls(s, separator);
for (Tuple2<String, Integer> decl : decls) {
if (decl.getItem1().trim().length() > 0) {
Attribute a = parseAttributeDef(action, decl, g);
dict.add(a);
}
}
return dict;
}
Aggregations