--- /dev/null
+(*
+ ||M|| This file is part of HELM, an Hypertextual, Electronic
+ ||A|| Library of Mathematics, developed at the Computer Science
+ ||T|| Department, University of Bologna, Italy.
+ ||I||
+ ||T|| HELM is free software; you can redistribute it and/or
+ ||A|| modify it under the terms of the GNU General Public License
+ \ / version 2 or (at your option) any later version.
+ \ / This software is distributed as is, NO WARRANTY.
+ V_______________________________________________________________ *)
+
+module U = NUri
+module H = U.UriHash
+module C = Cps
+module E = Entity
+module A = Aut
+module D = Drg
+
+(* qualified identifier: uri, name, qualifiers *)
+type qid = D.uri * D.id * D.id list
+
+type environment = D.lenv H.t
+
+type context_node = qid option (* context node: None = root *)
+
+type 'b status = {
+ henv: environment; (* optimized global environment *)
+ path: D.id list; (* current section path *)
+ hcnt: environment; (* optimized context *)
+ node: context_node; (* current context node *)
+ nodes: context_node list; (* context node list *)
+ line: int; (* line number *)
+ mk_uri:'b E.uri_generator (* uri generator *)
+}
+
+type resolver = Local of int
+ | Global of D.lenv
+
+let hsize = 7000 (* hash tables initial size *)
+
+(* Internal functions *******************************************************)
+
+let initial_status size mk_uri = {
+ path = []; node = None; nodes = []; line = 1; mk_uri = mk_uri;
+ henv = H.create size; hcnt = H.create size
+}
+
+let mk_lref f i = f (D.LRef ([], i))
+
+let mk_abst id w = D.Abst ([D.Name (id, true)], w)
+
+let id_of_name (id, _, _) = id
+
+let mk_qid f st id path =
+ let str = String.concat "/" path in
+ let str = Filename.concat str id in
+ let f str = f (U.uri_of_string str, id, path) in
+ st.mk_uri f str
+
+let uri_of_qid (uri, _, _) = uri
+
+let complete_qid f st (id, is_local, qs) =
+ let f path = C.list_rev_append (mk_qid f st id) path ~tail:qs in
+ let rec skip f = function
+ | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
+ | _ :: ptl, _ :: _ -> skip f (ptl, qs)
+ | _ -> f []
+ in
+ if is_local then f st.path else skip f (st.path, qs)
+
+let relax_qid f st (_, id, path) =
+ let f = function
+ | _ :: tl -> C.list_rev (mk_qid f st id) tl
+ | [] -> assert false
+ in
+ C.list_rev f path
+
+let relax_opt_qid f st = function
+ | None -> f None
+ | Some qid -> let f qid = f (Some qid) in relax_qid f st qid
+
+let resolve_gref err f st qid =
+ try let cnt = H.find st.henv (uri_of_qid qid) in f qid cnt
+ with Not_found -> err ()
+
+let resolve_gref_relaxed f st qid =
+ let rec err () = relax_qid (resolve_gref err f st) st qid in
+ resolve_gref err f st qid
+
+let get_cnt err f st = function
+ | None -> f []
+ | Some qid as node ->
+ try let cnt = H.find st.hcnt (uri_of_qid qid) in f cnt
+ with Not_found -> err node
+
+let get_cnt_relaxed f st =
+ let rec err node = relax_opt_qid (get_cnt err f st) st node in
+ get_cnt err f st st.node
+
+let rec xlate_term f st lenv = function
+ | A.Sort s ->
+ let f h = f (D.Sort ([], h)) in
+ if s then f 0 else f 1
+ | A.Appl (v, t) ->
+ let f vv tt = f (D.Appl ([], [vv], tt)) in
+ let f vv = xlate_term (f vv) st lenv t in
+ xlate_term f st lenv v
+ | A.Abst (name, w, t) ->
+ let f ww =
+ let b = mk_abst name ww in
+ let f tt = f (D.Bind (b, tt)) in
+ xlate_term f st (b :: lenv) t
+ in
+ xlate_term f st lenv w
+ | A.GRef (name, args) ->
+ let g qid cnt =
+ let map1 f = xlate_term f st lenv in
+ let map2 f b =
+ let f id _ = D.resolve_lref Cps.err (mk_lref f) id lenv in
+ D.name_of_binder C.err f b
+ in
+ let f tail =
+ let f args = f (D.Appl ([], args, D.GRef ([], uri_of_qid qid))) in
+ let f cnt = C.list_rev_map_append f map2 cnt ~tail in
+ C.list_sub_strict f cnt args
+ in
+ C.list_map f map1 args
+ in
+ let g qid = resolve_gref_relaxed g st qid in
+ let err () = complete_qid g st name in
+ D.resolve_lref err (mk_lref f) (id_of_name name) lenv
+
+let xlate_entity f st = function
+ | A.Section (Some (_, name)) ->
+ f {st with path = name :: st.path; nodes = st.node :: st.nodes} None
+ | A.Section None ->
+ begin match st.path, st.nodes with
+ | _ :: ptl, nhd :: ntl ->
+ f {st with path = ptl; node = nhd; nodes = ntl} None
+ | _ -> assert false
+ end
+ | A.Context None ->
+ f {st with node = None} None
+ | A.Context (Some name) ->
+ let f name = f {st with node = Some name} None in
+ complete_qid f st name
+ | A.Block (name, w) ->
+ let f qid =
+ let f cnt =
+ let f ww =
+ H.add st.hcnt (uri_of_qid qid) (mk_abst name ww :: cnt);
+ f {st with node = Some qid} None
+ in
+ xlate_term f st cnt w
+ in
+ get_cnt_relaxed f st
+ in
+ complete_qid f st (name, true, [])
+ | A.Decl (name, w) ->
+ let f cnt =
+ let f qid =
+ let f ww =
+ let b = D.Abst ([], D.Proj ([], cnt, ww)) in
+ let entry = st.line, uri_of_qid qid, b in
+ H.add st.henv (uri_of_qid qid) cnt;
+ f {st with line = succ st.line} (Some entry)
+ in
+ xlate_term f st cnt w
+ in
+ complete_qid f st (name, true, [])
+ in
+ get_cnt_relaxed f st
+ | A.Def (name, w, trans, v) ->
+ let f cnt =
+ let f qid =
+ let f ww vv =
+ let a = if trans then [] else [D.Priv] in
+ let b = D.Abbr (a, D.Proj ([], cnt, D.Cast ([], ww, vv))) in
+ let entry = st.line, uri_of_qid qid, b in
+ H.add st.henv (uri_of_qid qid) cnt;
+ f {st with line = succ st.line} (Some entry)
+ in
+ let f ww = xlate_term (f ww) st cnt v in
+ xlate_term f st cnt w
+ in
+ complete_qid f st (name, true, [])
+ in
+ get_cnt_relaxed f st
+
+(* Interface functions ******************************************************)
+
+let initial_status mk_uri =
+ initial_status hsize mk_uri
+
+let drg_of_aut = xlate_entity