From 830e7c5d27d161b91402414e6ecd49f1ed818bfb Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Fri, 27 Mar 2026 08:48:05 +0100 Subject: Initial commit --- lib/configuration/ast.ml | 154 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 lib/configuration/ast.ml (limited to 'lib/configuration/ast.ml') diff --git a/lib/configuration/ast.ml b/lib/configuration/ast.ml new file mode 100644 index 0000000..5857902 --- /dev/null +++ b/lib/configuration/ast.ml @@ -0,0 +1,154 @@ +open StdLabels + +module Splay = Splay.Make (struct + include String + + let pp = Format.pp_print_string +end) + +let escaped = String.escaped + +type list' = { id : string; source : string } + +type setting = { + mutable contents : Model.Reference.t Model.Content.t Splay.t; + lists : list' list; +} + +let init : unit -> setting = fun () -> { contents = Splay.empty (); lists = [] } + +(** Get the content from the setting. *) +let get_content : setting -> string -> Model.Reference.t Model.Content.t option + = + fun settings name -> Splay.find name settings.contents + +exception BrokenLink : ('a Type.Id.t * 'a * string) -> exn + +(** Convert the content having fields referencing element by name into content + having fields referencing other contents *) +let rec convert : + 'a Type.Id.t -> + setting -> + ('a * string) Model.Content.t -> + Model.Reference.t Model.Content.t = + fun ty setting content -> + let subcontents = + List.map (Model.Content.subcontents content) ~f:(convert ty setting) + in + let sections = + List.map + ~f:(fun section -> + let section' : Model.Reference.t Model.Section.t = + { + section with + fields = + List.map section.Model.Section.fields ~f:(fun field -> + Model.Field.map + (fun (value, name) -> + (* Raise a local exception here, which will be checked + later once the configuration will be completely + loaded *) + Model.Reference.v (fun () -> + match get_content setting name with + | Some v -> v + | None -> raise (BrokenLink (ty, value, name)))) + field); + } + in + section') + (Model.Content.sections content) + in + Model.Content.v (Model.Content.id content) sections subcontents + +(** Register a content and all the associated subcontent in the top level, + giving the possibility to fetch any content or subcontent by is’s name + without traversing the whole tree each time. *) +let register_content : + 'a Type.Id.t -> setting -> ('a * string) Model.Content.t -> unit = + fun ty setting content -> + let rec traverse content = + let () = + setting.contents <- + Splay.add (Model.Content.id content) content setting.contents + in + List.iter ~f:traverse (Model.Content.subcontents content) + in + traverse (convert ty setting content) + +let register_list : setting -> list' -> setting = + fun t l -> { t with lists = l :: t.lists } + +(** Force the evaluation of the field target and ensure it points to an existing + content in the application. + + Return the content targeted *) +let get_reference_target : + Model.Reference.t Model.Field.t -> Model.Reference.t Model.Content.t option + = + fun field -> + let (T ty) = Model.Field.type_ field in + match ty with + | Text -> None + | Integer -> None + | MonoReference r | MultiReference r -> ( + try Some (Model.Reference.get r.Model.Typ.t) + with BrokenLink _ as e -> raise_notrace e) + +(** Extract the other contents pointed by a given content, and the field used + for it. *) +let targeted_contents : + Model.Reference.t Model.Content.t -> + (Model.Reference.t Model.Field.t * Model.Reference.t Model.Content.t) Seq.t + = + fun t -> + Seq.filter_map + (fun field -> + match get_reference_target field with + | None -> None + | Some ref -> Some (field, ref)) + (Model.Content.fields t) + +let tree : ?required:bool -> setting -> Graph_tree.t = + fun ?(required = false) setting -> + let tree = Graph_tree.create () in + let () = + Splay.fold + (fun () (_, src) -> + let targets = targeted_contents src in + let root = Model.Content.root src in + Graph_tree.add_content tree root; + Seq.iter + (fun (field, dst) -> + (* We build the dependencies tree with the root contents only. + This does not change the logic, and this makes things easer + too *) + let add_edge = (not required) || Model.Field.required field in + match add_edge with + | false -> () + | true -> + Graph_tree.add_edge tree root field (Model.Content.root dst)) + targets) + () setting.contents + in + tree + +(** Return all the contents declared in the settings. + + The list provide the root contents in the topological order *) +let root_contents : setting -> Model.Reference.t Model.Content.t list = + fun setting -> Graph_tree.fold (fun a acc -> a :: acc) (tree setting) [] + +let check : setting -> (unit, exn) result = + fun setting -> + (* Ensure each content referenced exists. + + We accept mutually recursive dependancy and need to load all the content + first before checking the settings *) + try + let _ = tree setting in + + (* Check cycle dependencies *) + let required_tree = tree ~required:true setting in + Graph_tree.check_cycle required_tree; + Ok () + with e -> Error e -- cgit v1.2.3