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 (***********************************************************************)
16 type path = single_path list
17 and single_path = Absolute of step list | Relative of step list
18 and step = axis * test * expr list
19 and axis = Self | Attribute | Child
20 | Descendant of bool (* true = descendant-or-self, false = descendant *)
23 | Ancestor of bool (* true = ancestor-or-self, false = ancestor *)
25 | Preceding | Following
27 and test = QNameSet.t * Tree.NodeKind.t
29 and binop = Eq | Neq | Lt | Gt | Lte | Gte | Or | And | Add | Sub | Mult | Div | Mod
32 | Number of [ `Int of int | `Float of float ]
34 | Fun_call of QName.t * expr list
36 | Binop of expr * binop * expr
43 let text = QNameSet.singleton QName.text
44 let node = QNameSet.any
47 QNameSet.from_list [ QName.text;
53 let pp fmt e = Format.fprintf fmt e
59 | Number _ | String _ | Fun_call _ -> 9
60 | Binop (_,op,_) -> begin match op with
61 | Lt | Lte | Gt | Gte -> 7
65 | Mult | Div | Mod -> 3
69 let print_binop fmt o =
70 pp fmt "%s" begin match o with
85 let print_unop fmt o =
86 pp fmt "%s" begin match o with
90 let rec print_path fmt p =
91 Pretty.print_list ~sep:" | " print_single_path fmt p
93 and print_single_path fmt p =
95 | Absolute l -> pp fmt "/"; l
98 Pretty.print_list ~sep:"/" print_step fmt l
100 and print_step fmt (axis, test, expr) =
101 pp fmt "%a::%a" print_axis axis print_test test;
105 Pretty.print_list ~sep:" ][ " print_expr fmt l;
108 and print_axis fmt a = pp fmt "%s" begin
112 | Descendant false -> "descendant"
113 | Descendant true -> "descendant-or-self"
114 | FollowingSibling -> "following-sibling"
115 | Attribute -> "attribute"
116 | Ancestor false -> "ancestor"
117 | Ancestor true -> "ancestor-or-self"
118 | PrecedingSibling -> "preceding-sibling"
120 | Preceding -> "preceding"
121 | Following -> "following"
124 and print_test fmt (ts,kind) =
125 let open Tree.NodeKind in
127 Text -> pp fmt "%s" "text()"
128 | Element | Attribute ->
130 if ts == star then "*"
131 else QName.to_string (QName.remove_prefix (QNameSet.choose ts))
133 | Comment -> pp fmt "%s" "comment()"
134 | ProcessingInstruction ->
135 pp fmt "processing-instruction(%s)"
137 if ts == star then ""
138 else "\"" ^ (QName.to_string (QName.remove_prefix (QNameSet.choose ts))) ^ "\""
140 | Node -> pp fmt "%s" "node()"
141 | Document -> pp fmt "%s" "<DOCUMENT>"
143 and print_expr fmt = function
144 | Number (`Int(i)) -> pp fmt "%i" i
145 | Number (`Float(f)) -> pp fmt "%f" f
146 | String s -> pp fmt "'%S'" s
147 | Fun_call (n, args) ->
148 pp fmt "%a(" QName.print n;
149 Pretty.print_list ~sep:", " print_expr fmt args;
151 | Path p -> print_path fmt p
152 | Binop (e1, op, e2) as e ->
154 let need_par1 = prio e1 < pe in
155 if need_par1 then pp fmt "(";
156 pp fmt "%a" print_expr e1;
157 if need_par1 then pp fmt ")";
158 pp fmt " %a " print_binop op;
159 let need_par2 = prio e2 < pe in
160 if need_par2 then pp fmt "(";
161 pp fmt "%a" print_expr e2;
162 if need_par2 then pp fmt ")"
163 | Unop (op, e0) as e ->
164 let need_par0 = prio e0 < prio e in
166 if need_par0 then pp fmt "(";
168 if need_par0 then pp fmt ")"
172 let invert_axis = function
174 | Attribute -> Parent (* Improve *)
176 | Descendant (b) -> Ancestor (b)
177 | FollowingSibling -> PrecedingSibling
179 | Ancestor (b) -> Descendant (b)
180 | PrecedingSibling -> FollowingSibling
181 | Preceding -> Following
182 | Following -> Preceding