File: index.ml

package info (click to toggle)
hevea 2.38-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,824 kB
  • sloc: ml: 19,525; sh: 505; makefile: 311; ansic: 132
file content (402 lines) | stat: -rw-r--r-- 10,438 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
(***********************************************************************)
(*                                                                     *)
(*                          HEVEA                                      *)
(*                                                                     *)
(*  Luc Maranget, projet PARA, INRIA Rocquencourt                      *)
(*                                                                     *)
(*  Copyright 1998 Institut National de Recherche en Informatique et   *)
(*  Automatique.  Distributed only by permission.                      *)
(*                                                                     *)
(***********************************************************************)

open Misc
open Entry


let missing_index tag =
  Misc.warning
    ("Index structure not found, missing "^
    (match tag with
    | "default" -> "\\makeindex"
    | _  -> "\\newindex{"^tag^"}.."))
;;


type entry_t = {key : key ; see : string option ; item : string}
;;

type entry =
  | Good of entry_t
  | Bad

let first_key = function 
  | (x::_),_ -> x
  | _ -> raise (Misc.Fatal ("Empty key in first_key"))

let pretty_key (l,p) =
 let rec p_rec l p = match l,p with
   [],[] -> ""
 | [x],[""]-> x
 | [x],[y]-> x^"@"^y
 | x::xs,""::ys -> x^"!"^p_rec xs ys
 | x::xs,y::ys -> x^"@"^y^"!"^p_rec xs ys
 |  _,_ -> assert false in
 p_rec l p
;;


type t_index =
  {mutable name : string ;
  mutable onebad : bool ;
  mutable counter : int ;
  sufin : string ; sufout : string ;
  from_file : entry array option ;
  from_doc : entry Table.t ;
  out : Out.t}

let itable = Hashtbl.create 17
;;


let read_index_file name file =
  let lexbuf = Lexing.from_channel file in
  let r = Table.create Bad in
  let rec do_rec () =
    try
      let arg1,arg2 = read_indexentry lexbuf in
      let entry =
        try
          let k,see = read_key (MyLexing.from_string arg1) in
          Good {key=k ; see=see ; item = arg2}
        with Entry.NoGood ->
          Misc.warning
          ("Bad index arg syntax in file: "^name^
           ", index entry is '"^arg1^"'") ;
          Bad in
      Table.emit r entry ;
      do_rec ()
    with
    | Entry.Fini -> Table.trim r in

  let r = do_rec () in
  if !verbose > 0 then
    prerr_endline ("Index file: "^name^" successfully read");
  Some r

let find_index tag = Hashtbl.find itable tag

let changename tag name =
  try
    let idx = find_index  tag in
    idx.name <- name
  with Not_found -> missing_index tag

let index_lbl tag i = "hevea_"^tag^string_of_int i
let index_filename suff = Parse_opts.base_out^".h"^suff

let do_treat tag arg refvalue anchor =
(*  prerr_endline ("Index treat: "^tag^", "^arg^", "^refvalue) ; *)
  try
    if !verbose > 2 then prerr_endline ("Index.treat with arg: "^arg) ;
    let {from_doc = from_doc ; out = out} as idx =  find_index tag in
    let lbl =
      match anchor with
      | Some lbl -> lbl
      | None     -> index_lbl tag idx.counter in    
    let refvalue = match refvalue with "" -> "??" | s -> s in
    let item = "\\@locref{"^lbl^"}{"^refvalue^"}" in
    Out.put out "\\indexentry{" ;
    Out.put out arg ;
    Out.put out "}{" ;
    Out.put out item ;
    Out.put out "}\n" ;
    
    let lexbuf = MyLexing.from_string arg in
    let entry =
      try
        let key,see = read_key lexbuf in
        Good {key = key ; see = see ; item = item}
      with
      | Entry.NoGood ->
          idx.onebad <- true ;
          Misc.warning ("Bad index syntax: '"^arg^"'") ;
          Bad in
    Table.emit from_doc entry ;
    idx.counter <- idx.counter + 1 ;
    lbl
  with
  | Not_found -> missing_index tag ; ""
