Example usage for org.antlr.v4.runtime Parser getTokenStream

List of usage examples for org.antlr.v4.runtime Parser getTokenStream

Introduction

In this page you can find the example usage for org.antlr.v4.runtime Parser getTokenStream.

Prototype

public TokenStream getTokenStream() 

Source Link

Usage

From source file:com.facebook.presto.sql.parser.ErrorHandler.java

License:Apache License

@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
        String message, RecognitionException e) {
    try {/*from  w w  w .  j  a  v  a  2s.  c o  m*/
        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);
}

From source file:org.ballerinalang.composer.service.workspace.suggetions.CapturePossibleTokenStrategy.java

License:Open Source License

/**
 * Checks whether cursor is within the whitespace region between current token to last token
 * @param token Token to be evaluated/*from   w w  w  . j  a v  a 2 s  .c o m*/
 * @param parser Parser Instance
 * @return true|false
 */
protected boolean isCursorBetweenGivenTokenAndLastNonHiddenToken(Token token, Parser parser) {
    boolean isCursorBetween = false;
    if (cursorPosition.equals(getSourcePosition(token))) {
        isCursorBetween = true;
    } else {
        Token lastNonHiddenToken = null;
        for (int tokenIdx = token.getTokenIndex() - 1; tokenIdx >= 0; tokenIdx--) {
            Token lastToken = parser.getTokenStream().get(tokenIdx);
            if (lastToken.getChannel() != Token.HIDDEN_CHANNEL) {
                lastNonHiddenToken = lastToken;
                break;
            }
        }
        if (lastNonHiddenToken != null) {
            if (cursorPosition.getLine() >= lastNonHiddenToken.getLine()
                    && cursorPosition.getLine() <= token.getLine()) {
                if (cursorPosition.getLine() == lastNonHiddenToken.getLine()) {
                    isCursorBetween = cursorPosition
                            .getCharacter() >= (lastNonHiddenToken.getCharPositionInLine()
                                    + lastNonHiddenToken.getText().length());
                } else if (cursorPosition.getLine() == token.getLine()) {
                    isCursorBetween = cursorPosition.getCharacter() <= token.getCharPositionInLine();
                } else {
                    isCursorBetween = true;
                }
            }
        }

    }
    return isCursorBetween;
}

From source file:org.ballerinalang.composer.service.workspace.suggetions.SuggestionsFilterDataModel.java

License:Open Source License

private void init(Parser parser) {
    if (parser != null) {
        this.tokenStream = parser.getTokenStream();
        this.vocabulary = parser.getVocabulary();
        this.tokenIndex = parser.getCurrentToken().getTokenIndex();
    }//  www. j a  v  a2  s  .  c  o m
}

From source file:org.ballerinalang.langserver.completions.BallerinaCustomErrorStrategy.java

License:Open Source License

private void fillContext(Parser parser, Token currentToken) {
    ParserRuleContext currentContext = parser.getContext();
    if (isCursorBetweenGivenTokenAndLastNonHiddenToken(currentToken, parser)) {
        this.context.put(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY, currentContext);
        this.context.put(DocumentServiceKeys.TOKEN_STREAM_KEY, parser.getTokenStream());
        this.context.put(DocumentServiceKeys.VOCABULARY_KEY, parser.getVocabulary());
        this.context.put(DocumentServiceKeys.TOKEN_INDEX_KEY, parser.getCurrentToken().getTokenIndex());
    }/*from ww w .  jav a 2  s . co m*/
}

From source file:org.ballerinalang.langserver.completions.BallerinaCustomErrorStrategy.java

License:Open Source License

/**
 * Checks whether cursor is within the whitespace region between current token to last token.
 * @param token Token to be evaluated/*from   w  w w  .j  a  v a  2  s.c  o m*/
 * @param parser Parser Instance
 * @return true|false
 */
private boolean isCursorBetweenGivenTokenAndLastNonHiddenToken(Token token, Parser parser) {
    this.setContextException(parser);
    boolean isCursorBetween = false;
    int line = this.context.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine();
    int character = this.context.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter();

    Token lastNonHiddenToken = null;
    for (int tokenIdx = token.getTokenIndex() - 1; tokenIdx >= 0; tokenIdx--) {
        Token lastToken = parser.getTokenStream().get(tokenIdx);
        if (lastToken.getChannel() != Token.HIDDEN_CHANNEL) {
            lastNonHiddenToken = lastToken;
            break;
        }
    }
    if (lastNonHiddenToken != null) {

        // Convert the token lines and char positions to zero based indexing
        int lastNonHiddenTokenLine = lastNonHiddenToken.getLine() - 1;
        int lastNonHiddenTokenChar = lastNonHiddenToken.getCharPositionInLine();
        int tokenLine = token.getLine() - 1;
        int tokenChar = token.getCharPositionInLine();

        if (line >= lastNonHiddenTokenLine && line <= tokenLine) {
            if (line == lastNonHiddenTokenLine) {
                isCursorBetween = character >= (lastNonHiddenTokenChar + lastNonHiddenToken.getText().length());
            } else {
                isCursorBetween = line != tokenLine || character <= tokenChar;
            }
        }
    }
    return isCursorBetween;
}

From source file:org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy.java

License:Open Source License

@Override
public void reportInputMismatch(Parser parser, InputMismatchException e) {
    if (!parser.getContext().start.getTokenSource().getSourceName()
            .equals(context.get(DocumentServiceKeys.RELATIVE_FILE_PATH_KEY).replace("\\", "/"))) {
        return;//  www .ja v  a  2 s .c  o m
    }
    this.context.put(CompletionKeys.TOKEN_STREAM_KEY, parser.getTokenStream());
}

From source file:org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy.java

License:Open Source License

@Override
public void reportMissingToken(Parser parser) {
    if (!parser.getContext().start.getTokenSource().getSourceName()
            .equals(context.get(DocumentServiceKeys.RELATIVE_FILE_PATH_KEY).replace("\\", "/"))) {
        return;//from www. j av a 2 s.  c o  m
    }
    this.context.put(CompletionKeys.TOKEN_STREAM_KEY, parser.getTokenStream());
}

From source file:org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy.java

License:Open Source License

@Override
public void reportNoViableAlternative(Parser parser, NoViableAltException e) {
    if (!parser.getContext().start.getTokenSource().getSourceName()
            .equals(context.get(DocumentServiceKeys.RELATIVE_FILE_PATH_KEY).replace("\\", "/"))) {
        return;/*from ww  w. jav a2 s .co  m*/
    }
    this.context.put(CompletionKeys.TOKEN_STREAM_KEY, parser.getTokenStream());
}

From source file:org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy.java

License:Open Source License

@Override
public void reportUnwantedToken(Parser parser) {
    if (!parser.getContext().start.getTokenSource().getSourceName()
            .equals(context.get(DocumentServiceKeys.RELATIVE_FILE_PATH_KEY).replace("\\", "/"))) {
        return;/*from  w  w  w.j a  v  a  2  s .c o  m*/
    }
    this.context.put(CompletionKeys.TOKEN_STREAM_KEY, parser.getTokenStream());
}