]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nReference.ml
uri and references(uri)
[helm.git] / helm / software / components / ng_kernel / nReference.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 exception IllFormedReference of string Lazy.t
27
28 type spec = 
29  | Decl 
30  | Def
31  | Fix of int * int (* fixno, recparamno *)
32  | CoFix of int
33  | Ind of int
34  | Con of int * int (* indtyno, constrno *)
35
36 type reference = Ref of int * NUri.uri * spec
37
38 let eq = (==);;
39
40 module OrderedStrings =
41  struct
42   type t = string
43   let compare (s1 : t) (s2 : t) = compare s1 s2
44  end
45 ;;
46
47 module MapStringsToReference = Map.Make(OrderedStrings);;
48
49 let set_of_reference = ref MapStringsToReference.empty;;
50
51 (* '.' not allowed in path and foo
52  *
53  * Decl                   cic:/path/foo.dec
54  * Def                    cic:/path/foo.def
55  * Fix of int * int       cic:/path/foo.fix(i,j)
56  * CoFix of int           cic:/path/foo.cfx(i)
57  * Ind of int             cic:/path/foo.ind(i)
58  * Con of int * int       cic:/path/foo.con(i,j)
59  *)
60
61 let uri_suffix_of_ref_suffix = function
62     | "dec" 
63     | "def" -> "con"
64     | "ind" 
65     | "con" -> "ind"
66     | _ -> assert false
67 ;;
68
69 let reference_of_string =
70   let counter = ref 0 in 
71   let c () = incr counter; !counter in 
72   let get2 s dot =
73     let comma = String.rindex s ',' in
74     let i = int_of_string (String.sub s (dot+5) (comma-dot-5)) in
75     let j = int_of_string (String.sub s (comma+1) (String.length s-comma-2)) in
76     i,j
77   in
78   let get1 s dot =
79     let i = int_of_string (String.sub s (dot+5) (String.length s-1)) in
80     i
81   in
82 fun s ->
83   try MapStringsToReference.find s !set_of_reference
84   with Not_found ->
85     let new_reference =
86       try
87         let dot = String.rindex s '.' in
88         let prefix = String.sub s 0 (dot+1) in
89         let suffix = String.sub s (dot+1) 3 in
90         let u = NUri.uri_of_string (prefix ^ uri_suffix_of_ref_suffix suffix) in
91         match suffix with
92         | "dec" -> Ref (c (), u, Decl)
93         | "def" -> Ref (c (), u, Def)
94         | "fix" -> let i,j = get2 s dot in Ref (c (), u, Fix (i,j))
95         | "cfx" -> let i = get1 s dot in Ref (c (), u, CoFix (i))
96         | "ind" -> let i = get1 s dot in Ref (c (), u, Ind (i))
97         | "con" -> let i,j = get2 s dot in Ref (c (), u, Con (i,j))
98         | _ -> raise Not_found
99       with Not_found -> raise (IllFormedReference (lazy s))
100     in
101     set_of_reference := MapStringsToReference.add s new_reference !set_of_reference;
102     new_reference
103 ;;
104
105 let string_of_reference (Ref (_,u,indinfo)) =
106   let s = NUri.string_of_uri u in
107   let dot = String.rindex s '.' in
108   let s2 = String.sub s 0 dot in
109   match indinfo with
110   | Decl ->  s2 ^ ".dec"
111   | Def -> s2 ^ ".def"
112   | Fix (i,j)  -> s2 ^ ".fix(" ^ string_of_int i ^ "," ^ string_of_int j ^ ")"
113   | CoFix i -> s2 ^ ".cfx(" ^ string_of_int i ^ ")"
114   | Ind i -> s2 ^ ".ind(" ^ string_of_int i ^ ")"
115   | Con (i,j) -> s2 ^ ".con(" ^ string_of_int i ^ "," ^ string_of_int j ^ ")"
116 ;;
117
118 let reference_of_ouri u indinfo =
119   let u = NUri.uri_of_string (UriManager.string_of_uri u) in
120   reference_of_string (string_of_reference (Ref (~-1,u,indinfo)))
121 ;;
122
123 let ouri_of_reference (Ref (_,u,_)) =
124   UriManager.uri_of_string (NUri.string_of_uri u)
125 ;;