Add the compilation from existential XPath to automata.
[tatoo.git] / src / xpath / ast.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-14 16:45:52 CET by Kim Nguyen>
18 *)
19
20 open Utils
21
22 type path = single_path list
23 and single_path = Absolute of step list | Relative of step list
24 and step = axis * test * expr list
25 and axis = Self | Attribute | Child
26            | Descendant of bool  (* true = descendant-or-self, false = descendant *)
27            | FollowingSibling
28            | Parent
29            | Ancestor of bool (* true = ancestor-or-self, false = ancestor *)
30            | PrecedingSibling
31            | Preceding | Following
32
33 and test = QNameSet.t
34
35 and binop = Eq | Neq | Lt | Gt | Lte | Gte | Or | And | Add | Sub | Mult | Div | Mod
36 and unop =  Neg
37 and expr =
38   | Number of [ `Int of int | `Float of float ]
39   | String of string
40   | Fun_call of QName.t * expr list
41   | Path of path
42   | Binop of expr * binop * expr
43   | Unop of unop * expr
44
45
46 type t = path
47
48
49 let text = QNameSet.singleton QName.text
50 let node = QNameSet.any
51 let star =
52   QNameSet.complement (
53     QNameSet.from_list [ QName.text;
54                          QName.document;
55                          QName.cdata_section;
56                          QName.comment])
57
58
59
60 let pp fmt e = Format.fprintf fmt e
61
62 let prio e =
63   match e with
64   | Unop (Neg, _) -> 11
65   | Path _ -> 10
66   | Number _ | String _ | Fun_call _ -> 9
67   | Binop (_,op,_) -> begin match op with
68     | Lt | Lte | Gt | Gte -> 7
69     | Neq | Eq -> 6
70     | And -> 5
71     | Or -> 4
72     | Mult | Div | Mod -> 3
73     | Add | Sub -> 2
74   end
75
76 let print_binop fmt o =
77   pp fmt "%s" begin match o with
78   | Eq -> "="
79   | Neq -> "!="
80   | Lt -> "<"
81   | Gt -> ">"
82   | Lte -> "<="
83   | Gte -> ">="
84   | Or -> "or"
85   | And -> "and"
86   | Add -> "+"
87   | Sub -> "-"
88   | Mult -> "*"
89   | Div -> "div"
90   | Mod -> "mod"
91   end
92 let print_unop fmt o =
93   pp fmt "%s" begin match o with
94   | Neg -> "-"
95   end
96
97 let rec print_path fmt p =
98   Pretty.print_list ~sep:" | " print_single_path fmt p
99
100 and print_single_path fmt p =
101   let l = match p with
102   | Absolute l -> pp fmt "/"; l
103   | Relative l -> l
104   in
105   Pretty.print_list ~sep:"/" print_step fmt l
106
107 and print_step fmt (axis, test, expr) =
108   pp fmt "%a::%a" print_axis axis print_test test;
109   match expr with
110     [] -> ()
111   | l -> pp fmt "[ ";
112       Pretty.print_list ~sep:" ][ " print_expr fmt l;
113       pp fmt " ]"
114
115 and print_axis fmt a = pp fmt "%s" begin
116   match a with
117     Self -> "self"
118   | Child -> "child"
119   | Descendant false -> "descendant"
120   | Descendant true -> "descendant-or-self"
121   | FollowingSibling -> "following-sibling"
122   | Attribute -> "attribute"
123   | Ancestor false -> "ancestor"
124   | Ancestor true -> "ancestor-or-self"
125   | PrecedingSibling -> "preceding-sibling"
126   | Parent -> "parent"
127   | Preceding -> "preceding"
128   | Following -> "following"
129 end
130
131 and print_test fmt ts =
132   try
133     pp fmt "%s" (List.assoc ts
134                    [ text,"text()";
135                      node,"node()";
136                      star, "*" ] )
137   with
138     Not_found -> pp fmt "%s"
139       (if QNameSet.is_finite ts
140        then QName.to_string (QNameSet.choose ts)
141        else "<INFINITE>"
142       )
143
144 and print_expr fmt = function
145 | Number (`Int(i)) -> pp fmt "%i" i
146 | Number (`Float(f)) -> pp fmt "%f" f
147 | String s -> pp fmt "'%S'" s
148 | Fun_call (n, args) ->
149     pp fmt "%a(" QName.print n;
150     Pretty.print_list ~sep:", " print_expr fmt args;
151     pp fmt ")"
152 | Path p -> print_path fmt p
153 | Binop (e1, op, e2) as e ->
154     let pe = prio e in
155     let need_par1 = prio e1 < pe in
156     if need_par1 then pp fmt "(";
157     pp fmt "%a" print_expr e1;
158     if need_par1 then pp fmt ")";
159     pp fmt " %a "  print_binop op;
160     let need_par2 = prio e2 < pe in
161     if need_par2 then pp fmt "(";
162     pp fmt "%a" print_expr e2;
163     if need_par2 then pp fmt ")"
164 | Unop (op, e0) as e ->
165     let need_par0 = prio e0 < prio e in
166     print_unop fmt op;
167     if need_par0 then pp fmt "(";
168     print_expr fmt e0;
169     if need_par0 then pp fmt ")"
170