Remove support for outdated libGrammar, replaced by Grammar2
[SXSI/xpathcomp.git] / src / runtime.ml
index 9836952..3bc3256 100644 (file)
@@ -8,7 +8,7 @@ module type S = sig
   type result_set
   val top_down_run : Ata.t -> Tree.t -> Tree.node -> result_set
   val bottom_up_run : Ata.t -> Tree.t -> Compile.text_query * string -> result_set
-  val grammar_run : Ata.t -> Grammar.t -> unit -> result_set
+  val grammar_run : Ata.t -> Grammar2.t -> unit -> result_set
 
 end
 
@@ -425,6 +425,7 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
 
 
 (*  Grammar run *)
+
     external is_young : 'a array -> bool = "caml_custom_is_young" "noalloc"
     external blit : 'a array -> int -> 'a array -> int -> int -> unit = "caml_custom_array_blit"
     module M = Map.Make(struct type t = Grammar.n_symbol let compare = compare end)
@@ -446,9 +447,7 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
 
     let grammar_run auto g () =
 
-      let start_symbol = Node.of_int 0 in
-      let dummy_leaf = Grammar.Leaf (Node.nil) in
-      let nil_symbol = Grammar.nil_symbol g in
+      let dummy_leaf = Grammar2.Leaf (Node.nil) in
       let res_len = (StateSet.max_elt auto.states) + 1 in
       let empty_slot = Array.create res_len U.NS.empty in
       let nil_res = auto.bottom_states, empty_slot in
@@ -456,8 +455,6 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
       let cache3 = L3JIT.create () in
       let dummy2 = (StateSet.empty, StateSet.empty, Translist.nil) in
       let cache2 = Cache.Lvl2.create 512 dummy2 in
