developing domain-specific languages for the jvm travis dazell systems architect digi-key...
TRANSCRIPT
Developing Domain-Specific Languages for the JVM
Travis Dazell
Systems Architect
Digi-Key Corporation
Outline• What is a Domain Specific Language (DSL)?• Internal Versus External DSLs• Internal DSLs
• Java Example without a DSL• Improved Java Example using Internal DSL Patterns• Groovy for Internal DSLs
• External DSLs• Writing a new language to solve our example problem
• ANTLR• JetBrains MPS• Scala Parser Combinators
What is a DSL?• DSLs have been around for a long time
• SQL, CSS, Regular Expressions
• DSLs are small languages, focused on a specific problem
• DSLs are easy to use and understand, even for a non-programmer• Often accomplished through layers of abstraction
• The DSL language relates directly to the problem domain
Example Problem: Golf Course Scheduler• To demonstrate how DSLs can be used, we’ll look at
writing a simple language for scheduling tee times at a golf course
Our Goal• Before
TeeTime teeTime = new TeeTime();
teeTime.setTime(“Sep-23-2013 2:15 PM”);
Golfer golfer = new Golfer();
golfer.setFirstName(“Travis”);
golfer.setLastName(“Dazell”);
golfer.setNumberOfGuests(3);
teeTime.setGolfer(golfer);
TeeTimeScheduler.schedule(teeTime);
• After (as a DSL)
new tee time at Sep-23-2013 2:15 PM for Travis Dazell and 3 guests
Can We Make the Java DSL Even Better?• An internal Java DSL is limited by the inherent language
constraints• Semi-colons are required• Parentheses for invoking methods are not optional• Dots cannot be avoided when dispatching methods• We often need to build a significant amount of indirection to
achieve an expressive DSL• Example: Builder Pattern
Solving the Problem with a Groovy DSL• Optional semicolons• We can omit parentheses in many cases• Closures• methodMissing• Metaprogramming• DSL Descriptors for syntax highlighting and help hovers
Using External DSLs• What happens when we need to step outside of a host
language?• External DSLs are custom languages• We write a grammar to define the syntax for our
language• We need to write a lexer and parser• We may need to perform semantic analysis• We need to write an interpreter or code generator• Thankfully, there are tools that make this easier
ANTLR Review• We define our EBNF grammar• We can mix Java code into our grammar for processing
input scripts• Note that ASTs can be constructed for more complex
processing
• ANTLR will generate the lexer and parser for us
Scala Parser Combinators Review
• You have the privilege of working entirely within Scala• You define parsing rules in an EBNF-like format• You combine each of your parsing rules to form your DSL
syntax• Like the other examples, you can interpret the input and
generate whatever output you need
JetBrains MPS Review• Takes DSL design to a more abstract level• You work on the concepts and structure of your DSL,
instead of the low-level grammar• You do very little, if any, traditional coding in the IDE• You can generate all kinds of output. We auto-generated
Java code in this example• You can test your DSLs within the MPS IDE or export
your generated Java code to another IDE for integration
Applying DSLs in Your Projects• Adapt legacy code
• Wrapper APIs
• Develop a DSL for a particular sub-system (i.e. domain) of your application
• Flesh-out requirements and test cases• Using a DSL to code your business rules can help bridge the gap
from requirements to implementation• You can show DSL source code to a business analyst and they’ll
understand it completely
• Enhancing custom IT tasks• Operators, System Administrators
References• References
• Debasish Ghosh, DSLs In Action, Manning, 2011• Fergal Dearle, Groovy for Domain-Specific Languages,
PACKT, 2010• Cay S. Horstmann, Scala for the Impatient, Addison-
Wesley, 2012• Martin Fowler, Domain-Specific Languages, 2010• Markus Voelter, DSL Engineering: Designing, Implementing,
and Using Domain-Specific Languages, 2013
Related JavaOne Sessions• Attend these JavaOne sessions to learn more
• Embedded DSL: Groovy and Scala Fair Duel• Monday 4:30 PM – 6:30 PM, Hilton Continental Ballroom 1/2/3
• BOF2893: Scala Parser Combinators• Tuesday 7:30 PM – 8:15 PM, Hilton Yosemite B/C
• CON5389: Groovy DSLs: Beginner to Expert• Wednesday 11:30 AM-12:30 PM, Hilton Golden Gate 4/5
• CON2077: Integrating JVM Languages• Thursday 12:30 PM – 1:30 PM, Hilton Yosemite B/C
Conclusion• DSLs allow you to develop software using syntax that fits
within the problem domain• Internal DSLs can be written in many JVM languages
• Other JVM languages are also well-suited for internal DSL development, such as JRuby, Clojure, and Scala
• External DSLs give you the flexibility to design the language any way you want• More involved, but thanks to tools like ANTLR, JetBrains
MPS, and Scala Parser Combinators, this isn’t difficult
Miscellaneous• Contact Information:
• Code examples are available on GitHub:• https://github.com/travisdazell
• Follow me:• Blog: travisdazell.blogspot.com• Twitter: @travisdazell