Preliminary support for the grammar.
[SXSI/xpathcomp.git] / src / 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_get_tag_name"
17 external num_tags : pool -> int = "caml_xml_tree_num_tags"
18 let nullt = null_tag ()
19 let dummy = nullt
20 (* Defined in XMLTree.cpp *)
21 let document_node = 0
22 let attribute = 1
23 let pcdata = 2
24 let attribute_data= 3
25 let document_node_close = 4
26 let attribute_close = 5
27 let pcdata_close = 6
28 let attribute_data_close= 7
29
30
31 let pool = Weak.create 1
32
33 let init p = Weak.set pool 0 (Some p)
34
35 let get_pool () =  match Weak.get pool 0 with
36   | Some x -> x
37   | None -> failwith "Tag.ml: Uninitialized Document"
38
39 let tag s = match s with
40   | "<$>" -> pcdata
41   | "<@>" -> attribute
42   | "" -> document_node
43   | "<@$>" -> attribute_data
44   | _ -> register_tag (get_pool()) s
45
46 let compare = (-)
47 let equal = (==)
48
49 let hash x = x
50
51 let to_string t =
52   if t == pcdata then "<$>"
53   else if t == attribute_data then "<@$>"
54   else if t == attribute then "<@>"
55   else if t == nullt then "<!NIL!>"
56   else tag_name (get_pool()) t
57
58 let dump_tags () =
59   Format.eprintf "Tags are:\n";
60   let doc = get_pool() in
61   let ntags = num_tags doc in
62     for i = 0 to ntags - 1 do
63       Format.eprintf "%i, -><%s/>\n%!" i (to_string i)
64     done
65
66
67
68 let print ppf t = Format.fprintf ppf "%s" (to_string t)
69 (* Check internal invariants *)
70 let check t =
71   if (t != tag (to_string t))
72   then failwith "module Tag: internal check failed"
73
74 let dump = print
75