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.langserver.completions.util.filters.PackageActionFunctionAndTypesFilter.java

License:Open Source License

/**
 * Check whether the statement being writing is a connector init by analyzing the tokens.
 * @param startIndex    Search start index
 * @param context       Document service context
 * @return {@link Boolean} connector init or not
 *///from   w ww  .  j  av  a 2s  .  c  o  m
private boolean isConnectorInit(int startIndex, TextDocumentServiceContext context) {
    int nonHiddenTokenCount = 0;
    int counter = startIndex - 1;
    TokenStream tokenStream = context.get(DocumentServiceKeys.TOKEN_STREAM_KEY);

    while (true) {
        Token token = tokenStream.get(counter);
        if (nonHiddenTokenCount == 2 && tokenStream.get(counter + 1).getText().equals(CREATE_KEYWORD)) {
            return true;
        } else if (nonHiddenTokenCount == 2) {
            break;
        }

        if (token.getChannel() == Token.DEFAULT_CHANNEL) {
            nonHiddenTokenCount++;
        }
        counter--;
    }

    return false;
}

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();//from  w ww .  j a v  a  2s . 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.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   w  ww .  j a  va2 s  .com
    }
    ((CommonToken) token).setText(getNCharLengthEmptyLine(token.getText().length()));
    this.lastAlteredToken = token.getType();
}

From source file:org.eclipse.titan.common.parsers.ParserLogger.java

License:Open Source License

/**
 * Token info in string format for logging purpose
 * @param aToken token/* ww w  .  j  a v  a  2 s.  com*/
 * @param aTokenNameResolver resolver to get token name
 * @return &lt;token name&gt;: '&lt;token text&gt;', @&lt;token index&gt;, &lt;line&gt;:&lt;column&gt;[, channel=&lt;channel&gt;]
 *         <br>where
 *         <br>&lt;token index&gt; starts  from 0,
 *         <br>&lt;line&gt; starts from 1,
 *         <br>&lt;column&gt; starts from 0,
 *         <br>channel info is provided if &lt;channel&gt; > 0 (hidden channel)
 */
private static String getTokenInfo(final Token aToken, final TokenNameResolver aTokenNameResolver) {
    final StringBuilder sb = new StringBuilder();
    final int tokenType = aToken.getType();
    final String tokenName = getTokenName(tokenType, aTokenNameResolver);
    sb.append(tokenName);
    sb.append(": ");

    sb.append("'");
    sb.append(getEscapedTokenText(aToken));
    sb.append("'");

    sb.append(", @" + aToken.getTokenIndex());
    sb.append(", " + aToken.getLine() + ":" + aToken.getCharPositionInLine());
    if (aToken.getChannel() > 0) {
        sb.append(", channel=");
        sb.append(aToken.getChannel());
    }
    return sb.toString();
}

From source file:org.ledyba.sora.parser.FortranTokenStream.java

License:Open Source License

/**
 * Create a subset list of the non-whitespace tokens in the current line.
 *//*  w w  w  .  j  av  a 2  s.c o  m*/
