Download presentation
Presentation is loading. Please wait.
Published byJerry Seymour Modified over 9 years ago
1
Developing Domain-Specific Languages for the JVM Travis Dazell Systems Architect Digi-Key Corporation
2
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
3
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
4
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
5
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
6
Java Example with the Builder Pattern Live Coding Demo
7
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
8
Let’s Make the DSL Groovy Live Coding Demo
9
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
10
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
11
ANTLR Live Coding Demo
12
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
13
Scala Parser Combinators Live Coding Demo
14
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
15
JetBrains MPS Live Coding Demo
16
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
17
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
18
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
19
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
20
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
21
Miscellaneous Contact Information: travis.dazell@digikey.com Code examples are available on GitHub: https://github.com/travisdazell Follow me: Blog: travisdazell.blogspot.com Twitter: @travisdazell
22
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.