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-02-05 14:34:06 CET 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 | Descendant | DescendantOrSelf | FollowingSibling
25 | Parent | Ancestor | AncestorOrSelf | PrecedingSibling | Preceding | Following
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;
54 let pp fmt e = Format.fprintf fmt e
60 | Number _ | String _ | Fun_call _ -> 9
61 | Binop (_,op,_) -> begin match op with
62 | Lt | Lte | Gt | Gte -> 7
66 | Mult | Div | Mod -> 3
70 let print_binop fmt o =
71 pp fmt "%s" begin match o with
86 let print_unop fmt o =
87 pp fmt "%s" begin match o with
91 let rec print_path fmt p =
92 Pretty.print_list ~sep:" | " print_single_path fmt p
94 and print_single_path fmt p =
96 | Absolute l -> pp fmt "/"; l
99 Pretty.print_list ~sep:"/" print_step fmt l
101 and print_step fmt (axis, test, expr) =
102 pp fmt "%a::%a" print_axis axis print_test test;
106 Pretty.print_list ~sep:" ][ " print_expr fmt l;
109 and print_axis fmt a = pp fmt "%s" begin
113 | Descendant -> "descendant"
114 | DescendantOrSelf -> "descendant-or-self"
115 | FollowingSibling -> "following-sibling"
116 | Attribute -> "attribute"
117 | Ancestor -> "ancestor"
118 | AncestorOrSelf -> "ancestor-or-self"
119 | PrecedingSibling -> "preceding-sibling"
121 | Preceding -> "preceding"
122 | Following -> "following"
125 and print_test fmt ts =
127 pp fmt "%s" (List.assoc ts
132 Not_found -> pp fmt "%s"
133 (if QNameSet.is_finite ts
134 then QName.to_string (QNameSet.choose ts)
138 and print_expr fmt = function
139 | Number (`Int(i)) -> pp fmt "%i" i
140 | Number (`Float(f)) -> pp fmt "%f" f
141 | String s -> pp fmt "'%S'" s
142 | Fun_call (n, args) ->
143 pp fmt "%a(" QName.print n;
144 Pretty.print_list ~sep:", " print_expr fmt args;
146 | Path p -> print_path fmt p
147 | Binop (e1, op, e2) as e ->
149 let need_par1 = prio e1 < pe in
150 if need_par1 then pp fmt "(";
151 pp fmt "%a" print_expr e1;
152 if need_par1 then pp fmt ")";
153 pp fmt " %a " print_binop op;
154 let need_par2 = prio e2 < pe in
155 if need_par2 then pp fmt "(";
156 pp fmt "%a" print_expr e2;
157 if need_par2 then pp fmt ")"
158 | Unop (op, e0) as e ->
159 let need_par0 = prio e0 < prio e in
161 if need_par0 then pp fmt "(";
163 if need_par0 then pp fmt ")"