ahrefs/atd

Error recovery: ignore malformed elements

mjambon opened this issue · 0 comments

Goal: maximize backward compatibility of outdated JSON readers

This was inspired by what tree-sitter parsers do. Tree-sitter is a parser generator. When a tree-sitter parser encounters an unsupported syntax in an input file, instead of failing completely, it will try to contain the error by creating an error node that a client can happily ignore.

We could apply a similar strategy to JSON readers, to any target language: if an ignorable node is malformed, skip it. The ignored material could be reported as an error outside of the main result e.g. an OCaml signature for a JSON reader could be

type error = {
  expected_type: string;
  actual_data: json;
}

(* Interpret JSON data as a 'foo', raising an exception if malformed elements can't be ignored.
   Malformed JSON elements that can be ignored are collected as the error list. *)
val read_foo : json -> foo * error list

Ignorable nodes include list or array elements (ATD list) and optional values (ATD option, nullable).

Tasks:

  • Figure out how useful this would be. Your feedback is appreciated!
  • Implement this behavior for each target language of interest. It would be straightforward for atdpy but probably too complicated for atdgen (since the latter doesn't go through a JSON tree; see #313 for a remedy)