Search in sources :

Example 1 with NodePrinter

use of org.codehaus.groovy.antlr.treewalker.NodePrinter in project groovy by apache.

the class Java2GroovyProcessor method nodePrinter.

public static String nodePrinter(String input) throws Exception {
    JavaRecognizer parser = getJavaParser(input);
    String[] tokenNames = parser.getTokenNames();
    parser.compilationUnit();
    AST ast = parser.getAST();
    // modify the Java AST into a Groovy AST
    modifyJavaASTintoGroovyAST(tokenNames, ast);
    String[] groovyTokenNames = getGroovyTokenNames(input);
    // groovify the fat Java-Like Groovy AST
    groovifyFatJavaLikeGroovyAST(ast, groovyTokenNames);
    // now output
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Visitor visitor = new NodePrinter(new PrintStream(baos), groovyTokenNames);
    AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
    traverser.process(ast);
    return new String(baos.toByteArray());
}
Also used : PrintStream(java.io.PrintStream) AST(antlr.collections.AST) Visitor(org.codehaus.groovy.antlr.treewalker.Visitor) NodePrinter(org.codehaus.groovy.antlr.treewalker.NodePrinter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AntlrASTProcessor(org.codehaus.groovy.antlr.AntlrASTProcessor) SourceCodeTraversal(org.codehaus.groovy.antlr.treewalker.SourceCodeTraversal)

Example 2 with NodePrinter

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

the class Java2GroovyProcessor method nodePrinter.

public static String nodePrinter(String input) throws Exception {
    JavaRecognizer parser = getJavaParser(input);
    String[] tokenNames = parser.getTokenNames();
    parser.compilationUnit();
    AST ast = parser.getAST();
    // modify the Java AST into a Groovy AST
    modifyJavaASTintoGroovyAST(tokenNames, ast);
    String[] groovyTokenNames = getGroovyTokenNames(input);
    // groovify the fat Java-Like Groovy AST
    groovifyFatJavaLikeGroovyAST(ast, groovyTokenNames);
    // now output
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Visitor visitor = new NodePrinter(new PrintStream(baos), groovyTokenNames);
    AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
    traverser.process(ast);
    return new String(baos.toByteArray());
}
Also used : PrintStream(java.io.PrintStream) AST(antlr.collections.AST) Visitor(org.codehaus.groovy.antlr.treewalker.Visitor) NodePrinter(org.codehaus.groovy.antlr.treewalker.NodePrinter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AntlrASTProcessor(org.codehaus.groovy.antlr.AntlrASTProcessor) SourceCodeTraversal(org.codehaus.groovy.antlr.treewalker.SourceCodeTraversal)

Aggregations

AST (antlr.collections.AST)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintStream (java.io.PrintStream)2 AntlrASTProcessor (org.codehaus.groovy.antlr.AntlrASTProcessor)2 NodePrinter (org.codehaus.groovy.antlr.treewalker.NodePrinter)2 SourceCodeTraversal (org.codehaus.groovy.antlr.treewalker.SourceCodeTraversal)2 Visitor (org.codehaus.groovy.antlr.treewalker.Visitor)2