Search in sources :

Example 11 with Recognizer

use of org.antlr.v4.runtime.Recognizer 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 12 with Recognizer

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

the class BeetlAntlrErrorStrategy method reportNoViableAlternative.

protected void reportNoViableAlternative(@NotNull Parser recognizer, @NotNull NoViableAltException e) {
    TokenStream tokens = recognizer.getInputStream();
    String input;
    if (tokens instanceof TokenStream) {
        if (e.getStartToken().getType() == Token.EOF)
            input = "<文件尾>";
        else
            input = tokens.getText(e.getStartToken(), e.getOffendingToken());
    } else {
        input = "<未知输入>";
    }
    BeetlException exception = null;
    if (keys.contains(e.getOffendingToken().getText())) {
        exception = new BeetlParserException(BeetlException.PARSER_VIABLE_ERROR, "不允许" + e.getOffendingToken().getText() + "关键出现在这里" + ":" + escapeWSAndQuote(input), e);
    } else {
        exception = new BeetlParserException(BeetlException.PARSER_VIABLE_ERROR, escapeWSAndQuote(input), e);
    }
    // String msg = "no viable alternative at input " + escapeWSAndQuote(input);
    exception.pushToken(this.getGrammarToken(e.getOffendingToken()));
    throw exception;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) TokenStream(org.antlr.v4.runtime.TokenStream) BeetlParserException(org.beetl.core.exception.BeetlParserException)

Example 13 with Recognizer

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

the class BeetlAntlrErrorStrategy method reportError.

@Override
public void reportError(Parser recognizer, RecognitionException e) {
    // yet successfully, don't report any errors.
    if (inErrorRecoveryMode(recognizer)) {
        // don't report spurious errors
        return;
    }
    beginErrorCondition(recognizer);
    if (e instanceof NoViableAltException) {
        reportNoViableAlternative(recognizer, (NoViableAltException) e);
    } else if (e instanceof InputMismatchException) {
        reportInputMismatch(recognizer, (InputMismatchException) e);
    } else if (e instanceof FailedPredicateException) {
        reportFailedPredicate(recognizer, (FailedPredicateException) e);
    } else {
        // System.err.println("unknown recognition error type: " + e.getClass().getName());
        BeetlException exception = new BeetlException(BeetlException.PARSER_UNKNOW_ERROR, e.getClass().getName(), e);
        // exception.token = this.getGrammarToken(e.getOffendingToken());
        exception.pushToken(this.getGrammarToken(e.getOffendingToken()));
        throw exception;
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) NoViableAltException(org.antlr.v4.runtime.NoViableAltException) FailedPredicateException(org.antlr.v4.runtime.FailedPredicateException) InputMismatchException(org.antlr.v4.runtime.InputMismatchException)

Example 14 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project ihmc-pub-sub by ihmcrobotics.

the class DefaultErrorStrategy method reportUnwantedToken.

@Override
protected void reportUnwantedToken(@NotNull Parser recognizer) {
    if (inErrorRecoveryMode(recognizer)) {
        return;
    }
    beginErrorCondition(recognizer);
    Token t = recognizer.getCurrentToken();
    String tokenName = getTokenErrorDisplay(t);
    String msg = "Unexpected input " + ColorMessage.bold(tokenName);
    recognizer.notifyErrorListeners(t, msg, null);
}
Also used : Token(org.antlr.v4.runtime.Token)

Example 15 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project kripton by xcesco.

the class MigrationSQLChecker method prepareParser.

protected Pair<ParserRuleContext, CommonTokenStream> prepareParser(final String jql) {
    JqlLexer lexer = new JqlLexer(CharStreams.fromString(jql));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JqlParser parser = new JqlParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(new JQLBaseErrorListener() {

        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            AssertKripton.assertTrue(false, "unespected char at pos %s of SQL '%s'", charPositionInLine, jql);
        }
    });
    ParserRuleContext context = parser.parse();
    return new Pair<>(context, tokens);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) JqlLexer(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlLexer) JqlParser(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser) RecognitionException(org.antlr.v4.runtime.RecognitionException) Pair(com.abubusoft.kripton.common.Pair)

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