Example usage for org.antlr.v4.runtime Token getChannel

List of usage examples for org.antlr.v4.runtime Token getChannel

Introduction

In this page you can find the example usage for org.antlr.v4.runtime Token getChannel.

Prototype

int getChannel();

Source Link

Document

Return the channel this token.

Usage

From source file:org.ballerinalang.composer.service.workspace.langserver.util.completion.filters.PackageActionAndFunctionFilter.java

License:Open Source License

/**
 * Get the index of a certain token//from www  . j  av  a2 s . c o m
 * @param tokenString - token string
 * @param from - start searching from
 * @param dataModel - suggestions filter data model
 * @return {@link Integer}
 */
public int getIndexOfTokenString(String tokenString, int from, SuggestionsFilterDataModel dataModel) {
    TokenStream tokenStream = dataModel.getTokenStream();
    int resultTokenIndex = -1;
    int searchIndex = from;

    while (true) {
        if (searchIndex < 0 || tokenStream.size() - 1 < searchIndex) {
            break;
        }
        Token token = tokenStream.get(searchIndex);
        if (token.getChannel() != Token.DEFAULT_CHANNEL || !token.getText().equals(tokenString)) {
            searchIndex++;
        } else {
            resultTokenIndex = searchIndex;
            break;
        }
    }

    return resultTokenIndex;
}

From source file:org.ballerinalang.composer.service.workspace.langserver.util.completion.resolvers.AbstractItemResolver.java

License:Open Source License

/**
 * Get the index of the equal sign//  w w w.  j av a 2s .  c o  m
 * @param dataModel - suggestions filter data model
 * @return {@link Integer}
 */
public int isPreviousTokenEqualSign(SuggestionsFilterDataModel dataModel) {
    int equalSignIndex;
    int searchTokenIndex = dataModel.getTokenIndex() - 1;
    TokenStream tokenStream = dataModel.getTokenStream();

    while (true) {
        if (searchTokenIndex > -1) {
            Token token = tokenStream.get(searchTokenIndex);
            String tokenStr = token.getText();

            // If the token's channel is verbose channel we skip to the next token
            if (token.getChannel() != 0) {
                searchTokenIndex--;
            } else if (tokenStr.equals("=")) {
                equalSignIndex = searchTokenIndex;
                break;
            } else {
                // In this case the token channel is the default channel and also not the equal sign token.
                equalSignIndex = -1;
                break;
            }
        } else {
            equalSignIndex = -1;
            break;
        }
    }

    return equalSignIndex;
}

From source file:org.ballerinalang.composer.service.workspace.langserver.util.completion.resolvers.AbstractItemResolver.java

License:Open Source License

protected int findPreviousToken(SuggestionsFilterDataModel dataModel, String needle, int maxSteps) {
    TokenStream tokenStream = dataModel.getTokenStream();
    if (tokenStream == null) {
        return -1;
    }/*from ww w .  j ava  2 s .  c  om*/
    int searchIndex = dataModel.getTokenIndex() - 1;

    while (maxSteps > 0) {
        if (searchIndex < 0) {
            return -1;
        }
        Token token = tokenStream.get(searchIndex);
        if (token.getChannel() == 0) {
            if (token.getText().equals(needle)) {
                return searchIndex;
            }
            maxSteps--;
        }
        searchIndex--;
    }
    return -1;
}

From source file:org.ballerinalang.composer.service.workspace.langserver.util.filters.PackageActionAndFunctionFilter.java

License:Open Source License

/**
 * Get the index of a certain token//from  w  ww .  j  a  v  a2  s .c o m
 * @param tokenString - token string
 * @param from - start searching from
 * @param dataModel - suggestions filter data model
 * @return {@link Integer}
 */
private int getIndexOfTokenString(String tokenString, int from, SuggestionsFilterDataModel dataModel) {
    TokenStream tokenStream = dataModel.getTokenStream();
    int resultTokenIndex = -1;
    int searchIndex = from;

    while (true) {
        if (searchIndex < 0 || tokenStream.size() - 1 < searchIndex) {
            break;
        }
        Token token = tokenStream.get(searchIndex);
        if (token.getChannel() != Token.DEFAULT_CHANNEL || !token.getText().equals(tokenString)) {
            searchIndex++;
        } else {
            resultTokenIndex = searchIndex;
            break;
        }
    }

    return resultTokenIndex;
}

From source file:org.ballerinalang.composer.service.workspace.langserver.util.resolvers.parsercontext.ParserRuleExpressionVariableDefStatementContextResolver.java

License:Open Source License

/**
 * Get the index of the given token/* w w  w.j  av  a  2s.com*/
 * @param dataModel suggestions filter data model
 * @param tokenString token string to be found
 * @return {@link Integer}
 */
