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 fmt = function
46 | Stream_start { encoding } ->
47 Format.fprintf fmt "stream-start(%a)" Encoding.pp encoding
48 | Stream_end ->
49 Format.fprintf fmt "stream-end"
50 | Document_start { version; implicit } ->
51 Format.fprintf fmt "document-start(version=%s, implicit=%b)"
52 (match version with None -> "none" | Some (maj, min) -> Printf.sprintf "%d.%d" maj min)
53 implicit
54 | Document_end { implicit } ->
55 Format.fprintf fmt "document-end(implicit=%b)" implicit
56 | Alias { anchor } ->
57 Format.fprintf fmt "alias(%s)" anchor
58 | Scalar { anchor; tag; value; style; _ } ->
59 Format.fprintf fmt "scalar(anchor=%s, tag=%s, style=%a, value=%S)"
60 (Option.value anchor ~default:"none")
61 (Option.value tag ~default:"none")
62 Scalar_style.pp style
63 value
64 | Sequence_start { anchor; tag; implicit; style } ->
65 Format.fprintf fmt "sequence-start(anchor=%s, tag=%s, implicit=%b, style=%a)"
66 (Option.value anchor ~default:"none")
67 (Option.value tag ~default:"none")
68 implicit
69 Layout_style.pp style
70 | Sequence_end ->
71 Format.fprintf fmt "sequence-end"
72 | Mapping_start { anchor; tag; implicit; style } ->
73 Format.fprintf fmt "mapping-start(anchor=%s, tag=%s, implicit=%b, style=%a)"
74 (Option.value anchor ~default:"none")
75 (Option.value tag ~default:"none")
76 implicit
77 Layout_style.pp style
78 | Mapping_end ->
79 Format.fprintf fmt "mapping-end"
80
81let pp_spanned fmt { event; span } =
82 Format.fprintf fmt "%a at %a" pp event Span.pp span