Over a million developers have joined DZone.

Intro to DSLs in Groovy, Part 1

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Here in a couple of weeks I will be presenting at the Dallas/Ft. Worth Groovy User Group meeting on an introduction to DSLs, or Domain Specific Languages, in Groovy. What is particularly fun about this is how wonderfully easy Groovy makes this task. In this post I am basically giving a sneak peak into my presentation. Aren’t I nice? :)

What is a DSL, and why do you care? The simple version is a DSL is a small, focused language designed to make the description and execution of some task in a specific domain easier. In fact we all use DSLs every day and completely take for granted that we do. SQL, HTML, CSS, an Ant XML file… all DSLs. All designed to do something specific for the domain they are related to.

Here in Part 1 of this series I will show what our “language” will look like. This little “language” will allow us to easily define a food recipe. Once defined we can easily tell this code to display the recipe card, or even convert the recipe to XML. Here is what the “language” will look like, as well as the rendered XML output.

Recipe.create "Creamy Mac n' Cheese", { 
addIngredient 1.box, "Mac n' Cheese"
addIngredient 1.pound, "Hamburger meat"
addIngredient 1.can, "Cream of Mushroom"

addInstructions "Brown hamburger meat",
"Bring mac n' cheese to a boil",
"Drain water from noodles",
"Stir in cheese mixture",
"Add cream of mushroom and meat",

show RecipeXml
<recipe name='Creamy Mac n' Cheese'>
<ingredient><![CDATA[1 box of Mac n' Cheese]]></ingredient>
<ingredient><![CDATA[1 pound of Hamburger meat]]></ingredient>
<ingredient><![CDATA[1 can of Cream of Mushroom]]></ingredient>
<instruction><![CDATA[Brown hamburger meat]]></instruction>
<instruction><![CDATA[Bring mac n' cheese to a boil]]></instruction>
<instruction><![CDATA[Drain water from noodles]]></instruction>
<instruction><![CDATA[Stir in cheese mixture]]></instruction>
<instruction><![CDATA[Add cream of mushroom and meat]]></instruction>

Upon first glance it should be pretty apparent what we are trying to do. We first define a recipe with a name. Then we start adding ingredients, each with a specific measurement. Notice each measurement is different, and quite versatile. From here we add a list of instructions, and finally display the results in XML.

But how do we define such a language? Historically one might find themselves investigating compiler compilers, writing nasty BNF language descriptions, and maybe even writing parsers by hand. You might even start Googling Recursive-Descent Parsers and RPN expression parsing notation. Luckily, with the Groovy language, you don’t have to worry about that too much.

Tune in next time for how we will get started defining this language.

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.


Published at DZone with permission of Adam Presley. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}