private int getTokenIndexOf(SuggestionsFilterDataModel dataModel, String tokenString) {
    TokenStream tokenStream = dataModel.getTokenStream();
    int currentTokenIndex = dataModel.getTokenIndex();
    int searchIndex = currentTokenIndex - 1;
    List<String> terminatingCharacters = Arrays.asList(";", "{", "}", "@");

    while (true) {
        if (tokenStream != null && searchIndex < tokenStream.size()) {
            Token token = tokenStream.get(searchIndex);
            String tokenStr = token.getText();

            if (token.getChannel() == Token.DEFAULT_CHANNEL && terminatingCharacters.contains(tokenStr)) {
                return -1;
            } else if (tokenStr.equals(tokenString)) {
                return searchIndex;
            } else {
                searchIndex--;
            }
        } else {
            return -1;
        }
    }
}

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  ww w .  j  a v a  2  s.  c  om
 * @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.langserver.common.utils.CommonUtil.java

License:Open Source License

private static Token getDefaultTokenToLeftOrRight(TokenStream tokenStream, int startIndex, int direction) {
    Token token = null;
    while (true) {
        startIndex += direction;/*  w w  w . j  a va  2  s  . c  om*/
        if (startIndex < 0 || startIndex == tokenStream.size()) {
            break;
        }
        token = tokenStream.get(startIndex);
        if (token.getChannel() == Token.DEFAULT_CHANNEL) {
            break;
        }
    }
    return token;
}

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  .  ja  v  a2  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.TreeVisitor.java

License:Open Source License

/**
 * Check whether the cursor resides within the given node type's parameter context.
 * Node name is used to identify the correct node
 * /*from w  w  w . j a  va 2 s  .c  o  m*/
 * @param nodeName              Name of the node
 * @param nodeType              Node type (Function, Resource, Action or Connector)
 * @return {@link Boolean}      Whether the cursor is within the parameter context
 */
private boolean isWithinParameterContext(String nodeName, String nodeType, SymbolEnv env) {
    ParserRuleContext parserRuleContext = lsContext.get(CompletionKeys.PARSER_RULE_CONTEXT_KEY);
    TokenStream tokenStream = lsContext.get(CompletionKeys.TOKEN_STREAM_KEY);
    String terminalToken = "";

    // If the parser rule context is not parameter context or parameter list context, we skipp the calculation
    if (!(parserRuleContext instanceof BallerinaParser.ParameterContext
            || parserRuleContext instanceof BallerinaParser.ParameterListContext)) {
        return false;
    }

    int startTokenIndex = parserRuleContext.getStart().getTokenIndex();
    ArrayList<String> terminalKeywords = new ArrayList<>(
            Arrays.asList(UtilSymbolKeys.ACTION_KEYWORD_KEY, UtilSymbolKeys.CONNECTOR_KEYWORD_KEY,
                    UtilSymbolKeys.FUNCTION_KEYWORD_KEY, UtilSymbolKeys.RESOURCE_KEYWORD_KEY));
    ArrayList<Token> filteredTokens = new ArrayList<>();
    Token openBracket = null;
    boolean isWithinParams = false;

    // Find the index of the closing bracket
    while (true) {
        if (startTokenIndex > tokenStream.size()) {
            // In the ideal case, should not reach this point
            startTokenIndex = -1;
            break;
        }
        Token token = tokenStream.get(startTokenIndex);
        String tokenString = token.getText();
        if (tokenString.equals(")")) {
            break;
        }
        startTokenIndex++;
    }

    // Backtrack the token stream to find a terminal token
    while (true) {
        if (startTokenIndex < 0) {
            break;
        }
        Token token = tokenStream.get(startTokenIndex);
        String tokenString = token.getText();
        if (terminalKeywords.contains(tokenString)) {
            terminalToken = tokenString;
            break;
        }
        if (token.getChannel() == Token.DEFAULT_CHANNEL) {
            filteredTokens.add(token);
        }
        startTokenIndex--;
    }

    Collections.reverse(filteredTokens);

    /*
    This particular logic identifies a matching pair of closing and opening bracket and then check whether the
    cursor is within those bracket pair
     */
    if (nodeName.equals(filteredTokens.get(0).getText()) && terminalToken.equals(nodeType)) {
        String tokenText;
        for (Token token : filteredTokens) {
            tokenText = token.getText();
            if (tokenText.equals("(")) {
                openBracket = token;
            } else if (tokenText.equals(")") && openBracket != null) {
                Position cursorPos = lsContext.get(DocumentServiceKeys.POSITION_KEY).getPosition();
                int openBLine = openBracket.getLine() - 1;
                int openBCol = openBracket.getCharPositionInLine();
                int closeBLine = token.getLine() - 1;
                int closeBCol = token.getCharPositionInLine();
                int cursorLine = cursorPos.getLine();
                int cursorCol = cursorPos.getCharacter();

                isWithinParams = (cursorLine > openBLine && cursorLine < closeBLine)
                        || (cursorLine == openBLine && cursorCol > openBCol && cursorLine < closeBLine)
                        || (cursorLine > openBLine && cursorCol < closeBCol && cursorLine == closeBLine)
                        || (cursorLine == openBLine && cursorLine == closeBLine && cursorCol >= openBCol
                                && cursorCol <= closeBCol);
                if (isWithinParams) {
                    break;
                } else {
                    openBracket = null;
                }
            }
        }
    }

    if (isWithinParams) {
        this.populateSymbols(this.resolveAllVisibleSymbols(env), env);
        forceTerminateVisitor();
    }

    return isWithinParams;
}

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

