What are parser combinators?
Before you start, make sure you've had a look at the installation instructions.
Parsers
A parser is a function that takes some unstructured input (like a string) and turns it into structured output, that's easier to work with. This output could be as simple as a slightly better structured string, or an array, an object, up to a complete abstract syntax tree. You can then use this data structure for subsequent processing.
You're probably using parsers all the time, such as json_decode()
. And even just casting a string to a float footnote 1 really is parsing.
Parsica helps you build your own parsers, in a concise, declarative way. Behind the scenes it takes care of things like error handling, so you can focus on the parser itself.
Building a parser
There are many ways to build a parser for your own use case, ranging from formal grammars that get compiled into a parser, to regular expressions, to writing a parser entirely from scratch. They all have their own tradeoffs and limitations.
One of the great benefits of the parser combinator style is that, once you get the hang of it, they're generally easier to write, understand, and maintain. You start from building blocks, such as digitChar()
, which returns a function that parses a single digit.
Parser Combinators
Parser Combinators are functions (or methods) that combine parsers into new parsers. Instead of writing one big parser, we can now write smaller parsers and cleverly compose them into larger parsers.
To make this work, we need a small change in our original definition of a parser.
A parser is a function that takes some unstructured input (such as a string), and returns a more structured output, as well as the remaining unparsed part of the input.
This way, each parser function can parse a chunk of the input, and leave the remainder to another parser. The combinators take care of the heavy lifting: pass the input to the parser functions, pass the remainder to the next one, decide what to do with errors (eg, fail or backtrack or try another parser), ...
We can inspect the remainder:
So when we run our parser using $parser->try($input)
, the sequence()
combinator first tries to run char('a')
on the input "abc"
. If it succeeds, it takes the remainder "bc"
and successfully runs char('b')
on it and returns the result. That result consists of the output from the last parser "b"
, and the remainder "c"
.
In imperative code, it would look something like this:
If you've been working in PHP long enough and have never used parser combinators, the code above may look more familiar for now. But imagine scaling that to parse anything from simple formats like credit card numbers, recursive structures like JSON or XML, or even entire programming languages like PHP. And that doesn't even include the code you'd need for performance, testing and debugging tooling, code reuse, and reporting on bad input. If you'd rather write sequence(char('a'), char('b'))
, stick around.
Footnotes
Note 1
The above looks fine at first sight, but floatval()
really isn't a very good parser.
floatval()
claims that the float of "abc"
is 0
, which really should be an error. So you can only use floatval
when you already know that the string doesn't contain anything non-float. Parsica can help you do that: