Java Code Examples for org.antlr.v4.runtime.Parser#getATN()

The following examples show how to use org.antlr.v4.runtime.Parser#getATN() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ErrorHandler.java    From presto with Apache License 2.0 5 votes vote down vote up
public Analyzer(
        Parser parser,
        Map<Integer, String> specialRules,
        Map<Integer, String> specialTokens,
        Set<Integer> ignoredRules)
{
    this.parser = parser;
    this.stream = parser.getTokenStream();
    this.atn = parser.getATN();
    this.vocabulary = parser.getVocabulary();
    this.specialRules = specialRules;
    this.specialTokens = specialTokens;
    this.ignoredRules = ignoredRules;
}
 
Example 2
Source File: ParserWrapper.java    From antlr4-autosuggest with Apache License 2.0 5 votes vote down vote up
public ParserWrapper(ParserFactory parserFactory, Vocabulary lexerVocabulary) {
    this.lexerVocabulary = lexerVocabulary;
    
    Parser parserForAtnOnly = parserFactory.createParser(null);
    this.parserAtn = parserForAtnOnly.getATN();
    this.parserRuleNames = parserForAtnOnly.getRuleNames();
    logger.debug("Parser rule names: " + StringUtils.join(parserForAtnOnly.getRuleNames(), ", "));
}
 
Example 3
Source File: ErrorHandler.java    From presto with Apache License 2.0 4 votes vote down vote up
@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(parser, specialRules, specialTokens, ignoredRules);
        Result result = 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 = result.getExpected().stream()
                .sorted()
                .collect(Collectors.joining(", "));

        message = format("mismatched input '%s'. Expecting: %s", parser.getTokenStream().get(result.getErrorTokenIndex()).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);
}