This document discusses using Groovy to create domain-specific languages (DSLs). It defines external and internal DSLs and explains the goals of DSLs. It outlines tools in Groovy for DSLs, including the Groovy shell, compiler customizers, and AST transformations. The document provides examples of DSLs built with Groovy like Gradle, Grails, and Spock and demonstrates how to share data between a Groovy script and application using bindings.
2. Agenda
What are DSl
Types of DSL
Goals of a DSL
Groovy for Dsls
Groovy Shell and Binding
Compiler Customizers
Demo
3. What are DSL
A domain-specific language (DSL) is a computer language specialized to a
particular application domain.
Examples of domain-specific languages include HTML,Unix shell scripts,
spreadsheet formulas and macros, SQL for relational database queries,XSLT,
regular expressions
4. Types of DSL
External DSL is a language that's parsed independently of the host general
purpose language: good examples include regular expressions and CSS.
External DSLs have a strong tradition in the Unix community.
Internal DSLs are a particular form of API in a host general purpose language,
often referred to as a fluent interface.
5. Goals of a DSL
Use a more expressive language than a general-purpose one
Should be focussed towards one goal, one domain
It should not have too much of technical jargon because the target is a user
rather than machine as in case of general programming language
It should be able to embed other dsls
6. Why Groovy for Dsl
The syntax of language has less ceremony and is suited for designing
expressive syntax eg- optional parentheses, getters/setters
Groovy has support for operator overloading and metaprogramming at runtime
and compile time
Groovy has the capability to embed and execute scripts at runtime
IDE support
7. Tools in groovy for Dsl
Groovy Shell/Console
Compiler customizers
AST transformation
Builders
Type Checking Extensions
8. Tools that Use Groovy Dsl
Gradles
Grails
Spock Testing framework
Griffon
RatPack
9. Groovy Shell
Groovy Shell is a groovy Integration feature
The groovy.lang.GroovyShell class is the preferred way to evaluate scripts with
the ability to cache the resulting script instance.
Other mechanism in groovy are GroovyScriptEngine,GroovyClassLoader,Eval
10. Groovy Shell
Java provides its own integration feature with JSR-223 javax.script.* to hook in
multiple scripting languages
def shell = new GroovyShell(this.class.classLoader, binding)
shell.evaluate(new File("dsl_script_file"))
11. Binding in DSL
It is possible to share data between the application and the script using a
groovy.lang.Binding
def binding = new Binding([
robot: new Robot(),
h: new Duration(TimeUnit.hour)
])