;;

let treat tag arg refvalue =
  do_treat tag arg refvalue None

and treat_anchor tag arg refvalue lbl =
  ignore (do_treat tag arg refvalue (Some lbl))

(* Compare function for keys *)


let is_alpha c =  ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')

let compare_char c1 c2 =
  if is_alpha c1 && is_alpha c2 then
    let r = compare (Char.uppercase_ascii c1) (Char.uppercase_ascii c2) in
    if r <> 0 then r
    else compare c1 c2
  else if is_alpha c1 then 1
  else if is_alpha c2 then -1
  else compare c1 c2

exception Result of int

let compare_string s1 s2 =
  let i = ref 0
  and l1 = String.length s1
  and l2 = String.length s2 in
  begin try
    while true do
      begin if !i >= l1 then
        if !i >= l2 then raise (Result 0)
        else raise (Result (-1))
      else if !i >= l2 then raise (Result 1)
      else
        let c = compare_char s1.[!i] s2.[!i] in
        if c <> 0 then raise (Result c)
      end ;
      i := !i + 1
    done ;
    0
  with Result x -> x
  end
   
let comp (l1,p1) (l2,p2) =

  let rec c_rec l1 l2 p1 p2 = match l1,l2 with
  | [],[] -> 0    
  | [],_  -> -1
  | _,[]  -> 1
  | x1::r1,x2::r2 ->
      let t = compare_string x1 x2 in
      if t<> 0 then t
      else begin
        match p1,p2 with
        | y1::p1, y2::p2 ->
            let t = compare_string y1 y2 in
            if t <> 0 then t
            else
              c_rec r1 r2 p1 p2
        | _,_ -> assert false
      end in
  c_rec l1 l2 p1 p2
;;

module OrderedKey = struct
  type t = key
  let compare = comp
end
;;

module KeySet =  Set.Make(OrderedKey)
;;


let rec common e1 e2 = match e1,e2 with
  ([],_),_        -> e1,e2
| _,([],_)        -> e1,e2
| ([_],_),([_],_) -> e1,e2
| (_::_,_),([_],_) -> e1,e2
| (x1::r1,_::p1),(x2::r2,_::p2) ->
    if x1=x2 then
      common (r1,p1) (r2,p2)
    else
      e1,e2
|  _ -> assert false
;;

let rec close_prev out = function
  [],_ | [_],_ -> ()
| _::r,_::p    ->
    Out.put out "\\end{indexenv}\n" ;
    close_prev out (r,p)
|  _ -> assert false
;;

let rec open_this out k = match k with
  [],_ -> ()
| k::r,p::rp ->
    Out.put out "\\indexitem " ;
    let tag = if p <> "" then p else k in
    Out.put out tag  ;
    begin match r with
      [] -> ()
    | _  -> Out.put out "\\begin{indexenv}\n" ;
    end ;
    open_this out (r,rp)
|  _ -> assert false
;;

let start_change s1 s2 = match s1,s2 with
| "",_ -> false
| _,"" -> false
| _,_  -> Char.uppercase_ascii s1.[0] <> Char.uppercase_ascii s2.[0]

let print_entry out _ entries bk k xs  =
  let rp,rt = common bk k in
  close_prev out rp ;
  if fst rp = [] then
    Out.put out "\\begin{indexenv}\n"
  else begin
    let top_prev = first_key bk
    and top_now = first_key k in
    if start_change top_prev top_now then
      Out.put out "\\indexspace\n"
  end ;
  open_this out rt ;  

  let rec prints = function
    [] -> Out.put_char out '\n'
  | i::r ->
      Out.put out ", " ;
      begin match entries.(i) with
      | Good e ->
          begin match e.see with
          | None     ->  Out.put out e.item
          | Some see ->  Out.put out ("\\"^see^"{"^e.item^"}")
          end ;
      | Bad -> ()
      end ;
      prints r in

   prints (List.rev xs)
