Thursday, January 9, 2014

Scala External DSL's -- a 12 Step Program

It's been months since I've posted.  I've been working on various Scala projects and sharpening my skills.  I had a major breakthrough on crafting Domain-Specific Languages (DSL's).  The secret sauce was learning Scala Parser Combinators.  Parser Combinators make it easy to create lexical rules that tie to mini-parsers to build objects from the bottom up.  In addition to the basic JSON parser from "Programming in Scala" (published by Artima), I've created two useful DSL's.  The first was a DSL to configure a tic-tac-toe playing AI, and the second was a DSL for configuring badges for gamifying applications.

The JSON example from "Programming in Scala" does not demonstrate test-driving a DSL, so I created a project on Github to run through the JSON example as a code kata.  You can access this example here:  https://github.com/tflander/scalaJsonDsl

The first time you go through the kata, it's highly recommended that you either refer to the example in the book, or cheat by looking at the solutions.  You can't really learn parser combinators by guessing.  You either know how to use them or you don't.  Fortunately, they are pretty easy once they click in your head.  They are much easier than JavaCC or JParsec.
Note this project has branches step0 through stepN. These branches form a step-by-step kata for test driving a scala-json parser.
To work through this project as a code kata, check out step0. Build and run tests. Fix the broken test, then check out the next step for the next challenge. Repeat until you've build the entire JSON parser for adding JSON as a DSL.
The project has the eclipse plug-in configured. If you want to import your project into eclipse, do a 'sbt eclipse' to generate the eclipse project files for import. You can also use typesafe activator instead of sbt.
Enjoy!
adopted from "Programming in Scala: A comprehensive step-by-step guide", 2nd edition Martin Odersky, Lex Spoon, Bill Venners. published by Artima