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 token types produced by the scanner *)
7
8type t =
9 | Stream_start of Encoding.t
10 | Stream_end
11 | Version_directive of { major : int; minor : int }
12 | Tag_directive of { handle : string; prefix : string }
13 | Document_start (** --- *)
14 | Document_end (** ... *)
15 | Block_sequence_start
16 | Block_mapping_start
17 | Block_entry (** [-] *)
18 | Block_end (** implicit, from dedent *)
19 | Flow_sequence_start (** \[ *)
20 | Flow_sequence_end (** \] *)
21 | Flow_mapping_start (** \{ *)
22 | Flow_mapping_end (** \} *)
23 | Flow_entry (** [,] *)
24 | Key (** ? or implicit key *)
25 | Value (** : *)
26 | Anchor of string (** &name *)
27 | Alias of string (** *name *)
28 | Tag of { handle : string; suffix : string }
29 | Scalar of { style : Scalar_style.t; value : string }
30
31type spanned = {
32 token : t;
33 span : Span.t;
34}
35
36let pp_token fmt = function
37 | Stream_start enc ->
38 Format.fprintf fmt "STREAM-START(%a)" Encoding.pp enc
39 | Stream_end ->
40 Format.fprintf fmt "STREAM-END"
41 | Version_directive { major; minor } ->
42 Format.fprintf fmt "VERSION-DIRECTIVE(%d.%d)" major minor
43 | Tag_directive { handle; prefix } ->
44 Format.fprintf fmt "TAG-DIRECTIVE(%s, %s)" handle prefix
45 | Document_start ->
46 Format.fprintf fmt "DOCUMENT-START"
47 | Document_end ->
48 Format.fprintf fmt "DOCUMENT-END"
49 | Block_sequence_start ->
50 Format.fprintf fmt "BLOCK-SEQUENCE-START"
51 | Block_mapping_start ->
52 Format.fprintf fmt "BLOCK-MAPPING-START"
53 | Block_entry ->
54 Format.fprintf fmt "BLOCK-ENTRY"
55 | Block_end ->
56 Format.fprintf fmt "BLOCK-END"
57 | Flow_sequence_start ->
58 Format.fprintf fmt "FLOW-SEQUENCE-START"
59 | Flow_sequence_end ->
60 Format.fprintf fmt "FLOW-SEQUENCE-END"
61 | Flow_mapping_start ->
62 Format.fprintf fmt "FLOW-MAPPING-START"
63 | Flow_mapping_end ->
64 Format.fprintf fmt "FLOW-MAPPING-END"
65 | Flow_entry ->
66 Format.fprintf fmt "FLOW-ENTRY"
67 | Key ->
68 Format.fprintf fmt "KEY"
69 | Value ->
70 Format.fprintf fmt "VALUE"
71 | Anchor name ->
72 Format.fprintf fmt "ANCHOR(%s)" name
73 | Alias name ->
74 Format.fprintf fmt "ALIAS(%s)" name
75 | Tag { handle; suffix } ->
76 Format.fprintf fmt "TAG(%s, %s)" handle suffix
77 | Scalar { style; value } ->
78 Format.fprintf fmt "SCALAR(%a, %S)" Scalar_style.pp style value
79
80let pp fmt t = pp_token fmt t
81
82let pp_spanned fmt { token; span } =
83 Format.fprintf fmt "%a at %a" pp_token token Span.pp span