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-04-18 11:22:00 CEST by Kim Nguyen>
21 type path = single_path list
22 and single_path = Absolute of step list | Relative of step list
23 and step = axis * test * expr list
24 and axis = Self | Attribute | Child
25 | Descendant of bool (* true = descendant-or-self, false = descendant *)
28 | Ancestor of bool (* true = ancestor-or-self, false = ancestor *)
30 | Preceding | Following
32 and test = QNameSet.t * Tree.NodeKind.t
34 and binop = Eq | Neq | Lt | Gt | Lte | Gte | Or | And | Add | Sub | Mult | Div | Mod
37 | Number of [ `Int of int | `Float of float ]
39 | Fun_call of QName.t * expr list
41 | Binop of expr * binop * expr
48 let text = QNameSet.singleton QName.text
49 let node = QNameSet.any
52 QNameSet.from_list [ QName.text;
58 let pp fmt e = Format.fprintf fmt e
64 | Number _ | String _ | Fun_call _ -> 9
65 | Binop (_,op,_) -> begin match op with
66 | Lt | Lte | Gt | Gte -> 7
70 | Mult | Div | Mod -> 3
74 let print_binop fmt o =
75 pp fmt "%s" begin match o with
90 let print_unop fmt o =
91 pp fmt "%s" begin match o with
95 let rec print_path fmt p =
96 Pretty.print_list ~sep:" | " print_single_path fmt p
98 and print_single_path fmt p =
100 | Absolute l -> pp fmt "/"; l
103 Pretty.print_list ~sep:"/" print_step fmt l
105 and print_step fmt (axis, test, expr) =
106 pp fmt "%a::%a" print_axis axis print_test test;
110 Pretty.print_list ~sep:" ][ " print_expr fmt l;
113 and print_axis fmt a = pp fmt "%s" begin
117 | Descendant false -> "descendant"
118 | Descendant true -> "descendant-or-self"
119 | FollowingSibling -> "following-sibling"
120 | Attribute -> "attribute"
121 | Ancestor false -> "ancestor"
122 | Ancestor true -> "ancestor-or-self"
123 | PrecedingSibling -> "preceding-sibling"
125 | Preceding -> "preceding"
126 | Following -> "following"
129 and print_test fmt (ts,kind) =
130 let open Tree.NodeKind in
132 Text -> pp fmt "%s" "text()"
133 | Element | Attribute ->
135 if ts == star then "*"
136 else QName.to_string (QNameSet.choose ts)
138 | Comment -> pp fmt "%s" "comment()"
139 | ProcessingInstruction ->
140 pp fmt "processing-instruction(%s)"
142 if ts == star then ""
143 else "'" ^ (QName.to_string (QNameSet.choose ts)) ^ "'"
145 | Node -> pp fmt "%s" "node()"
146 | Document -> pp fmt "%s" "<DOCUMENT>"
148 and print_expr fmt = function
149 | Number (`Int(i)) -> pp fmt "%i" i
150 | Number (`Float(f)) -> pp fmt "%f" f
151 | String s -> pp fmt "'%S'" s
152 | Fun_call (n, args) ->
153 pp fmt "%a(" QName.print n;
154 Pretty.print_list ~sep:", " print_expr fmt args;
156 | Path p -> print_path fmt p
157 | Binop (e1, op, e2) as e ->
159 let need_par1 = prio e1 < pe in
160 if need_par1 then pp fmt "(";
161 pp fmt "%a" print_expr e1;
162 if need_par1 then pp fmt ")";
163 pp fmt " %a " print_binop op;
164 let need_par2 = prio e2 < pe in
165 if need_par2 then pp fmt "(";
166 pp fmt "%a" print_expr e2;
167 if need_par2 then pp fmt ")"
168 | Unop (op, e0) as e ->
169 let need_par0 = prio e0 < prio e in
171 if need_par0 then pp fmt "(";
173 if need_par0 then pp fmt ")"
177 let invert_axis = function
179 | Attribute -> Parent (* Improve *)
181 | Descendant (b) -> Ancestor (b)
182 | FollowingSibling -> PrecedingSibling
184 | Ancestor (b) -> Descendant (b)
185 | PrecedingSibling -> FollowingSibling
186 | Preceding -> Following
187 | Following -> Preceding