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 (***********************************************************************)
18 type path = single_path list
19 and single_path = Absolute of step list | Relative of step list
20 and step = axis * test * expr list
21 and axis = Self | Attribute | Child | Descendant | DescendantOrSelf | FollowingSibling
22 | Parent | Ancestor | AncestorOrSelf | PrecedingSibling | Preceding | Following
24 and test = Simple of QNameSet.t
26 and binop = Eq | Neq | Lt | Gt | Lte | Gte | Or | And | Add | Sub | Mult | Div | Mod
29 | Number of [ `Int of int | `Float of float ]
31 | Fun_call of QName.t * expr list
33 | Binop of expr * binop * expr
40 let text = QNameSet.singleton QName.text
41 let node = QNameSet.any
44 QNameSet.from_list [ QName.text;
48 let t_text = Simple text
49 let t_node = Simple node
50 let t_star = Simple star
54 let pp fmt e = Format.fprintf fmt e
56 let prio e = match e with
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" (match a with
111 | Descendant -> "descendant"
112 | DescendantOrSelf -> "descendant-or-self"
113 | FollowingSibling -> "following-sibling"
114 | Attribute -> "attribute"
115 | Ancestor -> "ancestor"
116 | AncestorOrSelf -> "ancestor-or-self"
117 | PrecedingSibling -> "preceding-sibling"
119 | Preceding -> "preceding"
120 | Following -> "following"
122 and print_test fmt ts =
124 pp fmt "%s" (List.assoc ts
129 Not_found -> pp fmt "%s"
131 Simple t -> if QNameSet.is_finite t
132 then QName.to_string (QNameSet.choose t)
136 and print_expr fmt = function
137 | Number (`Int(i)) -> pp fmt "%i" i
138 | Number (`Float(f)) -> pp fmt "%f" f
139 | String s -> pp fmt "'%S'" s
140 | Fun_call (n, args) ->
141 pp fmt "%a(" QName.print n;
142 Pretty.print_list ~sep:", " print_expr fmt args;
144 | Path p -> print_path fmt p
145 | Binop (e1, op, e2) as e ->
147 let need_par1 = prio e1 < pe in
148 if need_par1 then pp fmt "(";
149 pp fmt "%a" print_expr e1;
150 if need_par1 then pp fmt ")";
151 pp fmt " %a " print_binop op;
152 let need_par2 = prio e2 < pe in
153 if need_par2 then pp fmt "(";
154 pp fmt "%a" print_expr e2;
155 if need_par2 then pp fmt ")"
156 | Unop (op, e0) as e ->
157 let need_par0 = prio e0 < prio e in
159 if need_par0 then pp fmt "(";
161 if need_par0 then pp fmt ")"