RubyCademy
Published in

RubyCademy

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

Orders dispatcher

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

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 file looks like:

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

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

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

First, let’s have a look at the method

Here, we define a instance variable.

Then we assign a frozen hash to this instance variable.

In this hash we declare 2 entries

  • : an array that contains all the serializations of the command
  • : an array that contains all the serializations of the command

This hash is frozen because we wont add any other entries at this point.

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

The method

Let’s implement an class method

Here, the class method instantiates the class and calls the method on the freshly created instance.

The method is in charge of parsing and serializing the orders.

The OrdersDsl#parse method

This method reads the content of the file and evaluate it in the context of our instance.

This mean that the commands in the file will be evaluated as messages received by the evaluating scope – in our case, an instance of the class.

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

⚠️⚠️

The file that you “evaluate” must be trusted.

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

⚠️⚠️

Our instance must respond to these messages.

So we implement the and the instance methods — with a signature that matches against the commands in the file

These methods will simply serialize an order and push it into the right hash entry.

At this moment, our 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 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.

Voilà!

Thank you for your support! 😉

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store