Simplify the automaton encoding a bit (remove redundant predicates in formulae).
[tatoo.git] / src / xpath / compile.ml
index 20f06be..d33fbb4 100644 (file)
@@ -42,8 +42,8 @@ let root_set = QNameSet.singleton QName.document
 let compile_axis_test axis (test,kind) phi trans states =
   let q = State.next () in
   let phi = match kind with
-    Tree.NodeKind.Node -> phi
-  | _ -> phi %% F.is kind
+      Tree.NodeKind.Node -> phi
+    | _ -> phi %% F.is kind
   in
   let phi', trans', states' =
     match axis with
@@ -156,9 +156,9 @@ and compile_step_list l trans states =
         aphi %% ephi, etrans, estates) (phi1, trans1, states1) elist
 
 (**
-   Compile the top-level XPath query in reverse (doing downward
+   Compile the top-level XPath query in reverse (going downward
    to the last top-level state):
-   /a0::t0[p0]/.../an-1::tn-1[pn-1]/an::tn[pn] becomes:
+   /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()]
 
@@ -179,34 +179,34 @@ let compile_top_level_step_list l trans states =
         (* Only select attribute nodes if the previous axis
            is attribute *)
         let phi0 =
-          if axis != Attribute then
+          if axis != Attribute && kind == Tree.NodeKind.Node then
             phi0 %% (F.not_ F.is_attribute)
           else phi0
         in
         match ll with
           [] ->
-            let phi1, trans1, states1 =
-              List.fold_left (fun (aphi, atrans, astates) e ->
+          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
+          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 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
+          loop ll trans2 states2  phi2
   in
   let starting = State.next () in
   let phi0, trans0, states0 =