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

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

Introduction

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

Prototype

int getCharPositionInLine();

Source Link

Document

The index of the first character of this token relative to the beginning of the line at which it occurs, 0..n-1

Usage

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

License:Open Source License

private Token getLastTerminationToken(TokenStream tokenStream) {
    Token lastTerminationToken = null;//from  w  ww. ja  v a 2  s .c  o m
    Position cursorPosition = this.context.get(DocumentServiceKeys.POSITION_KEY).getPosition();
    int cursorLine = cursorPosition.getLine() + 1;
    int cursorCol = cursorPosition.getCharacter() + 1;

    int index = 1;
    Token beforeCursorToken = tokenStream.LT(index);
    int type = beforeCursorToken.getType();
    int tLine = beforeCursorToken.getLine();
    int tCol = beforeCursorToken.getCharPositionInLine();

    while (type != BallerinaParser.EOF && ((tLine < cursorLine) || (tLine == cursorLine && tCol < cursorCol))) {
        // Checking for terminating tokens with the type number is inconsistent. Thus, uses string comparisons
        String tokenText = beforeCursorToken.getText();
        if (UtilSymbolKeys.SEMI_COLON_SYMBOL_KEY.equals(tokenText)
                || UtilSymbolKeys.OPEN_BRACE_KEY.equals(tokenText)
                || UtilSymbolKeys.CLOSE_BRACE_KEY.equals(tokenText)
                || UtilSymbolKeys.COMMA_SYMBOL_KEY.equals(tokenText)) {
            lastTerminationToken = beforeCursorToken;
        }
        index++;
        beforeCursorToken = tokenStream.LT(index);
        type = beforeCursorToken.getType();
        tLine = beforeCursorToken.getLine();
        tCol = beforeCursorToken.getCharPositionInLine();
    }
    return lastTerminationToken;
}

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

License:Open Source License

private static boolean fillAnnotationAttachmentMetaInfo(LSContext ctx) {
    if (ctx.get(CompletionKeys.TOKEN_STREAM_KEY) == null) {
        return false;
    }//ww w .  ja  va2 s  .  co m
    TokenStream tokenStream = ctx.get(CompletionKeys.TOKEN_STREAM_KEY);
    Position cursorPosition = ctx.get(DocumentServiceKeys.POSITION_KEY).getPosition();
    Queue<String> fieldsQueue = new LinkedList<>();
    ArrayList<String> terminalTokens = new ArrayList<>(
            Arrays.asList(UtilSymbolKeys.FUNCTION_KEYWORD_KEY, UtilSymbolKeys.SERVICE_KEYWORD_KEY,
                    UtilSymbolKeys.ENDPOINT_KEYWORD_KEY, UtilSymbolKeys.OPEN_PARENTHESES_KEY));
    int currentTokenIndex = CommonUtil.getCurrentTokenFromTokenStream(ctx);
    int startIndex = currentTokenIndex;
    int line = cursorPosition.getLine();
    int col = cursorPosition.getCharacter();
    String annotationName;
    String pkgAlias = "";

    while (true) {
        if (startIndex > tokenStream.size()
                || CommonUtil.getPreviousDefaultToken(tokenStream, startIndex) == null) {
            return false;
        }
        Token token = CommonUtil.getPreviousDefaultToken(tokenStream, startIndex);
        if (token == null || terminalTokens.contains(token.getText())) {
            return false;
        }
        if (token.getText().equals(UtilSymbolKeys.ANNOTATION_START_SYMBOL_KEY)) {
            // Breaks when we meet the first annotation start before the given start index
            break;
        } else if (token.getText().equals(UtilSymbolKeys.OPEN_BRACE_KEY)) {
            startIndex = token.getTokenIndex();
            // For each annotation found, capture the package alias
            if (UtilSymbolKeys.PKG_DELIMITER_KEYWORD
                    .equals(CommonUtil.getNthDefaultTokensToLeft(tokenStream, startIndex, 2).getText())) {
                pkgAlias = CommonUtil.getNthDefaultTokensToLeft(tokenStream, startIndex, 3).getText();
            }
        } else {
            startIndex = token.getTokenIndex();
        }
    }

    String tempTokenString = "";
    while (true) {
        if (startIndex > tokenStream.size()) {
            return false;
        }
        Token token = CommonUtil.getNextDefaultToken(tokenStream, startIndex);
        int tokenLine = token.getLine() - 1;
        int tokenCol = token.getCharPositionInLine();
        if (terminalTokens.contains(token.getText()) || line < tokenLine
                || (line == tokenLine - 1 && col - 1 < tokenCol)) {
            break;
        } else if (UtilSymbolKeys.OPEN_BRACE_KEY.equals(token.getText())) {
            fieldsQueue.add(tempTokenString);
        } else if (UtilSymbolKeys.CLOSE_BRACE_KEY.equals(token.getText())) {
            fieldsQueue.remove();
        } else if (!UtilSymbolKeys.PKG_DELIMITER_KEYWORD.equals(token.getText())) {
            tempTokenString = token.getText();
        }
        startIndex = token.getTokenIndex();
    }

    annotationName = fieldsQueue.poll();
    ctx.put(CompletionKeys.ANNOTATION_ATTACHMENT_META_KEY, new AnnotationAttachmentMetaInfo(annotationName,
            fieldsQueue, pkgAlias, getNextNodeTypeFlag(tokenStream, currentTokenIndex, ctx)));
    ctx.put(CompletionKeys.SYMBOL_ENV_NODE_KEY, new BLangAnnotationAttachment());

    return true;
}

