···3232 let@ tree_dir = List.iter @~ paths in
3333 assert (Eio.Path.is_directory tree_dir);
3434 let@ tree = Seq.iter @~ Phases.load tree_dir in
3535- let lsp_uri = Tree.lsp_uri tree in
3535+ let lsp_uri = Tree.Loaded.uri tree in
3636 let uri = URI.of_lsp_uri ~base:forest.config.url lsp_uri in
3737 URI.Tbl.replace forest.resolver uri (Lsp.Uri.to_path lsp_uri);
3838 forest.={uri} <- Tree tree
···117117 (Done, forest)
118118 | Load_tree path ->
119119 let doc = Imports.load_tree path in
120120- let lsp_uri = Tree.lsp_uri doc in
120120+ let lsp_uri = Tree.Loaded.uri doc in
121121 let uri = URI.of_lsp_uri ~base:forest.config.url lsp_uri in
122122 forest.={uri} <- Tree doc;
123123 (Parse lsp_uri, forest)
-14
lib/compiler/Error.ml
···24242525and latex_error = {range: Grace.Range.t; msg: string}
26262727-(* There are only a couple of situations I can think of where it makes sense to
2828- have this error without a range:
2929- - In the server, if the user enters a nonexistent link via the url bar.
3030- - The case of a typo should actually be covered by the Broken_link error.
3131- - ... any others?
3232- *)
3333-and resource_not_found = {uri: URI.t; range: Range.t option}
3434-3527and unlinked_attribution_warning = {
3628 role: T.attribution_role;
3729 range: Range.t option;
···171163let render_parse_error Parse.{range; msg} =
172164 let labels = [empty_label ~range] in
173165 Diagnostic.createf Error ~code:`Parse_error ~labels "%s" msg
174174-175175-let render_resource_not_found ({range; _} : resource_not_found) =
176176- let labels =
177177- Option.fold ~none:[] ~some:(fun range -> [empty_label ~range]) range
178178- in
179179- Diagnostic.createf ~labels Error ""
180166181167let render_io_error _ = Diagnostic.createf Error "io_error"
182168
+8-2
lib/compiler/Eval.ml
···796796 source_path:string option ->
797797 Tree.(expanded tree) ->
798798 eval_result option * Error.t list =
799799- fun ~config ~uri ~source_path {tree = {nodes; _}; source; _} ->
799799+ fun ~config ~uri ~source_path tree ->
800800 let res = ref None in
801801 let errors =
802802 let@ () = Error.collect in
803803 let fm = T.default_frontmatter ~uri ?source_path () in
804804 let env = initial_eval_env config (ref fm) in
805805- let range = Option.map Range.(fun source -> total ~source) source in
805805+ let source = Tree.Expanded.source tree in
806806+ let nodes = Tree.Expanded.nodes tree in
807807+ let range =
808808+ Option.map
809809+ Range.(fun source -> total ~source)
810810+ (source :> Grace.Source.t option)
811811+ in
806812 match eval_tree_inner ~range ~env ~uri nodes with
807813 | Error error -> Error.yield_eval_error error
808814 | Ok main ->
+2-1
lib/compiler/Expand.ml
···10881088 let nodes = expand_eff ~forest Tree.(nodes code) in
10891089 let units = Sc.get_export () in
10901090 let tree : Tree.expanded = {nodes; code = Tree.nodes code; units} in
10911091- Tree.{tree; source = code.source; phase = Expanded}
10911091+ let source = Tree.Parsed.source code in
10921092+ Tree.Expanded.create ?source tree
1092109310931094let expand_tree ~(forest : State.t) (code : Tree.(parsed tree)) :
10941095 Tree.(expanded tree) * Error.t list =
+1-1
lib/compiler/Imports.ml
···3030 {languageId = "forester"; text = content; uri; version = 1};
3131 }
3232 in
3333- {phase = Loaded; tree = doc; source = Some (`File path_str)}
3333+ Tree.Loaded.create ~source:(`File path_str) doc
34343535(* Only add edge if both vertices are already present*)
3636let add_edge g v w =
+17-50
lib/compiler/State.ml
···7676 | None -> URI.Tbl.replace state.index uri tree
7777 | Some existing ->
7878 if
7979- Option.(compare Grace.Source.compare)
8080- (Tree.source tree) (Tree.source existing)
8181- = 0
7979+ (Option.compare compare) (Tree.source tree) (Tree.source existing) = 0
8280 && Tree.(equal_phases tree existing)
8381 then
8482 let () = state.?{uri} <- [Error.duplicate_tree ~uri] in
···9088 let ( ./{} ) state uri =
9189 Option.bind (URI.Tbl.find_opt state.index uri) Tree.get_units
92909393- (*
9494- (* updating units*)
9595- let ( ./{}<- ) state uri units : _ result =
9696- match URI.Tbl.find_opt state.index uri with
9797- | None ->
9898- error
9999- @@ Diagnostic.createf Error ~code:`Internal_error
100100- "Updating units: %a not found" URI.pp uri
101101- | Some (Document _) | Some (Parsed _) ->
102102- error
103103- @@ Diagnostic.createf Error ~code:`Internal_error
104104- "%a has not been expanded yet" URI.pp uri
105105- | Some (Expanded expanded) ->
106106- ok @@ URI.Tbl.replace state.index uri (Expanded {expanded with units})
107107- | Some (Resource _) -> Ok ()
108108- *)
109109-11091 (* @ for article/resource *)
11192 let ( .@{} ) state uri =
112112- match URI.Tbl.find_opt state.index uri with
113113- | Some (Tree {phase = Evaluated; tree; _}) -> Some tree.resource
114114- | _ -> None
9393+ Option.bind (URI.Tbl.find_opt state.index uri) Tree.to_resource
11594end
1169511796open Syntax
···296275 let@ wrong_variant = List.iter @~ wrong_variants_for_uri uri in
297276 URI.Tbl.add forest.suggestions wrong_variant uri
298277 end;
299299- match forest.={uri} with
300300- | None ->
301301- forest.={uri} <-
302302- Tree
303303- {
304304- phase = Evaluated;
305305- tree = {resource; route_locally; include_in_manifest; expanded = None};
306306- source = None;
307307- }
308308- | Some (Tree {phase; tree; source}) -> begin
309309- match phase with
310310- | Expanded ->
311311- forest.={uri} <-
312312- Tree
313313- {
314314- phase = Evaluated;
315315- tree =
316316- {
317317- resource;
318318- route_locally;
319319- include_in_manifest;
320320- expanded = Some tree;
321321- };
322322- source;
323323- }
324324- | Loaded | Evaluated | Parsed -> assert false
325325- end
278278+ let tree =
279279+ Option.fold forest.={uri}
280280+ ~some:begin fun tree ->
281281+ let expanded = Tree.to_expanded tree in
282282+ let source = Tree.source tree in
283283+ Tree.Evaluated.create ?source ~route_locally ~include_in_manifest
284284+ ?expanded resource
285285+ end
286286+ ~none:begin
287287+ let source = (* TODO: *) None in
288288+ Tree.Evaluated.create ?source ~route_locally ~include_in_manifest
289289+ resource
290290+ end
291291+ in
292292+ forest.={uri} <- Tree tree
+98-35
lib/core/Tree.ml
···1212 include Base
1313end
14141515+type source = [`File of string]
1616+1517type exports = (R.P.data, Grace.Range.t option) Trie.t
16181719type loaded = Lsp.Text_document.t
1820type parsed = Code.t
1919-2020-type expanded = {nodes: Syn.t; code: Code.t; units: exports [@opaque]}
2121-[@@deriving show]
2222-2121+type expanded = {nodes: Syn.t; code: Code.t; units: exports}
2322type evaluated = {
2423 resource: T.content T.resource;
2524 route_locally: bool;
2625 include_in_manifest: bool;
2726 expanded: expanded option;
2827}
2929-[@@deriving show]
30283129type 'a tag =
3230 | Loaded : loaded tag
···3432 | Expanded : expanded tag
3533 | Evaluated : evaluated tag
36343737-type 'a tree = {phase: 'a tag; tree: 'a; source: Grace.Source.t option}
3535+type 'a tree = {phase: 'a tag; tree: 'a; source: source option}
3636+3737+module Loaded = struct
3838+ type t = loaded tree
3939+ let create : ?source:source -> loaded -> t =
4040+ fun ?source tree -> {tree; phase = Loaded; source}
4141+ let source {source; _} = source
4242+ let uri {tree; _} = Lsp.Text_document.documentUri tree
4343+end
4444+4545+module Parsed = struct
4646+ type t = parsed tree
4747+ let create : ?source:source -> parsed -> t =
4848+ fun ?source tree -> {tree; phase = Parsed; source}
4949+ let source {source; _} = source
5050+end
5151+5252+module Expanded = struct
5353+ type t = expanded tree
5454+ let create : ?source:source -> expanded -> t =
5555+ fun ?source tree -> {tree; phase = Expanded; source}
5656+ let source {source; _} = source
5757+ let nodes {tree = {nodes; _}; _} = nodes
5858+end
5959+6060+module Evaluated = struct
6161+ type t = evaluated tree
6262+ let create ~route_locally ~include_in_manifest ?expanded ?source
6363+ (resource : _ T.resource) =
6464+ let tree = {resource; route_locally; include_in_manifest; expanded} in
6565+ {tree; phase = Evaluated; source}
6666+6767+ let source {source; _} = source
6868+end
38693970type t = Tree : 'a tree -> t
40714141-let source : t -> Grace.Source.t option = function
4242- | Tree {phase; tree; source} -> source
7272+type phase = Phase : 'a tag -> phase
7373+7474+let source : t -> _ option = function
7575+ | Tree {source; _} -> begin
7676+ match source with Some s -> Some s | _ -> None
7777+ end
7878+7979+let phase : type a. a tree -> phase = function
8080+ | {phase; _} -> begin
8181+ match phase with
8282+ | Loaded -> Phase Loaded
8383+ | Parsed -> Phase Parsed
8484+ | Expanded -> Phase Expanded
8585+ | Evaluated -> Phase Evaluated
8686+ end
43874488let equal_phases =
4589 fun (Tree {phase = phase1; _}) (Tree {phase = phase2; _}) ->
···5094 | Evaluated, Evaluated -> true
5195 | _ -> false
52965353-let lsp_uri : loaded tree -> Lsp.Uri.t = function
5454- | {phase; tree; source} -> Lsp.Text_document.documentUri tree
9797+let lsp_uri : t -> Lsp.Uri.t option = function
9898+ | Tree {tree; phase; source} -> begin
9999+ match phase with
100100+ | Loaded -> Some (Lsp.Text_document.documentUri tree)
101101+ | Parsed | Expanded | Evaluated -> begin
102102+ match source with
103103+ | None -> None
104104+ | Some (`File string) -> Some (Lsp.Uri.of_string string)
105105+ end
106106+ end
5510756108let to_doc : t -> loaded option = function
5757- | Tree {phase; tree; source} -> begin
5858- match phase with
5959- | Loaded -> Some tree
6060- | Parsed -> None
6161- | Expanded -> None
6262- | Evaluated -> None
109109+ | Tree {phase; tree; _} -> begin
110110+ match phase with Loaded -> Some tree | _ -> None
63111 end
641126565-let of_code : source:Grace.Source.t -> Code.t -> Code.t tree =
6666- fun ~source nodes -> {phase = Parsed; tree = nodes; source = Some source}
113113+let of_code : path:string -> Code.t -> t =
114114+ fun ~path nodes ->
115115+ Tree {phase = Parsed; tree = nodes; source = Some (`File path)}
671166868-let of_doc : Lsp.Text_document.t -> loaded tree =
117117+let of_doc : Lsp.Text_document.t -> t =
69118 fun doc ->
70119 let uri = Lsp.Text_document.documentUri doc in
71120 let path = Lsp.Uri.to_path uri in
7272- {phase = Loaded; tree = doc; source = Some (`File path)}
121121+ Tree {phase = Loaded; tree = doc; source = Some (`File path)}
731227474-let of_syn ~source syn = {phase = Expanded; tree = syn; source}
7575-let of_resource ~source resource = {phase = Evaluated; tree = resource; source}
123123+let of_syn ~source syn = Tree {phase = Expanded; tree = syn; source}
761247777-let show_phase = function
7878- | Loaded -> "loaded"
7979- | Parsed -> "parsed"
8080- | Expanded -> "expanded"
8181- | Evaluated -> "evaluated"
125125+let syn = function {tree; _} -> tree.nodes
126126+127127+let get_syn = function
128128+ | Tree {phase = Expanded; tree; _} -> Some (tree.nodes : Syn.t)
129129+ | Tree {phase = Evaluated; tree; _} -> begin
130130+ match tree.expanded with
131131+ | Some expanded -> Some expanded.nodes
132132+ | None -> None
133133+ end
134134+ | _ -> None
8213583136let to_resource : t -> T.content T.resource option = function
84137 | Tree {phase; tree; _} -> begin
···87140 | Parsed -> None
88141 | Expanded -> None
89142 | Evaluated -> Some tree.resource
143143+ end
144144+145145+let to_expanded : t -> expanded option = function
146146+ | Tree {phase; tree; _} -> begin
147147+ match phase with
148148+ | Expanded -> Some (tree : expanded)
149149+ | Evaluated -> tree.expanded
150150+ | _ -> None
90151 end
9115292153let to_evaluated : t -> evaluated option = function
···153214 match tree.expanded with Some {units; _} -> Some units | None -> None
154215 end)
155216156156-let is_unparsed = function
157157- | Tree {phase; _} -> begin match phase with Loaded -> true | _ -> false end
217217+let is_parsed = function
218218+ | Tree {phase; _} -> begin match phase with Loaded -> false | _ -> true end
158219159159-let is_parsed tree = not @@ is_unparsed tree
220220+let is_unparsed = is_parsed >>> not
160221161161-let is_unexpanded = function
222222+let is_expanded = function
162223 | Tree {phase; _} -> begin
163163- match phase with Loaded | Parsed -> true | Expanded | Evaluated -> false
224224+ match phase with Loaded | Parsed -> false | Expanded | Evaluated -> true
164225 end
165226166166-let is_expanded tree = not @@ is_unexpanded tree
227227+let is_unexpanded = is_expanded >>> not
167228168168-let is_unevaluated = function
229229+let is_evaluated = function
169230 | Tree {phase; _} -> begin
170170- match phase with Loaded | Parsed | Expanded -> true | Evaluated -> false
231231+ match phase with Loaded | Parsed | Expanded -> false | Evaluated -> true
171232 end
233233+234234+let is_unevaluated = is_evaluated >>> not
172235173236let is_asset : t -> bool = function
174237 | Tree {phase; tree; _} -> begin
-5
lib/frontend/Legacy_xml_client.ml
···4646 xmlns: Xmlns.t;
4747}
48484949-let range ~env =
5050- let@ uri = Option.bind env.uri in
5151- let@ path = Option.map @~ URI.Tbl.find_opt env.forest.resolver uri in
5252- Range.initial (`File path)
5353-5449let render_xml_qname qname =
5550 match qname.prefix with
5651 | "" -> qname.uname
+1-1
lib/language_server/Did_change.ml
···2323 | None -> assert false
2424 | Some doc ->
2525 let updated =
2626- Tree.of_doc
2626+ Tree.Loaded.create
2727 @@ Lsp.Text_document.apply_content_changes doc params.contentChanges
2828 in
2929 forest.={uri} <- Tree updated;
+1-4
lib/parser/Parse.ml
···5858 let lexbuf = Lexing.from_string text in
5959 lexbuf.lex_curr_p <- {lexbuf.lex_curr_p with pos_fname = path};
6060 parse (`String {content = text; name = Some path}) lexbuf
6161- |> Result.map (fun nodes ->
6262- let source = `File path in
6363- (*let identity = URI (URI_scheme.path_to_uri ~base:config.url path) *)
6464- Tree.of_code ~source nodes)
6161+ |> Result.map (Tree.Parsed.create ~source:(`File path))
65626663let parse_file filename =
6764 let ch = open_in filename in
+1-1
test/Print_syn.ml
···99 let forest = State.make ~env ~dev:true ~config:Config.(default ()) () in
1010 match result with
1111 | Ok code ->
1212- let tree = Tree.of_code ~source:(`File filename) code in
1212+ let tree = Tree.Parsed.create ~source:(`File filename) code in
1313 let Tree.{tree; _}, _ = Expand.(expand_tree ~forest tree) in
1414 Format.printf "%a@." Syn.pp tree.nodes
1515 | Error _ -> assert false