Search in sources :

Example 46 with GroovySourceAST

use of org.codehaus.groovy.antlr.GroovySourceAST in project groovy by apache.

the class PreJava2GroovyConverter method swapTwoChildren.

/** To swap two children of node t...
     *
     *<pre>
     *   (t)
     *    |
     *    |
     *   (a) -- (b)
     *
     * t.down = firstNode
     * a.right = b
     * b.right = null
     *</pre>
     * becomes
     *<pre>
     *   (t)
     *    |
     *    |
     *   (b) -- (a)
     *
     * t.down = b
     * a.right = null
     * b.right = a
     *</pre>
     *
     * todo - build API of basic tree mutations like this method.
     */
public void swapTwoChildren(GroovySourceAST t) {
    // this swaps the two child nodes, see javadoc above for explanation of implementation
    GroovySourceAST a = (GroovySourceAST) t.getFirstChild();
    GroovySourceAST b = (GroovySourceAST) a.getNextSibling();
    t.setFirstChild(b);
    a.setNextSibling(null);
    b.setNextSibling(a);
}
Also used : GroovySourceAST(org.codehaus.groovy.antlr.GroovySourceAST)

Example 47 with GroovySourceAST

use of org.codehaus.groovy.antlr.GroovySourceAST in project groovy by apache.

the class PreJava2GroovyConverter method visitJavaArrayInit.

/**
     * Handle Arrays. Examples:
     *
     * <pre>
     * String[] myArray = new String[] {"a","b","c"};
     *
     * becomes
     *
     * String[] myArray = ["a", "b", "c"]
     *
     * ---
     *
     * To convert node (t) and surrounding nodes into the right structure for List Constructor
     *
     * (a) java/EXPR
     *  |
     *  +- (b) java/new
     *      |
     *      + (t) java/ARRAY_INIT
     *
     *  becomes
     *
     * (a) groovy/LIST_CONSTRUCTOR (via ARRAY_INIT as temporary marker type)
     *  |
     *  +- (t) groovy/ELIST
     *
     *  * note: node (b) is thrown away...
     * </pre>
     */
private void visitJavaArrayInit(GroovySourceAST t) {
    // given that we might have a grandParent...
    if (stack.size() > 2) {
        GroovySourceAST grandParent = getGrandParentNode();
        if (grandParent.getType() == JavaTokenTypes.EXPR) {
            //set type as indicator for Java2GroovyConvertor to turn into LIST_CONSTRUCTOR
            grandParent.setType(JavaTokenTypes.ARRAY_INIT);
            grandParent.setFirstChild(t);
            t.setType(JavaTokenTypes.ELIST);
        }
    }
}
Also used : GroovySourceAST(org.codehaus.groovy.antlr.GroovySourceAST)

Example 48 with GroovySourceAST

use of org.codehaus.groovy.antlr.GroovySourceAST in project groovy by apache.

the class CompositeVisitor method pop.

public GroovySourceAST pop() {
    GroovySourceAST lastNodePopped = null;
    Iterator itr = backToFrontVisitors.iterator();
    while (itr.hasNext()) {
        lastNodePopped = (GroovySourceAST) ((Visitor) itr.next()).pop();
    }
    return lastNodePopped;
}
Also used : GroovySourceAST(org.codehaus.groovy.antlr.GroovySourceAST)

Example 49 with GroovySourceAST

use of org.codehaus.groovy.antlr.GroovySourceAST in project groovy by apache.

the class TraversalHelper method accept_v_FirstChildsFirstChild_v_Child2_Child3_v_Child4_v___v_LastChild.

protected void accept_v_FirstChildsFirstChild_v_Child2_Child3_v_Child4_v___v_LastChild(GroovySourceAST t) {
    openingVisit(t);
    GroovySourceAST expr2 = t.childAt(0);
    skip(expr2);
    accept(expr2.childAt(0));
    closingVisit(t);
    GroovySourceAST sibling = (GroovySourceAST) expr2.getNextSibling();
    boolean firstSList = true;
    while (sibling != null) {
        if (!firstSList) {
            subsequentVisit(t);
        }
        firstSList = false;
        accept(sibling);
        sibling = (GroovySourceAST) sibling.getNextSibling();
    }
}
Also used : GroovySourceAST(org.codehaus.groovy.antlr.GroovySourceAST)

Example 50 with GroovySourceAST

use of org.codehaus.groovy.antlr.GroovySourceAST in project groovy by apache.

the class TraversalHelper method accept_v_FirstChildsFirstChild_v_RestOfTheChildren.

protected void accept_v_FirstChildsFirstChild_v_RestOfTheChildren(GroovySourceAST t) {
    openingVisit(t);
    GroovySourceAST expr = t.childAt(0);
    skip(expr);
    accept(expr.childAt(0));
    closingVisit(t);
    acceptSiblings(expr);
}
Also used : GroovySourceAST(org.codehaus.groovy.antlr.GroovySourceAST)

Aggregations

GroovySourceAST (org.codehaus.groovy.antlr.GroovySourceAST)77 AST (antlr.collections.AST)4 Iterator (java.util.Iterator)2 List (java.util.List)2 AntlrASTProcessor (org.codehaus.groovy.antlr.AntlrASTProcessor)2 LineColumn (org.codehaus.groovy.antlr.LineColumn)2