From source file:org.ballerinalang.langserver.completions.resolvers.parsercontext.ParserRuleCallableUnitBodyContextResolver.java

License:Open Source License

private static boolean isCursorBeforeToken(Position cursor, Token token) {
    int cursorLine = cursor.getLine();
    int cursorCol = cursor.getCharacter();
    int tokenLine = token.getLine() - 1;
    int tokenCol = token.getCharPositionInLine();

    return cursorLine < tokenLine || (cursorLine == tokenLine && cursorCol <= tokenCol);
}

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  ww  .  j av a2  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  . j  a v  a 2s . c  o  m*/
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;
}

From source file:org.ballerinalang.langserver.completions.util.filters.ConnectorInitExpressionItemFilter.java

License:Open Source License

public List<SymbolInfo> filterItems(TextDocumentServiceContext context) {
    TokenStream tokenStream = context.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
    int capturedTokenIndex = context.get(DocumentServiceKeys.TOKEN_INDEX_KEY);
    // Since the lang server is zero based indexing, we need to increase the line number to align with antlr token
    int cursorLine = context.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine() + 1;
    int cursorChar = context.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter();

    Token capturedToken = tokenStream.get(capturedTokenIndex);
    Token evaluatorToken = null;//from   w  w w  .  ja v a  2  s. c  om
    int loopIncrementer = 1;
    int tokenIterator = capturedTokenIndex;

    if (capturedToken.getLine() > cursorLine
            || (capturedToken.getLine() == cursorLine && capturedToken.getCharPositionInLine() >= cursorChar)) {
        loopIncrementer = -1;
    }

    tokenIterator += loopIncrementer;

    while (true) {
        if (tokenIterator < 0) {
            break;
        }
        Token tempToken = tokenStream.get(tokenIterator);
        if (tempToken.getLine() == cursorLine && tempToken.getCharPositionInLine() == cursorChar) {
            evaluatorToken = getFirstNonHiddenToken(tempToken.getTokenIndex(), -1, tokenStream);
            break;
        }
        tokenIterator += loopIncrementer;
    }

    if (evaluatorToken == null) {
        return new ArrayList<>();
    }

    return getItemsList(evaluatorToken, context);
}

From source file:org.ballerinalang.langserver.completions.util.sorters.CallableUnitBodyItemSorter.java

License:Open Source License

