1 (***********************************************************************)
5 (* Kim Nguyen, LRI UMR8623 *)
6 (* Université Paris-Sud & CNRS *)
8 (* Copyright 2010-2013 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 (***********************************************************************)
19 let ( => ) a b = (a, b)
20 let ( ++ ) a b = Ata.Formula.or_ a b
21 let ( %% ) a b = Ata.Formula.and_ a b
22 let ( @: ) a b = StateSet.add a b
24 module F = Ata.Formula
27 let node_set = QNameSet.remove QName.document QNameSet.any
28 let star_set = QNameSet.diff QNameSet.any (
29 List.fold_right (QNameSet.add)
30 [ QName.document; QName.text; QName.comment ]
32 let root_set = QNameSet.singleton QName.document
34 (* [compile_axis_test axis test q phi trans states] Takes an xpath
35 [axis] and node [test], a formula [phi], a list of [trans]itions
36 and a set of [states] and returns a formula [phi'], a new set of
37 transitions, and a new set of states such that [phi'] holds iff
38 there exists a node reachable through [axis]::[test] where [phi]
42 let compile_axis_test axis (test,kind) phi trans states=
43 let q = State.make () in
44 let phi = match kind with
45 Tree.NodeKind.Node -> phi
46 | _ -> phi %% F.is kind
48 let phi', trans', states' =
52 (q, [ test => phi ]) :: trans,
58 QNameSet.any => F.next_sibling q ]) :: trans,
64 QNameSet.any => F.first_child q ++ F.next_sibling q;
68 let q' = State.make () in
69 (F.or_ (F.stay q) (F.first_child q'),
71 QNameSet.any => F.first_child q' ++ F.next_sibling q';
73 (q, [ test => phi]):: trans,
77 let q' = State.make () in
78 let move = F.parent q ++ F.previous_sibling q' in
81 :: (q', [ QNameSet.any => move ]) :: trans,
85 let q' = State.make () in
86 let move = F.parent q ++ F.previous_sibling q' in
87 (if self then F.stay q else move),
89 QNameSet.any => move ])
90 :: (q', [ QNameSet.any => move ]) :: trans,
93 | FollowingSibling | PrecedingSibling ->
95 if axis = PrecedingSibling then
101 QNameSet.any => move ]) :: trans,
107 QNameSet.any => F.next_sibling q]) :: trans,
112 phi', trans', q @: states'
114 let rec compile_expr e trans states =
116 | Binop (e1, (And|Or as op), e2) ->
117 let phi1, trans1, states1 = compile_expr e1 trans states in
118 let phi2, trans2, states2 = compile_expr e2 trans1 states1 in
119 (if op = Or then phi1 ++ phi2 else phi1 %% phi2),
122 | Fun_call (f, [ e0 ]) when (QName.to_string f) = "not" ->
123 let phi, trans0, states0 = compile_expr e0 trans states in
127 | Path p -> compile_path p trans states
130 and compile_path paths trans states =
131 List.fold_left (fun (aphi, atrans, astates) p ->
132 let phi, ntrans, nstates = compile_single_path p atrans astates in
135 nstates) (F.false_,trans,states) paths
137 and compile_single_path p trans states =
141 (Ancestor false, (QNameSet.singleton QName.document,
142 Tree.NodeKind.Node), [])
144 | Relative steps -> steps
146 compile_step_list steps trans states
148 and compile_step_list l trans states =
150 | [] -> F.true_, trans, states
151 | (axis, test, elist) :: ll ->
152 let phi0, trans0, states0 = compile_step_list ll trans states in
153 let phi1, trans1, states1 =
154 compile_axis_test axis test phi0 trans0 states0
156 List.fold_left (fun (aphi, atrans, astates) e ->
157 let ephi, etrans, estates = compile_expr e atrans astates in
158 aphi %% ephi, etrans, estates) (phi1, trans1, states1) elist
161 Compile the top-level XPath query in reverse (doing downward
162 to the last top-level state):
163 /a0::t0[p0]/.../an-1::tn-1[pn-1]/an::tn[pn] becomes:
165 self::tn[pn]/inv(an)::(tn-1)[pn-1]/.../inv(a1)::t0[p0]/inv(a0)::document()]
167 /child::a/attribute::b
168 self::@b/parent::a/parent::doc()
171 let compile_top_level_step_list l trans states =
172 let rec loop l trans states phi_above =
175 | (axis, (test,kind), elist) :: ll ->
176 let phi0, trans0, states0 =
177 compile_axis_test (invert_axis axis)
178 (QNameSet.any, Tree.NodeKind.Node)
179 phi_above trans states
181 (* Only select attribute nodes if the previous axis
184 if axis != Attribute then
185 phi0 %% (F.not_ F.is_attribute)
190 let phi1, trans1, states1 =
191 List.fold_left (fun (aphi, atrans, astates) e ->
192 let ephi, etrans, estates = compile_expr e atrans astates in
193 aphi %% ephi, etrans, estates) (phi0, trans0, states0) elist
195 let _, trans2, states2 =
196 compile_axis_test Self (test,kind) phi1 trans1 states1
199 StateSet.choose (StateSet.diff states2 states1)
201 marking_state, trans2, states2
203 let phi1, trans1, states1 =
204 compile_axis_test Self (test,kind) phi0 trans0 states0
206 let phi2, trans2, states2 =
207 List.fold_left (fun (aphi, atrans, astates) e ->
208 let ephi, etrans, estates = compile_expr e atrans astates in
209 aphi %% ephi, etrans, estates) (phi1, trans1, states1) elist
211 loop ll trans2 states2 phi2
213 let starting = State.make () in
214 let phi0, trans0, states0 =
217 (QNameSet.any, Tree.NodeKind.Node)
222 let mstates, trans, states = loop l trans0 states0 phi0 in
223 starting, mstates, trans, states
227 let sstates, mstates, trans, states =
228 List.fold_left (fun (ass, ams, atrs, asts) p ->
229 let ss, ms, natrs, nasts =
231 | Absolute l | Relative l -> compile_top_level_step_list l atrs asts
233 (StateSet.add ss ass),
234 (StateSet.add ms ams),
236 nasts) (StateSet.empty, StateSet.empty, [], StateSet.empty) p
238 let builder = Ata.Builder.make () in
239 (** ensure that we have a single selecting state at the end *)
240 let phi_sel = StateSet.fold (fun q acc -> F.or_ (F.stay q) acc) mstates F.false_ in
241 let q_sel = State.make () in
242 let states = StateSet.add q_sel states in
243 let mstates = StateSet.singleton q_sel in
244 let trans = (q_sel, [QNameSet.any, phi_sel]) :: trans in
246 (Ata.Builder.add_state builder ~starting:true) sstates;
248 (Ata.Builder.add_state builder ~selecting:true) mstates;
250 (Ata.Builder.add_state builder) states;
251 List.iter (fun (q, l) ->
252 List.iter (fun (lab, phi) ->
253 Ata.Builder.add_trans builder q lab phi
255 Ata.Builder.finalize builder