(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire . 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) s let pp_open_tag ppf tag = Fmt.kstr (pp_code ppf) "<%s>" tag let pp_close_tag ppf tag = Fmt.kstr (pp_code ppf) "" tag let pp_entity ppf name = Fmt.kstr (pp_code ppf) "&%s;" name let 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_kind let v = Loc.Error.v let msg = Loc.Error.msg let raise = Loc.Error.raise let fail = Loc.Error.fail let failf = Loc.Error.failf let push_array = Loc.Error.push_array let push_object = Loc.Error.push_object let pp = Loc.Error.pp let 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 kind let no_encoder meta ~kind = failf meta "No encoder for %a" pp_code kind