From 6bc104a1034ba2d526c44b853836e3b42dd0ab2c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kim=20Nguy=E1=BB=85n?= Date: Wed, 22 Feb 2012 11:15:50 +0100 Subject: [PATCH] Add missing functions in the grammar2 API. --- src/grammar2.ml | 68 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/src/grammar2.ml b/src/grammar2.ml index 82cf036..0c509bf 100644 --- a/src/grammar2.ml +++ b/src/grammar2.ml @@ -212,18 +212,18 @@ struct let rule_ = rule_ lor (List.length params) in r_array.((h_find ~msg:"9" tag_to_id head) - rules_offset ) <- rule_ ) rules; - let l = Array.length renum_tags in - let tag32 = Array32.create l 0 in + (*let l = Array.length renum_tags in *) + (*let tag32 = Array32.create l 0 in for i = 0 to l - 1 do Array32.set tag32 i (renum_tags.(i) land 0x7ffffff); - done; + done; *) (* Remove the non-terminal names from the hash tables *) let tag_to_id2 = Hashtbl.create 31 in Hashtbl.iter (fun s i -> if i < rules_offset then Hashtbl.add tag_to_id2 s i) tag_to_id; { start = bv; - tags = tag32; - rules = renum_tags; + tags = renum_tags; + rules = r_array; rules_offset = rules_offset; tag_to_id = tag_to_id2; tag_of_id = Array.sub old_new_mapping 0 rules_offset @@ -290,30 +290,32 @@ let load f = } -type node = [ `Grammar ] Node.t +type node = [ `Start ] Node.t -type p_type = [ `Parameter ] type n_type = [ `NonTerminal ] type t_type = [ `Terminal ] -type any_type = [ p_type | n_type | t_type ] -type symbol = [ any_type ] Node.t +type r_type = [ `Rule ] +type any_type = [ n_type | t_type ] +type rhs = [ r_type ] Node.t -type p_symbol = p_type Node.t type n_symbol = n_type Node.t type t_symbol = t_type Node.t -type tn_symbol = [ n_type | t_type ] Node.t +type tn_symbol = [ any_type ] Node.t -let is_nil : (t:t_symbol) = +let is_nil (t : t_symbol) = (Node.to_int t) == 4 let nil_symbol : t_symbol = (Node.of_int 4) let translate_tag _ t = if t == 4 then ~-1 else t -let to_string t tag = tag_of_id.(Tag.to_int tag) -let register_tag t tag = - try Hashtbl.find t.tag_to_id (Tag.to_int tag) with +let to_string t tag = + if tag < Array.length t.tag_of_id then t.tag_of_id.(Tag.to_int tag) + else "" + +let register_tag t s = + try Hashtbl.find t.tag_to_id s with Not_found -> 4 let tag_operations t = { @@ -322,21 +324,41 @@ let tag_operations t = { Tag.translate = (fun s -> translate_tag t s); } +let start_root : node = Node.of_int 0 +let start_tag t (idx : node) = + t.tags.(Bp.preorder_rank t.start (Node.to_int idx)) -let rhs_tag t idx = - t.tags.(Bp.preorder_rank t.start idx) - -let rhs_first_child t idx = - Bp.first_child t.start idx +let start_first_child t (idx : node) = + Bp.first_child t.start (Node.to_int idx) -let rhs_next_sibling t idx = - Bp.next_sibling t.start idx +let start_next_sibling t (idx : node) = + Bp.next_sibling t.start (Node.to_int idx) let is_non_terminal t (n : [< any_type ] Node.t) = let n = Node.to_int n in n >= t.rules_offset -let is_terminal t (n : [< any_type ] Node.t) = not(is_terminal t n) +let is_terminal t (n : [< any_type ] Node.t) = not(is_non_terminal t n) + +external terminal : [< any_type ] Node.t -> t_symbol = "%identity" +external non_terminal : [< any_type ] Node.t -> t_symbol = "%identity" + let tag (n : t_symbol) : Tag.t = Obj.magic n +let get_rule g (r : n_symbol) : rhs = + Node.of_int (g.rules.((Node.to_int r) - g.rules_offset)) + +let get_id1 (r : rhs) : tn_symbol = + Node.of_int( + ((Node.to_int r) lsr 4) land 0x7ffffff) + +let get_id2 (r : rhs) : tn_symbol = + Node.of_int((Node.to_int r) lsr 31) + +let get_param_pos (r : rhs) : int = + ((Node.to_int r) lsr 2) land 0b11 + +let num_params (r : rhs) : int = + (Node.to_int r) land 0b11 + -- 2.17.1