Search in sources :

Example 16 with GroovySourceAST

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

the class SourceMetaDataVisitor method extractParameters.

private void extractParameters(GroovySourceAST t, MethodMetaData method) {
    GroovySourceAST paramsAst = t.childOfType(PARAMETERS);
    for (GroovySourceAST child = (GroovySourceAST) paramsAst.getFirstChild(); child != null; child = (GroovySourceAST) child.getNextSibling()) {
        assert child.getType() == PARAMETER_DEF || child.getType() == VARIABLE_PARAMETER_DEF;
        TypeMetaData type = extractTypeName((GroovySourceAST) child.getFirstChild().getNextSibling());
        if (child.getType() == VARIABLE_PARAMETER_DEF) {
            type.setVarargs();
        }
        method.addParameter(extractIdent(child), type);
    }
}
Also used : GroovySourceAST(org.codehaus.groovy.antlr.GroovySourceAST)

Example 17 with GroovySourceAST

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

the class SourceMetaDataVisitor method extractTypeName.

private TypeMetaData extractTypeName(GroovySourceAST ast) {
    TypeMetaData type = new TypeMetaData();
    switch(ast.getType()) {
        case TYPE:
            GroovySourceAST typeName = (GroovySourceAST) ast.getFirstChild();
            extractTypeName(typeName, type);
            break;
        case WILDCARD_TYPE:
            // In the groovy grammar, the bounds are sibling of the ?, in the java grammar, they are the child
            GroovySourceAST bounds = (GroovySourceAST) (groovy ? ast.getNextSibling() : ast.getFirstChild());
            if (bounds == null) {
                type.setWildcard();
            } else if (bounds.getType() == TYPE_UPPER_BOUNDS) {
                type.setUpperBounds(extractTypeName((GroovySourceAST) bounds.getFirstChild()));
            } else if (bounds.getType() == TYPE_LOWER_BOUNDS) {
                type.setLowerBounds(extractTypeName((GroovySourceAST) bounds.getFirstChild()));
            }
            break;
        case IDENT:
        case DOT:
            extractTypeName(ast, type);
            break;
        default:
            throw new RuntimeException(String.format("Unexpected token in type name: %s", ast));
    }
    return type;
}
Also used : GroovySourceAST(org.codehaus.groovy.antlr.GroovySourceAST)

Example 18 with GroovySourceAST

use of org.codehaus.groovy.antlr.GroovySourceAST in project groovy-core by groovy.

the class MindMapPrinter method getName.

private String getName(GroovySourceAST t) {
    String name = tokenNames[t.getType()] + " <" + t.getType() + ">";
    if (!(escape(tokenNames[t.getType()]).equals(escape(t.getText())))) {
        name = name + " : " + t.getText();
    }
    switch(t.getType()) {
        case GroovyTokenTypes.METHOD_DEF:
        case GroovyTokenTypes.VARIABLE_DEF:
            GroovySourceAST identNode = t.childOfType(GroovyTokenTypes.IDENT);
            if (identNode != null) {
                name = name + " : " + identNode.getText() + "";
            }
    }
    name = escape(name);
    if (sourceBuffer != null) {
        name += "&#xa;";
        name += t.getLine() + "," + t.getColumn() + " - " + t.getLineLast() + "," + t.getColumnLast();
        name += "&#xa;";
        name += escape(sourceBuffer.getSnippet(new LineColumn(t.getLine(), t.getColumn()), new LineColumn(t.getLineLast(), t.getColumnLast())));
    }
    return name;
}
Also used : GroovySourceAST(org.codehaus.groovy.antlr.GroovySourceAST) LineColumn(org.codehaus.groovy.antlr.LineColumn)

Example 19 with GroovySourceAST

use of org.codehaus.groovy.antlr.GroovySourceAST in project groovy-core by groovy.

the class SourceCodeTraversal method accept.

