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

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

Introduction

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

Prototype

@Override
    public int size() 

Source Link

Usage

From source file:edu.odu.cs.cs350.yellow1.mutationgeneration.CFile.java

License:Open Source License

private boolean applyTokenTransformation(String original, String by, String operation) throws Exception {
    ANTLRInputStream input = new ANTLRInputStream(fileContents.toString());
    CLexer lexer = new CLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    CParser parser = new CParser(tokens);
    ParseTree tree = parser.compilationUnit();
    ParseTreeWalker walker = new ParseTreeWalker(); // create standard

    for (Integer i = 0; i < tokens.size(); i++) {
        System.out.println("Tokens: " + tokens.get(i).getText().toString());
        if (tokens.get(i).getText().toString().equals(original)) {
            String s1 = ((Integer) tokens.get(i).getStartIndex()).toString();
            String s2 = ((Integer) tokens.get(i).getStopIndex()).toString();
            String uniqId = operation;
            uniqId = uniqId.concat(s1).concat(s2);

            String newName = fileContents.toString();
            if (mutationsAppliedContains(uniqId) < 0) {

                newName = newName.substring(0, tokens.get(i).getStartIndex()) + by
                        + newName.substring(tokens.get(i).getStopIndex() + by.length());
                writeMutation(this.fileName + "." + uniqId, newName);
                this.addToApplied(uniqId);
            }//  w w w. j  a v  a  2  s. co  m
        }
    }
    return true;
}

From source file:edu.odu.cs.cs350.yellow1.mutationgeneration.JavaFile.java

License:Open Source License

/**
 * change tokens and add the mutuant cases to the mutant case vector
 * // w  w w  . j  a  v a2 s.c o  m
 * @param originalthe
 *            original token
 * 
 * @param by
 *            the replacement token
 * 
 * @param operation
 *            the operation name
 * 
 * 
 *            it is acceptable int y = +10; to be converted to int y = /10;
 *            because it will make the mutant project compilation fail and
 *            this mutation will be discarded because the compilation has
 *            failed
 * 
 * 
 */
private boolean applyTokenTransformation(String original, String by, String operation) throws Exception {
    ANTLRInputStream input = new ANTLRInputStream(fileContents.toString());
    JavaLexer lexer = new JavaLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);

    JavaParser parser = new JavaParser(tokens);

    ParseTree tree = parser.compilationUnit();

    TokenRewriteStream trs = new TokenRewriteStream();

    ParseTreeWalker walker = new ParseTreeWalker(); // create standard

    for (Integer i = 0; i < tokens.size(); i++) {
        if (tokens.get(i).getText().toString().equals(original)) {
            int startIndex = tokens.get(i).getStartIndex();
            int stopIndex = tokens.get(i).getStopIndex() + 1;
            mvc.add(startIndex, stopIndex, by);

        }
    }
    return true;
}

From source file:edu.odu.cs.cs350.yellow1.mutationgeneration.JavaFile.java

License:Open Source License

/**
 * @param blockstart/* w w w.j  av a  2  s .  c  o  m*/
 *            accepts if or while
 * 
 * @param set
 *            accepts true or false
 * 
 *            Replaces the condition after [blockstart] by [set]
 */
private void applyConditionTransformation(String blockstart, String set) {
    int lparen = 0;
    int rparen = 0;
    int startIndex = -1;
    int stopIndex = -1;
    boolean ifLock = false;

    ANTLRInputStream input = new ANTLRInputStream(fileContents.toString());
    JavaLexer lexer = new JavaLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JavaParser parser = new JavaParser(tokens);
    ParseTree tree = parser.compilationUnit();
    ParseTreeWalker walker = new ParseTreeWalker(); // create standard
    for (Integer i = 0; i < tokens.size(); i++) {

        if (tokens.get(i).getText().toString().equals(blockstart)) {
            ifLock = true;
        }

        if (ifLock == true && tokens.get(i).getText().toString().equals("(")) {
            lparen++;

            if (startIndex == -1)
                startIndex = tokens.get(i).getStartIndex();
        }

        if (ifLock == true && tokens.get(i).getText().toString().equals(")"))

        {

            rparen++;

        }

        if (lparen != 0 && rparen != 0 && lparen == rparen && ifLock == true) {
            ifLock = false;
            lparen = 0;
            rparen = 0;

            stopIndex = tokens.get(i).getStopIndex() + 1;

            String by = "(" + set + ")";

            this.mvc.add(startIndex, stopIndex, by);
            startIndex = -1;
            stopIndex = -1;
        }

    }

}

From source file:edu.odu.cs.cs350.yellow1.mutationgeneration.JavaFile.java

License:Open Source License

private void constantOperations(String required) {
    ANTLRInputStream input = new ANTLRInputStream(fileContents.toString());
    JavaLexer lexer = new JavaLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);

    JavaParser parser = new JavaParser(tokens);

    ParseTree tree = parser.compilationUnit();

    TokenRewriteStream trs = new TokenRewriteStream();

    ParseTreeWalker walker = new ParseTreeWalker(); // create standard

    for (Integer i = 0; i < tokens.size(); i++) {
        if (NumberUtils.isNumber(tokens.get(i).getText().toString())) {
            int startIndex = tokens.get(i).getStartIndex();
            int stopIndex = tokens.get(i).getStopIndex() + 1;
            String by = fileContents.substring(startIndex, stopIndex) + required;
            mvc.add(startIndex, stopIndex, by);
        }//from   ww w  .  j a  v a  2  s  .c o  m
    }
}

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

License:Apache License

public static void printTokenStream(CommonTokenStream tokens) {

    tokens.fill();/* w  w w. j a  v  a2 s  .c  om*/
    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;/*  w  w  w  . j  a  v  a2s.  co m*/
    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;

}