Example usage for org.antlr.v4.runtime BufferedTokenStream getHiddenTokensToRight

List of usage examples for org.antlr.v4.runtime BufferedTokenStream getHiddenTokensToRight

Introduction

In this page you can find the example usage for org.antlr.v4.runtime BufferedTokenStream getHiddenTokensToRight.

Prototype

public List<Token> getHiddenTokensToRight(int tokenIndex, int channel) 

Source Link

Document

Collect all tokens on specified channel to the right of the current token up until we see a token on DEFAULT_TOKEN_CHANNEL or EOF.

Usage

From source file:codesniffer.java8.adapter.AdapterUtil.java

License:Open Source License

/**
 * If there are no statements within a block, we need a special method to grab any comments that
 * might exist between braces.//www.  j av  a2  s. c om
 *
 * @param node
 * @param parserRuleContext
 * @param adapterParameters
 */
public static void setInternalComments(Node node, ParserRuleContext parserRuleContext,
        AdapterParameters adapterParameters) {
    BufferedTokenStream tokens = adapterParameters.getTokens();

    if (node == null || parserRuleContext == null || tokens == null) {
        throw new IllegalArgumentException("Parameters must not be null");
    }

    Token startToken = parserRuleContext.getStart();
    Token stopToken = parserRuleContext.getStop();

    List<Token> commentTokens;
    ArrayList<Comment> internalCommentList = new ArrayList<>();

    // Checking to the right of the start token will check inside the statement
    commentTokens = tokens.getHiddenTokensToRight(startToken.getTokenIndex(), Java8Lexer.COMMENTS);
    if (commentTokens != null) {
        internalCommentList.ensureCapacity(commentTokens.size());
        for (Token commentToken : commentTokens) {

            // Skip already claimed comments (prevents comment repeats)
            if (adapterParameters.isCommentTokenClaimed(commentToken.getTokenIndex())) {
                continue;
            } else {
                // Claim it
                adapterParameters.claimCommentToken(commentToken.getTokenIndex());
            }

            if (commentToken.getText().startsWith("/**")) {
                DocumentComment javadocComment = new DocumentComment(commentToken.getText());
                internalCommentList.add(javadocComment);
            } else if (commentToken.getText().startsWith("/*")) {
                BlockComment blockComment = new BlockComment(commentToken.getText());
                internalCommentList.add(blockComment);
            } else if (commentToken.getText().startsWith("//")) {
                LineComment lineComment = new LineComment(commentToken.getText());
                internalCommentList.add(lineComment);
            }
        }
    }
    if (internalCommentList.size() > 0) {
        if (node.getOrphanComments() != null) {
            node.getOrphanComments().addAll(internalCommentList);
        } else {
            node.setOrphanComments(internalCommentList);
        }
    }
    //        if (internalCommentList.size() > 0) {
    //            if (node.getInternalComments() != null) {
    //                node.getInternalComments().addAll(internalCommentList);
    //            } else {
    //                node.setInternalComments(internalCommentList);
    //            }
    //        }
}