Parser Compose
⚠️ Warning ☣️
Homemade, hand-rolled code ahead. Experimental. May not function as advertised.
parser-compose
is a library for writing and composing parsers for arbitrary
file or data formats. It has a strong focus on usability and API design.
It's based on the ideas around parser combinators and parsing expression grammars, but don't let those terms scare you off.
I made this because many of my projects involve parsing something (like a configuration file, a binary formt, an HTTP message), but the field of parsing and language theory sounds incredibly dull to me. I have always resorted to "string.split()"-type parsing, but it is tedious.
Turns out there is a better way! No theory required.
Crash course in parser combinators
Say you want to extract the letter 'a' from a sequence of bytes. You can write a
parser function takes the bytes as argument and returns successfully if it saw
the byte 97
(ascii for 'a') at the start:
Note: I am not handling edge cases to keep things brief
// If we find `97` successfully at the start of sequence, extract it and put it
// in a tuple. Return the remaining input as well
Ok, but what if you wanted to parse 98
now? You can write a function that
builds a parser. The argument to this parser builder will be the byte you'd
like to recognize. The parser builder will return a parser that accepts that byte.
For the final touch. What if you wanted to recognize 97
or 98
? We
can write a function that ... uhh ... combines (hint, hint, wink, wink) two
parsers it gets as arguments. When this combiner function is called, it returns
a parser that succeeds with the value of the first succeeding inner
parser.
// referred to as a "parser combinator"
That is the basic idea. You can now go crazy writing all sorts of combinators
like and()
and optional()
, and use them to combine your parsers
together ... or you could use this crate :)
Similar projects
Thanks
This crate would not have been possible without:
- This post called You could have invented Parser Combinators, which brought the concept of parser combinators down from "academic sounding term, no thank you" to "wow, i can understand this"
- This guide to writing parser combinators in rust
- This
article
by the author of
pom
, which lays out the various approaches to writing parser combinators in rust.