summaryrefslogtreecommitdiff
path: root/lib/configuration/ast.ml
blob: 8ea92b2fa972d0b32d9a39729ea08cf1ee806115 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
open StdLabels
module Splay = Splay.Make (Model.Identifier)

let escaped = String.escaped

type setting = {
  mutable contents : Model.content Splay.t;
  mutable lists : Model.list Splay.t;
}

let init : unit -> setting =
 fun () -> { contents = Splay.empty (); lists = Splay.empty () }

(** Get the content from the setting. *)
let get_content : setting -> Model.Identifier.t -> Model.content option =
 fun settings name -> Splay.find name settings.contents

(** Get the list declared with the given name, if any *)
let get_list : setting -> Model.Identifier.t -> Model.list option =
 fun setting name -> Splay.find name setting.lists

(** All the lists declared in the setting, in a deterministic (identifier) order
*)
let lists : setting -> Model.list list =
 fun setting ->
  Splay.fold (fun acc (_, list) -> list :: acc) [] setting.lists
  |> List.sort ~cmp:(fun a b ->
      Model.Identifier.compare a.Model.DataList.id b.Model.DataList.id)

exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn

exception ConflictingFields of Model.Identifier.t * Model.Identifier.t
(** [ConflictingFields (owner, field)]: the content or list [owner] declares two
    fields named [field] with different types *)

(** Register a content and all the associated subcontent in the top level,
    giving the possibility to fetch any content or subcontent by its name
    without traversing the whole tree each time. *)
let register_content : setting -> Model.content -> unit =
 fun 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 content

let register_list : setting -> Model.list -> setting =
 fun setting list ->
  let () =
    setting.lists <- Splay.add list.Model.DataList.id list setting.lists
  in
  setting

(** 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.field -> Model.content option =
 fun field ->
  let (T ty) = Model.Field.type_ field in
  match ty with
  | Text -> None
  | Integer -> None
  | MonoReference r | MultiReference r -> (
      (* References to static lists are leaves of the dependency graph: they
         are not followed here. *)
      match r.Model.Typ.t with
      | Model.Reference.List _ -> None
      | Model.Reference.Content ref -> (
          try Some (Model.Reference.get ref)
          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.content -> (Model.field * Model.content) 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.content list =
 fun setting -> Graph_tree.fold (fun a acc -> a :: acc) (tree setting) []

(** Ensure that two fields with the same name in a content or a list always have
    the same type — otherwise reading the field by name would be ambiguous. *)
let check_fields : Model.Identifier.t -> Model.field Seq.t -> unit =
 fun owner fields ->
  let seen : (Model.Identifier.t, Model.field) Hashtbl.t = Hashtbl.create 16 in
  Seq.iter
    (fun field ->
      let name = Model.Field.name field in
      match Hashtbl.find_opt seen name with
      | None -> Hashtbl.replace seen name field
      | Some first ->
          let (T t1) = Model.Field.type_ first in
          let (T t2) = Model.Field.type_ field in
          if Option.is_none (Model.Typ.eq t1 t2) then
            raise (ConflictingFields (owner, name)))
    fields

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;

    (* Check that fields sharing a name have the same type *)
    let () =
      Splay.fold
        (fun () (_, content) ->
          check_fields (Model.Content.id content) (Model.Content.fields content))
        () setting.contents
    in
    let () =
      Splay.fold
        (fun () (_, list) ->
          check_fields list.Model.DataList.id
            (List.to_seq list.Model.DataList.fields))
        () setting.lists
    in
    Ok ()
  with e -> Error e