1 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (******************************************************************************)
30 (* Claudio Sacerdoti Coen <sacerdot@cs.unibo.it> *)
33 (* This module is the terms level of a parser for cic objects from xml *)
34 (* files to the internal representation. It is used by the module cicParser2 *)
35 (* (objects level). It defines an extension of the standard dom using the *)
36 (* object-oriented extension machinery of markup: an object with a method *)
37 (* to_cic_term that returns the internal representation of the subtree is *)
38 (* added to each node of the dom tree *)
40 (******************************************************************************)
42 exception IllFormedXml of int;;
44 (* The list of tokens of the current section path. *)
45 (* Used to resolve relative URIs *)
46 let current_sp = ref [];;
48 (* The uri of the object been parsed *)
49 let current_uri = ref (UriManager.uri_of_string "cic:/.xml");;
51 (* Utility functions to map a markup attribute to something useful *)
53 let cic_attr_of_xml_attr =
55 Pxp_types.Value s -> Cic.Name s
56 | Pxp_types.Implied_value -> Cic.Anonimous
57 | _ -> raise (IllFormedXml 1)
59 let cic_sort_of_xml_attr =
61 Pxp_types.Value "Prop" -> Cic.Prop
62 | Pxp_types.Value "Set" -> Cic.Set
63 | Pxp_types.Value "Type" -> Cic.Type
64 | _ -> raise (IllFormedXml 2)
68 Pxp_types.Value n -> int_of_string n
69 | _ -> raise (IllFormedXml 3)
73 Pxp_types.Value s -> UriManager.uri_of_string s
74 | _ -> raise (IllFormedXml 4)
76 let string_of_xml_attr =
78 Pxp_types.Value s -> s
79 | _ -> raise (IllFormedXml 5)
81 let binder_of_xml_attr =
83 Pxp_types.Value s -> s
84 | _ -> raise (IllFormedXml 17)
87 (* the "interface" of the class linked to each node of the dom tree *)
89 class virtual cic_term =
92 (* fields and methods ever required by markup *)
93 val mutable node = (None : cic_term Pxp_document.node option)
104 (* a method that returns the internal representation of the tree (term) *)
105 (* rooted in this node *)
106 method virtual to_cic_term : Cic.annterm
110 (* the class of the objects linked to nodes that are not roots of cic terms *)
111 class eltype_not_of_cic =
116 method to_cic_term = raise (IllFormedXml 6)
120 (* the class of the objects linked to nodes whose content is a cic term *)
121 (* (syntactic sugar xml entities) e.g. <type> ... </type> *)
122 class eltype_transparent =
129 match n#sub_nodes with
130 [ t ] -> t#extension#to_cic_term
131 | _ -> raise (IllFormedXml 7)
135 (* A class for each cic node type *)
144 let nofun = int_of_xml_attr (n#attribute "noFun")
145 and id = string_of_xml_attr (n#attribute "id")
147 let sons = n#sub_nodes in
150 f when f#node_type = Pxp_document.T_element "FixFunction" ->
151 let name = string_of_xml_attr (f#attribute "name")
152 and recindex = int_of_xml_attr (f#attribute "recIndex")
154 match f#sub_nodes with
156 t#node_type = Pxp_document.T_element "type" &&
157 b#node_type = Pxp_document.T_element "body" ->
158 (t#extension#to_cic_term, b#extension#to_cic_term)
159 | _ -> raise (IllFormedXml 14)
161 (name, recindex, ty, body)
162 | _ -> raise (IllFormedXml 13)
165 Cic.AFix (id, nofun, functions)
176 let nofun = int_of_xml_attr (n#attribute "noFun")
177 and id = string_of_xml_attr (n#attribute "id")
179 let sons = n#sub_nodes in
182 f when f#node_type = Pxp_document.T_element "CofixFunction" ->
183 let name = string_of_xml_attr (f#attribute "name")
185 match f#sub_nodes with
187 t#node_type = Pxp_document.T_element "type" &&
188 b#node_type = Pxp_document.T_element "body" ->
189 (t#extension#to_cic_term, b#extension#to_cic_term)
190 | _ -> raise (IllFormedXml 16)
193 | _ -> raise (IllFormedXml 15)
196 Cic.ACoFix (id, nofun, functions)
200 class eltype_implicit =
207 let id = string_of_xml_attr (n#attribute "id") in
219 let value = int_of_xml_attr (n#attribute "value")
220 and binder = binder_of_xml_attr (n#attribute "binder")
221 and id = string_of_xml_attr (n#attribute "id") in
222 Cic.ARel (id,value,binder)
233 let value = int_of_xml_attr (n#attribute "no")
234 and id = string_of_xml_attr (n#attribute "id")
237 let sons = n#sub_nodes in
239 (function substitution ->
240 match substitution#sub_nodes with
242 | [he] -> Some he#extension#to_cic_term
243 | _ -> raise (IllFormedXml 20)
246 Cic.AMeta (id,value,local_context)
257 let name = string_of_xml_attr (n#attribute "relUri")
258 and xid = string_of_xml_attr (n#attribute "id") in
259 match Str.split (Str.regexp ",") name with
265 | (n,he::tl) when n > 0 -> "/" ^ he ^ aux (n - 1, tl)
266 | _ -> raise (IllFormedXml 19)
268 aux (List.length !current_sp - n,!current_sp)
272 (UriManager.uri_of_string
273 ("cic:" ^ get_prefix (int_of_string index) ^ id ^ ".var"))
275 | _ -> raise (IllFormedXml 18)
286 let children = n#sub_nodes
287 and id = string_of_xml_attr (n#attribute "id") in
288 if List.length children < 2 then raise (IllFormedXml 8)
291 (id,List.map (fun x -> x#extension#to_cic_term) children)
302 let sons = n#sub_nodes
303 and id = string_of_xml_attr (n#attribute "id") in
306 te#node_type = Pxp_document.T_element "term" &&
307 ty#node_type = Pxp_document.T_element "type" ->
308 let term = te#extension#to_cic_term
309 and typ = ty#extension#to_cic_term in
310 Cic.ACast (id,term,typ)
311 | _ -> raise (IllFormedXml 9)
322 let sort = cic_sort_of_xml_attr (n#attribute "value")
323 and id = string_of_xml_attr (n#attribute "id") in
334 let module U = UriManager in
336 let value = uri_of_xml_attr (n#attribute "uri")
337 and id = string_of_xml_attr (n#attribute "id") in
338 Cic.AConst (id,value, U.relative_depth !current_uri value 0)
342 class eltype_mutind =
348 let module U = UriManager in
350 let name = uri_of_xml_attr (n#attribute "uri")
351 and noType = int_of_xml_attr (n#attribute "noType")
352 and id = string_of_xml_attr (n#attribute "id") in
354 (id,name, U.relative_depth !current_uri name 0, noType)
358 class eltype_mutconstruct =
364 let module U = UriManager in
366 let name = uri_of_xml_attr (n#attribute "uri")
367 and noType = int_of_xml_attr (n#attribute "noType")
368 and noConstr = int_of_xml_attr (n#attribute "noConstr")
369 and id = string_of_xml_attr (n#attribute "id") in
371 (id, name, U.relative_depth !current_uri name 0,
383 let sons = n#sub_nodes
384 and id = string_of_xml_attr (n#attribute "id") in
387 s#node_type = Pxp_document.T_element "source" &&
388 t#node_type = Pxp_document.T_element "target" ->
389 let name = cic_attr_of_xml_attr (t#attribute "binder")
390 and source = s#extension#to_cic_term
391 and target = t#extension#to_cic_term in
392 Cic.AProd (id,name,source,target)
393 | _ -> raise (IllFormedXml 10)
397 class eltype_mutcase =
403 let module U = UriManager in
405 let sons = n#sub_nodes
406 and id = string_of_xml_attr (n#attribute "id") in
408 ty::te::patterns when
409 ty#node_type = Pxp_document.T_element "patternsType" &&
410 te#node_type = Pxp_document.T_element "inductiveTerm" ->
411 let ci = uri_of_xml_attr (n#attribute "uriType")
412 and typeno = int_of_xml_attr (n#attribute "noType")
413 and inductiveType = ty#extension#to_cic_term
414 and inductiveTerm = te#extension#to_cic_term
415 and lpattern= List.map (fun x -> x#extension#to_cic_term) patterns
417 Cic.AMutCase (id,ci,U.relative_depth !current_uri ci 0,
418 typeno,inductiveType,inductiveTerm,lpattern)
419 | _ -> raise (IllFormedXml 11)
423 class eltype_lambda =
430 let sons = n#sub_nodes
431 and id = string_of_xml_attr (n#attribute "id") in
434 s#node_type = Pxp_document.T_element "source" &&
435 t#node_type = Pxp_document.T_element "target" ->
436 let name = cic_attr_of_xml_attr (t#attribute "binder")
437 and source = s#extension#to_cic_term
438 and target = t#extension#to_cic_term in
439 Cic.ALambda (id,name,source,target)
440 | _ -> raise (IllFormedXml 12)
451 let sons = n#sub_nodes
452 and id = string_of_xml_attr (n#attribute "id") in
455 s#node_type = Pxp_document.T_element "term" &&
456 t#node_type = Pxp_document.T_element "letintarget" ->
457 let name = cic_attr_of_xml_attr (t#attribute "binder")
458 and source = s#extension#to_cic_term
459 and target = t#extension#to_cic_term in
460 Cic.ALetIn (id,name,source,target)
461 | _ -> raise (IllFormedXml 12)
465 (* The definition of domspec, an hashtable that maps each node type to the *)
466 (* object that must be linked to it. Used by markup. *)
469 let module D = Pxp_document in
470 D.make_spec_from_alist
471 ~data_exemplar: (new D.data_impl (new eltype_not_of_cic))
472 ~default_element_exemplar: (new D.element_impl (new eltype_not_of_cic))
474 [ "REL", (new D.element_impl (new eltype_rel)) ;
475 "VAR", (new D.element_impl (new eltype_var)) ;
476 "META", (new D.element_impl (new eltype_meta)) ;
477 "SORT", (new D.element_impl (new eltype_sort)) ;
478 "IMPLICIT", (new D.element_impl (new eltype_implicit)) ;
479 "CAST", (new D.element_impl (new eltype_cast)) ;
480 "PROD", (new D.element_impl (new eltype_prod)) ;
481 "LAMBDA", (new D.element_impl (new eltype_lambda)) ;
482 "LETIN", (new D.element_impl (new eltype_letin)) ;
483 "APPLY", (new D.element_impl (new eltype_apply)) ;
484 "CONST", (new D.element_impl (new eltype_const)) ;
485 "MUTIND", (new D.element_impl (new eltype_mutind)) ;
486 "MUTCONSTRUCT", (new D.element_impl (new eltype_mutconstruct)) ;
487 "MUTCASE", (new D.element_impl (new eltype_mutcase)) ;
488 "FIX", (new D.element_impl (new eltype_fix)) ;
489 "COFIX", (new D.element_impl (new eltype_cofix)) ;
490 "arity", (new D.element_impl (new eltype_transparent)) ;
491 "term", (new D.element_impl (new eltype_transparent)) ;
492 "type", (new D.element_impl (new eltype_transparent)) ;
493 "body", (new D.element_impl (new eltype_transparent)) ;
494 "source", (new D.element_impl (new eltype_transparent)) ;
495 "target", (new D.element_impl (new eltype_transparent)) ;
496 "letintarget", (new D.element_impl (new eltype_transparent)) ;
497 "patternsType", (new D.element_impl (new eltype_transparent)) ;
498 "inductiveTerm", (new D.element_impl (new eltype_transparent)) ;
499 "pattern", (new D.element_impl (new eltype_transparent))