In day 02 we wrote a simpler parser: using nom.
in day 04, we write a more complicated parser that can handle a variety of different attributes in different orders.
I went back through day 4's parser and introduced nom_locate's LocatedSpan to replace the input
&str. This allows the grabbing of positional data for tokens at any point. I want to use this information in a custom error type for when documents are invalid (although in advent of code, the input is usually not malformed).
spans acquired via
position have empty fragments.
Whereas if we
dbg! the input spans, they contain fragments
One interesting piece of using parser combinators is that we can build up some of our own parser functionality and re-use them. This example shows a
year parser that is used to implement the parsers for
eyr which all have similar requirements.
.map over the result to turn them into the
PassportParse type we need to satisfy
The runtime of the parser is about 500 us.
| example | lower bound | best guess | upper bound | | ------- | ----------- | ---------- | ----------- | | nom | 578.63 us | 581.37 us | 584.33 us |