Search in sources :

Example 16 with Recognizer

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

the class ContentUriChecker method preparePath.

private Pair<ParserRuleContext, CommonTokenStream> preparePath(final String input) {
    UriLexer lexer = new UriLexer(CharStreams.fromString(input));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    UriParser parser = new UriParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(new ContentUriBaseErrorListener() {

        @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, input);
        }
    });
    ParserRuleContext context = parser.path();
    return new Pair<>(context, tokens);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) RecognitionException(org.antlr.v4.runtime.RecognitionException) Pair(com.abubusoft.kripton.common.Pair)

Example 17 with Recognizer

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

the class ContentUriChecker method prepareUri.

private Pair<ParserRuleContext, CommonTokenStream> prepareUri(final String input) {
    UriLexer lexer = new UriLexer(CharStreams.fromString(input));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    UriParser parser = new UriParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(new ContentUriBaseErrorListener() {

        @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 URI '%s'", charPositionInLine, input);
        }

        @Override
        public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact, BitSet ambigAlts, ATNConfigSet configs) {
            AssertKripton.assertTrue(false, "ambiguity syntax at pos %s of URI '%s'", startIndex, input);
        }

        @Override
        public void reportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex, BitSet conflictingAlts, ATNConfigSet configs) {
            AssertKripton.assertTrue(false, "error at pos %s of URI '%s'", startIndex, input);
        }

        @Override
        public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, int prediction, ATNConfigSet configs) {
            AssertKripton.assertTrue(false, "context eror at pos %s of URI '%s'", startIndex, input);
        }
    });
    ParserRuleContext context = parser.uri();
    return new Pair<>(context, tokens);
}
Also used : ATNConfigSet(org.antlr.v4.runtime.atn.ATNConfigSet) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) BitSet(java.util.BitSet) Parser(org.antlr.v4.runtime.Parser) RecognitionException(org.antlr.v4.runtime.RecognitionException) DFA(org.antlr.v4.runtime.dfa.DFA) Pair(com.abubusoft.kripton.common.Pair)

Example 18 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project vespa by vespa-engine.

the class ProgramParser method prepareParser.

private yqlplusParser prepareParser(String programName, CharStream input) {
    yqlplusLexer lexer = new yqlplusLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(new BaseErrorListener() {

        @Override
        public void syntaxError(@NotNull Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol, int line, int charPositionInLine, @NotNull String msg, @Nullable RecognitionException e) {
            throw new ProgramCompileException(new Location(programName, line, charPositionInLine), msg);
        }
    });
    TokenStream tokens = new CommonTokenStream(lexer);
    yqlplusParser parser = new yqlplusParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(new BaseErrorListener() {

        @Override
        public void syntaxError(@NotNull Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol, int line, int charPositionInLine, @NotNull String msg, @Nullable RecognitionException e) {
            throw new ProgramCompileException(new Location(programName, line, charPositionInLine), msg);
        }
    });
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    return parser;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) TokenStream(org.antlr.v4.runtime.TokenStream) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BaseErrorListener(org.antlr.v4.runtime.BaseErrorListener) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 19 with Recognizer

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

the class RuleDependencyChecker method getRuleVersions.

private static int[] getRuleVersions(Class<? extends Recognizer<?, ?>> recognizerClass, String[] ruleNames) {
    int[] versions = new int[ruleNames.length];
    Field[] fields = recognizerClass.getFields();
    for (Field field : fields) {
        boolean isStatic = (field.getModifiers() & Modifier.STATIC) != 0;
        boolean isInteger = field.getType() == Integer.TYPE;
        if (isStatic && isInteger && field.getName().startsWith("RULE_")) {
            try {
                String name = field.getName().substring("RULE_".length());
                if (name.isEmpty() || !Character.isLowerCase(name.charAt(0))) {
                    continue;
                }
                int index = field.getInt(null);
                if (index < 0 || index >= versions.length) {
                    Object[] params = { index, field.getName(), recognizerClass.getSimpleName() };
                    LOGGER.log(Level.WARNING, "Rule index {0} for rule ''{1}'' out of bounds for recognizer {2}.", params);
                    continue;
                }
                Method ruleMethod = getRuleMethod(recognizerClass, name);
                if (ruleMethod == null) {
                    Object[] params = { name, recognizerClass.getSimpleName() };
                    LOGGER.log(Level.WARNING, "Could not find rule method for rule ''{0}'' in recognizer {1}.", params);
                    continue;
                }
                RuleVersion ruleVersion = ruleMethod.getAnnotation(RuleVersion.class);
                int version = ruleVersion != null ? ruleVersion.value() : 0;
                versions[index] = version;
            } catch (IllegalArgumentException ex) {
                LOGGER.log(Level.WARNING, null, ex);
            } catch (IllegalAccessException ex) {
                LOGGER.log(Level.WARNING, null, ex);
            }
        }
    }
    return versions;
}
Also used : Field(java.lang.reflect.Field) RuleVersion(org.antlr.v4.runtime.RuleVersion) Method(java.lang.reflect.Method)

Example 20 with Recognizer

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

the class DefaultErrorStrategy method reportUnwantedToken.

/**
 * This method is called to report a syntax error which requires the removal
 * of a token from the input stream. At the time this method is called, the
 * erroneous symbol is current {@code LT(1)} symbol and has not yet been
 * removed from the input stream. When this method returns,
 * {@code recognizer} is in error recovery mode.
 *
 * <p>This method is called when {@link #singleTokenDeletion} identifies
 * single-token deletion as a viable recovery strategy for a mismatched
 * input error.</p>
 *
 * <p>The default implementation simply returns if the handler is already in
 * error recovery mode. Otherwise, it calls {@link #beginErrorCondition} to
 * enter error recovery mode, followed by calling
 * {@link Parser#notifyErrorListeners}.</p>
 *
 * @param recognizer the parser instance
 */
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 = "extraneous input " + tokenName + " expecting " + expecting.toString(recognizer.getVocabulary());
    recognizer.notifyErrorListeners(t, msg, null);
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet)

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