-      let parameters = Array.create 2 dummy_leaf in
-      let parameters_tmp = Array.create 2 dummy_leaf in
       let rule_counter = ref 0 in
       let start_counter = ref 0 in
       let () = at_exit (fun () -> Printf.eprintf "start_couter=%i, rule_counter=%i\n%!"
@@ -491,88 +488,87 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
        if idx < Node.null then nil_res
        else if StateSet.is_empty states then empty_res
        else begin
-         let symbol = Grammar.get_symbol_at g start_symbol idx in
-         if Grammar.is_terminal symbol then
-           let symbol = Grammar.terminal symbol in
-           if symbol == nil_symbol then nil_res else
-             let tag = Grammar.tag symbol in
+         let symbol = Grammar2.start_tag g idx in
+         if Grammar2.is_terminal g symbol then
+           let symbol = Grammar2.terminal symbol in
+           if symbol == Grammar2.nil_symbol then nil_res else
+             let tag = Grammar2.tag symbol in
              let lst, rst, trans = get_trans tag states in
-             let fs = Grammar.start_first_child g idx in
+             let fs = Grammar2.start_first_child g idx in
              let s1, slot1 = start_loop fs lst in
-             let s2, slot2 = start_loop (Grammar.start_next_sibling g fs) rst in
+             let s2, slot2 = start_loop (Grammar2.start_next_sibling g fs) rst in
              let opcode = L3JIT.find cache3 trans s1 s2 in
              if opcode == L3JIT.dummy then
                (L3JIT.cache_apply cache3 auto trans s1 s2) empty_slot slot1 slot2 (Obj.magic ()) (Obj.magic ())
              else opcode empty_slot slot1 slot2 (Obj.magic ()) (Obj.magic())
          else
-           let nt = Grammar.non_terminal symbol in
-           let nparam = Grammar.num_params nt in
-           let child = ref (Grammar.first_child g start_symbol idx) in
-           for i = 0 to nparam - 1 do
-             let c = !child in
-             parameters.(i) <- Grammar.Leaf c;
-             child := Grammar.next_sibling g start_symbol c;
-           done;
-           rule_loop nt states parameters
-
+           let nt = Grammar2.non_terminal symbol in
+           let rhs = Grammar2.get_rule g nt in
+           let nparam = Grammar2.get_rank rhs in
+           match nparam with
+           | 0 -> rule_loop nt states 0 dummy_leaf dummy_leaf
+           | 1 -> rule_loop nt states 1 (Grammar2.Leaf(Grammar2.start_first_child g idx)) dummy_leaf
+           | 2 ->
+             let fc = Grammar2.start_first_child g idx in
+             let ns = Grammar2.start_next_sibling g fc in
+             rule_loop nt states 2 (Grammar2.Leaf fc) (Grammar2.Leaf ns)
+           | _ -> assert false
        end
-      and rule_loop (t : Grammar.n_symbol) states a_param =
+      and rule_loop (t : Grammar2.n_symbol) states rank y0 y1 =
        incr rule_counter;
-(*     log_symbol (t); *)
-       if !rule_counter land (4095) == 0 then begin Gc.minor() end;
-       let id1 = Grammar.get_id1 g t in
-       let id2 = Grammar.get_id2 g t in
-       let param_pos = Grammar.get_param_pos t in
-       let nparam1 = Grammar.num_children id1 in
-       let nparam2 =
-         if Grammar.is_terminal id2 && nil_symbol == (Grammar.terminal id2) then 0
-         else Grammar.num_children id2
-       in
-       let a_param2 = if nparam2 == 0 then [||] else Array.create nparam2 dummy_leaf in
-       let i = param_pos - 2 in
-       let ip1 = i + 1 in
-       let offset2d = i+2 in
-       let offset2s = i+nparam2 + 1 in
-       blit a_param 0 parameters_tmp 0 (i+1);
-       parameters_tmp.(ip1) <- Grammar.Node(id2, a_param2);  (* id2( ... ) *)
-       blit a_param offset2s parameters_tmp offset2d (nparam1 - i - 2);
-       blit a_param ip1 a_param2 0 nparam2;
-
-       blit parameters_tmp 0 parameters 0 nparam1;
-       if Grammar.is_non_terminal id1 then
-         let id1 = Grammar.non_terminal id1 in
-         rule_loop id1 states parameters
+       if !rule_counter land (65535) == 0 then begin Gc.minor() end;
+       let rhs = Grammar2.get_rule g t in
+       let id1 = Grammar2.get_id1 rhs in
+       let id2 = Grammar2.get_id2 rhs in
+       let conf = Grammar2.get_conf rhs in
+       if Grammar2.is_non_terminal g id1 then
+         let id1 = Grammar2.non_terminal id1 in
+         match conf with
+         | Grammar2.C0 -> rule_loop id1 states 1 (Grammar2.Node0 id2) dummy_leaf
+         | Grammar2.C1 -> rule_loop id1 states 1 (Grammar2.Node1(id2,y0)) dummy_leaf
+         | Grammar2.C2 -> rule_loop id1 states 2 (Grammar2.Node0 id2) y0
+         | Grammar2.C3 -> rule_loop id1 states 2 y0 (Grammar2.Node0 id2)
+         | Grammar2.C4 -> rule_loop id1 states 1 (Grammar2.Node2(id2, y0, y1)) dummy_leaf
+         | Grammar2.C5 -> rule_loop id1 states 2 (Grammar2.Node1(id2, y0)) y1
+         | Grammar2.C6 -> rule_loop id1 states 2 y0 (Grammar2.Node1(id2, y1))
        else
-         let id1 = Grammar.terminal id1 in
-         terminal_loop id1 states parameters
-
-      and terminal_loop (symbol : Grammar.t_symbol) states a_param =
-       if symbol == nil_symbol then nil_res else begin
+         let id1 = Grammar2.terminal id1 in
+         match conf with
+         | Grammar2.C0 | Grammar2.C1 -> assert false
+         | Grammar2.C2  -> terminal_loop id1 states (Grammar2.Node0 id2) y0
+         | Grammar2.C3  -> terminal_loop id1 states y0 (Grammar2.Node0 id2)
+         | Grammar2.C4  -> assert false
+         | Grammar2.C5  -> terminal_loop id1 states (Grammar2.Node1(id2, y0)) y1
+         | Grammar2.C6  -> terminal_loop id1 states y0 (Grammar2.Node1(id2, y1))
+
+      and terminal_loop (symbol : Grammar2.t_symbol) states y0 y1 =
+       if symbol == Grammar2.nil_symbol then nil_res else begin
          (* todo factor in from start_loop *)
-         let tag = Grammar.tag symbol in
+         let tag = Grammar2.tag symbol in
          let lst, rst, trans = get_trans tag states in
-         let next = a_param.(1) in
-         let s1, slot1 = partial_loop a_param.(0) lst in
-         let s2, slot2 = partial_loop next rst in
+         let s1, slot1 = partial_loop y0 lst in
+         let s2, slot2 = partial_loop y1 rst in
          let opcode = L3JIT.find cache3 trans s1 s2 in
          if opcode == L3JIT.dummy then
            (L3JIT.cache_apply cache3 auto trans s1 s2) empty_slot slot1 slot2 (Obj.magic ()) (Obj.magic ())
          else
            opcode empty_slot slot1 slot2 (Obj.magic())  (Obj.magic())
-
-           (* End: TODO refactor *)
-
        end
 
       and partial_loop l states =
        match l with
-       | Grammar.Leaf id -> start_loop id states
-       | Grammar.Node (id, a_param) ->
-         let is_term = Grammar.is_terminal id in
-         if is_term then
-           terminal_loop (Grammar.terminal id) states a_param
+       | Grammar2.Leaf id -> start_loop id states
+       | Grammar2.Node0 id ->
+         if (Grammar2.terminal id) == Grammar2.nil_symbol then nil_res
+         else
+           rule_loop (Grammar2.non_terminal id) states 0 dummy_leaf dummy_leaf
+       | Grammar2.Node1 (id, y0) ->
+         rule_loop (Grammar2.non_terminal id) states 1 y0 dummy_leaf
+       | Grammar2.Node2 (id, y0, y1) ->
+         if Grammar2.is_terminal g id then
+           terminal_loop (Grammar2.terminal id) states y0 y1
          else
-           rule_loop (Grammar.non_terminal id) states a_param
+           rule_loop (Grammar2.non_terminal id) states 1 y0 y1
       in
 
       let _, slot = start_loop (Node.null) auto.init in