Example usage for org.antlr.v4.runtime CommonTokenStream get

List of usage examples for org.antlr.v4.runtime CommonTokenStream get

Introduction

In this page you can find the example usage for org.antlr.v4.runtime CommonTokenStream get.

Prototype

@Override
    public Token get(int i) 

Source Link

Usage

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

License:Apache License

public static void printTokenStream(CommonTokenStream tokens) {

    tokens.fill();/* ww  w . j  a v  a2 s  . com*/
    for (int index = 0; index < tokens.size(); index++) {
        printToken(tokens, index, tokens.get(index));
        //         printToken(tokens, index, tokens.LA(index));
        //         System.out.println(tokens.LA(index));
        //         printToken(tokens, index, tokens.LT(index));
        //         printToken(tokens, index, tokens.LB(index));
    }
}

From source file:visao.view.tools.Sigla.java

public HashMap<String, String> reconheceSiglas(CommonTokenStream tokens) {

    CommonToken ob = null;/*from   www  . j a v  a  2s.  com*/
    for (int i = 0; i < tokens.size(); i++) {
        String descricaoSigla = null;
        ob = (CommonToken) tokens.get(i);
        if (ob.getText().matches(pattern)) {
            if ((tokens.get(i - 1).getText().equals("(")) && (tokens.get(i + 1).getText().equals(")"))) {
                descricaoSigla = verificaDescricaoAtras(i, tokens);
                if (descricaoSigla != null) {
                    siglas.put(ob.getText(), descricaoSigla);
                } else {
                    descricaoSigla = verificaDescricaoFrente(i, tokens);
                    if (descricaoSigla != null) {
                        siglas.put(ob.getText(), descricaoSigla);
                    }
                }
            } else {
                if ((tokens.get(i - 1).getType() == SrsGrammarLexer.TRACO)) {
                    descricaoSigla = verificaDescricaoAtras(i, tokens);
                    if (descricaoSigla != null) {
                        siglas.put(ob.getText(), descricaoSigla);
                    }
                }
                if (tokens.get(i + 1).getType() == SrsGrammarLexer.TRACO) {
                    descricaoSigla = verificaDescricaoFrente(i, tokens);
                    if (descricaoSigla != null) {
                        siglas.put(ob.getText(), descricaoSigla);
                    }
                }
            }
        }
    }
    return siglas;

}

From source file:visao.view.tools.Sigla.java

private String verificaDescricaoAtras(int i, CommonTokenStream tokens) {
    String token = tokens.get(i).getText();
    int numeroDePreposisoes = 0;
    int numeroDePalavrasAnteriores = i - 2;
    String descricaoSigla = "";
    String tokenAnterior = "";
    while ((i - numeroDePalavrasAnteriores) - numeroDePreposisoes <= token.length() + 1) {
        tokenAnterior = tokens.get(numeroDePalavrasAnteriores).getText();
        if (palavrasDescartaveis.contains(tokenAnterior.toLowerCase())) {
            descricaoSigla = tokenAnterior + " " + descricaoSigla;
            numeroDePreposisoes++;/* ww w . jav a  2  s.  com*/
            numeroDePalavrasAnteriores--;
            tokenAnterior = tokens.get(numeroDePalavrasAnteriores).getText();

        }
        while (tokenAnterior.length() <= 2) {
            if (tokenAnterior.length() <= 2) {
                descricaoSigla = tokenAnterior + " " + descricaoSigla;
                numeroDePreposisoes++;
            }
            numeroDePalavrasAnteriores--;
            tokenAnterior = tokens.get(numeroDePalavrasAnteriores).getText();
        }
        int indiceSigla = token.length() - (i - numeroDePalavrasAnteriores) + numeroDePreposisoes + 1;
        String charSigla = token.charAt(indiceSigla) + "";
        String charTokenAnterior = tokenAnterior.charAt(0) + "";
        if (charSigla.equals(charTokenAnterior.toUpperCase())) {
            descricaoSigla = tokenAnterior + " " + descricaoSigla;
            numeroDePalavrasAnteriores--;
        } else {
            return null;
        }
    }
    return descricaoSigla.substring(0, descricaoSigla.length() - 1);
}

From source file:visao.view.tools.Sigla.java

private String verificaDescricaoFrente(int i, CommonTokenStream tokens) {
    String sigla = tokens.get(i).getText();
    int numeroDePreposisoes = 0;
    int numeroDePalavrasPosteriores = i + 2;
    String descricaoSigla = "";
    String tokenAnterior = "";
    int indiceSigla = (numeroDePalavrasPosteriores - numeroDePreposisoes) - (i + 2);
    while (indiceSigla < sigla.length() - 1) {
        tokenAnterior = tokens.get(numeroDePalavrasPosteriores).getText();
        if (palavrasDescartaveis.contains(tokenAnterior.toLowerCase())) {
            descricaoSigla = descricaoSigla + " " + tokenAnterior;
            numeroDePreposisoes++;//from  w  ww.j ava 2s .c  om
            numeroDePalavrasPosteriores++;
            tokenAnterior = tokens.get(numeroDePalavrasPosteriores).getText();
        }
        while (tokenAnterior.length() <= 2) {
            if (tokenAnterior.length() <= 2) {
                descricaoSigla = descricaoSigla + " " + tokenAnterior;
                numeroDePreposisoes++;
            }
            numeroDePalavrasPosteriores++;
            tokenAnterior = tokens.get(numeroDePalavrasPosteriores).getText();
        }
        indiceSigla = (numeroDePalavrasPosteriores - numeroDePreposisoes) - (i + 2);
        if (indiceSigla >= sigla.length()) {
            return null;
        }
        String charSigla = sigla.charAt(indiceSigla) + "";
        String charTokenAnterior = tokenAnterior.charAt(0) + "";
        if (charSigla.equals(charTokenAnterior.toUpperCase())) {
            descricaoSigla = descricaoSigla + " " + tokenAnterior;
            numeroDePalavrasPosteriores++;
        } else {
            return null;
        }
    }
    return descricaoSigla.substring(1, descricaoSigla.length());
}

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   w w  w. jav  a 2s.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()]);
}