Pure OCaml Yaml 1.2 reader and writer using Bytesrw
1(*---------------------------------------------------------------------------
2 Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
3 SPDX-License-Identifier: ISC
4 ---------------------------------------------------------------------------*)
5
6(** YAML parser events *)
7
8type t =
9 | Stream_start of { encoding : Encoding.t }
10 | Stream_end
11 | Document_start of { version : (int * int) option; implicit : bool }
12 | Document_end of { implicit : bool }
13 | Alias of { anchor : string }
14 | Scalar of {
15 anchor : string option;
16 tag : string option;
17 value : string;
18 plain_implicit : bool;
19 quoted_implicit : bool;
20 style : Scalar_style.t;
21 }
22 | Sequence_start of {
23 anchor : string option;
24 tag : string option;
25 implicit : bool;
26 style : Layout_style.t;
27 }
28 | Sequence_end
29 | Mapping_start of {
30 anchor : string option;
31 tag : string option;
32 implicit : bool;
33 style : Layout_style.t;
34 }
35 | Mapping_end
36
37type spanned = { event : t; span : Span.t }
38
39let pp_opt_str = Option.value ~default:"none"
40
41let pp fmt = function
42 | Stream_start { encoding } ->
43 Format.fprintf fmt "stream-start(%a)" Encoding.pp encoding
44 | Stream_end -> Format.fprintf fmt "stream-end"
45 | Document_start { version; implicit } ->
46 let version_str =
47 match version with
48 | None -> "none"
49 | Some (maj, min) -> Printf.sprintf "%d.%d" maj min
50 in
51 Format.fprintf fmt "document-start(version=%s, implicit=%b)" version_str
52 implicit
53 | Document_end { implicit } ->
54 Format.fprintf fmt "document-end(implicit=%b)" implicit
55 | Alias { anchor } -> Format.fprintf fmt "alias(%s)" anchor
56 | Scalar { anchor; tag; value; style; _ } ->
57 Format.fprintf fmt "scalar(anchor=%s, tag=%s, style=%a, value=%S)"
58 (pp_opt_str anchor) (pp_opt_str tag) Scalar_style.pp style value
59 | Sequence_start { anchor; tag; implicit; style } ->
60 Format.fprintf fmt "sequence-start(anchor=%s, tag=%s, implicit=%b, style=%a)"
61 (pp_opt_str anchor) (pp_opt_str tag) implicit Layout_style.pp style
62 | Sequence_end -> Format.fprintf fmt "sequence-end"
63 | Mapping_start { anchor; tag; implicit; style } ->
64 Format.fprintf fmt "mapping-start(anchor=%s, tag=%s, implicit=%b, style=%a)"
65 (pp_opt_str anchor) (pp_opt_str tag) implicit Layout_style.pp style
66 | Mapping_end -> Format.fprintf fmt "mapping-end"
67
68let pp_spanned fmt { event; span } =
69 Format.fprintf fmt "%a at %a" pp event Span.pp span