[prev in list] [next in list] [prev in thread] [next in thread]
List: groovy-dev
Subject: Re: [groovy-dev] Snippet parsing
From: "Alex Shneyderman" <a.shneyderman () gmail ! com>
Date: 2006-11-22 6:07:46
Message-ID: 693a69ba0611212207y1d7c9e3fu2ec836b3f5068f38 () mail ! gmail ! com
[Download RAW message or body]
yep. Here is how it works:
import org.codehaus.groovy.antlr.GroovySourceAST;
import org.codehaus.groovy.antlr.UnicodeEscapingReader;
import org.codehaus.groovy.antlr.parser.GroovyLexer;
import org.codehaus.groovy.antlr.parser.GroovyRecognizer;
GroovyRecognizer parser = null;
UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(new
StringReader ("this.setText(\"my text\")"), null);
GroovyLexer lexer = new GroovyLexer(unicodeReader);
unicodeReader.setLexer(lexer);
parser = GroovyRecognizer.make(lexer);
parser.compilationUnit();
GroovySourceAST ast = (GroovySourceAST) parser.getAST();
ast is the AST you could traverse. It is an ANTLR ast node so it will
look like this:
root
|
first child - second child - .... - last child
There is also an API that allows you to exploit different phases of
compilation. Or run compiler up to a phase (and there are quite a
few). Here is a little snippet that will show how that works:
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.ImportNode;
import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.control.CompilationUnit;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.ErrorCollector;
import org.codehaus.groovy.control.Phases;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.control.messages.WarningMessage;
CompilerConfiguration compConfig;
private List units;
compConfig = CompilerConfiguration.DEFAULT;
compConfig.setOutput(new PrintWriter(System.err));
compConfig.setWarningLevel(WarningMessage.PARANOIA);
compConfig.setTolerance(100);
CompilationUnit cu = new CompilationUnit(_compConfig);
ErrorCollector eCollector = new ErrorCollector (_compConfig);
File [] files = (File []) units.toArray(new File[0]);
for (int i = 0; i < files.length; i++) {
cu.addSource(new SourceUnit (files[i], _compConfig,
cu.getClassLoader(), eCollector));
}
cu.compile( Phases.SEMANTIC_ANALYSIS );
for (Iterator iter = cu.getAST().getModules().iterator(); iter.hasNext();) {
ModuleNode mNode = (ModuleNode) iter.next();
SourceUnit src = mNode.getContext();
List imps = mNode.getImports();
for (Iterator iterator = imps.iterator(); iterator.hasNext();) {
ImportNode in = (ImportNode) iterator.next();
System.out.println(in.getClassName());
}
List impkgs = mNode.getImportPackages();
for (Iterator iterator = impkgs.iterator(); iterator.hasNext();) {
System.out.println(iterator.next());
}
}
This second API basically hides the call to the first and presents
your AST nodes that are already understood theese nodes are
descendants of ASTNode.
HTH,
Alex.
On 11/21/06, Scott Catherine <05013903@napier.ac.uk> wrote:
>
>
>
> There is a Java parser that can process code snippets such as
>
> this.setText("my text");
>
> so that you can get the ASTNode for this isolated code.
>
> Is there a similar facility in Groovy?
>
> Thanks,
> Cathy
>
>
> This message is intended for the addressee(s) only and should not be read,
> copied or disclosed to anyone else outwith the University without the
> permission of the sender.
> It is your responsibility to ensure that this message and any attachments
> are scanned for viruses or other defects. Napier University does not accept
> liability for any loss
> or damage which may result from this email or any attachment, or for errors
> or omissions arising after it was sent. Email is not a secure medium. Email
> entering the
> University's system is subject to routine monitoring and filtering by the
> University.
--
Thanks,
Alex.
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic