pest-parser / ast

Apache License 2.0
81 stars 15 forks source link

Committing to a conversion #10

Open CAD97 opened 5 years ago

CAD97 commented 5 years ago

In order to aid the creation of correct ASTs, it's sometimes useful to know when things go wrong when you don't expect. I propose adding a #[pest_ast(commit)] or similar, after which conversions in the sequence which would otherwise cause a Err(NoMatch) to propagate, instead cause a panic detailing where the error is.

CAD97 commented 5 years ago

This is probably a bit more involved than I'd wish, as it adds state to the conversion where it didn't exist before. However, here's a rough outline of what needs to change to support this:

Best guess: replace the FromPest::from_pest(inner)? with FromPest::from_pest(inner).map_err(|e| if commit { panic!(..) } else { e })?, and add code to manage the commit variable. We can rely on optimizing it out if it's never set.

https://github.com/pest-parser/ast/blob/db9e2b4d57fe61d85f7d3e768d026f0ec95776a7/derive/src/from_pest/field.rs#L58-L70