Refactor module organisation and build process.
[tatoo.git] / src / tree / naive.ml
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                               TAToo                                 *)
4 (*                                                                     *)
5 (*                     Kim Nguyen, LRI UMR8623                         *)
6 (*                   Université Paris-Sud & CNRS                       *)
7 (*                                                                     *)
8 (*  Copyright 2010-2012 Université Paris-Sud and Centre National de la *)
9 (*  Recherche Scientifique. All rights reserved.  This file is         *)
10 (*  distributed under the terms of the GNU Lesser General Public       *)
11 (*  License, with the special exception on linking described in file   *)
12 (*  ../LICENSE.                                                        *)
13 (*                                                                     *)
14 (***********************************************************************)
15
16 (*
17   Time-stamp: <Last modified on 2013-02-07 09:59:37 CET by Kim Nguyen>
18 *)
19 open Utils
20
21 type node = {
22   tag : QName.t;
23   preorder : int;
24   mutable data : string;
25   mutable first_child : node;
26   mutable next_sibling : node;
27   mutable parent: node;
28 }
29
30
31
32 let rec nil = {
33   tag = QName.nil;
34   preorder = -1;
35   data = "";
36   first_child = nil;
37   next_sibling = nil;
38   parent = nil;
39 }
40
41 let dummy_tag = QName.of_string "#dummy"
42 let rec dummy = {
43   tag = dummy_tag;
44   preorder = -1;
45   data = "";
46   first_child = dummy;
47   next_sibling = dummy;
48   parent = dummy;
49 }
50
51
52 type t = {
53   root : node;
54   (* TODO add other intersting stuff *)
55 }
56
57
58
59 module Parser =
60 struct
61
62   type context = {
63     mutable stack : node list;
64     text_buffer : Buffer.t;
65     mutable current_preorder : int;
66   }
67
68   let print_node_ptr fmt n =
69     Format.fprintf fmt "<%s>"
70       (if n == nil then "NIL" else
71         if n == dummy then "DUMMY" else
72           "NODE " ^  string_of_int n.preorder)
73
74   let debug_node fmt node =
75     Format.fprintf fmt "{ tag=%s; preorder=%i; data=%s; first_child=%a; next_sibling=%a; parent=%a }"
76       (QName.to_string node.tag)
77       node.preorder
78       node.data
79       print_node_ptr node.first_child
80       print_node_ptr node.next_sibling
81       print_node_ptr node.parent
82
83
84   let debug_ctx fmt ctx =
85     Format.fprintf fmt "Current context: { preorder = %i\n; stack = \n%a\n }\n-------------\n"
86       ctx.current_preorder
87       (Pretty.print_list ~sep:";\n" debug_node) ctx.stack
88
89
90   let push n ctx = ctx.stack <- n :: ctx.stack
91   let pop ctx =
92     match ctx.stack with
93       [] -> failwith "XML parse error"
94     | e :: l -> ctx.stack <- l; e
95
96   let top ctx = match ctx.stack with
97     | [] -> failwith "XML parse error"
98     | e :: _ -> e
99
100   let next ctx =
101     let i = ctx.current_preorder in
102     ctx.current_preorder <- 1 + i;
103     i
104
105   let is_left n = n.next_sibling == dummy
106
107
108   let text_string = QName.to_string QName.text
109   let attr_map_string = QName.to_string QName.attribute_map
110
111   let rec start_element_handler parser_ ctx tag attr_list =
112     do_text parser_ ctx;
113     let parent = top ctx in
114     let n = { tag = QName.of_string tag;
115               preorder = next ctx;
116               data = "";
117               first_child = dummy;
118               next_sibling = dummy;
119               parent = parent;
120             }
121     in
122     if parent.first_child == dummy then parent.first_child <- n
123     else parent.next_sibling <- n;
124     push n ctx;
125     match attr_list with
126       [] -> ()
127     | _ ->
128       start_element_handler parser_ ctx attr_map_string [];
129       List.iter (do_attribute parser_ ctx) attr_list;
130       end_element_handler parser_ ctx attr_map_string
131
132   and do_attribute parser_ ctx (att, value) =
133     let att_tag = " " ^ att in
134     start_element_handler parser_ ctx att_tag [];
135     start_element_handler parser_ ctx text_string [];
136     let n = top ctx in n.data <- value;
137     end_element_handler parser_ ctx text_string;
138     end_element_handler parser_ ctx att_tag
139
140   and consume_closing ctx n =
141     if n.next_sibling != dummy then
142       let _ = pop ctx in consume_closing ctx (top ctx)
143
144   and end_element_handler parser_ ctx tag =
145     do_text parser_ ctx;
146     let node = top ctx in
147     if node.first_child == dummy then node.first_child <- nil
148     else begin
149       node.next_sibling <- nil;
150       consume_closing ctx node
151     end
152
153   and do_text parser_ ctx =
154     if Buffer.length ctx.text_buffer != 0 then
155       let s = Buffer.contents ctx.text_buffer in
156       Buffer.clear  ctx.text_buffer;
157       start_element_handler parser_ ctx text_string [];
158       let node = top ctx in
159       node.data <- s;
160       end_element_handler parser_ ctx text_string
161
162
163
164   let character_data_handler parser_ ctx text =
165     Buffer.add_string ctx.text_buffer text
166
167   let create_parser () =
168     let ctx = { text_buffer = Buffer.create 512;
169                 current_preorder = 0;
170                 stack = [] } in
171     let parser_ = Expat.parser_create ~encoding:None in
172     Expat.set_start_element_handler parser_ (start_element_handler parser_ ctx);
173     Expat.set_end_element_handler parser_ (end_element_handler parser_ ctx);
174     Expat.set_character_data_handler parser_ (character_data_handler parser_ ctx);
175     push { tag = QName.document;
176            preorder = next ctx;
177            data = "";
178            first_child = dummy;
179            next_sibling = dummy;
180            parent = nil;
181          } ctx;
182     (parser_,
183      fun () ->
184        let node = top ctx in
185        node.next_sibling <- nil;
186        consume_closing ctx node;
187        match ctx.stack with
188          [ root ] ->
189            root.next_sibling <- nil;
190            { root = root }
191        | _ -> raise (Expat.Expat_error Expat.UNCLOSED_TOKEN)
192     )
193
194
195   let parse_string s =
196     let parser_, finalize = create_parser () in
197     Expat.parse parser_ s;
198     finalize ()
199
200   let parse_file fd =
201     let buffer = String.create 4096 in
202     let parser_, finalize = create_parser () in
203     let rec loop () =
204       let read = input fd buffer 0 4096 in
205       if read != 0 then
206         let () = Expat.parse_sub parser_ buffer 0 read in
207         loop ()
208     in loop (); finalize ()
209
210 end
211
212
213 let load_xml_file = Parser.parse_file
214 let load_xml_string = Parser.parse_string
215
216
217 let output_escape_string out s =
218   for i = 0 to String.length s - 1 do
219     match s.[i] with
220     | '<' -> output_string out "&lt;"
221     | '>' -> output_string out "&gt;"
222     | '&' -> output_string out "&amp;"
223     | '"' -> output_string out "&quot;"
224     | '\'' -> output_string out "&apos;"
225     | c -> output_char out c
226   done
227
228 let rec print_attributes out tree_ node =
229   if node != nil then begin
230     output_string out (QName.to_string node.tag);
231     output_string out "=\"";
232     output_escape_string out node.first_child.data;
233     output_char out '"';
234     print_attributes out tree_ node.next_sibling
235   end
236
237 let rec print_xml out tree_ node =
238   if node != nil then
239     let () =
240       if node.tag == QName.text then
241         output_escape_string out node.data
242       else
243         let tag = QName.to_string node.tag in
244         output_char out '<';
245         output_string out tag;
246         let fchild =
247           if node.first_child.tag == QName.attribute_map then
248             let () =
249               print_attributes out tree_ node.first_child.first_child
250             in
251             node.first_child.next_sibling
252           else
253             node.first_child
254         in
255         if fchild == nil then output_string out "/>"
256         else begin
257           output_char out '>';
258           print_xml out tree_ fchild;
259           output_string out "</";
260           output_string out tag;
261           output_char out '>'
262         end
263     in
264     print_xml out tree_ node.next_sibling
265
266
267 let root t = t.root
268 let first_child _ n = n.first_child
269 let next_sibling _ n = n.next_sibling
270 let parent _ n = n.parent
271 let tag _ n = n.tag
272 let data _ n = n.data
273 let preorder _ n = n.preorder