Search in sources :

Example 61 with Recognizer

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

the class BeetlAntlrErrorStrategy method reportMissingToken.

protected void reportMissingToken(@NotNull Parser recognizer) {
    if (inErrorRecoveryMode(recognizer)) {
        return;
    }
    beginErrorCondition(recognizer);
    // Token t = recognizer.getCurrentToken();
    Token t = recognizer.getTokenStream().LT(-1);
    IntervalSet expecting = getExpectedTokens(recognizer);
    String expect = expecting.toString(recognizer.getTokenNames());
    if (expects.containsKey(expect)) {
        expect = expects.get(expect);
    }
    if (expect.equals("'>>'")) {
        expect = "'模板的占位结束符号'";
    }
    String tokenStr = getTokenErrorDisplay(t);
    String msg = "缺少输入 " + expect + " 在 " + tokenStr + " 后面";
    BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg);
    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 62 with Recognizer

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

the class RuleDependencyProcessor method extractRuleRelations.

private RuleRelations extractRuleRelations(TypeMirror recognizer) {
    String serializedATN = getSerializedATN(recognizer);
    if (serializedATN == null) {
        return null;
    }
    ATN atn = new ATNDeserializer().deserialize(serializedATN.toCharArray());
    RuleRelations relations = new RuleRelations(atn.ruleToStartState.length);
    for (ATNState state : atn.states) {
        if (!state.epsilonOnlyTransitions) {
            continue;
        }
        for (Transition transition : state.getTransitions()) {
            if (transition.getSerializationType() != Transition.RULE) {
                continue;
            }
            RuleTransition ruleTransition = (RuleTransition) transition;
            relations.addRuleInvocation(state.ruleIndex, ruleTransition.target.ruleIndex);
        }
    }
    return relations;
}
Also used : ATNDeserializer(org.antlr.v4.runtime.atn.ATNDeserializer) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) Transition(org.antlr.v4.runtime.atn.Transition) ATN(org.antlr.v4.runtime.atn.ATN) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 63 with Recognizer

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

the class RuleDependencyProcessor method getRuleVersions.

private int[] getRuleVersions(TypeMirror recognizerClass, String[] ruleNames) {
    int[] versions = new int[ruleNames.length];
    List<? extends Element> elements = processingEnv.getElementUtils().getAllMembers((TypeElement) processingEnv.getTypeUtils().asElement(recognizerClass));
    for (Element element : elements) {
        if (element.getKind() != ElementKind.FIELD) {
            continue;
        }
        VariableElement field = (VariableElement) element;
        boolean isStatic = element.getModifiers().contains(Modifier.STATIC);
        Object constantValue = field.getConstantValue();
        boolean isInteger = constantValue instanceof Integer;
        String name = field.getSimpleName().toString();
        if (isStatic && isInteger && name.startsWith("RULE_")) {
            try {
                name = name.substring("RULE_".length());
                if (name.isEmpty() || !Character.isLowerCase(name.charAt(0))) {
                    continue;
                }
                int index = (Integer) constantValue;
                if (index < 0 || index >= versions.length) {
                    String message = String.format("Rule index %d for rule '%s' out of bounds for recognizer %s.", index, name, recognizerClass.toString());
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, element);
                    continue;
                }
                if (name.indexOf(ATNSimulator.RULE_VARIANT_DELIMITER) >= 0) {
                    // ignore left-factored pseudo-rules
                    continue;
                }
                ExecutableElement ruleMethod = getRuleMethod(recognizerClass, name);
                if (ruleMethod == null) {
                    String message = String.format("Could not find rule method for rule '%s' in recognizer %s.", name, recognizerClass.toString());
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, element);
                    continue;
                }
                RuleVersion ruleVersion = ruleMethod.getAnnotation(RuleVersion.class);
                int version = ruleVersion != null ? ruleVersion.value() : 0;
                versions[index] = version;
            } catch (IllegalArgumentException ex) {
                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Exception occurred while validating rule dependencies.", element);
            }
        }
    }
    return versions;
}
Also used : VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) RuleVersion(org.antlr.v4.runtime.RuleVersion) VariableElement(javax.lang.model.element.VariableElement)

Example 64 with Recognizer

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

the class BuildDependencyGenerator method getGeneratedFileList.

/**
 * From T.g return a list of File objects that
 *  name files ANTLR will emit from T.g.
 */