@Override
public void sortItems(TextDocumentServiceContext ctx, List<CompletionItem> completionItems) {
    BLangNode previousNode = ctx.get(CompletionKeys.PREVIOUS_NODE_KEY);
    TokenStream tokenStream = ctx.get(DocumentServiceKeys.TOKEN_STREAM_KEY);

    if (ctx.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY) != null) {
        int currentTokenStart = ctx.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY).getStart().getTokenIndex();
        Token nextToken = tokenStream.get(currentTokenStart + 1);
        int cursorLine = ctx.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine();
        int cursorChar = ctx.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter();

        if (nextToken.getChannel() != Token.DEFAULT_CHANNEL && (cursorLine > nextToken.getLine() - 1
                || (cursorLine == nextToken.getLine() - 1 && cursorChar > nextToken.getCharPositionInLine()))) {
            completionItems.clear();//  w  ww  .  j a  va 2  s  .  c o  m
            return;
        }
    }

    this.clearItemsIfWorkerExists(ctx, completionItems);
    if (previousNode == null) {
        this.populateWhenCursorBeforeOrAfterEp(completionItems);
    } else if (previousNode instanceof BLangVariableDef) {
        BType bLangType = ((BLangVariableDef) previousNode).var.type;
        if (bLangType instanceof BEndpointType) {
            this.populateWhenCursorBeforeOrAfterEp(completionItems);
        } else if (ctx.get(CompletionKeys.INVOCATION_STATEMENT_KEY) == null
                || !ctx.get(CompletionKeys.INVOCATION_STATEMENT_KEY)) {
            CompletionItem workerItem = this.getWorkerSnippet();
            workerItem.setSortText(Priority.PRIORITY160.toString());
            completionItems.add(workerItem);
        }
    } else if (previousNode instanceof BLangWorker) {
        completionItems.add(this.getWorkerSnippet());
    }
    this.setPriorities(completionItems);
}

From source file:org.ballerinalang.langserver.implementation.GotoImplementationCustomErrorStrategy.java

License:Open Source License

@Override
public void reportMatch(Parser recognizer) {
    super.reportMatch(recognizer);

    if (recognizer.getSourceName().equals(relativeSourceFilePath) && !terminateCheck) {
        Token currentToken = recognizer.getCurrentToken();
        // -1 added since the ANTLR line position is not zero based
        int tokenLine = currentToken.getLine() - 1;
        int tokenStartCol = currentToken.getCharPositionInLine();
        int tokenStopCol = tokenStartCol + currentToken.getText().length();

        if (this.line == tokenLine && this.col >= tokenStartCol && this.col <= tokenStopCol) {
            this.lsContext.put(GotoImplementationKeys.SYMBOL_TOKEN_KEY, currentToken.getText());
            this.terminateCheck = true;
        }/*from   w w w .j ava 2 s  .  c om*/
    }
}

From source file:org.ballerinalang.langserver.implementation.GotoImplementationCustomErrorStratergy.java

License:Open Source License

@Override
public void reportMatch(Parser recognizer) {
    super.reportMatch(recognizer);

    if (recognizer.getContext().start.getTokenSource().getSourceName().equals(
            lsContext.get(DocumentServiceKeys.RELATIVE_FILE_PATH_KEY).replace("\\", "/")) && !terminateCheck) {
        Token currentToken = recognizer.getCurrentToken();
        // -1 added since the ANTLR line position is not zero based
        int tokenLine = currentToken.getLine() - 1;
        int tokenStartCol = currentToken.getCharPositionInLine();
        int tokenStopCol = tokenStartCol + currentToken.getText().length();

        if (this.line == tokenLine && this.col >= tokenStartCol && this.col <= tokenStopCol) {
            this.lsContext.put(GotoImplementationKeys.SYMBOL_TOKEN_KEY, currentToken.getText());
            this.terminateCheck = true;
        }//from w ww.j  av a 2 s. co  m
    }
}

From source file:org.ballerinalang.langserver.sourceprune.SourcePruner.java

License:Open Source License

private static TokenPosition locateCursorAtToken(Token token, int cLine, int cCol) {
    int tokenLine = token.getLine() - 1;
    int tokenStartCol = token.getCharPositionInLine();
    int tokenEndCol = tokenStartCol
            + ((token.getText().equals("\r\n") || token.getText().equals("\n")) ? 0 : token.getText().length());

    /*/*from   w  w w  . j av  a  2 s.  com*/
    Token which is considered as the token at cursor is the token immediate before the cursor,
     where its end column is cursor column 
     */
    if (tokenLine == cLine && tokenStartCol < cCol && tokenEndCol >= cCol
            && token.getType() != BallerinaParser.NEW_LINE) {
        return TokenPosition.ON;
    } else if (cLine > tokenLine || (tokenLine == cLine && cCol > tokenEndCol)) {
        return TokenPosition.RIGHT;
    } else {
        return TokenPosition.LEFT;
    }
}