Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197open Coreopen Scanner
let locate m f = try f () with Loc.Error e when Loc.Meta.is_none e.Loc.Error.meta -> Loc.Error.raise ~ctx:e.Loc.Error.ctx ~meta:m e.Loc.Error.kind
let rec scalar : type a. a codec -> value -> a = fun codec value -> let m = Core.meta value in let s = match value with | String (s, _) -> s | Unset _ -> "" | _ -> Error.decode m "expected a configuration variable value" in match codec with | String_codec -> s | Bool -> ( match (value, String.lowercase_ascii s) with | Unset _, _ -> true | _, ("true" | "on" | "yes") -> true | _, ("" | "false" | "off" | "no") -> false | _ -> ( match Number.signed ~max:0x7fff_ffff s with | Some n -> n <> 0 | None -> Error.decode m "invalid Git boolean")) | Int -> ( match Number.signed ~max:max_int s with | Some n -> n | None -> Error.decode m "invalid Git integer") | Map (dec, _, c) -> locate m (fun () -> dec (scalar c value)) | Value -> value | Section_codec _ | Sections _ | File_codec _ -> Error.decode m "expected a file codec, found a variable"
let push name m f = try f () with Loc.Error e -> Loc.Error.push_object ("configuration", Loc.Meta.none) (name, m) e
let rec fields : type o f. (o, f) Core.fields -> (member -> bool) * (unit -> f) = function | Make f -> ((fun _ -> false), fun () -> f) | Member (before, key, codec, _, absent, duplicate) -> let previous, finish = fields before in let stored = ref None in let consume (member : member) = if fst member.name <> key then previous member else ( if duplicate = `Refuse && Option.is_some !stored then Error.decode member.meta ("duplicate variable " ^ key); stored := Some (push key member.meta (fun () -> scalar codec member.value)); true) in ( consume, fun () -> let v = match (!stored, absent) with | Some v, _ | None, Some v -> v | None, None -> Error.decode Loc.Meta.none ("missing variable " ^ key) in finish () v )
let compile_section name sub description = let consume, finish = fields description in let found = ref None in let matches h = fst h.name = name && Option.map fst h.sub = sub in ( (function | Header h when matches h -> found := Some h.meta | Variable (h, m) when matches h -> ignore (consume m) | _ -> ()), fun () -> match !found with | None -> Error.decode Loc.Meta.none ("missing section " ^ name) | Some m -> push name m finish )
let compile_sections name description = let table = Hashtbl.create 8 and ordered = ref [] in let ensure h = let sub = Option.map fst h.sub in match Hashtbl.find_opt table sub with | Some state -> state | None -> let consume, finish = fields description in let state = (consume, finish, h.meta) in Hashtbl.add table sub state; ordered := sub :: !ordered; state in ( (function | Header h when fst h.name = name -> ignore (ensure h) | Variable (h, m) when fst h.name = name -> let consume, _, _ = ensure h in ignore (consume m) | _ -> ()), fun () -> List.rev_map (fun sub -> let _, finish, m = Hashtbl.find table sub in (sub, push name m finish)) !ordered )
let compile_value () = let sections = ref [] and members = ref [] and current = ref None and root_meta = ref Loc.Meta.none in let finish_section () = match !current with | None -> () | Some h -> sections := Section ( { name = h.name; subsection = h.sub; members = List.rev !members }, h.meta ) :: !sections; members := [] in ( (function | Header h -> finish_section (); current := Some h | Variable (_, m) -> members := m :: !members | End m -> root_meta := m), fun () -> finish_section (); File (List.rev !sections, !root_meta) )
let rec compile : type a. a codec -> (event -> unit) * (unit -> a) = function | Section_codec (name, sub, description) -> compile_section name sub description | Sections (name, description) -> compile_sections name description | Value -> compile_value () | File_codec description -> file_fields description | Map (dec, _, codec) -> let consume, finish = compile codec in (consume, fun () -> dec (finish ())) | String_codec | Bool | Int -> Error.decode Loc.Meta.none "a configuration file needs a section or file codec"
and file_fields : type o f. (o, f) Core.fields -> (event -> unit) * (unit -> f) = function | Make f -> ((fun _ -> ()), fun () -> f) | Member (before, _, codec, _, absent, _) -> let consume_before, finish_before = file_fields before in let consume, finish = compile codec in let seen = ref false in ( (fun event -> consume_before event; (match (codec, event) with | Section_codec (name, sub, _), Header h when fst h.name = name && Option.map fst h.sub = sub -> seen := true | Sections (name, _), Header h when fst h.name = name -> seen := true | _ -> ()); consume event), fun () -> let v = match (!seen, absent) with false, Some v -> v | _ -> finish () in finish_before () v )
let of_string ?(file = Loc.file_none) ?(meta = `Locs) codec text = let consume, finish = compile codec in let m = Scanner.scan ~file ~meta text consume in locate m finish
let of_value : type a. a codec -> value -> a = fun codec value -> if Core.scalar codec then scalar codec value else match codec with | Value -> value | _ -> let consume, finish = compile codec in (match value with | File (sections, m) -> List.iter (function | Section (s, sm) -> let h = { name = s.name; sub = s.subsection; meta = sm } in consume (Header h); List.iter (fun variable -> consume (Variable (h, variable))) s.members | other -> Error.decode (Core.meta other) "expected a section") sections; consume (End m) | other -> Error.decode (Core.meta other) "expected a configuration file"); locate (Core.meta value) finish