use of org.codehaus.groovy.antlr.GroovySourceAST in project groovy by apache.
the class SimpleGroovyClassDocAssembler method getTypeNodeAsText.
private String getTypeNodeAsText(GroovySourceAST typeNode, String defaultText) {
// TODO refactor to retain richer type information rather than converting to String
if (typeNode == null) {
return defaultText;
}
if (typeNode.getType() == TYPE) {
return getAsText(typeNode, defaultText);
} else if (typeNode.getType() == TYPE_ARGUMENT) {
return getTypeNodeAsText((GroovySourceAST) typeNode.getFirstChild(), defaultText);
} else if (typeNode.getType() == WILDCARD_TYPE) {
AST next = typeNode.getNextSibling();
if (next == null && typeNode.getFirstChild() != null) {
// Java2Groovy produces a slightly different tree structure (TODO fix converter or java.g instead?)
next = typeNode.getFirstChild();
}
if (next == null)
return "?";
String boundType = getTypeNodeAsText((GroovySourceAST) next.getFirstChild(), defaultText);
if (next.getType() == TYPE_UPPER_BOUNDS)
return "? extends " + boundType;
if (next.getType() == TYPE_LOWER_BOUNDS)
return "? super " + boundType;
} else if (typeNode.getType() == IDENT) {
String ident = getAsTextCurrent(typeNode, defaultText);
AST next = typeNode.getNextSibling();
if (next == null && typeNode.getFirstChild() != null) {
// Java2Groovy produces a slightly different tree structure (TODO fix converter or java.g instead?)
next = typeNode.getFirstChild();
}
if (next == null)
return ident;
String boundType = getTypeNodeAsText((GroovySourceAST) next.getFirstChild(), defaultText);
if (next.getType() == TYPE_UPPER_BOUNDS)
return ident + " extends " + boundType;
if (next.getType() == TYPE_LOWER_BOUNDS)
return ident + " super " + boundType;
}
return defaultText;
}
use of org.codehaus.groovy.antlr.GroovySourceAST in project groovy by apache.
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 += "
";
name += t.getLine() + "," + t.getColumn() + " - " + t.getLineLast() + "," + t.getColumnLast();
name += "
";
name += escape(sourceBuffer.getSnippet(new LineColumn(t.getLine(), t.getColumn()), new LineColumn(t.getLineLast(), t.getColumnLast())));
}
return name;
}
use of org.codehaus.groovy.antlr.GroovySourceAST in project groovy by apache.
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);
}
}
use of org.codehaus.groovy.antlr.GroovySourceAST in project groovy by apache.
the class SourceCodeTraversal method accept.
protected void accept(GroovySourceAST currentNode) {
if (currentNode != null && unvisitedNodes != null && !unvisitedNodes.isEmpty()) {
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();
}
}
use of org.codehaus.groovy.antlr.GroovySourceAST in project groovy by apache.
the class SourcePrinter method visitType.
// visit TripleDot, not used in the AST
public void visitType(GroovySourceAST t, int visit) {
GroovySourceAST parent = getParentNode();
GroovySourceAST modifiers = parent.childOfType(GroovyTokenTypes.MODIFIERS);
// No need to print 'def' if we already have some modifiers
if (modifiers == null || modifiers.getNumberOfChildren() == 0) {
if (visit == OPENING_VISIT) {
if (t.getNumberOfChildren() == 0 && parent.getType() != GroovyTokenTypes.PARAMETER_DEF) {
// no need for 'def' if in a parameter list
print(t, visit, "def");
}
}
if (visit == CLOSING_VISIT) {
if (parent.getType() == GroovyTokenTypes.VARIABLE_DEF || parent.getType() == GroovyTokenTypes.METHOD_DEF || parent.getType() == GroovyTokenTypes.ANNOTATION_FIELD_DEF || (parent.getType() == GroovyTokenTypes.PARAMETER_DEF && t.getNumberOfChildren() != 0)) {
print(t, visit, " ");
}
}
/*if (visit == CLOSING_VISIT) {
print(t,visit," ");
}*/
} else {
if (visit == CLOSING_VISIT) {
if (t.getNumberOfChildren() != 0) {
print(t, visit, " ");
}
}
}
}
Aggregations