Pure OCaml Yaml 1.2 reader and writer using Bytesrw
at main 1.3 kB view raw
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 } 15let advance_byte t = { t with index = t.index + 1; column = t.column + 1 } 16let advance_line t = { index = t.index + 1; line = t.line + 1; column = 1 } 17let advance_char c t = if c = '\n' then advance_line t else advance_byte t 18 19let advance_utf8 uchar t = 20 let len = Uchar.utf_8_byte_length uchar in 21 let code = Uchar.to_int uchar in 22 if code = 0x0A (* LF *) then 23 { index = t.index + len; line = t.line + 1; column = 1 } 24 else { t with index = t.index + len; column = t.column + 1 } 25 26let advance_bytes n t = { t with index = t.index + n; column = t.column + n } 27let pp fmt t = Format.fprintf fmt "line %d, column %d" t.line t.column 28let to_string t = Format.asprintf "%a" pp t 29let compare a b = Int.compare a.index b.index 30let equal a b = a.index = b.index