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-07 10:04:30 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 | Descendant | DescendantOrSelf | FollowingSibling
26 | Parent | Ancestor | AncestorOrSelf | PrecedingSibling | Preceding | Following
30 and binop = Eq | Neq | Lt | Gt | Lte | Gte | Or | And | Add | Sub | Mult | Div | Mod
33 | Number of [ `Int of int | `Float of float ]
35 | Fun_call of QName.t * expr list
37 | Binop of expr * binop * expr
44 let text = QNameSet.singleton QName.text
45 let node = QNameSet.any
48 QNameSet.from_list [ QName.text;
55 let pp fmt e = Format.fprintf fmt e
61 | Number _ | String _ | Fun_call _ -> 9
62 | Binop (_,op,_) -> begin match op with
63 | Lt | Lte | Gt | Gte -> 7
67 | Mult | Div | Mod -> 3
71 let print_binop fmt o =
72 pp fmt "%s" begin match o with
87 let print_unop fmt o =
88 pp fmt "%s" begin match o with
92 let rec print_path fmt p =
93 Pretty.print_list ~sep:" | " print_single_path fmt p
95 and print_single_path fmt p =
97 | Absolute l -> pp fmt "/"; l
100 Pretty.print_list ~sep:"/" print_step fmt l
102 and print_step fmt (axis, test, expr) =
103 pp fmt "%a::%a" print_axis axis print_test test;
107 Pretty.print_list ~sep:" ][ " print_expr fmt l;
110 and print_axis fmt a = pp fmt "%s" begin
114 | Descendant -> "descendant"
115 | DescendantOrSelf -> "descendant-or-self"
116 | FollowingSibling -> "following-sibling"
117 | Attribute -> "attribute"
118 | Ancestor -> "ancestor"
119 | AncestorOrSelf -> "ancestor-or-self"
120 | PrecedingSibling -> "preceding-sibling"
122 | Preceding -> "preceding"
123 | Following -> "following"
126 and print_test fmt ts =
128 pp fmt "%s" (List.assoc ts
133 Not_found -> pp fmt "%s"
134 (if QNameSet.is_finite ts
135 then QName.to_string (QNameSet.choose ts)
139 and print_expr fmt = function
140 | Number (`Int(i)) -> pp fmt "%i" i
141 | Number (`Float(f)) -> pp fmt "%f" f
142 | String s -> pp fmt "'%S'" s
143 | Fun_call (n, args) ->
144 pp fmt "%a(" QName.print n;
145 Pretty.print_list ~sep:", " print_expr fmt args;
147 | Path p -> print_path fmt p
148 | Binop (e1, op, e2) as e ->
150 let need_par1 = prio e1 < pe in
151 if need_par1 then pp fmt "(";
152 pp fmt "%a" print_expr e1;
153 if need_par1 then pp fmt ")";
154 pp fmt " %a " print_binop op;
155 let need_par2 = prio e2 < pe in
156 if need_par2 then pp fmt "(";
157 pp fmt "%a" print_expr e2;
158 if need_par2 then pp fmt ")"
159 | Unop (op, e0) as e ->
160 let need_par0 = prio e0 < prio e in
162 if need_par0 then pp fmt "(";
164 if need_par0 then pp fmt ")"