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:PostParser.java

License:Open Source License

/**
 * {@inheritDoc}// w w  w .j ava 2 s  .com
 *
 * <p>This takes a ParserRuleContext of a binary operator and "flattens"
 * the operator if one of its operands is the same binary operator context.</p>
 */
public void flatten(ParserRuleContext ctx) {
    /* This function only flattens if the operator is the same and also 
     * keeps the operators intact. 
     *
     * Since ANTLR4 parses this operator as left associative, we only
     * need to check the left hand side expr.
     */

    //If the child isn't the same construct, nothing to do.
    if (!(ctx.getChild(0).getClass() == ctx.getClass()))
        return;

    ParserRuleContext lhs = (ParserRuleContext) ctx.getChild(0);
    ParseTree rhs = ctx.getChild(2);
    TerminalNode op = (TerminalNode) ctx.getChild(1);

    /*If the operator of the nested Context isn't the same, nothing to do.
     *The operator is always in position 1 for infix operators. We do this
     *check because some Contexts that use the same context for multiple
     *operators.
    */
    if (!op.getText().equals(lhs.getChild(1).getText()))
        return;

    //Clear all children.
    ctx.children.clear();

    //Add all children of lhs. (Also adds the operator of the lhs.)
    ctx.children.addAll(lhs.children);

    //Finally, add the rhs back in.
    ctx.children.add(rhs);
}

From source file:PostParser.java

License:Open Source License

/**
 * {@inheritDoc}/*  ww  w .j  a  va2  s  .c o m*/
 *
 * <p>And[expr1,expr2]</p>
 */
@Override
public void exitAnd(FoxySheepParser.AndContext ctx) {
    //The usual flatten function won't work, because there are two And operators,
    //and we need to flatten over both.

    //If the child isn't the same construct, nothing to do.
    if (!(ctx.getChild(0).getClass() == ctx.getClass()))
        return;

    ParserRuleContext lhs = (ParserRuleContext) ctx.getChild(0);
    ParseTree rhs = ctx.getChild(2);
    TerminalNode op = (TerminalNode) ctx.getChild(1);

    /*If the operator of the nested Context isn't the same, nothing to do.
     *The operator is always in position 1 for infix operators. We do this
     *check because some Contexts that use the same context for multiple
     *operators.
    */
    //Here's the part that's different from flatten().
    //If childOp is an Nand or parentOp is a Nand, then we need child==parent.
    String childOp = lhs.getChild(1).getText();
    if (childOp.equals("\u22bc") || op.getText().equals("\u22bc")) {
        if (!op.getText().equals(childOp))
            return;
    }

    //Clear all children.
    ctx.children.clear();

    //Add all children of lhs. (Also adds the operator of the lhs.)
    ctx.children.addAll(lhs.children);

    //Finally, add the rhs back in.
    ctx.children.add(rhs);
}

From source file:PostParser.java

License:Open Source License

/**
 * {@inheritDoc}//  w  ww.j  a  v a  2 s .  c om
 *
 * <p>Or[expr1,expr2]</p>
 */
@Override
public void exitOr(FoxySheepParser.OrContext ctx) {
    //The usual flatten function won't work, because there are two Or operators,
    //and we need to flatten over both.

    //If the child isn't the same construct, nothing to do.
    if (!(ctx.getChild(0).getClass() == ctx.getClass()))
        return;

    ParserRuleContext lhs = (ParserRuleContext) ctx.getChild(0);
    ParseTree rhs = ctx.getChild(2);
    TerminalNode op = (TerminalNode) ctx.getChild(1);

    /*If the operator of the nested Context isn't the same, nothing to do.
     *The operator is always in position 1 for infix operators. We do this
     *check because some Contexts that use the same context for multiple
     *operators.
    */
    //Here's the part that's different from flatten().
    //If childOp is an Nor or parentOp is a Nor, then we need child==parent.
    String childOp = lhs.getChild(1).getText();
    if (childOp.equals("\u22bd") || op.getText().equals("\u22bd")) {
        if (!op.getText().equals(childOp))
            return;
    }

    //Clear all children.
    ctx.children.clear();

    //Add all children of lhs. (Also adds the operator of the lhs.)
    ctx.children.addAll(lhs.children);

    //Finally, add the rhs back in.
    ctx.children.add(rhs);
}

From source file:FullFormEmitter.java

License:Open Source License

/**
 * {@inheritDoc}//from   w w  w . j a va2s. c o  m
 *
 * <p>This is code factored out of both visitSpanA and visitSpanB.</p>
 */
public String visitSpan(ParserRuleContext ctx) {
    StringBuilder val = new StringBuilder("Span[");
    int curChild = 0;

    //Because this SpanA might have been created by a subtree rewrite, we
    //cannot guarantee it begins with an expr.
    if (ctx.getChild(curChild).getText().equals(";;")) {
        //Begins with ";;", implicit start of 1.
        val.append("1");
        curChild++;
    } else {
        //Begins with expr
        val.append(getFullForm(ctx.getChild(curChild)));
        curChild += 2;
    }
    //Cursor now points to one past the first ";;"
    if (curChild < ctx.children.size() && !ctx.getChild(curChild).getText().equals(";;")) {
        //The middle expr has not been omitted
        val.append(",");
        val.append(getFullForm(ctx.getChild(curChild)));
        curChild++;
    } else {
        //The middle expr has been omitted.
        val.append(",All");
    }

    //Cursor now points to either the second ";;" or past the end of the expr.
    if (curChild < ctx.children.size() && ctx.getChild(curChild).getText().equals(";;")) {
        //There is a skip amount.
        val.append(",");
        val.append(getFullForm(ctx.getChild(curChild + 1)));
    }

    val.append("]");
    return val.toString();
}

