An error recovering parser
We want to make the parser a bit more tolerant to errors so that we can parse and return an AST with error nodes in it that represent some form of partial / incomplete thing. We also want to accumulate the actual error messages. I was thinking the parser would return AST and a list of accumulated errors, so a caller than expects a completely successful parse can just check if the list of accumulated errors is len() == 0.
The simplest way of adding in Error nodes is to add:
- ErrorStmt
- ErrorExpr
- ErrorDecl
- ErrorPat
since there are functions etc that are expected to return Stmt, Expr etc sum types, this fits into that. I don't really see any other way, at least not without completely restructuring the AST / parser.. but even with that freedom, I struggle to see a better way. I don't think it's worthwhile changing everything around just so we can have a single Error type, or is it?
So
- parser to return: AST, [ErrorMessages]
- 4 new Error types for Stmt, Expr, Decl & Pat
@sydow / @nordlander WDYT?
I assume ErrorStmt, ErrorExpr, etc aren't new types but rather new constructors in the respective AST types. If so I fully agree and support the idea!
@nordlander yes exactly. I was initially thinking, before reading any of our existing code, that I would add a new Error type that would capture "couldn't parse this correctly" and save the incomplete string we could read. After reading Syntax, I seems to me that it is best to maintain the overall structure and types there, which practically means we can't have a single Error type but need 4, the ones I listed. Sounds like you agree, so good :)