Search in sources :

Example 11 with Nullable

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;
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) Nullable(org.antlr.v4.runtime.misc.Nullable)

Example 12 with Nullable

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;
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) BitSet(java.util.BitSet) HashSet(java.util.HashSet) Nullable(org.antlr.v4.runtime.misc.Nullable)

Example 13 with Nullable

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);
    }
}
Also used : ClassNode(kalang.ast.ClassNode) GenericType(kalang.core.GenericType) NullableKind(kalang.core.NullableKind) Token(org.antlr.v4.runtime.Token) LinkedList(java.util.LinkedList) ObjectType(kalang.core.ObjectType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) ObjectType(kalang.core.ObjectType) Nullable(javax.annotation.Nullable)

Example 14 with 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;
}
Also used : LiteralContext(kalang.antlr.KalangParser.LiteralContext) ClassNode(kalang.ast.ClassNode) ConstExpr(kalang.ast.ConstExpr) AnnotationNode(kalang.ast.AnnotationNode) Token(org.antlr.v4.runtime.Token) Nullable(javax.annotation.Nullable)

Example 15 with Nullable

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;
}
Also used : Attribute(org.antlr.v4.tool.Attribute) Tuple2(org.antlr.v4.runtime.misc.Tuple2) AttributeDict(org.antlr.v4.tool.AttributeDict)

Aggregations

ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)5 Nullable (org.antlr.v4.runtime.misc.Nullable)5 Token (org.antlr.v4.runtime.Token)4 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)4 ATNState (org.antlr.v4.runtime.atn.ATNState)3 BitSet (java.util.BitSet)2 HashSet (java.util.HashSet)2 Nullable (javax.annotation.Nullable)2 ClassNode (kalang.ast.ClassNode)2 CommonToken (org.antlr.v4.runtime.CommonToken)2 NotNull (org.antlr.v4.runtime.misc.NotNull)2 GraqlLexer (ai.grakn.graql.internal.antlr.GraqlLexer)1 GraqlParser (ai.grakn.graql.internal.antlr.GraqlParser)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Verify (com.google.common.base.Verify)1 Cache (com.google.common.cache.Cache)1 AbstractIterator (com.google.common.collect.AbstractIterator)1 ImmutableList (com.google.common.collect.ImmutableList)1