Search in sources :

Example 6 with Nullable

use of org.antlr.v4.runtime.misc.Nullable in project titan.EclipsePlug-ins by eclipse.

the class TitanListener method syntaxError.

@Override
public void syntaxError(@NotNull final Recognizer<?, ?> recognizer, @Nullable final Object offendingSymbol, final int line, final int charPositionInLine, @NotNull final String msg, @Nullable final RecognitionException e) {
    SyntacticErrorStorage errorStorage;
    if (offendingSymbol instanceof CommonToken) {
        final CommonToken token = (CommonToken) offendingSymbol;
        errorStorage = new SyntacticErrorStorage(line, token.getStartIndex(), token.getStopIndex() + 1, msg, e);
    } else {
        errorStorage = new SyntacticErrorStorage(line, charPositionInLine, charPositionInLine + 1, msg, e);
    }
    errorsStored.add(errorStorage);
}
Also used : CommonToken(org.antlr.v4.runtime.CommonToken)

Example 7 with Nullable

use of org.antlr.v4.runtime.misc.Nullable in project antlr4 by tunnelvisionlabs.

the class ParserATNFactory method newState.

@NotNull
public ATNState newState(@Nullable GrammarAST node) {
    ATNState n = new BasicState();
    n.setRuleIndex(currentRule.index);
    atn.addState(n);
    return n;
}
Also used : BasicState(org.antlr.v4.runtime.atn.BasicState) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 8 with Nullable

use of org.antlr.v4.runtime.misc.Nullable in project antlr4 by tunnelvisionlabs.

the class Trees method getNodeText.

public static String getNodeText(@NotNull Tree t, @Nullable List<String> ruleNames) {
    if (ruleNames != null) {
        if (t instanceof RuleNode) {
            RuleContext ruleContext = ((RuleNode) t).getRuleContext();
            int ruleIndex = ruleContext.getRuleIndex();
            String ruleName = ruleNames.get(ruleIndex);
            int altNumber = ruleContext.getAltNumber();
            if (altNumber != ATN.INVALID_ALT_NUMBER) {
                return ruleName + ":" + altNumber;
            }
            return ruleName;
        } else if (t instanceof ErrorNode) {
            return t.toString();
        } else if (t instanceof TerminalNode) {
            Token symbol = ((TerminalNode) t).getSymbol();
            if (symbol != null) {
                String s = symbol.getText();
                return s;
            }
        }
    }
    // no recog for rule names
    Object payload = t.getPayload();
    if (payload instanceof Token) {
        return ((Token) payload).getText();
    }
    return t.getPayload().toString();
}
Also used : RuleContext(org.antlr.v4.runtime.RuleContext) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) CommonToken(org.antlr.v4.runtime.CommonToken) Token(org.antlr.v4.runtime.Token)

Example 9 with Nullable

use of org.antlr.v4.runtime.misc.Nullable in project antlr4 by tunnelvisionlabs.

the class ScopeParser method parseAttributeDef.

/**
 * For decls like "String foo" or "char *foo32[]" compute the ID
 * and type declarations.  Also handle "int x=3" and 'T t = new T("foo")'
 * but if the separator is ',' you cannot use ',' in the initvalue
 * unless you escape use "\," escape.
 */
