From a9628fda0fc027c54b5ed5313a0c41731d79e41f Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Thu, 19 Nov 2026 08:47:29 +0100 Subject: Create all the datatypes in a single time. Use aliases for the type name for clarification --- lib/configuration/ast.ml | 85 +++++++++--------------------------------------- 1 file changed, 15 insertions(+), 70 deletions(-) (limited to 'lib/configuration/ast.ml') diff --git a/lib/configuration/ast.ml b/lib/configuration/ast.ml index d874428..607031e 100644 --- a/lib/configuration/ast.ml +++ b/lib/configuration/ast.ml @@ -1,81 +1,31 @@ open StdLabels - module Splay = Splay.Make (Model.Identifier) 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; -} +type setting = { mutable contents : Model.content Splay.t; lists : list' list } let init : unit -> setting = fun () -> { contents = Splay.empty (); lists = [] } (** Get the content from the setting. *) -let get_content : - setting -> Model.Identifier.t -> Model.Reference.t Model.Content.t option = +let get_content : setting -> Model.Identifier.t -> Model.content option = fun settings name -> Splay.find name settings.contents +(** Check whether a list with the given name is declared in the setting *) +let has_list : setting -> Model.Identifier.t -> bool = + fun setting name -> + List.exists setting.lists ~f:(fun { id; _ } -> + String.equal id (Model.Identifier.to_string name)) + exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn exception NotImplemented : ('a Type.Id.t * 'a * Model.Identifier.t) -> 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 * Model.Identifier.t) 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 -> - (* Also check from the list catalog *) - begin match - List.find_opt setting.lists ~f:(fun { id; _ } -> - String.equal id - (Model.Identifier.to_string name)) - with - | Some v -> - ignore v; - raise_notrace - (NotImplemented (ty, value, name)) - | None -> - raise_notrace (BrokenLink (ty, value, name)) - end)) - 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 + giving the possibility to fetch any content or subcontent by its name without traversing the whole tree each time. *) -let register_content : - 'a Type.Id.t -> setting -> ('a * Model.Identifier.t) Model.Content.t -> unit = - fun ty setting content -> +let register_content : setting -> Model.content -> unit = + fun setting content -> let rec traverse content = let () = setting.contents <- @@ -83,7 +33,7 @@ let register_content : in List.iter ~f:traverse (Model.Content.subcontents content) in - traverse (convert ty setting content) + traverse content let register_list : setting -> list' -> setting = fun t l -> { t with lists = l :: t.lists } @@ -92,9 +42,7 @@ let register_list : setting -> list' -> setting = 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 - = +let get_reference_target : Model.field -> Model.content option = fun field -> let (T ty) = Model.Field.type_ field in match ty with @@ -106,10 +54,7 @@ let get_reference_target : (** 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 - = +let targeted_contents : Model.content -> (Model.field * Model.content) Seq.t = fun t -> Seq.filter_map (fun field -> @@ -145,7 +90,7 @@ let tree : ?required:bool -> setting -> Graph_tree.t = (** 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 = +let root_contents : setting -> Model.content list = fun setting -> Graph_tree.fold (fun a acc -> a :: acc) (tree setting) [] let check : setting -> (unit, exn) result = -- cgit v1.2.3