Search in sources :

Example 56 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project kalang by kasonyang.

the class AntlrErrorString method noViableAlt.

public static String noViableAlt(Parser recognizer, NoViableAltException e) {
    TokenStream tokens = recognizer.getInputStream();
    String input = null;
    if (tokens != null) {
        Token startToken = e.getStartToken();
        if (startToken.getType() == Token.EOF) {
            input = "<EOF>";
        } else {
            input = tokens.getText(startToken, e.getOffendingToken());
        }
    }
    return "syntax error at input:" + input;
}
Also used : TokenStream(org.antlr.v4.runtime.TokenStream) Token(org.antlr.v4.runtime.Token)

Example 57 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project ksql by confluentinc.

the class KsqlParserErrorStrategy method reportUnwantedToken.

protected void reportUnwantedToken(Parser recognizer) {
    if (!this.inErrorRecoveryMode(recognizer)) {
        this.beginErrorCondition(recognizer);
        Token t = recognizer.getCurrentToken();
        String tokenName = this.getTokenErrorDisplay(t);
        IntervalSet expecting = this.getExpectedTokens(recognizer);
        String msg = "extraneous input " + tokenName + " expecting " + expecting.toString(recognizer.getVocabulary());
        recognizer.notifyErrorListeners(t, msg, (RecognitionException) null);
    }
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) Token(org.antlr.v4.runtime.Token)

Example 58 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project batfish by batfish.

the class BatfishANTLRErrorStrategy method consumeBlocksUntilWanted.

/**
 * Consume all tokens a whole line at a time until the next token is one expected by the current
 * rule. Each line (as delimited by supplied separator token) starting from the current line up to
 * the last line consumed is placed in an {@link ErrorNode} and inserted as a child of the current
 * rule.
 *
 * @param recognizer The {@link Parser} to whom to delegate creation of each {@link ErrorNode}
 */
private void consumeBlocksUntilWanted(Parser recognizer) {
    IntervalSet expecting = recognizer.getExpectedTokens();
    IntervalSet whatFollowsLoopIterationOrRule = expecting.or(getErrorRecoverySet(recognizer));
    int nextToken;
    do {
        // Eat tokens until we are at the end of the line
        consumeUntilEndOfLine(recognizer);
        // Get the line number and separator text from the separator token
        Token separatorToken = recognizer.getCurrentToken();
        // Insert the current line as an {@link ErrorNode} as a child of the current rule
        createErrorNode(recognizer, recognizer.getContext(), separatorToken);
        // Eat the separator token
        recognizer.consume();
        nextToken = recognizer.getInputStream().LA(1);
    } while (!whatFollowsLoopIterationOrRule.contains(nextToken) && nextToken != Lexer.EOF);
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) Token(org.antlr.v4.runtime.Token)

Example 59 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project beetl2.0 by javamonkey.

the class BeetlAntlrErrorStrategy method reportUnwantedToken.

protected void reportUnwantedToken(@NotNull Parser recognizer) {
    if (inErrorRecoveryMode(recognizer)) {
        return;
    }
    beginErrorCondition(recognizer);
    Token t = recognizer.getCurrentToken();
    String tokenName = getTokenErrorDisplay(t);
    IntervalSet expecting = getExpectedTokens(recognizer);
    String msg = "多余输入 " + tokenName + " 期望 " + expecting.toString(recognizer.getTokenNames());
    BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg);
    // exception.token = this.getGrammarToken(t);
    exception.pushToken(this.getGrammarToken(t));
    throw exception;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BeetlParserException(org.beetl.core.exception.BeetlParserException) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) GrammarToken(org.beetl.core.statement.GrammarToken) Token(org.antlr.v4.runtime.Token)

Example 60 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project beetl2.0 by javamonkey.

the class BeetlAntlrErrorStrategy method reportInputMismatch.

protected void reportInputMismatch(@NotNull Parser recognizer, @NotNull InputMismatchException e) {
    Token t1 = recognizer.getInputStream().LT(-1);
    String msg = "缺少输入在 " + getTokenErrorDisplay(t1) + " 后面, 期望 " + e.getExpectedTokens().toString(recognizer.getTokenNames());
    BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg, e);
    // exception.token = this.getGrammarToken(e.getOffendingToken());
    exception.pushToken(this.getGrammarToken(t1));
    throw exception;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BeetlParserException(org.beetl.core.exception.BeetlParserException) GrammarToken(org.beetl.core.statement.GrammarToken) Token(org.antlr.v4.runtime.Token)

Aggregations

IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)24 Token (org.antlr.v4.runtime.Token)22 RecognitionException (org.antlr.v4.runtime.RecognitionException)19 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)15 File (java.io.File)11 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)10 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)10 ATNState (org.antlr.v4.runtime.atn.ATNState)9 IOException (java.io.IOException)8 BaseErrorListener (org.antlr.v4.runtime.BaseErrorListener)8 Parser (org.antlr.v4.runtime.Parser)8 BaseRuntimeTest.writeFile (org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)8 ArrayList (java.util.ArrayList)7 ATN (org.antlr.v4.runtime.atn.ATN)6 Pair (com.abubusoft.kripton.common.Pair)5 InputMismatchException (org.antlr.v4.runtime.InputMismatchException)5 TokenStream (org.antlr.v4.runtime.TokenStream)5 BeetlException (org.beetl.core.exception.BeetlException)5 STGroupString (org.stringtemplate.v4.STGroupString)5 CommonToken (org.antlr.v4.runtime.CommonToken)4