public static Attribute parseAttributeDef(@Nullable ActionAST action, @NotNull Tuple2<String, Integer> decl, Grammar g) {
    if (decl.getItem1() == null)
        return null;
    Attribute attr = new Attribute();
    int rightEdgeOfDeclarator = decl.getItem1().length() - 1;
    int equalsIndex = decl.getItem1().indexOf('=');
    if (equalsIndex > 0) {
        // everything after the '=' is the init value
        attr.initValue = decl.getItem1().substring(equalsIndex + 1, decl.getItem1().length()).trim();
        rightEdgeOfDeclarator = equalsIndex - 1;
    }
    String declarator = decl.getItem1().substring(0, rightEdgeOfDeclarator + 1);
    Tuple2<Integer, Integer> p;
    String text = decl.getItem1();
    text = text.replaceAll("::", "");
    if (text.contains(":")) {
        // declarator has type appearing after the name like "x:T"
        p = _parsePostfixDecl(attr, declarator, action, g);
    } else {
        // declarator has type appearing before the name like "T x"
        p = _parsePrefixDecl(attr, declarator, action, g);
    }
    int idStart = p.getItem1();
    int idStop = p.getItem2();
    attr.decl = decl.getItem1();
    if (action != null) {
        String actionText = action.getText();
        int[] lines = new int[actionText.length()];
        int[] charPositionInLines = new int[actionText.length()];
        for (int i = 0, line = 0, col = 0; i < actionText.length(); i++, col++) {
            lines[i] = line;
            charPositionInLines[i] = col;
            if (actionText.charAt(i) == '\n') {
                line++;
                col = -1;
            }
        }
        int[] charIndexes = new int[actionText.length()];
        for (int i = 0, j = 0; i < actionText.length(); i++, j++) {
            charIndexes[j] = i;
            // skip comments
            if (i < actionText.length() - 1 && actionText.charAt(i) == '/' && actionText.charAt(i + 1) == '/') {
                while (i < actionText.length() && actionText.charAt(i) != '\n') {
                    i++;
                }
            }
        }
        int declOffset = charIndexes[decl.getItem2()];
        int declLine = lines[declOffset + idStart];
        int line = action.getToken().getLine() + declLine;
        int charPositionInLine = charPositionInLines[declOffset + idStart];
        if (declLine == 0) {
            /* offset for the start position of the ARG_ACTION token, plus 1
				 * since the ARG_ACTION text had the leading '[' stripped before
				 * reaching the scope parser.
				 */
            charPositionInLine += action.getToken().getCharPositionInLine() + 1;
        }
        int offset = ((CommonToken) action.getToken()).getStartIndex();
        attr.token = new CommonToken(action.getToken().getInputStream(), ANTLRParser.ID, BaseRecognizer.DEFAULT_TOKEN_CHANNEL, offset + declOffset + idStart + 1, offset + declOffset + idStop);
        attr.token.setLine(line);
        attr.token.setCharPositionInLine(charPositionInLine);
        assert attr.name.equals(attr.token.getText()) : "Attribute text should match the pseudo-token text at this point.";
    }
    return attr;
}
Also used : Attribute(org.antlr.v4.tool.Attribute) CommonToken(org.antlr.runtime.CommonToken)

Example 10 with Nullable

use of org.antlr.v4.runtime.misc.Nullable in project antlr4 by tunnelvisionlabs.

the class ParserATNSimulator method adaptivePredict.

public int adaptivePredict(@NotNull TokenStream input, int decision, @Nullable ParserRuleContext outerContext, boolean useContext) {
    DFA dfa = atn.decisionToDFA[decision];
    assert dfa != null;
    if (optimize_ll1 && !dfa.isPrecedenceDfa() && !dfa.isEmpty()) {
        int ll_1 = input.LA(1);
        if (ll_1 >= 0 && ll_1 <= Short.MAX_VALUE) {
            int key = (decision << 16) + ll_1;
            Integer alt = atn.LL1Table.get(key);
            if (alt != null) {
                return alt;
            }
        }
    }
    this.dfa = dfa;
    if (force_global_context) {
        useContext = true;
    } else if (!always_try_local_context) {
        useContext |= dfa.isContextSensitive();
    }
    userWantsCtxSensitive = useContext || (predictionMode != PredictionMode.SLL && outerContext != null && !atn.decisionToState.get(decision).sll);
    if (outerContext == null) {
        outerContext = ParserRuleContext.emptyContext();
    }
    SimulatorState state = null;
    if (!dfa.isEmpty()) {
        state = getStartState(dfa, input, outerContext, useContext);
    }
    if (state == null) {
        if (outerContext == null)
            outerContext = ParserRuleContext.emptyContext();
        if (debug)
            System.out.println("ATN decision " + dfa.decision + " exec LA(1)==" + getLookaheadName(input) + ", outerContext=" + outerContext.toString(parser));
        state = computeStartState(dfa, outerContext, useContext);
    }
    int m = input.mark();
    int index = input.index();
    try {
        int alt = execDFA(dfa, input, index, state);
        if (debug)
            System.out.println("DFA after predictATN: " + dfa.toString(parser.getVocabulary(), parser.getRuleNames()));
        return alt;
    } finally {
        this.dfa = null;
        input.seek(index);
        input.release(m);
    }
}
Also used : DFA(org.antlr.v4.runtime.dfa.DFA)

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