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 {
12 version : (int * int) option;
13 implicit : bool;
14 }
15 | Document_end of { implicit : bool }
16 | Alias of { anchor : string }
17 | Scalar of {
18 anchor : string option;
19 tag : string option;
20 value : string;
21 plain_implicit : bool;
22 quoted_implicit : bool;
23 style : Scalar_style.t;
24 }
25 | Sequence_start of {
26 anchor : string option;
27 tag : string option;
28 implicit : bool;
29 style : Layout_style.t;
30 }
31 | Sequence_end
32 | Mapping_start of {
33 anchor : string option;
34 tag : string option;
35 implicit : bool;
36 style : Layout_style.t;
37 }
38 | Mapping_end
39
40type spanned = {
41 event : t;
42 span : Span.t;
43}
44
45let pp_opt_str = Option.value ~default:"none"
46
47let pp fmt = function
48 | Stream_start { encoding } ->
49 Format.fprintf fmt "stream-start(%a)" Encoding.pp encoding
50 | Stream_end ->
51 Format.fprintf fmt "stream-end"
52 | Document_start { version; implicit } ->
53 let version_str = match version with
54 | None -> "none"
55 | Some (maj, min) -> Printf.sprintf "%d.%d" maj min
56 in
57 Format.fprintf fmt "document-start(version=%s, implicit=%b)" version_str implicit
58 | Document_end { implicit } ->
59 Format.fprintf fmt "document-end(implicit=%b)" implicit
60 | Alias { anchor } ->
61 Format.fprintf fmt "alias(%s)" anchor
62 | Scalar { anchor; tag; value; style; _ } ->
63 Format.fprintf fmt "scalar(anchor=%s, tag=%s, style=%a, value=%S)"
64 (pp_opt_str anchor) (pp_opt_str tag) Scalar_style.pp style value
65 | Sequence_start { anchor; tag; implicit; style } ->
66 Format.fprintf fmt "sequence-start(anchor=%s, tag=%s, implicit=%b, style=%a)"
67 (pp_opt_str anchor) (pp_opt_str tag) implicit Layout_style.pp style
68 | Sequence_end ->
69 Format.fprintf fmt "sequence-end"
70 | Mapping_start { anchor; tag; implicit; style } ->
71 Format.fprintf fmt "mapping-start(anchor=%s, tag=%s, implicit=%b, style=%a)"
72 (pp_opt_str anchor) (pp_opt_str tag) implicit Layout_style.pp style
73 | Mapping_end ->
74 Format.fprintf fmt "mapping-end"
75
76let pp_spanned fmt { event; span } =
77 Format.fprintf fmt "%a at %a" pp event Span.pp span