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(** Source spans representing ranges in input *)
7
8type t = {
9 start : Position.t;
10 stop : Position.t;
11}
12
13let make ~start ~stop = { start; stop }
14
15let point pos = { start = pos; stop = pos }
16
17let merge a b =
18 let start = if Position.compare a.start b.start <= 0 then a.start else b.start in
19 let stop = if Position.compare a.stop b.stop >= 0 then a.stop else b.stop in
20 { start; stop }
21
22let extend span pos =
23 { span with stop = pos }
24
25let pp fmt t =
26 if t.start.line = t.stop.line then
27 Format.fprintf fmt "line %d, columns %d-%d"
28 t.start.line t.start.column t.stop.column
29 else
30 Format.fprintf fmt "lines %d-%d" t.start.line t.stop.line
31
32let to_string t =
33 Format.asprintf "%a" pp t
34
35let compare a b =
36 let c = Position.compare a.start b.start in
37 if c <> 0 then c else Position.compare a.stop b.stop
38
39let equal a b =
40 Position.equal a.start b.start && Position.equal a.stop b.stop