public void visitStringLiteral(GroovySourceAST t,int visit) { if (visit == OPENING_VISIT) { String theString = escape(t.getText()); if (getParentNode().getType() != GroovyTokenTypes.LABELED_ARG && getParentNode().getType() != GroovyTokenTypes.STRING_CONSTRUCTOR) { theString = "\"" + theString + "\""; } print(t,visit,theString); } }
protected void print(GroovySourceAST t,int visit,String value) { if(visit == OPENING_VISIT) { printNewlineAndIndent(t, visit); } if (visit == CLOSING_VISIT) { printNewlineAndIndent(t, visit); } out.print(value); }
public void visitMapConstructor(GroovySourceAST t, int visit) { if (t.getNumberOfChildren() == 0) { print(t,visit,"[:]",null,null); } else { printUpdatingTabLevel(t,visit,"[",null,"]"); } }
public void visitElist(GroovySourceAST t,int visit) { if (getParentNode().getType() == GroovyTokenTypes.ENUM_CONSTANT_DEF) { print(t,visit,"(",", ",")"); } else { print(t,visit,null,", ",null); } }
public void visitParameters(GroovySourceAST t,int visit) { if (getParentNode().getType() == GroovyTokenTypes.CLOSABLE_BLOCK) { printUpdatingTabLevel(t,visit,null,","," "); } else { printUpdatingTabLevel(t,visit,"(",", ",") "); } }
Visitor visitor = new SourcePrinter(new PrintStream(baos), groovyTokenNames, withNewLines); AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
public void visitLiteralIf(GroovySourceAST t,int visit) { // slightly strange as subsequent visit is done after closing visit printUpdatingTabLevel(t,visit,"if ("," else ",") "); }
public void visitLiteralVolatile(GroovySourceAST t,int visit) { print(t,visit,"volatile ",null,null); } public void visitLiteralWhile(GroovySourceAST t,int visit) {
public void visitArrayDeclarator(GroovySourceAST t, int visit) { //<ARRAY_DECLARATOR>int</ARRAY_DECLARATOR> primes = new int(<ARRAY_DECLARATOR>5</ARRAY_DECLARATOR>) if (getParentNode().getType() == GroovyTokenTypes.TYPE || getParentNode().getType() == GroovyTokenTypes.TYPECAST) { // ugly hack // type definition, i.e. int[] x; print(t,visit,null,null,"[]"); } else { // usually in new, i.e. def y = new int[5]; print(t,visit,"[",null,"]"); } }
public void visitParameters(GroovySourceAST t,int visit) { if (getParentNode().getType() == GroovyTokenTypes.CLOSABLE_BLOCK) { printUpdatingTabLevel(t,visit,null,","," "); } else { printUpdatingTabLevel(t,visit,"(",", ",") "); } }
try { PrintStream out = new PrintStream(new FileOutputStream(sourceUnit.getName() + ".pretty.groovy")); Visitor visitor = new SourcePrinter(out, tokenNames); AntlrASTProcessor treewalker = new SourceCodeTraversal(visitor); treewalker.process(ast); List<VisitorAdapter> v = new ArrayList<VisitorAdapter>(); v.add(new NodeAsHTMLPrinter(out, tokenNames)); v.add(new SourcePrinter(out, tokenNames)); Visitor visitors = new CompositeVisitor(v); AntlrASTProcessor treewalker = new SourceCodeTraversal(visitors);
public void visitListConstructor(GroovySourceAST t, int visit) { printUpdatingTabLevel(t,visit,"[",null,"]"); }
public void visitStringLiteral(GroovySourceAST t,int visit) { if (visit == OPENING_VISIT) { String theString = escape(t.getText()); if (getParentNode().getType() != GroovyTokenTypes.LABELED_ARG && getParentNode().getType() != GroovyTokenTypes.STRING_CONSTRUCTOR) { theString = "\"" + theString + "\""; } print(t,visit,theString); } }
public void visitPlusAssign(GroovySourceAST t, int visit) { print(t,visit," += ",null,null); } public void visitPostDec(GroovySourceAST t, int visit) {
public void visitType(GroovySourceAST t,int visit) { GroovySourceAST parent = getParentNode(); GroovySourceAST modifiers = parent.childOfType(GroovyTokenTypes.MODIFIERS); if (t.getNumberOfChildren() == 0 && print(t,visit,"def"); (parent.getType() == GroovyTokenTypes.PARAMETER_DEF && t.getNumberOfChildren()!=0)) print(t,visit," "); if (visit == CLOSING_VISIT) { if (t.getNumberOfChildren() != 0) { print(t,visit," ");
public void visitMapConstructor(GroovySourceAST t, int visit) { if (t.getNumberOfChildren() == 0) { print(t,visit,"[:]",null,null); } else { printUpdatingTabLevel(t,visit,"[",null,"]"); } }
public void visitParameters(GroovySourceAST t,int visit) { if (getParentNode().getType() == GroovyTokenTypes.CLOSABLE_BLOCK) { printUpdatingTabLevel(t,visit,null,","," "); } else { printUpdatingTabLevel(t,visit,"(",", ",") "); } }
private void assertCompositeTransparency(String input) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); GroovyRecognizer parser; SourceBuffer sourceBuffer = new SourceBuffer(); UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(new StringReader(input), sourceBuffer); GroovyLexer lexer = new GroovyLexer(unicodeReader); unicodeReader.setLexer(lexer); parser = GroovyRecognizer.make(lexer); parser.setSourceBuffer(sourceBuffer); String[] tokenNames = parser.getTokenNames(); parser.compilationUnit(); AST ast = parser.getAST(); // determine direct result Visitor directVisitor = new SourcePrinter(new PrintStream(baos), tokenNames, false); AntlrASTProcessor traverser = new SourceCodeTraversal(directVisitor); traverser.process(ast); String directResult = new String(baos.toByteArray()); // determine composite result baos.reset(); List wrappedVisitors = new ArrayList(); wrappedVisitors.add(directVisitor); Visitor compositeVisitor = new CompositeVisitor(wrappedVisitors); traverser = new SourceCodeTraversal(compositeVisitor); traverser.process(ast); String compositeResult = new String(baos.toByteArray()); assertEquals(directResult, compositeResult); }
public void visitCtorCall(GroovySourceAST t,int visit) { printUpdatingTabLevel(t,visit,"this("," ",")"); }