Implement set-theoretic operation on 2WSATA (union, intersection,
[tatoo.git] / src / tree.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 (** The different kind of XML nodes and utility functions *)
17
18 module NodeKind =
19   struct
20     type t =
21         Document | Element | Text | Comment | Attribute | ProcessingInstruction | Node
22
23     let to_string =
24       function
25     Document -> "document"
26     | Element -> "element"
27     | Attribute -> "attribute"
28     | Text -> "text"
29     | Comment -> "comment"
30     | ProcessingInstruction -> "processing-instruction"
31     | Node -> "node"
32
33     let print ppf k = Format.fprintf ppf "%s" (to_string k)
34
35
36     let is_a k1 k2 =
37       k1 == Node || k2 == Node || k1 == k2
38 end
39
40 (** Signatures for trees *)
41
42 exception Parse_error of string
43
44 module type S =
45 sig
46   type node
47   (** The type of a tree node *)
48
49   type t
50   (** The type of trees *)
51
52   val size : t -> int
53   (** Return the number of nodes *)
54
55   val nil : node
56   (** Nil node, denoting the first/second child of a leaf or the parent of
57       the root *)
58
59   val dummy : node
60   (** Dummy node that is guaranteed to never occur in any tree *)
61
62   val load_xml_file : in_channel -> t
63   (** Takes a file descriptor and returns the XML data stored in the
64       corresponding file. Start at the current position in the file
65       descriptor (which is not necessarily the begining of file)
66   *)
67
68   val load_xml_string : string -> t
69   (** Loads XML data stored in a string *)
70
71   val print_xml : out_channel -> t -> node -> unit
72   (** Outputs the tree as an XML document on the given output_channel *)
73
74   val root : t -> node
75   (** Returns the root of the tree *)
76
77   val first_child : t -> node -> node
78   (** [first_child t n] returns the first child of node [n] in tree [t].
79       Returns [nil] if [n] is a leaf. Returns [nil] if [n == nil].
80   *)
81
82   val next_sibling : t -> node -> node
83   (** [next_sibling t n] returns the next_sibling of node [n] in tree [t].
84       Returns [nil] if [n] is the last child of a node.
85       Returns [nil] if [n == nil].
86   *)
87
88   val parent : t -> node -> node
89   (** [next_sibling t n] returns the parent of node [n] in tree [t].
90       Returns [nil] if [n] is the root of the tree.
91       Returns [nil] if [n == nil].
92   *)
93
94   val tag : t -> node -> QName.t
95   (** Returns the label of a given node *)
96
97   val data : t -> node -> string
98   (** Returns the character data associated with a node.
99       The only node having character data are those whose label is
100       QName.text, QName.cdata_section or QName.comment
101   *)
102
103   val kind : t -> node -> NodeKind.t
104   (** Returns the kind of the given node *)
105
106   val preorder : t -> node -> int
107   (** [preorder t n] returns the pre-order position of [n] in [t].
108       [preodrder t (root t) == 0] and [preorder t nil < 0].
109   *)
110
111   val by_preorder : t -> int -> node
112   (** [by_preorder t i] returns the node with preorder [i]
113   *)
114   val print_node : Format.formatter -> node -> unit
115 end