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 = { token : t; span : Span.t }
32
33let pp_token fmt = function
34 | Stream_start enc -> Format.fprintf fmt "STREAM-START(%a)" Encoding.pp enc
35 | Stream_end -> Format.fprintf fmt "STREAM-END"
36 | Version_directive { major; minor } ->
37 Format.fprintf fmt "VERSION-DIRECTIVE(%d.%d)" major minor
38 | Tag_directive { handle; prefix } ->
39 Format.fprintf fmt "TAG-DIRECTIVE(%s, %s)" handle prefix
40 | Document_start -> Format.fprintf fmt "DOCUMENT-START"
41 | Document_end -> Format.fprintf fmt "DOCUMENT-END"
42 | Block_sequence_start -> Format.fprintf fmt "BLOCK-SEQUENCE-START"
43 | Block_mapping_start -> Format.fprintf fmt "BLOCK-MAPPING-START"
44 | Block_entry -> Format.fprintf fmt "BLOCK-ENTRY"
45 | Block_end -> Format.fprintf fmt "BLOCK-END"
46 | Flow_sequence_start -> Format.fprintf fmt "FLOW-SEQUENCE-START"
47 | Flow_sequence_end -> Format.fprintf fmt "FLOW-SEQUENCE-END"
48 | Flow_mapping_start -> Format.fprintf fmt "FLOW-MAPPING-START"
49 | Flow_mapping_end -> Format.fprintf fmt "FLOW-MAPPING-END"
50 | Flow_entry -> Format.fprintf fmt "FLOW-ENTRY"
51 | Key -> Format.fprintf fmt "KEY"
52 | Value -> Format.fprintf fmt "VALUE"
53 | Anchor name -> Format.fprintf fmt "ANCHOR(%s)" name
54 | Alias name -> Format.fprintf fmt "ALIAS(%s)" name
55 | Tag { handle; suffix } -> Format.fprintf fmt "TAG(%s, %s)" handle suffix
56 | Scalar { style; value } ->
57 Format.fprintf fmt "SCALAR(%a, %S)" Scalar_style.pp style value
58
59let pp fmt t = pp_token fmt t
60
61let pp_spanned fmt { token; span } =
62 Format.fprintf fmt "%a at %a" pp_token token Span.pp span