Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<h1>How Groovy works behind the scenes</h1> <p>With your Groovy scripts, you have multiple options on how to use them:</p> <ul> <li>run groovyc on them to create Java *.class files</li> <li>evaluate the script at runtime (either from a file or from a Java String)</li> <li>make your script available as Java Class-objects through the GroovyClassLoader (no *.class files generated!)</li> </ul> <p><em>No *.java source files are ever generated.</em></p> <p>All the above may seem like magic and it somehow is: the magic of imaginative, mindful software engineering.</p> <h2>Everything starts with the Groovy grammar.</h2> <p><em>There is the notion of the 'new' and the 'old' parser. Only the new one (as of Feb.05) is described here.</em></p> <p>The Groovy grammar is written in terms of an ANTLR (= <em>ANother Tool for Language Recognition</em>) grammar. The tool can handle grammars of type LL(k), where the Java grammar is of type LL(2) and Groovy is of type LL(3).</p> <p>The difference is in the number of tokens that the parser needs to look ahead for recognizing e.g. "==" (2 tokens for Java) or "===" (3 tokens for Groovy). To be more correct, the problem is in recognizing the first "=" character. The parser needs to "look ahead" to derive its meaning.</p> <p>ANTLR formulates the grammar in terms of "rules" that fully implement EBNF (Extended Backus-Naur Form) enriched with Java code blocks, special functions and some other things.</p> <p>With the recognition of a rule, actions can be triggered that come as usual Java code. ANTLR dumps out Javacode representing a parser capable to recognize the given grammar. And this parser executes the embedded code blocks from the grammar - the "action".</p> <h2>Parser Generation and AST</h2> <p>ANTLR takes the Groovy grammar file "Groovy.g" to create the Groovy parser. When the parser is fed with the source code of a Groovy script, it produces the AST (= <em>Abstract Syntax Tree</em>) that represents that code as a run-time structure.</p> <h2>Byte Code Generation</h2> <p>From the AST, it is possible to create Java Byte Code: either for making it persistent as *.class files or for making it directly available as Class objects through the GroovyClassLoader.</p> <p>This ClassGeneration is done with the help of objectweb's ASM tool. (The ASM name does not mean anything: it is just a reference to the "asm" keyword in C, which allows some functions to be implemented in assembly language.)</p> <p>ASM provides a Java API to construct or modify Byte Code on a given AST.</p> <p>The API for bytecode generation heavily relies on the Visitor Pattern. The main entry point for the class generation is org.org.codehaus.groovy.classgen.AsmClassGenerator.java.</p> <p>It is a large class. There are visitXYExpression methods called when converting the AST to bytecode. For example visitArrayExpression is called when creating arrays in bytecode.</p> <h2>More Links on the topic</h2> <p><a class="confluence-link" href="/display/GROOVY/Groovy+Method+Invokation" data-linked-resource-id="26770" data-linked-resource-type="page" data-linked-resource-default-alias="Groovy Method Invokation" data-base-url="http://docs.codehaus.org">Groovy Method Invokation</a></p> <p>Tools:</p> <ul> <li><a href="http://www.antlr.org/">http://www.antlr.org/</a></li> <li><a href="http://asm.objectweb.org/">http://asm.objectweb.org/</a></li> </ul> <p>General:</p> <ul> <li><a href="http://compilers.iecc.com/comparch/article/99-02-109">http://compilers.iecc.com/comparch/article/99-02-109</a></li> <li><a href="http://en.wikipedia.org/wiki/Category:Parsing_algorithms">http://en.wikipedia.org/wiki/Category:Parsing_algorithms</a></li> <li><a href="http://lambda.uta.edu/cse5317/spring02/notes/notes.html">http://lambda.uta.edu/cse5317/spring02/notes/notes.html</a></li> </ul>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced