Merged -correctxpath branch
[SXSI/xpathcomp.git] / tag.ml
1 (******************************************************************************)
2 (*  SXSI : XPath evaluator                                                    *)
3 (*  Kim Nguyen (Kim.Nguyen@nicta.com.au)                                      *)
4 (*  Copyright NICTA 2008                                                      *)
5 (*  Distributed under the terms of the LGPL (see LICENCE)                     *)
6 (******************************************************************************)
7 (*  maybe utf8 string... *)
8
9
10 type t = int
11 type pool
12
13 external null_pool : unit -> pool = "caml_xml_tree_nullt"
14 external null_tag : unit -> t = "caml_xml_tree_nullt"
15 external register_tag : pool -> string -> t = "caml_xml_tree_register_tag"
16 external tag_name : pool -> t -> string = "caml_xml_tree_tag_name"
17
18 let nullt = null_tag ()   
19 (* Defined in XMLTree.cpp *)
20 let pcdata = 1
21 let attribute = 0 
22
23 let pool = Weak.create 1
24
25 let init p = Weak.set pool 0 (Some p)
26
27 let get_pool () =  match Weak.get pool 0 with
28   | Some x -> x
29   | None -> failwith "Tag.ml: Uninitialized Document"
30
31 let tag s = match s with
32   | "<$>" -> pcdata
33   | "<@>" -> attribute
34   | _ -> register_tag (get_pool()) s
35
36 let compare = (-)
37 let equal = (==)
38
39 let hash x = x
40
41
42 let to_string t = 
43   if t = pcdata then "<$>"
44   else if t = attribute then "<@>"
45   else tag_name (get_pool()) t
46
47
48 let print ppf t = Format.fprintf ppf "%s" (to_string t)
49 (* Check internal invariants *)
50 let check t = 
51   if (t != tag (to_string t))
52   then failwith "module Tag: internal check failed"
53
54 let dump = print
55