private ArrayList<Token> createPackedList() {
    int i = 0;
    Token tk = null;

    ArrayList<Token> pList = new ArrayList<>(this.lineLength + 1);

    for (i = 0; i < currLine.size(); i++) {
        tk = getTokenFromCurrLine(i);
        try {
            if (tk.getChannel() != Token.HIDDEN_CHANNEL) {
                pList.add(tk);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    // need to make sure the line was terminated with a T_EOS.  this may
    // not happen if we're working on a file that ended w/o a newline
    Token last = pList.get(pList.size() - 1);
    if (last.getType() != FortranLexer.T_EOS) {
        Pair<TokenSource, CharStream> src = new Pair<>(last.getTokenSource(), last.getInputStream());
        FortranToken eos = new FortranToken(src, FortranLexer.T_EOS, Token.DEFAULT_CHANNEL,
                last.getTokenIndex(), last.getTokenIndex() + 1);
        eos.setText("\n");
        packedList.add(eos);
    }

    return pList;
}

From source file:org.ledyba.sora.parser.FortranTokenStream.java

License:Open Source License

public int getLineLength(int start) {
    int lineLength;
    Token token;

    lineLength = 0;/*from w ww  . ja  v  a  2 s  .  co m*/
    if (start >= super.tokens.size())
        return lineLength;

    // this will not give you a lexer.EOF, so may need to
    // add a T_EOS token when creating the packed list if the file
    // ended w/o a T_EOS (now new line at end of the file).
    do {
        token = super.get(start + lineLength);
        lineLength++;
    } while ((start + lineLength) < super.tokens.size() && (token.getChannel() == Token.HIDDEN_CHANNEL
            || token.getType() != FortranLexer.T_EOS && token.getType() != FortranLexer.EOF));

    return lineLength;
}

From source file:org.ledyba.sora.parser.FortranTokenStream.java

License:Open Source License

/**
 * This will use the super classes methods to keep track of the
 * start and end of the original line, not the line buffered by
 * this class./* ww  w . j  a  va2  s.  c  o m*/
 */
public int findTokenInSuper(int lineStart, int desiredToken) {
    int lookAhead = 0;
    int tk, channel;

    /*****OBSOLETE NOTE: returning -1 is painful when looking for T_EOS
     // if this line is a comment, skip scanning it
     if (super.LA(1) == FortranLexer.LINE_COMMENT) {
     return -1;
     }
     OBSOLETE*****/

    do {
        // lookAhead was initialized to 0
        lookAhead++;

        // get the token
        Token token = LT(lookAhead);
        tk = token.getType();
        channel = token.getChannel();

        // continue until find what looking for or reach end
    } while ((tk != FortranLexer.EOF && tk != FortranLexer.T_EOS && tk != desiredToken)
            || channel == Token.HIDDEN_CHANNEL);

    if (tk == desiredToken) {
        // we found a what we wanted to
        return lookAhead;
    }

    return -1;
}

From source file:org.sourcepit.ltk.parser.ParseTreeBuilder.java

License:Apache License

private ParseNode handleTerminalNode(Terminal origin, Rule parent, TerminalNode terminalNode) {
    final ParseResult parseResult = parseResultStack.peek();
    final Class<? extends Lexer> sourceType = parseResult.getLexer().getClass();
    final org.antlr.v4.runtime.Token antlrToken = terminalNode.getSymbol();

    final int tokenType = antlrToken.getType();
    final int offset = offsetStack.peek().intValue() + antlrToken.getStartIndex();
    final int channel = antlrToken.getChannel();
    final String text = antlrToken.getText();
    final TokenType type = new TokenType(sourceType, tokenType);
    final Token token = new Token(type, channel, offset, text);

    final Terminal previous = terminals.isEmpty() ? null : terminals.getLast();
    final Terminal terminal = new Terminal(previous, parent, token, origin);

    final ParseResult nestedParseResult = parserDelegeate.parseNestedLanguage(sourceType, antlrToken);
    if (nestedParseResult == null) {
        terminals.add(terminal);//  w w w  .  j  a  v a  2s . c o m
        return terminal;
    } else {
        offsetStack.push(Integer.valueOf(antlrToken.getStartIndex()));
        try {
            return handleParseResult(terminal, parent, nestedParseResult);
        } finally {
            offsetStack.pop();
        }
    }
}

From source file:org.tvl.goworks.editor.go.completion.GoCompletionProvider.java

License:Open Source License

static boolean isGoContext(Token token, int offset, boolean allowInStrings) {
    if (token == null) {
        return false;
    }/* w  ww  . ja v a2  s.  co m*/

    switch (token.getType()) {
    case GoLexer.COMMENT:
        return false;

    case GoLexer.CharLiteral:
    case GoLexer.StringLiteral:
        return allowInStrings;

    case GoLexer.WS:
    case GoLexer.NEWLINE:
        return true;

    default:
        return token.getChannel() == Lexer.DEFAULT_TOKEN_CHANNEL;
    }
}

From source file:org.tvl.goworks.editor.go.parser.GoLexer.java

License:Open Source License

@Override
public Token nextToken() {
    Token result;
    if (deferredEol != null) {
        result = deferredEol;//from  www  . ja  v  a2 s  .  co m
        deferredEol = null;
    } else {
        result = super.nextToken();
    }

    switch (result.getType()) {
    case IDENTIFIER:
    case INT_LITERAL:
    case FLOAT_LITERAL:
    case IMAGINARY_LITERAL:
    case CharLiteral:
    case StringLiteral:
    case Break:
    case Continue:
    case Fallthrough:
    case Return:
    case Inc:
    case Dec:
    case RightParen:
    case RightBrack:
    case RightBrace:
        insertSemicolonAtEol = true;
        break;

    default:
        if (result.getChannel() == Token.DEFAULT_CHANNEL) {
            insertSemicolonAtEol = false;
        }
        break;
    }

    return result;
}