;;

     
let make_index t =
  let table = Hashtbl.create 17
  and all = ref KeySet.empty in
  for i = 0 to Array.length t - 1 do
    match t.(i) with
    | Good e ->
        all := KeySet.add e.key !all ;
        Hashtbl.add table e.key i
    | Bad -> ()
  done ;
  !all,table


let output_index tag entries out =
  if !verbose > 1 then prerr_endline ("Print index ``"^tag^"''") ;
  let all_keys,table = make_index entries in
  let prev = ref ([],[]) in
  KeySet.iter (fun k ->
    if !verbose > 2 then
      prerr_endline ("Print_entry: "^pretty_key k);
    print_entry out tag entries !prev k (Hashtbl.find_all table k) ;
    prev := k)
    all_keys ;
  let pk,_ = !prev in
  List.iter (fun _ -> Out.put out "\\end{indexenv}\n") pk


let create_hind t tag sufout = 
  let outname = index_filename sufout in
  try 
    let chan = open_out outname in
    output_index tag t (Out.create_chan chan) ;
    close_out chan
  with
  | Sys_error s ->
      Misc.warning ("File error for "^outname^": "^s)

let newindex tag sufin sufout name =  
(*  prerr_endline ("New index: "^tag) ; *)
  Hashtbl.remove itable tag ;
  let from_file =
    try
      let filename = index_filename sufin in
      let file = open_in filename in
      read_index_file filename file
    with Sys_error _ -> None in
  begin match from_file with
  | None -> ()
  | Some t -> create_hind t tag sufout
  end ;
  Hashtbl.add itable tag
    {name = name ;
    onebad = false ;
    counter = 0 ;
    sufin = sufin ; sufout = sufout ;
    from_file = from_file ;
    from_doc = Table.create Bad ;
    out = Out.create_buff ()}

let print main tag =
  try
    let idx = find_index tag in
    main ("\\label{section@the@hevea@index@" ^ tag ^ "}");
    main ("\\@indexsection{" ^ idx.name ^ "}") ;
    main "\\begin{the@hevea@index}";
    let indname = index_filename idx.sufout in
    begin match idx.from_file with
    | None ->
        create_hind  (Table.trim idx.from_doc) tag idx.sufout
    | _ -> ()
    end ;
    main ("\\input{"^indname^"}");
    main "\\end{the@hevea@index}"
  with
  | Not_found -> missing_index tag

let diff_entries e1 e2 =
  let l1 = Array.length e1 and l2 = Array.length e2 in
  if l1 <> l2 then true
  else
    let rec diff_rec i =
      if i >= l1 then false
      else
        e1.(i) <> e2.(i) || diff_rec (i+1) in
    diff_rec 0

let do_dump_index idxname idx = 
  try
    let chan = open_out idxname in
    Out.to_chan chan idx.out ;
    close_out chan
  with
  | Sys_error s ->
      Misc.warning
        ("File error on "^idxname^": "^s)          

let finalize check =
  if check then begin
    let top_changed = ref false in
    Hashtbl.iter
      (fun _ idx ->
        let entries = Table.trim idx.from_doc in
        let changed =
          match idx.from_file with
          | Some t -> diff_entries t entries
          | None   -> Array.length entries <> 0 in
        let idxname = index_filename idx.sufin in
        if changed || idx.onebad then begin
          top_changed := !top_changed || changed ;
          try
            if Array.length entries = 0 && not idx.onebad then
              Mysys.remove idxname 
            else begin
              do_dump_index idxname idx
            end
          with
          | Sys_error s ->
              Misc.warning
                ("File error on "^idxname^": "^s)
        end else if !Misc.dump_index then begin
          do_dump_index idxname idx
        end)
      itable ;
    if !top_changed then
      Misc.message
        "HeVeA Warning: Index(es) may have changed. Rerun me to get them right." ;
    !top_changed
  end else false