Hand written recursive descent and non-backtracking parsing "combinator" library designed with nice error in mind and lossless data.
I'm somehow inspired by this post. It's about parser generators but I prefer writing them manually.
Use cargo-edit:
sh
cargo add alder
Or add it manually:
toml
alder = "0.4.0"
You may want to enable a derive feature as well:
toml
alder = { version = "0.4.0" , features = ["derive"] }
``rust
// Doc tests are treated as a test cases for snapshots.
// To enable them use
derivefeature and add
#[alder_test]` macro.
// It also supports /* multiline comments */
/// []
/// [true]
/// [true,false]
/// [ ]
/// [ true ]
/// [ true, false ]
/// [ true, false, ]
/// [trua, falsa]
/// [truadsadsa, falsa]
/// [true, false
/// [truad sadsa, falsa]
/*
[
true,
false,
"foo"
]
*/
fn array() -> impl Parser { withextra( extra(), node(Json::Array, |state| { state.add("["); match peek(1).parse(state).asref() { "]" => (), _ => 'outer: loop { state.add(value()); 'inner: loop { // Until we find either ']' or ',' match peek(1).parse(state).asref() { "]" => { break 'outer; } "," => { state.add(recover(",")); if let "]" = peek(1).parse(state).asref() { // Trailing comma break 'outer; } break 'inner; } "" => { // EOF state.add(raise(Problem::InvalidTokenArray, 1)); break 'outer; } _ => state.add(raise(Problem::InvalidTokenArray, 1)), }; } }, } state.add(recover("]")); }), ) } ```
Parsers should return information about what happened and where it happened:
``
--------------------------------- SYNTAX ERROR ---------------------------------
I was parsing Boolean when found issue:
0 |[truadsadsa, falsa]\EOF
~ | ^^^^^^^^^^ I expected
true`
--------------------------------- SYNTAX ERROR ---------------------------------
I was parsing Boolean when found issue:
0 |[truadsadsa, falsa]\EOF
~ | ^^^^^ I expected false
```