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.wso2.ballerinalang.compiler.parser.BLangWSPreservingParserListener.java

License:Open Source License

private void addWSFromRange(Stack<Whitespace> ws, TokenRange range) {
    int rangeStart = range.from;
    int rangeEnd = range.to;
    boolean lastTokenWasHidden = true;

    Token previousNonWS = null;/* www . j  a va2  s.c om*/
    for (int j = rangeEnd - 1; j >= -1; j--) {
        if (j == -1) {
            if (!lastTokenWasHidden) {
                // capturing (non-exiting) WS at the start of range, if the range starts at 0.
                // this happens if the file starts with a non-ws token.
                pushWS(ws, previousNonWS, "");
            }
            break;
        }

        Token token = this.tokenStream.get(j);
        if (previousNonWS == null && token.getChannel() == Token.HIDDEN_CHANNEL) {
            continue;
        }
        if (token.getChannel() == Token.DEFAULT_CHANNEL) {
            // we need to capture WS before the start of a range,
            // therefor only break after previous range's first non-WS.
            if (j < rangeStart) {
                if (!lastTokenWasHidden) {
                    pushWS(ws, previousNonWS, "");
                    // capturing (non-exiting) WS at the start of range (when there is no space between ranges).
                }
                break;
            }
            // capturing (non-exiting) WS between two default tokens.
            if (!lastTokenWasHidden) {
                pushWS(ws, previousNonWS, "");
            }
            lastTokenWasHidden = false;
            previousNonWS = token;
        } else {
            if (lastTokenWasHidden) {
                // merging adjacent WS tokens.
                ws.peek().prependWS(token.getText());
            } else {
                // capturing (non-zero-len) WS.
                pushWS(ws, previousNonWS, token.getText());
            }
            lastTokenWasHidden = true;
        }
    }
}

From source file:us.ihmc.idl.generator.IDLGenerator.java

License:Apache License

private static void printToken(CommonTokenStream tokens, int index, Token token) {
    if (token.getType() != IDLParser.WS) {
        String out = "";
        out += " Index: " + token.getTokenIndex();
        out += " Start: " + token.getStartIndex();
        out += " Stop: " + token.getStopIndex();
        out += " Channel: " + token.getChannel();
        out += " Type: " + token.getType();
        //         out += " Hidden: ";
        //         List<Token> hiddenTokensToLeft = tokens.getHiddenTokensToLeft(index);
        //         for (int i = 0; hiddenTokensToLeft != null && i < hiddenTokensToLeft.size(); i++)
        //         {
        //            if (hiddenTokensToLeft.get(i).getType() != IDLParser.WS)
        //            {
        //               out += "\n\t" + i + ":";
        //               out += "\n\tChannel: " + hiddenTokensToLeft.get(i).getChannel() + "  Type: " + hiddenTokensToLeft.get(i).getType();
        //               out += hiddenTokensToLeft.get(i).getText().replaceAll("\\s", "");
        //            }
        //         }
        out += " " + token.getText().replaceAll("\\s", "");
        System.out.println(out);// ww w .  jav a2s  .co m
    }
}

From source file:x10dt.ui.contentProposer.X10ContentProposer.java

License:Open Source License

public ICompletionProposal[] getContentProposals(IParseController controller, int offset, ITextViewer viewer) {

    ArrayList<ICompletionProposal> list = new ArrayList<ICompletionProposal>();

    CommonTokenStream tokens = ((ParseController) controller).getTokens();

    Token tokenToComplete = null;/*from ww  w . java 2  s .  c om*/
    Token previousToken = null;
    Token nextToken = null;

    int index = 0;
    for (Token t : tokens.getTokens()) {
        index++;
        if (t.getChannel() == Token.DEFAULT_CHANNEL) {
            if (t.getStartIndex() <= offset && t.getStopIndex() + 1 >= offset) {
                tokenToComplete = t;
                break;
            }
            if (t.getStartIndex() > offset) {
                break;
            }
            previousToken = t;
        }
    }

    if (tokenToComplete == null) {
        nextToken = tokens.getTokens().get(index);
    }

    String prefix = tokenToComplete == null ? ""
            : computePrefixOfToken(tokenToComplete, offset, (ParseController) controller);

    PolyglotNodeLocator locator = new PolyglotNodeLocator(
            controller.getProject()/*,((ParseController) controller).getLexStream()*/);
    Node currentAst = (Node) controller.getCurrentAst();
    Node node = tokenToComplete != null
            ? (Node) locator.findNode(currentAst, tokenToComplete.getStartIndex(),
                    tokenToComplete.getStopIndex())
            : null;
    Node previousNode = (previousToken != null)
            ? (Node) locator.findNode(currentAst, previousToken.getStartIndex(), previousToken.getStopIndex())
            : null;
    Node nextNode = (nextToken != null)
            ? (Node) locator.findNode(currentAst, nextToken.getStartIndex(), nextToken.getStopIndex())
            : null;

    if (node != null && node instanceof Eval && tokenToComplete.getType() == X10Parser.DOT) {
        Type type = ((Eval_c) node).expr().type();
        if (type != null && type.isReference()) {
            getCandidates((ObjectType) type, list, prefix, offset, true);
        }
    } else if (node != null && node instanceof Id && previousNode instanceof Field) {
        Type type = ((Field_c) previousNode).target().type();
        if (type != null && type.isReference()) {
            getCandidates((ObjectType) type, list, prefix, offset, true);
        }
    } else if (node != null && node instanceof Id && previousNode instanceof Call) {
        Type type = ((Call_c) previousNode).target().type();
        if (type != null && type.isReference()) {
            getCandidates((ObjectType) type, list, prefix, offset, true);
        }

        //The next case completes an Id with names in scope  
    } else if (node != null && node instanceof Id) {
        Node n = (node instanceof Id) ? node : previousNode;
        String pref = (node instanceof Id) ? prefix
                : computePrefixOfToken(previousToken, offset, (ParseController) controller);
        addNamesInScope(currentAst, n, pref, list, offset, !EMPTY_PREFIX_MATCHES);

    } else if (node == null && previousNode != null) { //Display templates, names in scope -- index < 0 when we are at a white space or comment
        Node location = location(previousNode, nextNode, locator, currentAst);
        if (location instanceof Block && (justAfter(X10Parser.SEMICOLON, previousToken)
                || justAfter(X10Parser.RBRACE, previousToken) || justAfter(X10Parser.LBRACE, previousToken))) { //Statement context. 
            addTemplateProposals(offset, viewer, list, prefix, fTemplates);
            //addNamesInScope(currentAst, node, prefix, list, offset, EMPTY_PREFIX_MATCHES);
        } else if (justAfter(X10Parser.EQUAL, previousToken)
                && (location instanceof Assign || location instanceof LocalDecl)) {
            Template[] templates = new Template[] { fAtExpressionTemplate, fCoercionTemplate, fRegion1DTemplate,
                    fRegion2DTemplate };
            addTemplateProposals(offset, viewer, list, prefix, templates);
        } else if (location instanceof ClassBody) { //Class context
            Template[] templates = new Template[] { fVariableDeclaration, fValueDeclaration, fConstDeclaration,
                    fPropertyDeclaration, fMainMethod, fMethodTemplate, fConstructorTemplate, fClassTemplate,
                    fStructTemplate, fDependentTypeDeclaration, };
            addTemplateProposals(offset, viewer, list, prefix, templates);

        }
    }

    return (ICompletionProposal[]) list.toArray(new ICompletionProposal[list.size()]);
}