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
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
<h2>Book on Domain-Specific Languages with Groovy</h2> <p>You can have a look at our <a class="confluence-link" href="/display/GROOVY/Books" data-linked-resource-id="73818" data-linked-resource-type="page" data-linked-resource-default-alias="Books" data-base-url="http://docs.codehaus.org">books section</a> listing Fergal Dearle's book titled "<a href="https://www.packtpub.com/groovy-for-domain-specific-languages-dsl/book">Groovy for Domain-Specific Languages</a>" published by Packt Publishing.</p> <h2>Tutorial on DSLs </h2> <p>Guillaume Laforge and Paul King presented a <a href="http://glaforge.appspot.com/article/groovy-domain-specific-languages-in-chicago">session at SpringOne2GX on the various techniques for authoring DSLs</a>, and integrating them in your applications.</p> <p>Guillaume Laforge and John Wilson presented a <a href="http://glaforge.free.fr/groovy/QCon-Tutorial-Groovy-DSL-2-colour.pdf">tutorial on Groovy DSLs</a> at the QCon 2007 conference in London.</p> <p>Matt Secoske presented a session on <a href="http://blog.secosoft.net/wp-content/oscon2007/groovy_dsl_oscon.ppt">Implementing DSLs in Groovy</a> at the OSCON 2007 conference.</p> <h2>Groovy features enabling DSLs </h2> <p>Groovy is particularly well suited for writing a DSL: <a href="http://en.wikipedia.org/wiki/Domain-specific_programming_language">Domain-Specific Language</a>. A DSL is a mini-language aiming at representing constructs for a given domain. Groovy provides various features to let you easily embed DSLs in your Groovy code:</p> <ul> <li>the <a class="confluence-link" href="/display/GROOVY/BuilderSupport" data-linked-resource-id="10594" data-linked-resource-type="page" data-linked-resource-default-alias="BuilderSupport" data-base-url="http://docs.codehaus.org">builder</a> concept lets you write tree structured languages</li> <li>you can add new methods and properties on arbitrary classes through <a class="confluence-link" href="/display/GROOVY/TMPGroovy+Categories" data-linked-resource-id="27920" data-linked-resource-type="page" data-linked-resource-default-alias="TMPGroovy Categories" data-base-url="http://docs.codehaus.org">categories</a> or custom metaclasses, even numbers: 3.euros, 5.days, etc, as shown in this <a href="http://groovy.dzone.com/news/domain-specific-language-unit%2d">article explaining how to write a unit handling mini-DSL</a></li> <li>most <a class="confluence-link" href="/display/GROOVY/Operator+Overloading" data-linked-resource-id="2764" data-linked-resource-type="page" data-linked-resource-default-alias="Operator Overloading" data-base-url="http://docs.codehaus.org">operators can be overloaded</a>: 5.days + 6.hours, myAccount += 400.euros</li> <li>passing maps to methods makes your code look like methods have named parameters: move( x: 500.meters, y: 1.kilometer )</li> <li>you can also create your own control structures by passing <a class="confluence-link" href="/display/GROOVY/Closures+-+Informal+Guide" data-linked-resource-id="2729" data-linked-resource-type="page" data-linked-resource-default-alias="Closures - Informal Guide" data-base-url="http://docs.codehaus.org">closures</a> as the last argument of a method call: ifOnce( condition )<br /> { ... }; inTransaction { ... }</li> <li>it is also possible to add dynamic methods or properties (methods or properties which don't really exist but that can be intercepted and acted upon) by implementing <a href="http://groovy.codehaus.org/apidocs/groovy/lang/GroovyObject.html">GroovyObject</a> or creating a custom <a href="http://groovy.codehaus.org/apidocs/groovy/lang/MetaClass.html">MetaClass</a></li> </ul> <p>Guillaume Laforge gave some <a href="http://glaforge.free.fr/weblog/index.php?itemid=187">thoughts and examples</a> on that topic on his blog. John Wilson has implemented a DSL in his <a href="http://docs.codehaus.org/display/GROOVY/Google+Data+support">Google Data Support</a> to make operations on dates easier.</p> <p>Joachim Baumann wrote an article showing how to <a href="http://joesgroovyblog.blogspot.com/2007/09/and-miles-to-go-before-i-sleep.html">implement a small DSL for measurement calculation</a>, which uses some of the techniques like adding properties to numbers, or overloading operators. Guillaume Laforge also wrote an article on <a href="http://groovy.dzone.com/news/domain-specific-language-unit-">representing units using the JScience library</a>.</p> <p>Andy Glover also plays with internal DSLs in Groovy by producing a <a href="http://thediscoblog.com/2007/11/05/internal-dsls-groovy-style/">behavior testing DSL</a>.</p> <p>Inspired by an article from Bruce Tate <a href="http://www-128.ibm.com/developerworks/java/library/j-cb03137/index.html">IBMs Alphaworks</a> a couple of samples were written in groovy.</p> <ul> <li><a href="http://www.bytemycode.com/snippets/snippet/639/">Chris vanBuskirk</a> uses arrays and maps to model the injection of the state transitions.</li> <li><a href="http://www.bytemycode.com/snippets/snippet/640/">Edward Sumerfield</a> uses closures to inject the state transitions.</li> </ul> <p>Inspired by <a href="http://rspec.rubyforge.org/">RSpec</a> (and also by random episodes of Family Guy) <a href="http://codeforfun.wordpress.com/2007/04/09/gspec-for-java-bdd/">an example</a> of a unit testing DSL using Groovy</p> <ul> <li><a href="http://codeforfun.wordpress.com">Clifton Craig</a> uses Builders in Groovy to create <a href="http://codeforfun.wordpress.com/gspec/">GSpec.</a></li> </ul> <h3>When Groovy's not enough </h3> <p>If you have the need to write your own language completely, consider using a compiler compiler. There are many to choose from, e.g. Antlr, JavaCC, SableCC, Coco/R, Cup/JLex/JFl;ex, BYacc/J, Beaver, etc. See <a href="http://en.wikipedia.org/wiki/List_of_compiler-compilers">wikipedia</a> for an interesting list. Some of these can even benefit from Groovy. Here is a groovy example for <a href="http://jparsec.codehaus.org/Groovy+Parser">JParsec</a>.</p>
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