Groovy DSL

NexThoughts 380 views 13 slides Jun 01, 2016
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

A domain-specific language (DSL) is a computer language specialized to a particular application domain.


Slide Content

Dsl In Groovy

Agenda What are DSl Types of DSL Goals of a DSL Groovy for Dsls Groovy Shell and Binding Compiler Customizers Demo

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

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 .

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

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

Tools in groovy for Dsl Groovy Shell/Console Compiler customizers AST transformation Builders Type Checking Extensions

Tools that Use Groovy Dsl Gradles Grails Spock Testing framework Griffon RatPack

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

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" ))

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 ) ])

Demo https://github.com/NexThoughts/GroovyDSL

Reference http://martinfowler.com/books/dsl.html http://groovy.jmiguel.eu/groovy.codehaus.org/Advanced+compiler+configuration.html http://www.slideshare.net/glaforge/groovy-dsls-from-beginner-to-expert-guillaume-laforge-and-paul-king-springone2gx-2011 https://www.youtube.com/watch?v=rk9ZR2EkE3g https://www.youtube.com/watch?v=nqqGtdvPzus