private String getChildTextFromSource(GroovySourceAST child) { return sourceBuffer.getSnippet( new LineColumn(child.getLine(), child.getColumn()), new LineColumn(child.getLineLast(), child.getColumnLast())); }
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; }
l.add(new LineColumn(t.getLine(),t.getColumn())); if (t.getLineLast() == 0) { int nextLine = lc.getLine(); int nextColumn = lc.getColumn(); if (nextLine < t.getLine() || (nextLine == t.getLine() && nextColumn < t.getColumn())) { nextLine = t.getLine(); nextColumn = t.getColumn(); t.setLineLast(nextLine); t.setColumnLast(nextColumn); GroovySourceAST child = (GroovySourceAST)t.getFirstChild(); if (child != null) { traverse(child,l,itr); t = (GroovySourceAST)t.getNextSibling();
public void visitClassDef(GroovySourceAST t,int visit) { if (visit == OPENING_VISIT) { currentClassName = t.childOfType(GroovyTokenTypes.IDENT).getText(); } } public void visitDefault(GroovySourceAST t,int visit) {
private String getDefaultValue(GroovySourceAST t) { GroovySourceAST child = (GroovySourceAST) t.getFirstChild(); if (t.getNumberOfChildren() != 4) return null; for (int i = 1; i < t.getNumberOfChildren(); i++) { child = (GroovySourceAST) child.getNextSibling(); } GroovySourceAST nodeToProcess = child; if (child.getType() != ANNOTATION_ARRAY_INIT && child.getNumberOfChildren() > 0) { nodeToProcess = (GroovySourceAST) child.getFirstChild(); } return getChildTextFromSource(nodeToProcess, ";"); }
private String getFolded(GroovySourceAST t) { if (depth > 2 && t.getNumberOfChildren() > 0) { switch (t.getType()) { case GroovyTokenTypes.EXPR : case GroovyTokenTypes.METHOD_DEF : case GroovyTokenTypes.VARIABLE_DEF : return " FOLDED='true'"; } } if (t.getType() == GroovyTokenTypes.IMPORT) { return " FOLDED='true'"; } return ""; }
private void addParametersTo(GroovySourceAST t, SimpleGroovyExecutableMemberDoc executableMemberDoc) { // parameters GroovySourceAST parametersNode = t.childOfType(PARAMETERS); if (parametersNode != null && parametersNode.getNumberOfChildren() > 0) { GroovySourceAST currentNode = (GroovySourceAST) parametersNode.getFirstChild(); while (currentNode != null) { String parameterTypeName = getTypeOrDefault(currentNode); String parameterName = getText(currentNode.childOfType(IDENT)); SimpleGroovyParameter parameter = new SimpleGroovyParameter(parameterName); parameter.setVararg(currentNode.getType() == VARIABLE_PARAMETER_DEF); parameter.setTypeName(parameterTypeName); GroovySourceAST modifiers = currentNode.childOfType(MODIFIERS); if (modifiers != null) { List<GroovySourceAST> annotations = modifiers.childrenOfType(ANNOTATION); for (GroovySourceAST a : annotations) { addAnnotationRef(parameter, a); } } executableMemberDoc.add(parameter); if (currentNode.getNumberOfChildren() == 4) { handleDefaultValue(currentNode, parameter); } currentNode = (GroovySourceAST) currentNode.getNextSibling(); } } }
public void initialize(AST ast) { super.initialize(ast); line = ast.getLine(); col = ast.getColumn(); if (ast instanceof GroovySourceAST) { GroovySourceAST node = (GroovySourceAST)ast; lineLast = node.getLineLast(); colLast = node.getColumnLast(); } }
private String buildName(GroovySourceAST t) { if (t != null) { if (t.getType() == DOT) { GroovySourceAST firstChild = (GroovySourceAST) t.getFirstChild(); GroovySourceAST secondChild = (GroovySourceAST) firstChild.getNextSibling(); return (buildName(firstChild) + "/" + buildName(secondChild)); } if (t.getType() == IDENT) { return t.getText(); } } return ""; }
public void visitDefault(GroovySourceAST t,int visit) { if (visit == OPENING_VISIT) { // only want to do this once per node... t.setType(typeMapping[t.getType()]); // ---- // need to remove double quotes in string literals // as groovy AST doesn't expect to have them if (t.getType() == GroovyTokenTypes.STRING_LITERAL) { String text = t.getText(); if (isSingleQuoted(text) || isDoubleQuoted(text)) { t.setText(text.substring(1, text.length() - 1)); // chop off the single quotes at start and end } } } }
private void handleDefaultValue(GroovySourceAST currentNode, SimpleGroovyParameter parameter) { GroovySourceAST paramPart = (GroovySourceAST) currentNode.getFirstChild(); for (int i = 1; i < currentNode.getNumberOfChildren(); i++) { paramPart = (GroovySourceAST) paramPart.getNextSibling(); } GroovySourceAST nodeToProcess = paramPart; if (paramPart.getNumberOfChildren() > 0) { nodeToProcess = (GroovySourceAST) paramPart.getFirstChild(); } // hack warning! // TODO handle , and ) when they occur within Strings parameter.setDefaultValue(getChildTextFromSource(nodeToProcess, ",)")); }
private String getIdentPlusTypeArgsFor(GroovySourceAST gpn) { GroovySourceAST groovySourceAST = gpn.childOfType(IDENT); StringBuilder ident = new StringBuilder(); ident.append(groovySourceAST.getText()); GroovySourceAST typeParams = (GroovySourceAST) groovySourceAST.getNextSibling(); getTypeParameters(typeParams, ident, "def"); return ident.toString(); } }
public void visitType(GroovySourceAST t,int visit) { GroovySourceAST parent = getParentNode(); GroovySourceAST modifiers = parent.childOfType(GroovyTokenTypes.MODIFIERS); if (modifiers == null || modifiers.getNumberOfChildren() == 0) { if (t.getNumberOfChildren() == 0 && parent.getType() != GroovyTokenTypes.PARAMETER_DEF) { // no need for 'def' if in a parameter list print(t,visit,"def"); 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)) if (t.getNumberOfChildren() != 0) { print(t,visit," ");
/** * 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); } }
private boolean isMajorType(GroovySourceAST t) { if (t == null) return false; int tt = t.getType(); return tt == CLASS_DEF || tt == TRAIT_DEF || tt == INTERFACE_DEF || tt == METHOD_DEF || tt == ANNOTATION_DEF || tt == ENUM_DEF || tt == VARIABLE_DEF || tt == ANNOTATION_FIELD_DEF || tt == ENUM_CONSTANT_DEF || tt == CTOR_IDENT; }
protected void accept_FirstChild_v_RestOfTheChildren_v_LastChild(GroovySourceAST t) { int count = 0; accept(t.childAt(0)); count++; openingVisit(t); if (t.childAt(0) != null) { GroovySourceAST sibling = (GroovySourceAST) t.childAt(0).getNextSibling(); while (sibling != null) { if (count == t.getNumberOfChildren() - 1) { closingVisit(t); } accept(sibling); count++; sibling = (GroovySourceAST) sibling.getNextSibling(); } } }
switch (t.getType()) { case GroovyTokenTypes.QUESTION: // expr?foo:bar accept_FirstChild_v_SecondChild_v_ThirdChild_v(t); case GroovyTokenTypes.STAR_ASSIGN: // a *= b case GroovyTokenTypes.STAR_STAR_ASSIGN: // x **= 3 if (t.childAt(1) != null) { accept_FirstChild_v_RestOfTheChildren(t); } else { if (t.getNumberOfChildren() == 2 && t.childAt(1) != null && t.childAt(1).getType() == GroovyTokenTypes.CLOSABLE_BLOCK) { GroovySourceAST lastChild = t.childAt(t.getNumberOfChildren() -1); if (lastChild != null && lastChild.getType() == GroovyTokenTypes.CLOSABLE_BLOCK) { if (t.childAt(0) != null && t.childAt(0).getType() == GroovyTokenTypes.IMPLICIT_PARAMETERS) { accept_v_AllChildren_v(t); } else {
private String getText(GroovySourceAST node) { String returnValue = null; if (node != null) { returnValue = node.getText(); } return returnValue; }
protected void printNewlineAndIndent(GroovySourceAST t, int visit) { int currentLine = t.getLine(); if (lastLinePrinted == 0) { lastLinePrinted = currentLine; } if (lastLinePrinted != currentLine) { if (newLines) { if (!(visit == OPENING_VISIT && t.getType() == GroovyTokenTypes.SLIST)) { for (int i=lastLinePrinted;i<currentLine;i++) { out.println(); } if (lastLinePrinted > currentLine) { out.println(); lastLinePrinted = currentLine; } if (visit == OPENING_VISIT || (visit == CLOSING_VISIT && lastLinePrinted > currentLine)) { for (int i=0;i<tabLevel;i++) { out.print(" "); } } } } lastLinePrinted = Math.max(currentLine,lastLinePrinted); } }