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

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

Introduction

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

Prototype

@Override
    public ParseTree getChild(int i) 

Source Link

Usage

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

License:Open Source License

/**
 * Endpoint for ANTLR to call after parsing a method declaration.
 *
 * <p>//from   w  w  w.  jav  a2 s .  com
 *   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  .  ja v  a2s.c om*/
    return Leaf;
}