Parsing a DSL in Ruby in less than 30 LoC

Tech - RubyCademy
RubyCademy
3 min readApr 15, 2019

--

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 Orders file.

Orders dispatcher

The goal here is to be able to parse and serialize the content of the Orders file.

Then this serialization can be passed to our engine which is in charge of controlling the execution flow and processing the commands.

So let’s see how our Orders file looks like this:

Here we can see that our DSL is composed of 2 commands: order_now and schedule.

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 OrdersDsl class.

So, let’s detail step-by-step the content of this class.

First, let’s have a look at the OrdersDsl#initialize method

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 order_now command
  • to_schedule: an array that contains all the serializations of the schedule command

This hash is frozen because we won't add any other entries at this point.

Now, let’s see how to run our DSL parser.

The OrdersDsl.run method

Let’s implement an OrdersDsl.run class method

Here, the OrdersDsl.run class method instantiates the OrdersDsl class and calls the OrdersDsl#parse method on the freshly created instance.

The #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 means that the commands in the Orders file will be evaluated as messages received by the evaluating scope – in our case, an instance of the OrdersDsl class.

Feel free to have a look to the Private & Protected in Ruby article if you are not familiar with the notion of message in Ruby.

⚠️⚠️

The file that you “evaluate” must be trusted.

Indeed, using eval or instance_eval in an unsafe context can lead to some serious vulnerabilities.

⚠️⚠️

Our instance must respond to these messages.

So let’s implement the OrdersDsl#order_now and the OrdersDsl#schedule instance methods — with a signature that matches against the commands in the Orders file

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 on, we just have to implement the logic that consumes and processes this serialized information.

Note that we could have used a method_missing hook method, to orchestrate the serialization and storage of each order without having to define a method per command of our DSL.

Conclusion

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.

Ruby Mastery

We’re currently finalizing our first online course: Ruby Mastery.

Join the list for an exclusive release alert! 🔔

🔗 Ruby Mastery by RubyCademy

Also, you can follow us on x.com as we’re very active on this platform. Indeed, we post elaborate code examples every day.

💚

--

--