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

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

Introduction

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

Prototype

String getText();

Source Link

Document

Get the text of the token.

Usage

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

License:Open Source License

private static List<SymbolInfo> filterEndpointContextSymbolInfo(TextDocumentServiceContext context) {
    List<SymbolInfo> symbolInfos = context.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
    int currentTokenIndex = context.get(DocumentServiceKeys.TOKEN_INDEX_KEY) - 1;
    TokenStream tokenStream = context.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
    Token packageAlias = CommonUtil.getPreviousDefaultToken(tokenStream, currentTokenIndex);
    Token constraintStart = CommonUtil.getPreviousDefaultToken(tokenStream, packageAlias.getTokenIndex() - 1);
    List<SymbolInfo> returnList = new ArrayList<>();

    if (!(constraintStart.getText().equals("<") || constraintStart.getText().equals("create"))) {
        PackageActionFunctionAndTypesFilter filter = new PackageActionFunctionAndTypesFilter();
        returnList.addAll(filter.filterItems(context));
    } else {// w  w  w.  j  a  v a2s.  c  om
        SymbolInfo packageSymbolInfo = symbolInfos.stream().filter(item -> {
            Scope.ScopeEntry scopeEntry = item.getScopeEntry();
            return item.getSymbolName().equals(packageAlias.getText())
                    && scopeEntry.symbol instanceof BPackageSymbol;
        }).findFirst().orElse(null);

        if (packageSymbolInfo != null) {
            packageSymbolInfo.getScopeEntry().symbol.scope.entries.forEach((name, value) -> {
                if (value.symbol.kind != null && value.symbol.kind.toString().equals(CONNECTOR_KIND)) {
                    returnList.add(new SymbolInfo(name.toString(), value));
                }
            });
        }
    }

    return returnList;
}

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 v  a2s. com*/
 * @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
 */// w  ww  . ja  v  a 2 s. 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

/**
 * Get the suggesting item list from the visible symbols. (As a rule these are packages such as http, etc).
 * @param evaluatorToken    Token which is used to determine this is a connector init
 * @param context           TextDocumentServiceCOntext
 * @return  {@link List}    List of filtered SymbolsInfos
 *///from   w  w w .  ja v  a 2 s.  c om
private List<SymbolInfo> getItemsList(Token evaluatorToken, TextDocumentServiceContext context) {
    List<SymbolInfo> filteredList = new ArrayList<>();
    if (evaluatorToken == null) {
        return filteredList;
    }
    String tokenValue = evaluatorToken.getText();
    if (tokenValue.equals(CREATE_KEYWORD)) {
        List<SymbolInfo> visibleSymbols = context.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
        filteredList.addAll(visibleSymbols.stream()
                .filter(symbolInfo -> symbolInfo.getScopeEntry().symbol.type instanceof BPackageType)
                .collect(Collectors.toList()));
    }

    return filteredList;
}

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  a  va 2s  .  c  o  m
    }
}

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. c  o m*/
    }
}

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

License:Open Source License

void alterTokenText(Token token) {
    this.removedTokens.add(new CommonToken(token));
    if (token.getType() == BallerinaParser.NEW_LINE || token.getChannel() != Token.DEFAULT_CHANNEL) {
        return;//from   ww  w.j  av a2  s. co  m
    }
    ((CommonToken) token).setText(getNCharLengthEmptyLine(token.getText().length()));
    this.lastAlteredToken = token.getType();
}

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 a  v a2s  .  c om*/
    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;
    }
}

From source file:org.ballerinalang.langserver.util.references.ReferenceFindTokenErrorStrategy.java

License:Open Source License

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

    if (!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(NodeContextKeys.NODE_NAME_KEY, currentToken.getText());
            this.terminateCheck = true;
        }/* www  .  j  a  v  a2s .  c o m*/
    }
}

From source file:org.beetl.core.AntlrProgramBuilder.java

License:BSD License

/** directive dynamic xxx,yy
 * @param node/*from w w  w . j a v  a2s.c  o m*/
 * @return
 */
protected DirectiveStatement parseDirectiveStatement(DirectiveStContext node) {
    DirectiveStContext stContext = (DirectiveStContext) node;
    DirectiveExpContext direExp = stContext.directiveExp();
    Token token = direExp.Identifier().getSymbol();
    String directive = token.getText().toLowerCase().intern();
    TerminalNode value = direExp.StringLiteral();
    List<TerminalNode> idNodeList = null;
    DirectiveExpIDListContext directExpidLisCtx = direExp.directiveExpIDList();
    if (directExpidLisCtx != null) {
        idNodeList = directExpidLisCtx.Identifier();
    }

    Set<String> idList = null;
    DirectiveStatement ds = null;

    if (value != null) {
        String idListValue = this.getStringValue(value.getText());
        idList = new HashSet(Arrays.asList(idListValue.split(",")));
        ds = new DirectiveStatement(directive, idList, this.getBTToken(token));

    } else if (idNodeList != null) {
        idList = new HashSet<String>();
        for (TerminalNode t : idNodeList) {
            idList.add(t.getText());
        }
        ds = new DirectiveStatement(directive, idList, this.getBTToken(token));

    } else {
        ds = new DirectiveStatement(directive, Collections.EMPTY_SET, this.getBTToken(token));
    }

    if (directive.equals("dynamic")) {

        if (ds.getIdList().size() == 0) {
            data.allDynamic = true;
        } else {
            data.dynamicObjectSet = ds.getIdList();
        }
        return null;

    } else if (directive.equalsIgnoreCase("safe_output_open".intern())) {
        this.pbCtx.isSafeOutput = true;
        return ds;
    } else if (directive.equalsIgnoreCase("safe_output_close".intern())) {
        this.pbCtx.isSafeOutput = false;
        return ds;
    } else {
        return ds;
    }
}