Parsa is a functional combinator parsing library. It focuses on concise error handling, modularity, and reusability, as well as 0-copy string consumption and backtracking.
Examples
Lets parse the string "var = 123". Note that examples will be using thiserror
to minimize boilerplate.
First, our struct and error type:
use ;
use *;
use Error; //simple errors
use FromNever; //From<Infallible> conversion
use ParseIntError;
The first thing we need to do is parse the name of the variable, which we can do with word
.
We also want to get rid of whitespace, so we can use whitespace
with the
after
combinator, as we dont care about its output.
let name = word
. //explicitly set our target error type
//not always needed, but usually helps with inference
.after //whitespace is infallible, so we dont need an explicit variant
//in our error type to coerce from it.
.parse?;
Next, we want to just make sure that the =
sign comes after.
let _ = take.after.parse?; //coerces to MissingEqual
Our final step is to get the number. We will use word
again, but this time map the result.
let val = word
. //will save headaches
.and_then
.parse?;
And now we can build our struct!
Ok(Var { name, val })
And because this function has the correct signature, it can be used with any method in Parser
.
let vars: Vec<Var> = Var::parse.many().parse(s)?;