protected void accept(GroovySourceAST currentNode) {
    if (currentNode != null && unvisitedNodes != null && unvisitedNodes.size() > 0) {
        GroovySourceAST t = currentNode;
        if (!(unvisitedNodes.contains(currentNode))) {
            return;
        }
        push(t);
        switch(t.getType()) {
            case // expr?foo:bar
            GroovyTokenTypes.QUESTION:
                accept_FirstChild_v_SecondChild_v_ThirdChild_v(t);
                break;
            // 
            case GroovyTokenTypes.CASE_GROUP:
            case // foo instanceof MyType
            GroovyTokenTypes.LITERAL_instanceof:
                accept_FirstChild_v_SecondChildsChildren_v(t);
                break;
            case GroovyTokenTypes.ANNOTATION:
                accept_v_FirstChild_2ndv_SecondChild_v___LastChild_v(t);
                break;
            // (a=1; a<10; a++)
            case GroovyTokenTypes.CLOSURE_LIST:
            // a,b,c
            case GroovyTokenTypes.ELIST:
            // a,b,c
            case GroovyTokenTypes.PARAMETERS:
            // <String, Object>
            case GroovyTokenTypes.TYPE_ARGUMENTS:
            // "foo${bar}wibble"
            case GroovyTokenTypes.STRING_CONSTRUCTOR:
            // class Foo<T extends F>
            case GroovyTokenTypes.TYPE_PARAMETER:
            // class Foo<T>
            case GroovyTokenTypes.TYPE_PARAMETERS:
            case // class Foo<T extends F>
            GroovyTokenTypes.TYPE_UPPER_BOUNDS:
                accept_v_FirstChild_v_SecondChild_v___LastChild_v(t);
                // todo : confirm that TYPE_LOWER_BOUNDS does not have multiple children
                break;
            case // void f(String ... others) {}
            GroovyTokenTypes.VARIABLE_PARAMETER_DEF:
                accept_v_FirstChild_SecondChild_v_ThirdChild_v(t);
                break;
            case GroovyTokenTypes.INDEX_OP:
                // accept_FirstChild_v_SecondChild_v(t);
                accept_SecondChild_v_ThirdChild_v(t);
                break;
            // enum Foo(THESE,ARE,THEY)
            case GroovyTokenTypes.ENUM_CONSTANT_DEF:
            case GroovyTokenTypes.EXPR:
            case GroovyTokenTypes.IMPORT:
            case GroovyTokenTypes.STATIC_IMPORT:
            case GroovyTokenTypes.VARIABLE_DEF:
            case GroovyTokenTypes.METHOD_DEF:
            // class Foo {def bar()}  <-- this block
            case GroovyTokenTypes.OBJBLOCK:
            // void f(String me) {}
            case GroovyTokenTypes.PARAMETER_DEF:
            case // list of expressions, variable defs etc
            GroovyTokenTypes.SLIST:
                accept_v_AllChildren_v(t);
                break;
            // @Blue(foo=123)
            case GroovyTokenTypes.ANNOTATION_MEMBER_VALUE_PAIR:
            // a = b
            case GroovyTokenTypes.ASSIGN:
            // a &= b
            case GroovyTokenTypes.BAND_ASSIGN:
            // a |= b
            case GroovyTokenTypes.BOR_ASSIGN:
            // a >>>= b
            case GroovyTokenTypes.BSR_ASSIGN:
            // a ^= b
            case GroovyTokenTypes.BXOR_ASSIGN:
            // a <=> b
            case GroovyTokenTypes.COMPARE_TO:
            // a /= b
            case GroovyTokenTypes.DIV_ASSIGN:
            // a == b
            case GroovyTokenTypes.EQUAL:
            // a -= b
            case GroovyTokenTypes.MINUS_ASSIGN:
            // a %= b
            case GroovyTokenTypes.MOD_ASSIGN:
            // a != b
            case GroovyTokenTypes.NOT_EQUAL:
            // a += b
            case GroovyTokenTypes.PLUS_ASSIGN:
            // a =~ b
            case GroovyTokenTypes.REGEX_FIND:
            // a ==~ b
            case GroovyTokenTypes.REGEX_MATCH:
            // a <<= b
            case GroovyTokenTypes.SL_ASSIGN:
            // a >>= b
            case GroovyTokenTypes.SR_ASSIGN:
            // a *= b
            case GroovyTokenTypes.STAR_ASSIGN:
            case // x **= 3
            GroovyTokenTypes.STAR_STAR_ASSIGN:
                if (t.childAt(1) != null) {
                    accept_FirstChild_v_RestOfTheChildren(t);
                } else {
                    accept_v_FirstChild_v_RestOfTheChildren(t);
                }
                break;
            case // @interface Foo{ int bar()...
            GroovyTokenTypes.ANNOTATION_FIELD_DEF:
                accept_FirstSecondAndThirdChild_v_v_ForthChild(t);
                break;
            // @interface Foo...
            case GroovyTokenTypes.ANNOTATION_DEF:
            // 1 & 2
            case GroovyTokenTypes.BAND:
            // 1 | 2
            case GroovyTokenTypes.BOR:
            // 1 >>> 2
            case GroovyTokenTypes.BSR:
            // 1 ^ 2
            case GroovyTokenTypes.BXOR:
            // class Foo...
            case GroovyTokenTypes.CLASS_DEF:
            // private Foo() {...
            case GroovyTokenTypes.CTOR_IDENT:
            // 3/4
            case GroovyTokenTypes.DIV:
            // foo.bar
            case GroovyTokenTypes.DOT:
            // enum Foo...
            case GroovyTokenTypes.ENUM_DEF:
            // a >= b
            case GroovyTokenTypes.GE:
            // a > b
            case GroovyTokenTypes.GT:
            // interface Foo...
            case GroovyTokenTypes.INTERFACE_DEF:
            // myMethod(name:"Jez")
            case GroovyTokenTypes.LABELED_ARG:
            // foo:x=1
            case GroovyTokenTypes.LABELED_STAT:
            // true && false
            case GroovyTokenTypes.LAND:
            // a <= b
            case GroovyTokenTypes.LE:
            // foo as Bar
            case GroovyTokenTypes.LITERAL_as:
            // if (i in myList) ...
            case GroovyTokenTypes.LITERAL_in:
            // true && false
            case GroovyTokenTypes.LOR:
            // a < b
            case GroovyTokenTypes.LT:
            // this.&foo()
            case GroovyTokenTypes.MEMBER_POINTER:
            // 4 % 3
            case GroovyTokenTypes.MOD:
            // 1 - 1
            case GroovyTokenTypes.MINUS:
            // foo?.bar
            case GroovyTokenTypes.OPTIONAL_DOT:
            case GroovyTokenTypes.PACKAGE_DEF:
            // 1 + 1
            case GroovyTokenTypes.PLUS:
            // [1..<10]
            case GroovyTokenTypes.RANGE_EXCLUSIVE:
            // [1..10]
            case GroovyTokenTypes.RANGE_INCLUSIVE:
            // a << b
            case GroovyTokenTypes.SL:
            // foo*.bar
            case GroovyTokenTypes.SPREAD_DOT:
            // a >> b
            case GroovyTokenTypes.SR:
            // a * b   or    import foo.*
            case GroovyTokenTypes.STAR:
            // x ** 3
            case GroovyTokenTypes.STAR_STAR:
            case // trait Foo...
            GroovyTokenTypes.TRAIT_DEF:
                accept_FirstChild_v_RestOfTheChildren(t);
                break;
            case GroovyTokenTypes.CTOR_CALL:
            case GroovyTokenTypes.METHOD_CALL:
                if (t.getNumberOfChildren() == 2 && t.childAt(1) != null && t.childAt(1).getType() == GroovyTokenTypes.CLOSABLE_BLOCK) {
                    // myMethod {...
                    accept_FirstChild_v_SecondChild(t);
                } else {
                    GroovySourceAST lastChild = t.childAt(t.getNumberOfChildren() - 1);
                    if (lastChild != null && lastChild.getType() == GroovyTokenTypes.CLOSABLE_BLOCK) {
                        // myMethod(a,b) {...
                        accept_FirstChild_v_RestOfTheChildren_v_LastChild(t);
                    } else {
                        // myMethod(a,b)
                        accept_FirstChild_v_RestOfTheChildren_v(t);
                    }
                }
                break;
            case GroovyTokenTypes.LITERAL_while:
            // deprecated                case GroovyTokenTypes.LITERAL_with:
            case // (String)itr.next()
            GroovyTokenTypes.TYPECAST:
                accept_v_FirstChildsFirstChild_v_RestOfTheChildren(t);
                break;
            case // if (grandchild) {child1} else {child2} ...
            GroovyTokenTypes.LITERAL_if:
                accept_v_FirstChildsFirstChild_v_Child2_Child3_v_Child4_v___v_LastChild(t);
                break;
            case // [1,2,3].each {foo(it)}  <-- Closure
            GroovyTokenTypes.CLOSABLE_BLOCK:
                if (t.childAt(0) != null && t.childAt(0).getType() == GroovyTokenTypes.IMPLICIT_PARAMETERS) {
                    accept_v_AllChildren_v(t);
                } else {
                    accept_v_FirstChild_v_RestOfTheChildren_v(t);
                }
                break;
            case GroovyTokenTypes.FOR_IN_ITERABLE:
            case GroovyTokenTypes.LITERAL_for:
            case GroovyTokenTypes.LITERAL_new:
            case GroovyTokenTypes.LITERAL_switch:
                accept_v_FirstChild_v_RestOfTheChildren_v(t);
                break;
            // just like modifiers but for package/enum declarations
            case GroovyTokenTypes.ANNOTATIONS:
            case GroovyTokenTypes.LITERAL_assert:
            case GroovyTokenTypes.LITERAL_catch:
            case GroovyTokenTypes.LITERAL_synchronized:
            case GroovyTokenTypes.LITERAL_try:
            case GroovyTokenTypes.MODIFIERS:
                accept_v_FirstChild_v_RestOfTheChildren(t);
                break;
            case GroovyTokenTypes.WILDCARD_TYPE:
                accept_v_Siblings_v(t);
                break;
            default:
                accept_v_FirstChild_v(t);
                break;
        }
        pop();
    }
}
Also used : GroovySourceAST(org.codehaus.groovy.antlr.GroovySourceAST)

Example 20 with GroovySourceAST

use of org.codehaus.groovy.antlr.GroovySourceAST in project groovy-core by groovy.

the class SourceCodeTraversal method traverse.

/**
 * traverse an AST node
 * @param t the AST node to traverse
 */
private void traverse(GroovySourceAST t) {
    if (t == null) {
        return;
    }
    if (unvisitedNodes != null) {
        unvisitedNodes.add(t);
    }
    GroovySourceAST child = (GroovySourceAST) t.getFirstChild();
    if (child != null) {
        traverse(child);
    }
    GroovySourceAST sibling = (GroovySourceAST) t.getNextSibling();
    if (sibling != null) {
        traverse(sibling);
    }
}
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