A parser for Ordeal::Model


Where I look back at a parser I wrote some time ago.

In previous post Global string matching quirks I anticipated that I would be looking into Ordeal::Model::Parser. Here we come.

NOTE: I apologize with the fine folks at Perl Weekly for all the sea of boredom that they endure in looking through my posts. Sorry 😅

A parser?

Ordeal::Model allows you to build moderately complex expressions to describe what kind of random draw you need. For example, the following expression:

die@1 * {3,4..5,6}

will roll a random amount of dice (between 3 and 6 dice) and give you the result, while the following:

(food + games)@2

will make a deck with both food and games and take two out of it. If you are curious, you can take a look at section EXPRESS YOURSELF in the Tutorial.

We have an expression according to a grammar, hence we need a parser.

A coded parser?

There are a few ways to translate a grammar into code, where direct translation might not be the most obvious or maintainable one. I guess the best you can do if you have to implement a parser for a reasonably complicated grammar is to look into Marpa::R2 or read just about anything that Jeffrey Kegler wrote on the topic.


In this case we have a very small grammar, without particular quirks… so why not solve it all in a nicecly packed Perl module? So yes, a coded parser.

Before you go on…

… you should be aware that Mark Jason Dominus wrote about doing this in a much better and cleaner way in Higher Order Perl. I took inspiration from there for what I did here, although I adapted it to my needs. If you want, you can also download the book (including the individual chapter on Parsing).

If you continue to read from this series… it’s now entirely upon you!

The whole series

Want to look at the other parts of this series? Here’s a list of them:

Comments? Octodon, , GitHub, Reddit, or drop me a line!