+open Table
+
+
+
+let all_nodes tree = let root = Naive_tree.root tree in
+ eval_axis tree [root] (Descendant true)
+
+let element_by_tag tree tagset = let dom = all_nodes tree in
+ List.filter (fun c -> QNameSet.mem (Naive_tree.tag tree c) tagset ) dom
+
+let rec compile_single_path p =
+ let open Xpath.Ast in
+ match p with
+ | Absolute p | Relative p -> compile_step_list (List.rev p)
+
+and compile_step_list p =
+ match p with
+ | [] -> Start
+ | (a,(test,_),el) :: r ->
+ let qtree = compile_step_list r in
+ let res = Binop ( Inter,Axis (a,qtree), Tag (test) ) in
+ List.fold_left (fun acc e ->
+ Binop(Inter, acc, compile_expr e)) res el (*avant j'ai utilise une function compile_expr_list ,c'est pas genial*)
+
+ and compile_expr (e : Xpath.Ast.expr ) = match e with
+ | Fun_call (f, [ e0 ]) when (QName.to_string f) = "not" ->
+ let qtree = compile_expr e0 in
+ Binop (Diff , Dom, qtree)
+
+ | Binop (e1,op,e2) -> let qtree1 = compile_expr e1 in
+ let qtree2 = compile_expr e2 in
+ begin
+ match op with
+ | Or -> Binop (Union , qtree1,qtree2)
+ | And -> Binop (Inter ,qtree1,qtree2)
+ | _ -> failwith "Unknown operator"
+ end
+ | Path p -> compile_path_rev p
+ | _ -> failwith "Unknown expression"
+
+ and compile_path_rev p =
+ match p with
+ | [] -> assert false
+ | [p] -> compile_single_path_rev p
+ | p::r -> List.fold_left (fun acc p -> Binop (Union , acc, compile_single_path_rev p) ) (compile_single_path_rev p) r
+
+ and compile_single_path_rev p =
+ match p with
+ | Absolute p | Relative p -> compile_step_list_rev (List.rev p)
+
+ and compile_step_list_rev p = match p with
+ | [] -> Dom (*assert false*) (*on fait rien , mais comment signifer ???*)
+ | (a,(test,_),el) :: r ->
+ let qtree = compile_step_list_rev r in
+ let res = Binop (Inter , qtree, Tag(test)) in
+ let qtree2 = List.fold_left (fun acc e ->
+ Binop(Inter, acc, compile_expr e)) res el in
+ let a_rev = axis_rev a in
+ Axis (a_rev , qtree2)
+
+
+ and axis_rev a =
+ let open Xpath.Ast in
+ match a with
+ Self -> Self
+ | Attribute -> assert false
+ | Child -> Parent
+ | Descendant b ->
+ if not b then (Ancestor false)
+ else (Ancestor true) (* true = descendant-or-self, false = descendant *)
+ | FollowingSibling -> PrecedingSibling
+ | Parent -> Child
+ | Ancestor b ->
+ if not b then (Descendant false)
+ else (Descendant true) (* true = ancestor-or-self, false = ancestor *)
+ | PrecedingSibling -> FollowingSibling
+ | Preceding -> Following
+ | Following -> Preceding
+
+
+let compile_xpath p = match p with
+ | [] -> assert false
+ | [p] -> compile_single_path p
+ | p::r -> List.fold_left (fun acc p -> Binop (Union , acc, compile_single_path p) ) (compile_single_path p) r
+
+let comp_node t n1 n2 = (Naive_tree.preorder t n1) < (Naive_tree.preorder t n2)
+
+
+let rec union_list t l1 l2 =
+ match l1,l2 with
+ | [],l2 -> l2
+ | l1, [] -> l1
+ | h1::ll1, h2::ll2 -> if (comp_node t h2 h1) then h2 :: (union_list t l1 ll2)
+ else if (comp_node t h1 h2) then h1::(union_list t ll1 l2)
+ else h1 ::(union_list t ll1 ll2)
+
+let rec inter_list t l1 l2 =
+ match l1,l2 with
+ | [],l2 -> []
+ | l1, [] -> []
+ | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then inter_list t ll1 l2
+ else if (comp_node t h2 h1) then inter_list t l1 ll2
+ else h1 :: (inter_list t ll1 ll2)
+
+let rec diff_list t l1 l2 =
+ match l1,l2 with
+ | [],l2 -> []
+ | l1, [] -> l1
+ | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then h1::(diff_list t ll1 l2)
+ else if (comp_node t h2 h1) then h2 :: (diff_list t l1 ll2)
+ else diff_list t ll1 ll2
+
+
+let do_debug = ref true
+
+let debug tree q l =
+ if !do_debug then begin
+ Format.fprintf Format.std_formatter "Evaluation de: ";
+ print_query_tree Format.std_formatter q;
+ Format.fprintf Format.std_formatter "\nResultat: %i"
+ (List.length l);
+ Format.pp_print_flush Format.std_formatter ();
+ print_node_list tree l;
+ Format.fprintf Format.std_formatter "\n----------------\n";
+ Format.pp_print_flush Format.std_formatter ();
+ end
+
+
+let rec eval_query_tree tree start q =
+ let resultat =
+ match q with
+ | Start -> start
+ | Dom -> all_nodes tree
+ | Tag t -> element_by_tag tree t
+ | Axis (a,q1) -> let ls = eval_query_tree tree start q1 in
+ eval_axis tree ls a
+ | Binop (op,q1,q2)-> begin
+ let ls1 = eval_query_tree tree start q1 in
+ let ls2 = eval_query_tree tree start q2 in
+ match op with
+ | Union -> union_list tree ls1 ls2
+ | Inter -> inter_list tree ls1 ls2
+ | Diff -> diff_list tree ls1 ls2
+ end
+ in
+ debug tree q resultat;
+ resultat
+