Example usage for org.antlr.v4.runtime ParserRuleContext getChildCount

List of usage examples for org.antlr.v4.runtime ParserRuleContext getChildCount

Introduction

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

Prototype

@Override
    public int getChildCount() 

Source Link

Usage

From source file:org.xgmtk.lore.ast.ASTBuilder.java

License:Apache License

protected static void printContext(ParserRuleContext ctx) {
    System.err.println("(Context object id: " + ctx + ", line: " + ctx.start.getLine() + ", \"" + ctx.getText()
            + "\", child count: " + ctx.getChildCount() + "){");
    for (int i = 0; i < ctx.getChildCount(); ++i) {
        ParseTree ptree = ctx.getChild(i);
        System.err.println("\t\"" + ptree.getText() + "\"(has: " + ptree.getChildCount() + ")");
    }/*from  w  w  w .ja v a  2s  . c o m*/
    System.err.println("}");
}

From source file:org.xgmtk.lore.ast.ASTBuilder.java

License:Apache License

/**
 * TODO write JavaDoc comment./*from   w  w  w . ja v a 2  s . c  o  m*/
 * 
 * @param ctx
 * @param string
 * @return
 */
protected static int indexOf(ParserRuleContext ctx, String string) {
    for (int i = 0; i < ctx.getChildCount(); ++i) {
        if (Objects.equals(ctx.getChild(i).getText(), string)) {
            return i;
        }
    }
    return -1;
}

From source file:org.xgmtk.lore.ast.ASTBuilder.java

License:Apache License

protected AST buildOperatorTree(ParserRuleContext ctx, final NonTerminalSymbol[] ops) {
    //      printContext(ctx);
    List<AST> cs = this.getChildrenList();
    //      System.err.println("ParseTree: \""+ctx.getText()+"\"");
    //      System.err.println("AST Children: "+cs.size());
    //      System.err.println("ParseTree Children: "+ctx.getChildCount());
    AST left = cs.get(0);/*from  w ww  . j  a va2 s. c  om*/
    int ci = 1;
    for (int i = 1; i < ctx.getChildCount(); i += 2) {
        AST right = cs.get(ci++);
        NonTerminalSymbol t = selectNonterminalSymbol(ctx, ctx.getChild(i).getText(), ops);
        left = node(t, loc(src, ctx.getStart().getLine()), left, right);
    }
    return left;
}

From source file:processing.mode.java.preproc.PdeParseTreeListener.java

License:Open Source License

/**
 * Determine if a method declaration is for setup.
 *
 * @param declaration The method declaration to parse.
 * @return True if setup and false otherwise.
 */// w  w w.  j av a2 s.c o m
private boolean isMethodSetup(ParserRuleContext declaration) {
    if (declaration.getChildCount() < 2) {
        return false;
    }
    return declaration.getChild(1).getText().equals("setup");
}

From source file:processing.mode.java.preproc.PdeParseTreeListener.java

License:Open Source License

/**
 * Endpoint for ANTLR to call after parsing a method declaration.
 *
 * <p>//w  w w. j a  va2  s .  c o m
 *   Endpoint for ANTLR to call after parsing a method declaration, making any method "public"
 *   that has:
 *
 *   <ul>
 *     <li>no other access modifier</li>
 *     <li>return type "void"</li>
 *     <li>is either in the context of the sketch class</li>
 *     <li>is in the context of a class definition that extends PApplet</li>
 *   </ul>
 * </p>
 *
 * @param ctx ANTLR context for the method declaration
 */
public void exitMethodDeclaration(ProcessingParser.MethodDeclarationContext ctx) {
    ParserRuleContext memCtx = ctx.getParent();
    ParserRuleContext clsBdyDclCtx = memCtx.getParent();
    ParserRuleContext clsBdyCtx = clsBdyDclCtx.getParent();
    ParserRuleContext clsDclCtx = clsBdyCtx.getParent();

    boolean inSketchContext = clsBdyCtx instanceof ProcessingParser.StaticProcessingSketchContext
            || clsBdyCtx instanceof ProcessingParser.ActiveProcessingSketchContext;

    boolean inPAppletContext = inSketchContext || (clsDclCtx instanceof ProcessingParser.ClassDeclarationContext
            && clsDclCtx.getChildCount() >= 4 && clsDclCtx.getChild(2).getText().equals("extends")
            && clsDclCtx.getChild(3).getText().endsWith("PApplet"));

    // Find modifiers
    ParserRuleContext possibleModifiers = ctx;

    while (!(possibleModifiers instanceof ProcessingParser.ClassBodyDeclarationContext)) {
        possibleModifiers = possibleModifiers.getParent();
    }

    // Look for visibility modifiers and annotations
    boolean hasVisibilityModifier = false;

    int numChildren = possibleModifiers.getChildCount();

    ParserRuleContext annoationPoint = null;

    for (int i = 0; i < numChildren; i++) {
        boolean childIsVisibility;

        ParseTree child = possibleModifiers.getChild(i);
        String childText = child.getText();

        childIsVisibility = childText.equals("public");
        childIsVisibility = childIsVisibility || childText.equals("private");
        childIsVisibility = childIsVisibility || childText.equals("protected");

        hasVisibilityModifier = hasVisibilityModifier || childIsVisibility;

        boolean isModifier = child instanceof ProcessingParser.ModifierContext;
        if (isModifier && isAnnoation((ProcessingParser.ModifierContext) child)) {
            annoationPoint = (ParserRuleContext) child;
        }
    }

    // Insert at start of method or after annoation
    if (!hasVisibilityModifier) {
        if (annoationPoint == null) {
            createInsertBefore(possibleModifiers.getStart(), " public ");
        } else {
            createInsertAfter(annoationPoint.getStop(), " public ");
        }
    }

    // Check if this was main
    if ((inSketchContext || inPAppletContext) && hasVisibilityModifier
            && ctx.getChild(1).getText().equals("main")) {
        foundMain = true;
    }
}

From source file:tilda.grammar.TildaSQLTreePrinter.java

License:Apache License

protected static boolean isLeafNode(ParserRuleContext ctx) {
    if (ctx.getChildCount() == 0)
        return false;
    boolean Leaf = true;
    for (int i = 0; i < ctx.getChildCount(); ++i)
        if (isTerminalNode(ctx.getChild(i)) == false)
            Leaf = false;/* w  w  w  .  j a  v a  2  s. c om*/
    return Leaf;
}