| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
module CharMap = Map.Make (struct |
| 21 |
type t = char |
| 22 |
let compare (x : t) (y : t) = compare x y |
| 23 |
end) |
| 24 |
|
| 25 |
type ttree = { |
| 26 |
node : string option; |
| 27 |
branch : ttree CharMap.t } |
| 28 |
|
| 29 |
let empty_ttree = { node = None; branch = CharMap.empty } |
| 30 |
|
| 31 |
let ttree_add ttree str translated = |
| 32 |
let rec insert tt i = |
| 33 |
if i == String.length str then |
| 34 |
{node = Some translated; branch = tt.branch} |
| 35 |
else |
| 36 |
let c = str.[i] in |
| 37 |
let br = |
| 38 |
match try Some (CharMap.find c tt.branch) with Not_found -> None with |
| 39 |
| Some tt' -> |
| 40 |
CharMap.add c (insert tt' (i + 1)) (CharMap.remove c tt.branch) |
| 41 |
| None -> |
| 42 |
let tt' = {node = None; branch = CharMap.empty} in |
| 43 |
CharMap.add c (insert tt' (i + 1)) tt.branch |
| 44 |
in |
| 45 |
{ node = tt.node; branch = br } |
| 46 |
in |
| 47 |
insert ttree 0 |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
let ttree_remove ttree str = |
| 52 |
let rec remove tt i = |
| 53 |
if i == String.length str then |
| 54 |
{node = None; branch = tt.branch} |
| 55 |
else |
| 56 |
let c = str.[i] in |
| 57 |
let br = |
| 58 |
match try Some (CharMap.find c tt.branch) with Not_found -> None with |
| 59 |
| Some tt' -> |
| 60 |
CharMap.add c (remove tt' (i + 1)) (CharMap.remove c tt.branch) |
| 61 |
| None -> tt.branch |
| 62 |
in |
| 63 |
{ node = tt.node; branch = br } |
| 64 |
in |
| 65 |
remove ttree 0 |
| 66 |
|
| 67 |
let ttree_descend ttree c = CharMap.find c ttree.branch |
| 68 |
|
| 69 |
let ttree_find ttree str = |
| 70 |
let rec proc_rec tt i = |
| 71 |
if i == String.length str then tt |
| 72 |
else proc_rec (CharMap.find str.[i] tt.branch) (i+1) |
| 73 |
in |
| 74 |
proc_rec ttree 0 |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
type out_function = bool -> bool -> Index.index_entry option -> string -> unit |
| 79 |
|
| 80 |
let token_tree = ref (ref empty_ttree) |
| 81 |
let outfun = ref (fun _ _ _ _ -> failwith "outfun not initialized") |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
let buff = Buffer.create 4 |
| 86 |
|
| 87 |
let flush_buffer was_symbolchar tag tok = |
| 88 |
let hastr = String.length tok <> 0 in |
| 89 |
if hastr then !outfun false was_symbolchar tag tok; |
| 90 |
if Buffer.length buff <> 0 then |
| 91 |
!outfun true (if hastr then not was_symbolchar else was_symbolchar) |
| 92 |
tag (Buffer.contents buff); |
| 93 |
Buffer.clear buff |
| 94 |
|
| 95 |
type sublexer_state = |
| 96 |
| Neutral |
| 97 |
| Buffering of bool * Index.index_entry option * string * ttree |
| 98 |
|
| 99 |
let translation_state = ref Neutral |
| 100 |
|
| 101 |
let buffer_char is_symbolchar ctag c = |
| 102 |
let rec aux = function |
| 103 |
| Neutral -> |
| 104 |
restart_buffering () |
| 105 |
| Buffering (was_symbolchar,tag,translated,tt) -> |
| 106 |
if tag <> ctag then |
| 107 |
|
| 108 |
|
| 109 |
let translated = |
| 110 |
match tt.node with |
| 111 |
| Some tok -> Buffer.clear buff; tok |
| 112 |
| None -> translated in |
| 113 |
flush_buffer was_symbolchar tag translated; |
| 114 |
restart_buffering () |
| 115 |
else |
| 116 |
begin |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
let translated = |
| 121 |
if is_symbolchar <> was_symbolchar then |
| 122 |
match tt.node with |
| 123 |
| Some tok -> Buffer.clear buff; tok |
| 124 |
| None -> translated |
| 125 |
else translated in |
| 126 |
|
| 127 |
|
| 128 |
try |
| 129 |
let tt = ttree_descend tt c in |
| 130 |
Buffer.add_char buff c; |
| 131 |
Buffering (is_symbolchar,ctag,translated,tt) |
| 132 |
with Not_found -> |
| 133 |
|
| 134 |
if is_symbolchar <> was_symbolchar then |
| 135 |
|
| 136 |
|
| 137 |
(flush_buffer was_symbolchar tag translated; |
| 138 |
restart_buffering ()) |
| 139 |
else |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
(Buffer.add_char buff c; |
| 144 |
Buffering (is_symbolchar,tag,translated,empty_ttree)) |
| 145 |
end |
| 146 |
|
| 147 |
and restart_buffering () = |
| 148 |
let tt = try ttree_descend !(!token_tree) c with Not_found -> empty_ttree in |
| 149 |
Buffer.add_char buff c; |
| 150 |
Buffering (is_symbolchar,ctag,"",tt) |
| 151 |
|
| 152 |
in |
| 153 |
translation_state := aux !translation_state |
| 154 |
|
| 155 |
let output_tagged_ident_string s = |
| 156 |
for i = 0 to String.length s - 1 do buffer_char false None s.[i] done |
| 157 |
|
| 158 |
let output_tagged_symbol_char tag c = |
| 159 |
buffer_char true tag c |
| 160 |
|
| 161 |
let flush_sublexer () = |
| 162 |
match !translation_state with |
| 163 |
| Neutral -> () |
| 164 |
| Buffering (was_symbolchar,tag,translated,tt) -> |
| 165 |
let translated = |
| 166 |
match tt.node with |
| 167 |
| Some tok -> Buffer.clear buff; tok |
| 168 |
| None -> translated in |
| 169 |
flush_buffer was_symbolchar tag translated; |
| 170 |
translation_state := Neutral |
| 171 |
|
| 172 |
|
| 173 |
let translate s = |
| 174 |
try (ttree_find !(!token_tree) s).node with Not_found -> None |