Something went wrong. Try again.
XML parser, serializer, and typed codecs
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire <thomas@gazagnaire.org>. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(* XML-specific error module. Extends Loc.Error with XML-typed error kinds and high-level message helpers used by the codec. *)
module Sort = Sort
type kind = Loc.Error.kind = ..
type Loc.Error.kind += | Sort_mismatch of { exp : Sort.t; fnd : Sort.t } | Kinded_sort_mismatch of { exp : string; fnd : Sort.t } | Missing_attr of { name : string } | Missing_child of { tag : string } | Mismatched_tags of { opened : string; closed : string } | Unknown_entity of string | Invalid_char_ref of string | Char_ref_range of string | Index_out_of_range of { n : int; len : int }
let pp_code ppf s = Fmt.pf ppf "%a" Fmt.(styled `Bold string) slet pp_open_tag ppf tag = Fmt.kstr (pp_code ppf) "<%s>" taglet pp_close_tag ppf tag = Fmt.kstr (pp_code ppf) "</%s>" taglet pp_entity ppf name = Fmt.kstr (pp_code ppf) "&%s;" namelet pp_char_ref ppf s = Fmt.kstr (pp_code ppf) "&#%s;" s
let () = Loc.Error.register_kind_printer (function | Sort_mismatch { exp; fnd } -> Some (fun ppf -> Fmt.pf ppf "Expected %a but found %a" Sort.pp exp Sort.pp fnd) | Kinded_sort_mismatch { exp; fnd } -> Some (fun ppf -> Fmt.pf ppf "Expected %a but found %a" pp_code exp Sort.pp fnd) | Missing_attr { name } -> Some (fun ppf -> Fmt.pf ppf "Missing required attribute %a" pp_code name) | Missing_child { tag } -> Some (fun ppf -> Fmt.pf ppf "Missing required child element %a" pp_open_tag tag) | Mismatched_tags { opened; closed } -> Some (fun ppf -> Fmt.pf ppf "Mismatched tags: opened %a but closed %a" pp_open_tag opened pp_close_tag closed) | Unknown_entity name -> Some (fun ppf -> Fmt.pf ppf "Unknown entity reference: %a" pp_entity name) | Invalid_char_ref s -> Some (fun ppf -> Fmt.pf ppf "Invalid character reference: %a" pp_char_ref s) | Char_ref_range s -> Some (fun ppf -> Fmt.pf ppf "Character reference out of range: %a" pp_char_ref s) | Index_out_of_range { n; len } -> Some (fun ppf -> Fmt.pf ppf "Index %d out of bounds (element has %d children)" n len) | _ -> None)
type t = Loc.Error.t = { ctx : Loc.Context.t; meta : Loc.Meta.t; kind : kind }
let string_of_kind = Loc.Error.string_of_kindlet v = Loc.Error.vlet msg = Loc.Error.msglet raise = Loc.Error.raiselet fail = Loc.Error.faillet failf = Loc.Error.failflet push_array = Loc.Error.push_arraylet push_object = Loc.Error.push_objectlet pp = Loc.Error.pplet to_string = Loc.Error.to_string
let expected meta exp ~fnd = failf meta "Expected %a but found %a" pp_code exp pp_code fnd
let sort meta ~exp ~fnd = raise ~ctx:Loc.Context.empty ~meta (Sort_mismatch { exp; fnd })
let kinded_sort meta ~exp ~fnd = raise ~ctx:Loc.Context.empty ~meta (Kinded_sort_mismatch { exp; fnd })
let missing_attr meta name = raise ~ctx:Loc.Context.empty ~meta (Missing_attr { name })
let missing_child meta tag = raise ~ctx:Loc.Context.empty ~meta (Missing_child { tag })
let mismatched_tags meta ~opened ~closed = raise ~ctx:Loc.Context.empty ~meta (Mismatched_tags { opened; closed })
let unknown_entity meta name = raise ~ctx:Loc.Context.empty ~meta (Unknown_entity name)
let invalid_char_ref meta s = raise ~ctx:Loc.Context.empty ~meta (Invalid_char_ref s)
let char_ref_range meta s = raise ~ctx:Loc.Context.empty ~meta (Char_ref_range s)
let index_out_of_range meta ~n ~len = raise ~ctx:Loc.Context.empty ~meta (Index_out_of_range { n; len })
let no_decoder meta ~kind = failf meta "No decoder for %a" pp_code kindlet no_encoder meta ~kind = failf meta "No encoder for %a" pp_code kind