License:Open Source License

/**
 * Check whether the cursor resides within the given node type's parameter context.
 * Node name is used to identify the correct node
 *
 * @param nodeName              Name of the node
 * @param nodeType              Node type (Function, Resource, Action or Connector)
 * @param env                   Symbol Environment
 * @param lsContext             Language Server Operation Context
 * @param treeVisitor           Completion tree visitor instance
 * @return {@link Boolean}      Whether the cursor is within the parameter context
 *//*from ww  w . ja v  a  2  s  .c  om*/
public static boolean isWithinParameterContext(String nodeName, String nodeType, SymbolEnv env,
        LSContext lsContext, TreeVisitor treeVisitor) {
    ParserRuleContext parserRuleContext = lsContext.get(CompletionKeys.PARSER_RULE_CONTEXT_KEY);
    TokenStream tokenStream = lsContext.get(CompletionKeys.TOKEN_STREAM_KEY);
    String terminalToken = "";

    // If the parser rule context is not parameter context or parameter list context, we skipp the calculation
    if (!(parserRuleContext instanceof BallerinaParser.ParameterContext
            || parserRuleContext instanceof BallerinaParser.ParameterListContext)) {
        return false;
    }

    int startTokenIndex = parserRuleContext.getStart().getTokenIndex();
    ArrayList<String> terminalKeywords = new ArrayList<>(
            Arrays.asList(UtilSymbolKeys.ACTION_KEYWORD_KEY, UtilSymbolKeys.CONNECTOR_KEYWORD_KEY,
                    UtilSymbolKeys.FUNCTION_KEYWORD_KEY, UtilSymbolKeys.RESOURCE_KEYWORD_KEY));
    ArrayList<Token> filteredTokens = new ArrayList<>();
    Token openBracket = null;
    boolean isWithinParams = false;

    // Find the index of the closing bracket
    while (true) {
        if (startTokenIndex > tokenStream.size()) {
            // In the ideal case, should not reach this point
            startTokenIndex = -1;
            break;
        }
        Token token = tokenStream.get(startTokenIndex);
        String tokenString = token.getText();
        if (tokenString.equals(")")) {
            break;
        }
        startTokenIndex++;
    }

    // Backtrack the token stream to find a terminal token
    while (true) {
        if (startTokenIndex < 0) {
            break;
        }
        Token token = tokenStream.get(startTokenIndex);
        String tokenString = token.getText();
        if (terminalKeywords.contains(tokenString)) {
            terminalToken = tokenString;
            break;
        }
        if (token.getChannel() == Token.DEFAULT_CHANNEL) {
            filteredTokens.add(token);
        }
        startTokenIndex--;
    }

    Collections.reverse(filteredTokens);

    /*
    This particular logic identifies a matching pair of closing and opening bracket and then check whether the
    cursor is within those bracket pair
     */
    if (nodeName.equals(filteredTokens.get(0).getText()) && terminalToken.equals(nodeType)) {
        String tokenText;
        for (Token token : filteredTokens) {
            tokenText = token.getText();
            if (tokenText.equals("(")) {
                openBracket = token;
            } else if (tokenText.equals(")") && openBracket != null) {
                Position cursorPos = lsContext.get(DocumentServiceKeys.POSITION_KEY).getPosition();
                int openBLine = openBracket.getLine() - 1;
                int openBCol = openBracket.getCharPositionInLine();
                int closeBLine = token.getLine() - 1;
                int closeBCol = token.getCharPositionInLine();
                int cursorLine = cursorPos.getLine();
                int cursorCol = cursorPos.getCharacter();

                isWithinParams = (cursorLine > openBLine && cursorLine < closeBLine)
                        || (cursorLine == openBLine && cursorCol > openBCol && cursorLine < closeBLine)
                        || (cursorLine > openBLine && cursorCol < closeBCol && cursorLine == closeBLine)
                        || (cursorLine == openBLine && cursorLine == closeBLine && cursorCol >= openBCol
                                && cursorCol <= closeBCol);
                if (isWithinParams) {
                    break;
                } else {
                    openBracket = null;
                }
            }
        }
    }

    if (isWithinParams) {
        treeVisitor.populateSymbols(treeVisitor.resolveAllVisibleSymbols(env), env);
        treeVisitor.forceTerminateVisitor();
    }

    return isWithinParams;
}