Skip to content

atdml: support one-way conversions from a generic JSON-like data tree with locations #453

@mjambon

Description

@mjambon

The primary goal is to support YAML, TOML, INI, or even XML or CSV input and report accurate errors when encountering invalid data.

Workflow: YAML -> YAML AST -> JSON-like AST -> OCaml value. The conversion from YAML input to a YAML AST (abstract syntax tree) with locations would be done by any good YAML parser. Converting the YAML AST to the proposed JSON-like AST would have to be done once and for all for each YAML parsing library and is expected to be relatively easy. The conversion from the JSON-like AST to an OCaml value would be done by the OCaml code generated by atdml.

I suggest the following minimal tree type based on Yojson.Basic.t with the addition of location info for each node and the use of classic variants:

(* OCaml definitions for a JSON-compatible data tree expected as input *)

(* A position in a file using zero-based matrix notation *)
type pos = {
  source_file: string; (* empty string if unknown or implicit *)
  row: int;
  column: int;
}

(* A location or range. It represents a substring in the input *)
type loc = { start: pos; end_: pos }

(* A general-purpose tree that accommodates data
   coming from JSON or some other data format. *)
type data_tree =
| Unit of loc
| Bool of loc * bool
| Int of loc * int
| Float of loc * float
| String of loc * string
| Assoc of loc * (loc * string * data_tree) list
| List of loc * data_tree list

Atdml would generate functions to read from such an AST. However, it would not generate write functions because it wouldn't know how to generate a location for each node. Converting an OCaml value to say YAML would have to go through Yojson.Safe.t which is already supported. The conversion from Yojson.Safe.t to idiomatic YAML that is not just JSON would be done by a proper YAML library that's independent from ATD.

If an ATD user wishes to inject a location into the final OCaml value, we don't offer a direct mechanism. The recommended way is encode the locations into the payload when converting the YAML AST to the JSON-like AST.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestBig and small feature requeststarget:ocamlIssues related to the OCaml backend (atdgen)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions