Add a kind element to the node tree. Improve support for XPath by
[tatoo.git] / src / xpath / compile.ml
index 123583f..2588432 100644 (file)
@@ -14,7 +14,7 @@
 (***********************************************************************)
 
 (*
-  Time-stamp: <Last modified on 2013-03-09 11:09:12 CET by Kim Nguyen>
+  Time-stamp: <Last modified on 2013-03-13 11:02:32 CET by Kim Nguyen>
 *)
 
 open Ast
@@ -33,9 +33,8 @@ module F = Ata.SFormula
 let node_set = QNameSet.remove QName.document QNameSet.any
 let star_set = QNameSet.diff QNameSet.any (
   List.fold_right (QNameSet.add)
-    [ QName.document; QName.text; QName.attribute_map ]
+    [ QName.document; QName.text; QName.comment ]
     QNameSet.empty)
-let attribute = QNameSet.singleton QName.attribute_map
 let root_set = QNameSet.singleton QName.document
 
 (* [compile_axis_test axis test q phi trans states] Takes an xpath
@@ -46,8 +45,12 @@ let root_set = QNameSet.singleton QName.document
    holds.
 *)
 
-let compile_axis_test axis test phi trans states =
+let compile_axis_test axis (test,kind) phi trans states =
   let q = State.make () in
+  let phi = match kind with
+    Tree.Common.NodeKind.Node -> phi
+  | _ -> phi %% F.mk_kind kind
+  in
   let phi', trans', states' =
     match axis with
     | Self ->
@@ -61,12 +64,20 @@ let compile_axis_test axis test phi trans states =
                QNameSet.any => F.next_sibling q ]) :: trans,
          states)
 