From source file:br.edu.ifrn.potigol.editor.PrettyListener.java

License:Open Source License

@Override
public void exitEveryRule(ParserRuleContext ctx) {
    if (getValue(ctx) == null)
        setValue(ctx, getValue(ctx.getChild(0)));
}

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

License:Apache License

protected void setAnnotations(ParserRuleContext context, final NodeWithModifiers node) {
    List<Annotation> annotations = transform(context, ClassOrInterfaceModifierContext.class,
            new Function<ClassOrInterfaceModifierContext, Annotation>() {
                @Override/* w  ww.j a v  a 2s. c  o m*/
                public Annotation apply(ClassOrInterfaceModifierContext context) {
                    if (context.getChildCount() > 0 && context.getChild(0) instanceof AnnotationContext) {
                        AnnotationContext annotationContext = (AnnotationContext) context.getChild(0);
                        return getAdapter(AnnotationAdapter.class).adapt(annotationContext);
                    } else {
                        return null;
                    }
                }
            });
    node.setAnnotations(annotations);
}

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

License:Apache License

protected void setClassOrInterfaceModifiers(ParserRuleContext context, final NodeWithModifiers node) {
    setAnnotations(context, node);//from   w  ww.j  a v  a 2 s. c  o  m

    forEachChild(context, ClassOrInterfaceModifierContext.class,
            new Function<ClassOrInterfaceModifierContext, Void>() {
                @Override
                public Void apply(ClassOrInterfaceModifierContext context) {
                    if (context.getChildCount() > 0 && context.getChild(0) instanceof TerminalNode) {
                        TerminalNode child = (TerminalNode) context.getChild(0);
                        int type = child.getSymbol().getType();
                        switch (type) {
                        case JavaParser.PUBLIC:
                            node.setPublic(true);
                            break;
                        case JavaParser.PROTECTED:
                            node.setProtected(true);
                            break;
                        case JavaParser.PRIVATE:
                            node.setPrivate(true);
                            break;
                        case JavaParser.STATIC:
                            node.setStatic(true);
                            break;
                        case JavaParser.ABSTRACT:
                            node.setAbstract(true);
                            break;
                        case JavaParser.FINAL:
                            node.setFinal(true);
                            break;
                        case JavaParser.STRICTFP:
                            node.setStrictfp(true);
                            break;
                        default:
                        }
                    }
                    return null;
                }
            });
}

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

License:Apache License

protected void setModifiers(ParserRuleContext context, final NodeWithModifiers node) {
    setClassOrInterfaceModifiers(context, node);

    forEachChild(context, ModifierContext.class, new Function<ModifierContext, Void>() {
        @Override// ww w.  j av a 2 s .  com
        public Void apply(ModifierContext context) {
            if (context.getChildCount() > 0 && context.getChild(0) instanceof TerminalNode) {
                TerminalNode child = (TerminalNode) context.getChild(0);
                int type = child.getSymbol().getType();
                switch (type) {
                case JavaParser.NATIVE:
                    node.setNative(true);
                    break;
                case JavaParser.SYNCHRONIZED:
                    node.setSynchronized(true);
                    break;
                case JavaParser.TRANSIENT:
                    node.setTransient(true);
                    break;
                case JavaParser.VOLATILE:
                    node.setVolatile(true);
                    break;
                default:
                }
            }
            return null;
        }
    });
}

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

License:Apache License

public Expression visitMacroExpression(String macroName, ParserRuleContext ctx) {
    List<Expression> funcArgs = new ArrayList<Expression>(ctx.getChildCount());
    // Special handling of empty invocation, the position 2 contains an empty child node
    if (ctx.getChildCount() != 4 || !ctx.getChild(2).getText().isEmpty()) {
        for (int i = 0; i < ctx.getChildCount(); i++) {
            if (!(ctx.getChild(i) instanceof TerminalNode)) {
                funcArgs.add(ctx.getChild(i).accept(this));
            }//from   w w  w.j  a  v  a  2 s .co  m
        }
    }

    MacroFunction macro = macros.get(macroName);
    if (macro == null) {
        throw new SyntaxErrorException("The macro '" + macroName + "' could not be found in the macro map!");
    }
    if (usedMacros != null) {
        usedMacros.add(macroName);
    }
    try {
        return macro.apply(funcArgs);
    } catch (RuntimeException ex) {
        throw new IllegalArgumentException("Could not apply the macro for the expression: " + ctx.getText(),
                ex);
    }
}

From source file:com.gigaspaces.persistency.parser.SQL2MongoBaseVisitor.java

License:Open Source License

private boolean IsLogic(ParserRuleContext ctx, String text) {

    for (int i = 0; i < ctx.getChildCount(); i++) {
        ParseTree c = ctx.getChild(i);

        if (c.getText().equals(text))
            return true;
    }//w w  w.  j  a  va  2  s  .  c  om
    return false;
}