Parsing a DSL in Ruby in less than 30 LoC
In this article we’re going to see how to parse and serialize a DSL in less than 30 lines of codes.
To do so, we are going to parse the Retaurant Order DSL through the
The goal here is to be able to parse and serialize the content of the
Then this serialization can be passed to our engine that is in charge of controlling the execution flow and processing the commands.
So let’s see how our
Orders file looks like:
Here we can see that our DSL is composed of 2 commands:
The first one indicates that the order must be prepared immediately.
The second one indicates that the order preparation is scheduled at a given time.
The OrdersDsl class
All the parsing and serialization logic is defined in the
So, let’s detail step-by-step the content of this class.
First, let’s have a look at the
Here, we define a
@commands instance variable.
Then we assign a frozen hash to this instance variable.
In this hash we declare 2 entries
to_perform: an array that contains all the serializations of the
to_schedule: an array that contains all the serializations of the
This hash is frozen because we wont add any other entries at this point.
Now, let’s see how to run our DSL parser.
Let’s implement an
OrdersDsl.run class method
OrdersDsl.run class method instantiates the
OrdersDsl class and calls the
OrdersDsl#parse method on the freshly created instance.
#parse method is in charge of parsing and serializing the orders.
The OrdersDsl#parse method
This method reads the content of the
Orders file and evaluate it in the context of our instance.
This mean that the commands in the
Orders file will be evaluated as messages received by the evaluating scope – in our case, an instance of the
Feel free to have a look to the
Private & Protected in Rubyarticle if you are not familiar with the notion of message in Ruby.
The file that you “evaluate” must be trusted.
instance_evalin an unsafe context can lead to some serious vulnerabilities.
Our instance must respond to these messages.
So we implement the
OrdersDsl#order_now and the
OrdersDsl#schedule instance methods — with a signature that matches against the commands in the
These methods will simply serialize an order and push it into the right
@orders hash entry.
At this moment, our
Orders file has been parsed and the orders have been serialized.
From now, we just have to implement the logic that consume and process these serialized informations.
Note that we could have used a
method_missinghook method, to orchestrate the serialization and storage of each order without having to define a method per command of our DSL.
Parsing and serializing the commands of a given DSL is helpful when you want to control the execution flow of the commands — running a specific command before the others whatever their order of appearance in the DSL file.
These 2 operations can be easily and elegantly accomplished in Ruby thanks to the builtins provided by the language.
ONE MORE THING ⬇
Feel free to subscribe here: www.rubycademy.com
Thank you for taking the time to read this post :-)
Feel free to 👏 and share this article if it has been useful for you. 🚀
Also, as I post fews articles per month, feel free to follow me to be notified of my new article releases.
Here is a link to my last article: