1 (******************************************************************************)
5 (* Claudio Sacerdoti Coen <sacerdot@cs.unibo.it> *)
8 (* This module is the terms level of a parser for cic objects from xml *)
9 (* files to the internal representation. It is used by the module cicParser2 *)
10 (* (objects level). It defines an extension of the standard dom using the *)
11 (* object-oriented extension machinery of markup: an object with a method *)
12 (* to_cic_term that returns the internal representation of the subtree is *)
13 (* added to each node of the dom tree *)
15 (******************************************************************************)
17 exception IllFormedXml of int;;
19 (* The hashtable from the current identifiers to the object or the terms *)
20 let ids_to_targets = ref None;;
22 (* The list of tokens of the current section path. *)
23 (* Used to resolve relative URIs *)
24 let current_sp = ref [];;
26 (* The uri of the object been parsed *)
27 let current_uri = ref (UriManager.uri_of_string "cic:/.xml");;
29 (* True if annotation really matter *)
30 let process_annotations = ref false;;
32 (* Utility functions to map a markup attribute to something useful *)
34 let cic_attr_of_xml_attr =
36 Pxp_types.Value s -> Cic.Name s
37 | Pxp_types.Implied_value -> Cic.Anonimous
38 | _ -> raise (IllFormedXml 1)
40 let cic_sort_of_xml_attr =
42 Pxp_types.Value "Prop" -> Cic.Prop
43 | Pxp_types.Value "Set" -> Cic.Set
44 | Pxp_types.Value "Type" -> Cic.Type
45 | _ -> raise (IllFormedXml 2)
49 Pxp_types.Value n -> int_of_string n
50 | _ -> raise (IllFormedXml 3)
54 Pxp_types.Value s -> UriManager.uri_of_string s
55 | _ -> raise (IllFormedXml 4)
57 let string_of_xml_attr =
59 Pxp_types.Value s -> s
60 | _ -> raise (IllFormedXml 5)
62 let binder_of_xml_attr =
64 Pxp_types.Value s -> if !process_annotations then Some s else None
65 | _ -> raise (IllFormedXml 17)
68 let register_id id node =
69 if !process_annotations then
70 match !ids_to_targets with
72 | Some ids_to_targets ->
73 Hashtbl.add ids_to_targets id (Cic.Term node)
76 (* the "interface" of the class linked to each node of the dom tree *)
78 class virtual cic_term =
81 (* fields and methods ever required by markup *)
82 val mutable node = (None : cic_term Pxp_document.node option)
93 (* a method that returns the internal representation of the tree (term) *)
94 (* rooted in this node *)
95 method virtual to_cic_term : Cic.annterm
99 (* the class of the objects linked to nodes that are not roots of cic terms *)
100 class eltype_not_of_cic =
105 method to_cic_term = raise (IllFormedXml 6)
109 (* the class of the objects linked to nodes whose content is a cic term *)
110 (* (syntactic sugar xml entities) e.g. <type> ... </type> *)
111 class eltype_transparent =
118 match n#sub_nodes with
119 [ t ] -> t#extension#to_cic_term
120 | _ -> raise (IllFormedXml 7)
124 (* A class for each cic node type *)
133 let nofun = int_of_xml_attr (n#attribute "noFun")
134 and id = string_of_xml_attr (n#attribute "id")
136 let sons = n#sub_nodes in
139 f when f#node_type = Pxp_document.T_element "FixFunction" ->
140 let name = string_of_xml_attr (f#attribute "name")
141 and recindex = int_of_xml_attr (f#attribute "recIndex")
143 match f#sub_nodes with
145 t#node_type = Pxp_document.T_element "type" &&
146 b#node_type = Pxp_document.T_element "body" ->
147 (t#extension#to_cic_term, b#extension#to_cic_term)
148 | _ -> raise (IllFormedXml 14)
150 (name, recindex, ty, body)
151 | _ -> raise (IllFormedXml 13)
154 let res = Cic.AFix (id, ref None, nofun, functions) in
167 let nofun = int_of_xml_attr (n#attribute "noFun")
168 and id = string_of_xml_attr (n#attribute "id")
170 let sons = n#sub_nodes in
173 f when f#node_type = Pxp_document.T_element "CofixFunction" ->
174 let name = string_of_xml_attr (f#attribute "name")
176 match f#sub_nodes with
178 t#node_type = Pxp_document.T_element "type" &&
179 b#node_type = Pxp_document.T_element "body" ->
180 (t#extension#to_cic_term, b#extension#to_cic_term)
181 | _ -> raise (IllFormedXml 16)
184 | _ -> raise (IllFormedXml 15)
187 let res = Cic.ACoFix (id, ref None, nofun, functions) in
193 class eltype_implicit =
200 let id = string_of_xml_attr (n#attribute "id") in
201 let res = Cic.AImplicit (id, ref None) in
214 let value = int_of_xml_attr (n#attribute "value")
215 and binder = binder_of_xml_attr (n#attribute "binder")
216 and id = string_of_xml_attr (n#attribute "id") in
217 let res = Cic.ARel (id,ref None,value,binder) in
230 let value = int_of_xml_attr (n#attribute "no")
231 and id = string_of_xml_attr (n#attribute "id") in
232 let res = Cic.AMeta (id,ref None,value) in
245 let name = string_of_xml_attr (n#attribute "relUri")
246 and xid = string_of_xml_attr (n#attribute "id") in
247 match Str.split (Str.regexp ",") name with
253 | (n,he::tl) when n > 0 -> "/" ^ he ^ aux (n - 1, tl)
254 | _ -> raise (IllFormedXml 19)
256 aux (List.length !current_sp - n,!current_sp)
261 (UriManager.uri_of_string
262 ("cic:" ^ get_prefix (int_of_string index) ^ id ^ ".var"))
267 | _ -> raise (IllFormedXml 18)
278 let children = n#sub_nodes
279 and id = string_of_xml_attr (n#attribute "id") in
280 if List.length children < 2 then raise (IllFormedXml 8)
284 (id,ref None,List.map (fun x -> x#extension#to_cic_term) children)
298 let sons = n#sub_nodes
299 and id = string_of_xml_attr (n#attribute "id") in
302 te#node_type = Pxp_document.T_element "term" &&
303 ty#node_type = Pxp_document.T_element "type" ->
304 let term = te#extension#to_cic_term
305 and typ = ty#extension#to_cic_term in
306 let res = Cic.ACast (id,ref None,term,typ) in
309 | _ -> raise (IllFormedXml 9)
320 let sort = cic_sort_of_xml_attr (n#attribute "value")
321 and id = string_of_xml_attr (n#attribute "id") in
322 let res = Cic.ASort (id,ref None,sort) in
335 let value = uri_of_xml_attr (n#attribute "uri")
336 and id = string_of_xml_attr (n#attribute "id") in
337 let res = Cic.AAbst (id,ref None,value) in
349 let module U = UriManager in
351 let value = uri_of_xml_attr (n#attribute "uri")
352 and id = string_of_xml_attr (n#attribute "id") in
354 Cic.AConst (id,ref None,value, U.relative_depth !current_uri value 0)
361 class eltype_mutind =
367 let module U = UriManager in
369 let name = uri_of_xml_attr (n#attribute "uri")
370 and noType = int_of_xml_attr (n#attribute "noType")
371 and id = string_of_xml_attr (n#attribute "id") in
374 (id,ref None,name, U.relative_depth !current_uri name 0, noType)
381 class eltype_mutconstruct =
387 let module U = UriManager in
389 let name = uri_of_xml_attr (n#attribute "uri")
390 and noType = int_of_xml_attr (n#attribute "noType")
391 and noConstr = int_of_xml_attr (n#attribute "noConstr")
392 and id = string_of_xml_attr (n#attribute "id") in
395 (id, ref None, name, U.relative_depth !current_uri name 0,
410 let sons = n#sub_nodes
411 and id = string_of_xml_attr (n#attribute "id") in
414 s#node_type = Pxp_document.T_element "source" &&
415 t#node_type = Pxp_document.T_element "target" ->
416 let name = cic_attr_of_xml_attr (t#attribute "binder")
417 and source = s#extension#to_cic_term
418 and target = t#extension#to_cic_term in
419 let res = Cic.AProd (id,ref None,name,source,target) in
422 | _ -> raise (IllFormedXml 10)
426 class eltype_mutcase =
432 let module U = UriManager in
434 let sons = n#sub_nodes
435 and id = string_of_xml_attr (n#attribute "id") in
437 ty::te::patterns when
438 ty#node_type = Pxp_document.T_element "patternsType" &&
439 te#node_type = Pxp_document.T_element "inductiveTerm" ->
440 let ci = uri_of_xml_attr (n#attribute "uriType")
441 and typeno = int_of_xml_attr (n#attribute "noType")
442 and inductiveType = ty#extension#to_cic_term
443 and inductiveTerm = te#extension#to_cic_term
444 and lpattern= List.map (fun x -> x#extension#to_cic_term) patterns
447 Cic.AMutCase (id,ref None,ci,U.relative_depth !current_uri ci 0,
448 typeno,inductiveType,inductiveTerm,lpattern)
452 | _ -> raise (IllFormedXml 11)
456 class eltype_lambda =
463 let sons = n#sub_nodes
464 and id = string_of_xml_attr (n#attribute "id") in
467 s#node_type = Pxp_document.T_element "source" &&
468 t#node_type = Pxp_document.T_element "target" ->
469 let name = cic_attr_of_xml_attr (t#attribute "binder")
470 and source = s#extension#to_cic_term
471 and target = t#extension#to_cic_term in
472 let res = Cic.ALambda (id,ref None,name,source,target) in
475 | _ -> raise (IllFormedXml 12)
486 let sons = n#sub_nodes
487 and id = string_of_xml_attr (n#attribute "id") in
490 s#node_type = Pxp_document.T_element "term" &&
491 t#node_type = Pxp_document.T_element "letintarget" ->
492 let name = cic_attr_of_xml_attr (t#attribute "binder")
493 and source = s#extension#to_cic_term
494 and target = t#extension#to_cic_term in
495 let res = Cic.ALetIn (id,ref None,name,source,target) in
498 | _ -> raise (IllFormedXml 12)
502 (* The definition of domspec, an hashtable that maps each node type to the *)
503 (* object that must be linked to it. Used by markup. *)
506 let module D = Pxp_document in
507 D.make_spec_from_alist
508 ~data_exemplar: (new D.data_impl (new eltype_not_of_cic))
509 ~default_element_exemplar: (new D.element_impl (new eltype_not_of_cic))
511 [ "REL", (new D.element_impl (new eltype_rel)) ;
512 "VAR", (new D.element_impl (new eltype_var)) ;
513 "META", (new D.element_impl (new eltype_meta)) ;
514 "SORT", (new D.element_impl (new eltype_sort)) ;
515 "IMPLICIT", (new D.element_impl (new eltype_implicit)) ;
516 "CAST", (new D.element_impl (new eltype_cast)) ;
517 "PROD", (new D.element_impl (new eltype_prod)) ;
518 "LAMBDA", (new D.element_impl (new eltype_lambda)) ;
519 "LETIN", (new D.element_impl (new eltype_letin)) ;
520 "APPLY", (new D.element_impl (new eltype_apply)) ;
521 "CONST", (new D.element_impl (new eltype_const)) ;
522 "ABST", (new D.element_impl (new eltype_abst)) ;
523 "MUTIND", (new D.element_impl (new eltype_mutind)) ;
524 "MUTCONSTRUCT", (new D.element_impl (new eltype_mutconstruct)) ;
525 "MUTCASE", (new D.element_impl (new eltype_mutcase)) ;
526 "FIX", (new D.element_impl (new eltype_fix)) ;
527 "COFIX", (new D.element_impl (new eltype_cofix)) ;
528 "arity", (new D.element_impl (new eltype_transparent)) ;
529 "term", (new D.element_impl (new eltype_transparent)) ;
530 "type", (new D.element_impl (new eltype_transparent)) ;
531 "body", (new D.element_impl (new eltype_transparent)) ;
532 "source", (new D.element_impl (new eltype_transparent)) ;
533 "target", (new D.element_impl (new eltype_transparent)) ;
534 "patternsType", (new D.element_impl (new eltype_transparent)) ;
535 "inductiveTerm", (new D.element_impl (new eltype_transparent)) ;
536 "pattern", (new D.element_impl (new eltype_transparent))