1 (***********************************************************************)
5 (* Kim Nguyen, LRI UMR8623 *)
6 (* Université Paris-Sud & CNRS *)
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 *)
14 (***********************************************************************)
17 Time-stamp: <Last modified on 2013-03-04 16:24:20 CET by Kim Nguyen>
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 *)
29 | Ancestor of bool (* true = ancestor-or-self, false = ancestor *)
31 | Preceding | Following
35 and binop = Eq | Neq | Lt | Gt | Lte | Gte | Or | And | Add | Sub | Mult | Div | Mod
38 | Number of [ `Int of int | `Float of float ]
40 | Fun_call of QName.t * expr list
42 | Binop of expr * binop * expr
49 let text = QNameSet.singleton QName.text
50 let node = QNameSet.any
53 QNameSet.from_list [ QName.text;
60 let pp fmt e = Format.fprintf fmt e
66 | Number _ | String _ | Fun_call _ -> 9
67 | Binop (_,op,_) -> begin match op with
68 | Lt | Lte | Gt | Gte -> 7
72 | Mult | Div | Mod -> 3
76 let print_binop fmt o =
77 pp fmt "%s" begin match o with
92 let print_unop fmt o =
93 pp fmt "%s" begin match o with
97 let rec print_path fmt p =
98 Pretty.print_list ~sep:" | " print_single_path fmt p
100 and print_single_path fmt p =
102 | Absolute l -> pp fmt "/"; l
105 Pretty.print_list ~sep:"/" print_step fmt l
107 and print_step fmt (axis, test, expr) =
108 pp fmt "%a::%a" print_axis axis print_test test;
112 Pretty.print_list ~sep:" ][ " print_expr fmt l;
115 and print_axis fmt a = pp fmt "%s" begin
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"
127 | Preceding -> "preceding"
128 | Following -> "following"
131 and print_test fmt ts =
133 pp fmt "%s" (List.assoc ts
138 Not_found -> pp fmt "%s"
139 (if QNameSet.is_finite ts
140 then QName.to_string (QNameSet.choose ts)
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;
152 | Path p -> print_path fmt p
153 | Binop (e1, op, e2) as e ->
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
167 if need_par0 then pp fmt "(";
169 if need_par0 then pp fmt ")"
173 let invert_axis = function
174 | Self | Attribute as a -> a
176 | Descendant (b) -> Ancestor (b)
177 | FollowingSibling -> PrecedingSibling
179 | Ancestor (b) -> Descendant (b)
180 | PrecedingSibling -> FollowingSibling
181 | Preceding -> Following
182 | Following -> Preceding