Skip to content
Kaspar Schiess edited this page Sep 11, 2013 · 6 revisions

Parsing

Why is parslet slow?

Parslet is written in Ruby. Also, it tries very hard to be a traceable parsing engine - it produces useful parse errors. This means that if you need a complex parser that will parse millions of lines in miliseconds, parslet is not your tool. Use C or something.

If you have a simple parser that needs to go fast, take a look at Parslet::Accelerator. (and also the optimized ERB sample) With this tool, you can keyhole optimize your parser to go faster, while keeping the explicit description of your language that a well written parser provides.

If you have a complex parser for small inputs, you should be fine. You still might have a look at the accelerator engine, but not until you have a working prototype.

And if your parser is trivial and the input is small - why are you using parslet at all?

How can I influence the parse tree (intermediary tree)?

You have very limited options for influencing the output of parser#parse. The only thing you can do is add your .as(...) marks in strategic places. This is intentional, parslet is opinionated that way.

The recommended way of making the output of your parser look like you want it is by using the transformers to transform the hash into an abstract syntax tree (or anything else you'd like). The parse tree (intermediary tree) is not supposed to be the output of your parser.

And since the parse tree (intermediary tree) is a plain old Ruby hash, you could also use any bit of Ruby code to traverse and modify that hash.

Features

When are you going to implement left recursion?

Probably never. Here's where all attempts at doing so have failed up until now:

  • Code complexity - left recursion introduces another indirection that makes the evolution of parslet hard.
  • Speed - Execution speed matters and left recursion doesn't help.
  • Understandability - Once we introduce left recursion, the top down metaphor of methods calling methods doesn't hold at all anymore. (See infinite recursion question)

If you're parsing infix expressions of any kind, take a look at the infix expression parsing engine instead, it will get you there quicker.