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(** Position tracking for source locations *) 7 8type t = { 9 index : int; (** Byte offset from start *) 10 line : int; (** 1-indexed line number *) 11 column : int; (** 1-indexed column number *) 12} 13 14let initial = { index = 0; line = 1; column = 1 } 15 16let advance_byte t = 17 { t with index = t.index + 1; column = t.column + 1 } 18 19let advance_line t = 20 { index = t.index + 1; line = t.line + 1; column = 1 } 21 22let advance_char c t = 23 if c = '\n' then advance_line t 24 else advance_byte t 25 26let advance_utf8 uchar t = 27 let len = Uchar.utf_8_byte_length uchar in 28 let code = Uchar.to_int uchar in 29 if code = 0x0A (* LF *) then 30 { index = t.index + len; line = t.line + 1; column = 1 } 31 else 32 { t with index = t.index + len; column = t.column + 1 } 33 34let advance_bytes n t = 35 { t with index = t.index + n; column = t.column + n } 36 37let pp fmt t = 38 Format.fprintf fmt "line %d, column %d" t.line t.column 39 40let to_string t = 41 Format.asprintf "%a" pp t 42 43let compare a b = 44 Int.compare a.index b.index 45 46let equal a b = 47 a.index = b.index