-    | Descendant self ->
-        ((if self then F.stay q else F.first_child q),
+    | Descendant false ->
+        (F.first_child q,
          (q, [ test => phi;
                QNameSet.any => F.first_child q ++ F.next_sibling q;
              ]) :: trans,
          states)
+    | Descendant true ->
+        let q' = State.make () in
+        (F.or_ (F.stay q) (F.first_child q'),
+         (q', [ test => phi;
+               QNameSet.any => F.first_child q' ++ F.next_sibling q';
+             ])::
+         (q, [ test => phi]):: trans,
+         states)
 
     | Parent ->
         let q' = State.make () in
@@ -97,14 +108,8 @@ let compile_axis_test axis test phi trans states =
         states
 
     | Attribute ->
-        let test = if QNameSet.is_finite test then
-            QNameSet.fold (fun tag acc ->
-              QNameSet.add (QName.add_attribute_prefix tag) acc)
-              test QNameSet.empty
-          else test
-        in
         (F.first_child q,
-         (q, [ test => phi %% F.is_attribute;
+         (q, [ test => phi;
                QNameSet.any => F.next_sibling q]) :: trans,
          states)
     | _ -> assert false
@@ -112,12 +117,6 @@ let compile_axis_test axis test phi trans states =
   in
   phi', trans', q @: states'
 
-
-let compile_rev_axis_test axis test phi trans states =
-  match axis with
-  | Attribute -> assert false
-  | _ -> compile_axis_test (invert_axis axis) test phi trans states
-
 let rec compile_expr e trans states =
   match e with
   | Binop (e1, (And|Or as op), e2) ->
@@ -145,7 +144,9 @@ and compile_single_path p trans states =
   let steps =
     match p with
     | Absolute steps ->
-        (Ancestor false, QNameSet.singleton QName.document, [])::steps
+        (Ancestor false, (QNameSet.singleton QName.document,
+                          Tree.Common.NodeKind.Node), [])
+        :: steps
     | Relative steps -> steps
   in
   compile_step_list steps trans states
@@ -162,44 +163,63 @@ and compile_step_list l trans states =
         let ephi, etrans, estates = compile_expr e atrans astates in
         aphi %% ephi, etrans, estates) (phi1, trans1, states1) elist
 
+(**
+   Compile the top-level XPath query in reverse (doing downward
+   to the last top-level state):
+   /a0::t0[p0]/.../an-1::tn-1[pn-1]/an::tn[pn] becomes:
+   self::node()[ pn and
+   self::tn[pn]/inv(an)::(tn-1)[pn-1]/.../inv(a1)::t0[p0]/inv(a0)::document()]
+
+   /child::a/attribute::b
+   self::@b/parent::a/parent::doc()
+*)
+
 let compile_top_level_step_list l trans states =
   let rec loop l trans states phi_above =
     match l with
-    | (axis, test, elist) :: [] ->
-        let phi0, trans0, states0 =
-          compile_rev_axis_test axis QNameSet.any phi_above trans states
-        in
-        let phi1, trans1, states1 =
-          List.fold_left (fun (aphi, atrans, astates) e ->
-            let ephi, etrans, estates = compile_expr e atrans astates in
-            aphi %% ephi, etrans, estates) (phi0, trans0, states0) elist
-        in
-        let _, trans2, states2 =
-          compile_axis_test Self test phi1 trans1 states1
-          in
-        let marking_state =
-          StateSet.choose (StateSet.diff states2 states1)
-        in
-        marking_state, trans2, states2
-    | (axis, test, elist) :: ll ->
+    | [] -> assert false
+    | (axis, (test,kind), elist) :: ll ->
         let phi0, trans0, states0 =
-          compile_rev_axis_test axis QNameSet.any phi_above trans states
+          compile_axis_test (invert_axis axis)
+            (QNameSet.any, Tree.Common.NodeKind.Node)
+            phi_above trans states
         in
-        let phi1, trans1, states1 =
-          compile_axis_test Self test phi0 trans0 states0
+        (* Only select attribute nodes if the previous axis
+           is attribute *)
+        let phi0 =
+          if axis != Attribute then
+            phi0 %% (Ata.SFormula.not_ Ata.SFormula.is_attribute)
+          else phi0
         in
-          let phi2, trans2, states2 =
-            List.fold_left (fun (aphi, atrans, astates) e ->
-              let ephi, etrans, estates = compile_expr e atrans astates in
-              aphi %% ephi, etrans, estates) (phi1, trans1, states1) elist
-          in
-          loop ll trans2 states2 phi2
-    | _ -> assert false
+        match ll with
+          [] ->
+            let phi1, trans1, states1 =
+              List.fold_left (fun (aphi, atrans, astates) e ->
+                let ephi, etrans, estates = compile_expr e atrans astates in
+                aphi %% ephi, etrans, estates) (phi0, trans0, states0) elist
+            in
+            let _, trans2, states2 =
+              compile_axis_test Self (test,kind) phi1 trans1 states1
+            in
+            let marking_state =
+              StateSet.choose (StateSet.diff states2 states1)
+            in
+            marking_state, trans2, states2
+        | _ ->
+            let phi1, trans1, states1 =
+              compile_axis_test Self (test,kind) phi0 trans0 states0
+            in
+            let phi2, trans2, states2 =
+              List.fold_left (fun (aphi, atrans, astates) e ->
+                let ephi, etrans, estates = compile_expr e atrans astates in
+                aphi %% ephi, etrans, estates) (phi1, trans1, states1) elist
+            in
+            loop ll trans2 states2  phi2
   in
   let phi0, trans0, states0 =
     compile_axis_test
       Self
-      (QNameSet.singleton QName.document)
+      (QNameSet.singleton QName.document, Tree.Common.NodeKind.Node)
       Ata.SFormula.true_
       trans
       states
@@ -207,7 +227,6 @@ let compile_top_level_step_list l trans states =
   loop l trans0 states0 phi0
 ;;
 
-
 let path p =
   let mstates, trans, states = List.fold_left (fun (ams, atrs, asts) p ->
     let ms, natrs, nasts =