Example usage for org.antlr.v4.runtime TokenStream getText

List of usage examples for org.antlr.v4.runtime TokenStream getText

Introduction

In this page you can find the example usage for org.antlr.v4.runtime TokenStream getText.

Prototype

public String getText();

Source Link

Document

Return the text of all tokens in the stream.

Usage

From source file:io.mindmaps.graql.internal.parser.QueryParser.java

License:Open Source License

/**
 * Parse any part of a Graql query//  ww  w.  j  a v  a  2s . com
 * @param parseRule a method on GraqlParser that yields the parse rule you want to use (e.g. GraqlParser::variable)
 * @param visit a method on QueryVisitor that visits the parse rule you specified (e.g. QueryVisitor::visitVariable)
 * @param tokens the token stream to read
 * @param <T> The type the query is expected to parse to
 * @param <S> The type of the parse rule being used
 * @return the parsed result
 */
private <T, S extends ParseTree> T parseQueryFragment(Function<GraqlParser, S> parseRule,
        BiFunction<QueryVisitor, S, T> visit, TokenStream tokens) {
    GraqlErrorListener errorListener = new GraqlErrorListener(tokens.getText());
    return parseQueryFragment(parseRule, visit, errorListener, tokens);
}

From source file:org.ballerinalang.langserver.completions.util.CompletionUtil.java

License:Open Source License

public static void getPrunedSource(LSContext context) throws WorkspaceDocumentException, SourcePruneException {
    WorkspaceDocumentManager documentManager = context.get(CompletionKeys.DOC_MANAGER_KEY);
    String uri = context.get(DocumentServiceKeys.FILE_URI_KEY);
    Position position = context.get(DocumentServiceKeys.POSITION_KEY).getPosition();
    Path path = Paths.get(URI.create(uri));
    String documentContent = documentManager.getFileContent(path);
    BallerinaParser parser = CommonUtil.prepareParser(documentContent, true);
    parser.removeErrorListeners();//from  w  w w  .  j a v  a2  s.com
    parser.compilationUnit();
    if (parser.getNumberOfSyntaxErrors() == 0) {
        return;
    }
    TokenStream tokenStream = parser.getTokenStream();
    List<Token> tokenList = new ArrayList<>(((CommonTokenStream) tokenStream).getTokens());

    Optional<Token> tokenAtCursor = searchTokenAtCursor(tokenList, position.getLine(), position.getCharacter());

    if (!tokenAtCursor.isPresent()) {
        throw new SourcePruneException("Could not find token at cursor");
    }

    SourcePruner.pruneSource(tokenStream, tokenAtCursor.get().getTokenIndex(), context);
    documentManager.setPrunedContent(path, tokenStream.getText());
}