public List<File> getGeneratedFileList() {
    Target target = generator.getTarget();
    if (target == null) {
        // if the target could not be loaded, no code will be generated.
        return new ArrayList<File>();
    }
    List<File> files = new ArrayList<File>();
    // add generated recognizer; e.g., TParser.java
    if (generator.getTarget().needsHeader()) {
        files.add(getOutputFile(generator.getRecognizerFileName(true)));
    }
    files.add(getOutputFile(generator.getRecognizerFileName(false)));
    // add output vocab file; e.g., T.tokens. This is always generated to
    // the base output directory, which will be just . if there is no -o option
    // 
    files.add(getOutputFile(generator.getVocabFileName()));
    // are we generating a .h file?
    ST headerExtST = null;
    ST extST = target.getTemplates().getInstanceOf("codeFileExtension");
    if (target.getTemplates().isDefined("headerFile")) {
        headerExtST = target.getTemplates().getInstanceOf("headerFileExtension");
        String suffix = Grammar.getGrammarTypeToFileNameSuffix(g.getType());
        String fileName = g.name + suffix + headerExtST.render();
        files.add(getOutputFile(fileName));
    }
    if (g.isCombined()) {
        // add autogenerated lexer; e.g., TLexer.java TLexer.h TLexer.tokens
        String suffix = Grammar.getGrammarTypeToFileNameSuffix(ANTLRParser.LEXER);
        String lexer = g.name + suffix + extST.render();
        files.add(getOutputFile(lexer));
        String lexerTokens = g.name + suffix + CodeGenerator.VOCAB_FILE_EXTENSION;
        files.add(getOutputFile(lexerTokens));
        // TLexer.h
        if (headerExtST != null) {
            String header = g.name + suffix + headerExtST.render();
            files.add(getOutputFile(header));
        }
    }
    if (g.tool.gen_listener) {
        // add generated listener; e.g., TListener.java
        if (generator.getTarget().needsHeader()) {
            files.add(getOutputFile(generator.getListenerFileName(true)));
        }
        files.add(getOutputFile(generator.getListenerFileName(false)));
        // add generated base listener; e.g., TBaseListener.java
        if (generator.getTarget().needsHeader()) {
            files.add(getOutputFile(generator.getBaseListenerFileName(true)));
        }
        files.add(getOutputFile(generator.getBaseListenerFileName(false)));
    }
    if (g.tool.gen_visitor) {
        // add generated visitor; e.g., TVisitor.java
        if (generator.getTarget().needsHeader()) {
            files.add(getOutputFile(generator.getVisitorFileName(true)));
        }
        files.add(getOutputFile(generator.getVisitorFileName(false)));
        // add generated base visitor; e.g., TBaseVisitor.java
        if (generator.getTarget().needsHeader()) {
            files.add(getOutputFile(generator.getBaseVisitorFileName(true)));
        }
        files.add(getOutputFile(generator.getBaseVisitorFileName(false)));
    }
    // handle generated files for imported grammars
    List<Grammar> imports = g.getAllImportedGrammars();
    if (imports != null) {
        for (Grammar g : imports) {
            // File outputDir = tool.getOutputDirectory(g.fileName);
            // String fname = groomQualifiedFileName(outputDir.toString(), g.getRecognizerName() + extST.render());
            // files.add(new File(outputDir, fname));
            files.add(getOutputFile(g.fileName));
        }
    }
    if (files.isEmpty()) {
        return null;
    }
    return files;
}
Also used : ST(org.stringtemplate.v4.ST) Target(org.antlr.v4.codegen.Target) ArrayList(java.util.ArrayList) STGroupFile(org.stringtemplate.v4.STGroupFile) File(java.io.File)

Example 65 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project presto by prestodb.

the class ErrorHandler method syntaxError.

@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String message, RecognitionException e) {
    try {
        Parser parser = (Parser) recognizer;
        ATN atn = parser.getATN();
        ATNState currentState;
        Token currentToken;
        RuleContext context;
        if (e != null) {
            currentState = atn.states.get(e.getOffendingState());
            currentToken = e.getOffendingToken();
            context = e.getCtx();
            if (e instanceof NoViableAltException) {
                currentToken = ((NoViableAltException) e).getStartToken();
            }
        } else {
            currentState = atn.states.get(parser.getState());
            currentToken = parser.getCurrentToken();
            context = parser.getContext();
        }
        Analyzer analyzer = new Analyzer(atn, parser.getVocabulary(), specialRules, specialTokens, ignoredRules, parser.getTokenStream());
        Multimap<Integer, String> candidates = analyzer.process(currentState, currentToken.getTokenIndex(), context);
        // pick the candidate tokens associated largest token index processed (i.e., the path that consumed the most input)
        String expected = candidates.asMap().entrySet().stream().max(Comparator.comparing(Map.Entry::getKey)).get().getValue().stream().sorted().collect(Collectors.joining(", "));
        message = String.format("mismatched input '%s'. Expecting: %s", ((Token) offendingSymbol).getText(), expected);
    } catch (Exception exception) {
        LOG.error(exception, "Unexpected failure when handling parsing error. This is likely a bug in the implementation");
    }
    throw new ParsingException(message, e, line, charPositionInLine);
}
Also used : RuleContext(org.antlr.v4.runtime.RuleContext) Token(org.antlr.v4.runtime.Token) NoViableAltException(org.antlr.v4.runtime.NoViableAltException) RecognitionException(org.antlr.v4.runtime.RecognitionException) Parser(org.antlr.v4.runtime.Parser) ATNState(org.antlr.v4.runtime.atn.ATNState) NoViableAltException(org.antlr.v4.runtime.NoViableAltException) ATN(org.antlr.v4.runtime.atn.ATN)

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