ptal / oak

A typed parser generator embedded in Rust code for Parsing Expression Grammars
Apache License 2.0
142 stars 14 forks source link

No backtracking error, extend the choice operator with `\`. #30

Open ptal opened 10 years ago

ptal commented 10 years ago

We are sometimes sure that if a rule fails, the program can't be correct. It often happens after a specific keyword has been encountered. For example in Rust, a let must be followed by a declaration:

let-decl = "let" decl ";"

If we parse the "let" but that decl fail, it's useless to backtrack and can put a context-sensitive message here:

let-decl = 
    "let" decl ";"
  \ "let" decl !";" { "A let declaration must be terminated by a semi-colon." } 
  \ "let" .* { "A let must be followed by a declaration." }

We use the \ character because it's a bit like the dual of the choice: if the rule succeed it will fail (instead of succeed).

ptal commented 8 years ago

This approach is different from existing error reporting mechanism, it leaves the user to report himself the error (which is often desirable in compiler—for better error messages) instead of trying to generate automatically all the errors. Note that we still generate errors if the user didn't specify anything so it lets the user free to improve the grammar later.