Example usage for org.antlr.v4.runtime.tree ParseTree getChildCount

List of usage examples for org.antlr.v4.runtime.tree ParseTree getChildCount

Introduction

In this page you can find the example usage for org.antlr.v4.runtime.tree ParseTree getChildCount.

Prototype

int getChildCount();

Source Link

Document

How many children are there?

Usage

From source file:AST.java

License:Open Source License

private static boolean containsTokens(ParseTree node) {
    if (node.getPayload() instanceof Token) {
        return true;
    }//from   www  .  jav  a 2  s .c om
    for (int i = 0; i < node.getChildCount(); i++) {
        ParseTree child = node.getChild(i);
        if (containsTokens(child)) {
            return true;
        }
    }
    return false;
}

From source file:AST.java

License:Open Source License

private static void walk(ParseTree tree, AST ast) {

    //System.out.println(String.valueOf(AST.getPayload(tree)) + ":" + tree.getText() + " = " + tree.getPayload().getClass().getName() + " @" + (containsTokens(tree) ? "true" : "false"));
    if (tree.getChildCount() == 0) {
        // We've reached a leaf. We must create a new instance of an AST because
        // the constructor will make sure this new instance is added to its parent's
        // child nodes.
        new AST(ast, null, tree);
    } /*else if (tree.getChildCount() == 1) {
      // We've reached an inner node with a single child: we don't include this in
      // our AST./*from w w w . j  a v  a2  s  .  co m*/
      walk(tree.getChild(0), ast);
      } */ else /*if (tree.getChildCount() > 1)*/ {

        for (int i = 0; i < tree.getChildCount(); i++) {

            ParseTree child = tree.getChild(i);

            if (!containsTokens(child)) {
                continue;
            }

            //System.out.println(String.valueOf(AST.getPayload(child)) + ":" + child.getText() + " = " + child.getPayload().getClass().getName() + " @" + (containsTokens(child) ? "true" : "false"));
            AST temp = new AST(ast, null, tree.getChild(i));

            if (!(temp.payload instanceof Token)) {
                //if ((temp.payload instanceof String)) {
                // Only traverse down if the payload is not a Token.
                walk(tree.getChild(i), temp);
            }
        }
    }
}

From source file:SExpr.java

License:Open Source License

public SExpr eval(ParseTree tree) throws Exception {
    //        System.out.println(tree.getClass());
    //        System.out.println(tree.getChild(1).getClass());
    //        System.out.println(tree.getChild(2).getClass());
    //        System.out.println(tree.getChild(3).getClass());

    if (tree instanceof TerminalNodeImpl) {
        SExpr sexpr = new SExpr(tree.toString());
        String[] tokens = sexpr.value.split(":");
        String action = tokens[0];
        String term = tokens[1];//from w w w  .j a  va 2  s.  c  om
        System.out.println("Searching relation: " + action);
        System.out.println("With term: " + term);
        QueryEngine queryEngine = new QueryEngine(term, action, musicOntology);
        ArrayList<Track> tracks = queryEngine.getResults();
        System.out.println("GOT: " + tracks.size());
        sexpr.tracks = tracks;
        return sexpr;
    } else {
        if (tree.getChildCount() == 1) {
            System.out.println("the start tag");
            return (eval(tree.getChild(0)));
        }
        if (tree.getChild(2).toString().equalsIgnoreCase("&")) {
            //and operation
            SExpr left_evaled = eval(tree.getChild(1));
            SExpr right_evaled = eval(tree.getChild(3));

            SExpr sexpr = new SExpr(tree.getChild(1).toString() + " and " + tree.getChild(1).toString());
            sexpr.tracks = (ArrayList<Track>) intersection(left_evaled.tracks, right_evaled.tracks);

            return sexpr;
        } else if (tree.getChild(2).toString().equalsIgnoreCase("|")) {
            //or operation

            SExpr left_evaled = eval(tree.getChild(1));
            SExpr right_evaled = eval(tree.getChild(3));

            SExpr sexpr = new SExpr(tree.getChild(1).toString() + " or " + tree.getChild(1).toString());
            sexpr.tracks = (ArrayList<Track>) union(left_evaled.tracks, right_evaled.tracks);

            return sexpr;

        }

    }

    return null;
}

From source file:annis.ql.parser.RawAqlListener.java

License:Apache License

private static void collectToken(ParseTree node, List<Token> token) {
    for (int i = 0; i < node.getChildCount(); i++) {
        ParseTree child = node.getChild(i);
        if (child.getPayload() instanceof Token) {
            token.add((Token) child.getPayload());
        } else {// w  ww  .  j  a v  a 2  s.  c  o m
            collectToken(child, token);
        }
    }
}

From source file:br.unicamp.cst.util.SimulateConfiguration.java

License:Open Source License

