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

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

Introduction

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

Prototype

int getStartIndex();

Source Link

Document

The starting character index of the token This method is optional; return -1 if not implemented.

Usage

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);//from   w w w .j  av  a2  s  . c  o 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;//ww w  .  j  a  va2  s . c  o m
    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()]);
}

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

License:Open Source License

private String computePrefixOfToken(Token tokenToComplete, int offset, ParseController pc) {
    String prefix = "";
    if (tokenToComplete.getType() == X10Parser.IDENTIFIER
            || /*tokenToComplete.getType() == X10Parser.ErrorId ||*/ pc.isKeyword(tokenToComplete.getType())) {
        if (offset >= tokenToComplete.getStartIndex() && offset <= tokenToComplete.getStopIndex() + 1) {
            prefix = tokenToComplete.getText().toString().substring(0,
                    offset - tokenToComplete.getStartIndex());
        }/* w ww .ja  va  2s.  c  om*/
    }
    return prefix;
}

From source file:x10dt.ui.parser.ParseController.java

License:Open Source License

private IToken getIToken(final Token token) {
    IToken ret = new IToken() {

        public int getAdjunctIndex() {
            // TODO Auto-generated method stub
            return 0;
        }/* w  w w  .  jav a  2 s  .c om*/

        public int getColumn() {
            return token.getCharPositionInLine();
        }

        public int getEndColumn() {
            return token.getCharPositionInLine() + token.getStopIndex() - token.getStartIndex();
        }

        public int getEndLine() {
            return token.getLine();
        }

        public int getEndOffset() {
            return token.getStopIndex();
        }

        public IToken[] getFollowingAdjuncts() {
            // TODO Auto-generated method stub
            return null;
        }

        public ILexStream getILexStream() {
            // TODO Auto-generated method stub
            return null;
        }

        public IPrsStream getIPrsStream() {
            // TODO Auto-generated method stub
            return null;
        }

        public int getKind() {
            return token.getType();
        }

        public ILexStream getLexStream() {
            // TODO Auto-generated method stub
            return null;
        }

        public int getLine() {
            return token.getLine();
        }

        public IToken[] getPrecedingAdjuncts() {
            // TODO Auto-generated method stub
            return null;
        }

        public IPrsStream getPrsStream() {
            // TODO Auto-generated method stub
            return null;
        }

        public int getStartOffset() {
            return token.getStartIndex();
        }

        public int getTokenIndex() {
            return token.getTokenIndex();
        }

        public String getValue(char[] arg0) {
            return token.getText();
        }

        public void setAdjunctIndex(int arg0) {
            // TODO Auto-generated method stub

        }

        public void setEndOffset(int arg0) {
        }

        public void setKind(int arg0) {
            // TODO Auto-generated method stub

        }

        public void setStartOffset(int arg0) {
            // TODO Auto-generated method stub

        }

        public void setTokenIndex(int arg0) {
            // TODO Auto-generated method stub

        }

    };
    return ret;
}