acton icon indicating copy to clipboard operation
acton copied to clipboard

An error recovering parser

Open plajjan opened this issue 6 months ago • 2 comments

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?

plajjan avatar Jun 30 '25 06:06 plajjan

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 avatar Jul 04 '25 18:07 nordlander

@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 :)

plajjan avatar Jul 05 '25 02:07 plajjan