private void jButtonApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonApplyActionPerformed
    // TODO add your handling code here:

    String s = jTextAreaComand.getText();
    //String s = jTextAreaComand.getText();
    jTextAreaComand.setText(null);//from   w w  w .  j  av a 2 s.  co  m
    // jTextAreaComand.setText(null);
    s = s + "\n";

    /**
     * ******************** to Parse *******************
     */
    ANTLRInputStream input = new ANTLRInputStream(s);

    // create a lexer that feeds off of input CharStream
    OwrlLexer lexer = new OwrlLexer(input);

    // create a buffer of tokens pulled from the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer);

    // create a parser that feeds off the tokens buffer
    OwrlParser parser = new OwrlParser(tokens);

    ParseTree tree = parser.conf(); // begin parsing at init rule

    // Walk it and attach our listener
    ParseTreeWalker walker = new ParseTreeWalker();
    CustomizedListener listener = new CustomizedListener();

    for (int i = 1; i < (tree.getChildCount() - 2); i++) {

        walker.walk(listener, tree.getChild(i));

    }

    List<Pair<String, List<AbstractObject>>> memory = listener.getMemory();
    //
    //        if (!configurations.applyCommands(memory)) {
    //            jTextAreaComand.setText(jTextAreaComand.getText().concat("\nErro: Falha ao aplicar instrues. Verifique seu comando.\n"));
    //        }
    buildTree();
}

From source file:com.antsdb.saltedfish.sql.mysql.InstructionGenerator.java

License:Open Source License

static void scan(GeneratorContext ctx, ParseTree node) {
    if (node instanceof TerminalNode) {
        if (node.getText().equals("?")) {
            ctx.addParameter((TerminalNode) node);
        }/*  ww  w .j av  a 2 s.c  o m*/
    }
    if (node instanceof Function_nameContext) {
        if (node.getText().equalsIgnoreCase("sum")) {
            ctx.setHasAggregateFunctions(true);
        } else if (node.getText().equalsIgnoreCase("count")) {
            ctx.setHasAggregateFunctions(true);
        } else if (node.getText().equalsIgnoreCase("GROUP_CONCAT")) {
            ctx.setHasAggregateFunctions(true);
        } else if (node.getText().equalsIgnoreCase("max")) {
            ctx.setHasAggregateFunctions(true);
        } else if (node.getText().equalsIgnoreCase("min")) {
            ctx.setHasAggregateFunctions(true);
        }
    }
    for (int j = 0; j < node.getChildCount(); j++) {
        scan(ctx, node.getChild(j));
    }
}

From source file:com.bacoder.parser.java.adapter.JavaAdapter.java

License:Apache License

protected boolean isTerminalNode(ParseTree tree, int index, int symbolType) {
    if (tree.getChildCount() > index) {
        ParseTree node = tree.getChild(index);
        return node instanceof TerminalNode && ((TerminalNode) node).getSymbol().getType() == symbolType;
    } else {/*from w  ww .  j a  v a 2 s.  c o  m*/
        return false;
    }
}

From source file:com.blazebit.persistence.impl.expression.JPQLSelectExpressionVisitorImpl.java

License:Apache License

private FunctionExpression handleFunction(String name, ParseTree ctx) {
    List<Expression> funcArgs = new ArrayList<Expression>(ctx.getChildCount());
    for (int i = 0; i < ctx.getChildCount(); i++) {
        if (!(ctx.getChild(i) instanceof TerminalNode)) {
            funcArgs.add(ctx.getChild(i).accept(this));
        }/*from ww w .j a  v a2s.  c o  m*/
    }
    return new FunctionExpression(name, funcArgs);
}

From source file:com.blazebit.persistence.parser.expression.JPQLSelectExpressionVisitorImpl.java

License:Apache License

private FunctionExpression handleFunction(String name, ParseTree ctx) {
    List<Expression> funcArgs = new ArrayList<Expression>(ctx.getChildCount());
    for (int i = 0; i < ctx.getChildCount(); i++) {
        if (!(ctx.getChild(i) instanceof TerminalNode)) {
            funcArgs.add(ctx.getChild(i).accept(this));
        }//  w w  w .j  a va2s .c  om
    }

    if ("FUNCTION".equalsIgnoreCase(name) && funcArgs.size() > 0
            && aggregateFunctions.contains(getLiteralString(funcArgs.get(0)).toLowerCase())) {
        return new AggregateExpression(false, name, funcArgs);
    } else {
        return new FunctionExpression(name, funcArgs);
    }
}

From source file:com.cisco.yangide.core.parser.ValidationUtil.java

License:Open Source License

static String getName(ParseTree child) {
    String result = "";
    for (int i = 0; i < child.getChildCount(); ++i) {
        if (child.getChild(i) instanceof StringContext) {
            final StringContext context = (StringContext) child.getChild(i);
            if (context != null) {
                return stringFromStringContext(context);

            }// www .j a  v a  2  s.  c  o m
        }
    }
    return result;
}