Skip to content

Add error recovery #289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Helpful `rustc` error messages for errors in the grammar definition or the Rust
code embedded within it
* Rule-level tracing to debug grammars
* Error recovery

## Example

Expand All @@ -30,21 +31,21 @@ peg::parser!{
}

pub fn main() {
assert_eq!(list_parser::list("[1,1,2,3,5,8]"), Ok(vec![1, 1, 2, 3, 5, 8]));
assert_eq!(list_parser::list("[1,1,2,3,5,8]").into_result(), Ok(vec![1, 1, 2, 3, 5, 8]));
}
```

[See the tests for more examples](./tests/run-pass/)
[See the tests for more examples](./tests/run-pass/)
[Grammar rule syntax reference in rustdoc](https://docs.rs/peg)

## Comparison with similar parser generators

| crate | parser type | action code | integration | input type | precedence climbing | parameterized rules | streaming input |
|----------- |------------- |------------- |-------------------- |------------------------ |--------------------- |-------------------- |----------------- |
| peg | PEG | in grammar | proc macro (block) | `&str`, `&[T]`, custom | Yes | Yes | No |
| [pest] | PEG | external | proc macro (file) | `&str` | Yes | No | No |
| [nom] | combinators | in source | library | `&[u8]`, custom | No | Yes | Yes |
| [lalrpop] | LR(1) | in grammar | build script | `&str` | No | Yes | No |
| crate | parser type | action code | integration | input type | precedence climbing | parameterized rules | streaming input | recovery |
|----------- |------------- |------------- |-------------------- |------------------------ |--------------------- |-------------------- |----------------- |--------- |
| peg | PEG | in grammar | proc macro (block) | `&str`, `&[T]`, custom | Yes | Yes | No | Yes |
| [pest] | PEG | external | proc macro (file) | `&str` | Yes | No | No | No |
| [nom] | combinators | in source | library | `&[u8]`, custom | No | Yes | Yes | No |
| [lalrpop] | LR(1) | in grammar | build script | `&str` | No | Yes | No | Yes |

[pest]: https://github.com/pest-parser/pest
[nom]: https://github.com/geal/nom
Expand All @@ -60,6 +61,13 @@ pub fn main() {
[annotate-snippets]: https://crates.io/crates/annotate-snippets
[codespan-reporting]: https://crates.io/crates/codespan-reporting
[codemap-diagnostic]: https://crates.io/crates/codemap-diagnostic

## Upgrade guide

The rule return type has changed between 0.8 to 0.9,
and now supports recovery and reporting multiple errors.
To upgrade, add a call to `.into_result()` to convert the new rule return type
to a simple `Result`.
## Development

The `rust-peg` grammar is written in `rust-peg`: `peg-macros/grammar.rustpeg`. To avoid the circular dependency, a precompiled grammar is checked in as `peg-macros/grammar.rs`. To regenerate this, run the `./bootstrap.sh` script.
Expand Down
10 changes: 5 additions & 5 deletions peg-macros/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl<'a> LeftRecursionVisitor<'a> {
nullable
}

LiteralExpr(_) | PatternExpr(_) | MethodExpr(_, _) | FailExpr(_) | MarkerExpr(_) => false,
LiteralExpr(_) | PatternExpr(_) | MethodExpr(_, _) | FailExpr(_) | ErrorIfExpr(..) | ErrorUnlessExpr(..) |MarkerExpr(_) => false,

PositionExpr => true,
}
Expand Down Expand Up @@ -220,7 +220,7 @@ impl<'a> LoopNullabilityVisitor<'a> {
let name = rule_ident.to_string();
*self.rule_nullability.get(&name).unwrap_or(&false)
}

ActionExpr(ref elems, ..) => {
let mut nullable = true;
for elem in elems {
Expand Down Expand Up @@ -250,7 +250,7 @@ impl<'a> LoopNullabilityVisitor<'a> {
if inner_nullable && sep_nullable && !bound.has_upper_bound() {
self.errors.push(LoopNullabilityError { span: this_expr.span });
}

inner_nullable | !bound.has_lower_bound()
}

Expand All @@ -269,10 +269,10 @@ impl<'a> LoopNullabilityVisitor<'a> {
}
}

nullable
nullable
}

LiteralExpr(_) | PatternExpr(_) | MethodExpr(_, _) | FailExpr(_) | MarkerExpr(_) => false,
LiteralExpr(_) | PatternExpr(_) | MethodExpr(_, _) | FailExpr(_) | ErrorIfExpr(..) | ErrorUnlessExpr(..) | MarkerExpr(_) => false,
PositionExpr => true,
}
}
Expand Down
2 changes: 2 additions & 0 deletions peg-macros/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub enum Expr {
PositionExpr,
QuietExpr(Box<SpannedExpr>),
FailExpr(Literal),
ErrorIfExpr(Box<SpannedExpr>, Literal, Box<SpannedExpr>),
ErrorUnlessExpr(Box<SpannedExpr>, Literal, Box<SpannedExpr>),
PrecedenceExpr {
levels: Vec<PrecedenceLevel>,
},
Expand Down
2 changes: 1 addition & 1 deletion peg-macros/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn main() {

let source_tokens = source.parse().expect("Error tokenizing input");
let input_tokens = tokens::FlatTokenStream::new(source_tokens);
let grammar = match grammar::peg::peg_grammar(&input_tokens) {
let grammar = match grammar::peg::peg_grammar(&input_tokens).into_result() {
Ok(g) => g,
Err(err) => {
eprintln!("Failed to parse grammar: expected {}", err.expected);
Expand Down
Loading