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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
|
{
(***********************************************************************)
(* OCamldoc *)
(* *)
(* Maxence Guesdon, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 2001 Institut National de Recherche en Informatique et *)
(* en Automatique. All rights reserved. This file is distributed *)
(* under the terms of the Q Public License version 1.0. *)
(* *)
(***********************************************************************)
(* $Id$ *)
(** The lexer for string to build text structures. *)
open Lexing
open Odoc_text_parser
let line_number = ref 0
let char_number = ref 0
let string_buffer = Buffer.create 32
(** Fonction de remise à zéro de la chaine de caractères tampon *)
let reset_string_buffer () = Buffer.reset string_buffer
(** Fonction d'ajout d'un caractère dans la chaine de caractères tampon *)
let ajout_char_string = Buffer.add_char string_buffer
(** Add a string to the buffer. *)
let ajout_string = Buffer.add_string string_buffer
let lecture_string () = Buffer.contents string_buffer
(** the variable which will contain the description string.
Is initialized when we encounter the start of a special comment. *)
let description = ref ""
let blank = "[ \013\009\012]"
let print_DEBUG s = print_string s; print_newline ()
(** this flag indicates whether we're in a string between begin_code and end_code tokens, to
remember the number of open '[' and handle ']' correctly. *)
let open_brackets = ref 0
(** this flag indicates if we're in verbatim mode or not, to handle any special expression
like a string when we're in verbatim mode.*)
let verb_mode = ref false
(** this flag indicates if we're in latex mode or not, to handle any special expression
like a string when we're in latex mode.*)
let latex_mode = ref false
(** this flag indicates if we're in shortcut list mode or not, to handle end_shortcut_list correctly.*)
let shortcut_list_mode = ref false
(** this flag indicates if we're in an element reference. *)
let ele_ref_mode = ref false
(** this flag indicates if we're in a preformatted code string. *)
let code_pre_mode = ref false
let init () =
open_brackets := 0;
verb_mode := false;
latex_mode := false;
shortcut_list_mode := false;
ele_ref_mode := false ;
code_pre_mode := false ;
line_number := 0 ;
char_number := 0
let incr_cpts lexbuf =
let s = Lexing.lexeme lexbuf in
let l = Str.split_delim (Str.regexp_string "\n") s in
match List.rev l with
[] -> () (* should not occur *)
| [s2] -> (* no newline *)
char_number := !char_number + (String.length s2)
| s2 :: _ ->
line_number := !line_number + ((List.length l) - 1) ;
char_number := String.length s2
}
(** html marks, to use as alternative possible special strings *)
let html_bold = "<"('b'|'B')">"
let html_end_bold = "</"('b'|'B')">"
let html_italic = "<"('i'|'I')">"
let html_end_italic = "</"('i'|'I')">"
let html_title = "<"('h'|'H')(['0'-'9'])+">"
let html_end_title = "</"('h'|'H')(['0'-'9'])+">"
let html_list = "<"('u'|'U')('l'|'L')">"
let html_end_list = "</"('u'|'U')('l'|'L')">"
let html_enum = "<"('o'|'O')('l'|'L')">"
let html_end_enum = "</"('o'|'O')('l'|'L')">"
let html_item = "<"('l'|'L')('i'|'I')">"
let html_end_item = "</"('l'|'L')('i'|'I')">"
let html_code = "<"('c'|'C')('o'|'O')('d'|'D')('e'|'E')">"
let html_end_code = "</"('c'|'C')('o'|'O')('d'|'D')('e'|'E')">"
let html_center = "<"('c'|'C')('e'|'E')('n'|'N')('t'|'T')('e'|'E')('r'|'R')">"
let html_end_center = "</"('c'|'C')('e'|'E')('n'|'N')('t'|'T')('e'|'E')('r'|'R')">"
let html_left = "<"('l'|'L')('e'|'E')('f'|'F')('t'|'T')">"
let html_end_left = "</"('l'|'L')('e'|'E')('f'|'F')('t'|'T')">"
let html_right = "<"('r'|'R')('i'|'I')('g'|'G')('h'|'H')('t'|'T')">"
let html_end_right = "</"('r'|'R')('i'|'I')('g'|'G')('h'|'H')('t'|'T')">"
let blank = [' ' '\013' '\009' '\012']
let blank_nl = [' ' '\013' '\009' '\012' '\010']
let label = ['a'-'z']+['a'-'z' 'A'-'Z' '0'-'9' '_']*
(** special strings *)
let end = "}"
| html_end_bold
| html_end_italic
| html_end_title
| html_end_list
| html_end_enum
| html_end_item
| html_end_center
let begin_title =
("{" ['0'-'9']+(":"label)? blank_nl)
| html_title
let begin_bold = "{b"blank_nl | html_bold
let begin_emp = "{e"blank_nl
let begin_center = "{C"blank_nl | html_center
let begin_left = "{L"blank_nl
let begin_right = "{R"blank_nl
let begin_italic = "{i"blank_nl | html_italic
let begin_list = "{ul" | html_list
let begin_enum = "{ol" | html_enum
let begin_item = "{li"blank_nl | "{- " | html_item
let begin_link = "{{:"
let begin_latex = "{%"blank_nl
let end_latex = "%}"
let begin_code = "[" | html_code
let end_code = "]" | html_end_code
let begin_code_pre = "{["
let end_code_pre = "]}"
let begin_verb = "{v"blank_nl
let end_verb = blank_nl"v}"
let begin_ele_ref = "{!"blank_nl | "{!"
let begin_val_ref = "{!val:"blank_nl | "{!val:"
let begin_typ_ref = "{!type:"blank_nl | "{!type:"
let begin_exc_ref = "{!exception:"blank_nl | "{!exception:"
let begin_mod_ref = "{!module:"blank_nl | "{!module:"
let begin_modt_ref = "{!modtype:"blank_nl | "{!modtype:"
let begin_cla_ref = "{!class:"blank_nl | "{!class:"
let begin_clt_ref = "{!classtype:"blank_nl | "{!classtype:"
let begin_att_ref = "{!attribute:"blank_nl | "{!attribute:"
let begin_met_ref = "{!method:"blank_nl | "{!method:"
let begin_sec_ref = "{!section:"blank_nl | "{!section:"
let begin_superscript = "{^"blank_nl | "{^"
let begin_subscript = "{_"blank_nl | "{_"
let shortcut_list_item = '\n'blank*"- "
let shortcut_enum_item = '\n'blank*"+ "
let end_shortcut_list = '\n'(blank*'\n')+
rule main = parse
| "\\{"
| "\\}"
| "\\["
| "\\]"
{
incr_cpts lexbuf ;
let s = Lexing.lexeme lexbuf in
Char (String.sub s 1 1)
}
| end
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or
(!open_brackets >= 1) then
Char (Lexing.lexeme lexbuf)
else
let _ =
if !ele_ref_mode then
ele_ref_mode := false
in
END
}
| begin_title
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or
(!open_brackets >= 1) or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
let s = Lexing.lexeme lexbuf in
try
(* chech if the "{..." or html_title mark was used. *)
if s.[0] = '<' then
let (n, l) = (2, (String.length s - 3)) in
let s2 = String.sub s n l in
Title (int_of_string s2, None)
else
let (n, l) = (1, (String.length s - 2)) in
let s2 = String.sub s n l in
try
let i = String.index s2 ':' in
let s_n = String.sub s2 0 i in
let s_label = String.sub s2 (i+1) (l-i-1) in
Title (int_of_string s_n, Some s_label)
with
Not_found ->
Title (int_of_string s2, None)
with
_ ->
Title (1, None)
}
| begin_bold
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or
(!open_brackets >= 1) or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
BOLD
}
| begin_italic
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or
(!open_brackets >= 1) or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
ITALIC
}
| begin_link
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or
(!open_brackets >= 1) or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
LINK
}
| begin_emp
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or
(!open_brackets >= 1) or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
EMP
}
| begin_superscript
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or
(!open_brackets >= 1) or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
SUPERSCRIPT
}
| begin_subscript
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or
(!open_brackets >= 1) or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
SUBSCRIPT
}
| begin_center
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or
(!open_brackets >= 1) or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
CENTER
}
| begin_left
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or
(!open_brackets >= 1) or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
LEFT
}
| begin_right
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode
or (!open_brackets >= 1) or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
RIGHT
}
| begin_list
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or
(!open_brackets >= 1) or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
LIST
}
| begin_enum
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or
(!open_brackets >= 1) or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
ENUM
}
| begin_item
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or
(!open_brackets >= 1) or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
ITEM
}
| begin_latex
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or
(!open_brackets >= 1) or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
(
latex_mode := true;
LATEX
)
}
| end_latex
{
incr_cpts lexbuf ;
if !verb_mode or (!open_brackets >= 1) or !code_pre_mode or
!ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
(
latex_mode := false;
END_LATEX
)
}
| begin_code end_code
{
incr_cpts lexbuf ;
Char (Lexing.lexeme lexbuf)
}
| begin_code
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
if !open_brackets <= 0 then
(
open_brackets := 1;
CODE
)
else
(
incr open_brackets;
Char (Lexing.lexeme lexbuf)
)
}
| end_code
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
if !open_brackets > 1 then
(
decr open_brackets;
Char "]"
)
else
(
open_brackets := 0;
END_CODE
)
}
| begin_code_pre end_code_pre
{
incr_cpts lexbuf ;
Char (Lexing.lexeme lexbuf)
}
| begin_code_pre
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
(
code_pre_mode := true;
CODE_PRE
)
}
| end_code_pre
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
if !open_brackets >= 1 then
(
lexbuf.Lexing.lex_curr_pos <- lexbuf.Lexing.lex_curr_pos - 1;
lexbuf.Lexing.lex_curr_p <-
{ lexbuf.Lexing.lex_curr_p with
pos_cnum = lexbuf.Lexing.lex_curr_p.pos_cnum - 1
} ;
decr char_number ;
if !open_brackets > 1 then
(
decr open_brackets;
Char "]"
)
else
(
open_brackets := 0;
END_CODE
)
)
else
if !code_pre_mode then
(
code_pre_mode := false;
END_CODE_PRE
)
else
Char (Lexing.lexeme lexbuf)
}
| begin_ele_ref end
{
incr_cpts lexbuf ;
Char (Lexing.lexeme lexbuf)
}
| begin_ele_ref
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or !open_brackets >= 1 then
Char (Lexing.lexeme lexbuf)
else
if not !ele_ref_mode then
(
ele_ref_mode := true;
ELE_REF
)
else
(
Char (Lexing.lexeme lexbuf)
)
}
| begin_val_ref
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or !open_brackets >= 1 then
Char (Lexing.lexeme lexbuf)
else
if not !ele_ref_mode then
(
ele_ref_mode := true;
VAL_REF
)
else
(
Char (Lexing.lexeme lexbuf)
)
}
| begin_typ_ref
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or !open_brackets >= 1 then
Char (Lexing.lexeme lexbuf)
else
if not !ele_ref_mode then
(
ele_ref_mode := true;
TYP_REF
)
else
(
Char (Lexing.lexeme lexbuf)
)
}
| begin_exc_ref
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or !open_brackets >= 1 then
Char (Lexing.lexeme lexbuf)
else
if not !ele_ref_mode then
(
ele_ref_mode := true;
EXC_REF
)
else
(
Char (Lexing.lexeme lexbuf)
)
}
| begin_mod_ref
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or !open_brackets >= 1 then
Char (Lexing.lexeme lexbuf)
else
if not !ele_ref_mode then
(
ele_ref_mode := true;
MOD_REF
)
else
(
Char (Lexing.lexeme lexbuf)
)
}
| begin_modt_ref
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or !open_brackets >= 1 then
Char (Lexing.lexeme lexbuf)
else
if not !ele_ref_mode then
(
ele_ref_mode := true;
MODT_REF
)
else
(
Char (Lexing.lexeme lexbuf)
)
}
| begin_cla_ref
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or !open_brackets >= 1 then
Char (Lexing.lexeme lexbuf)
else
if not !ele_ref_mode then
(
ele_ref_mode := true;
CLA_REF
)
else
(
Char (Lexing.lexeme lexbuf)
)
}
| begin_clt_ref
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or !open_brackets >= 1 then
Char (Lexing.lexeme lexbuf)
else
if not !ele_ref_mode then
(
ele_ref_mode := true;
CLT_REF
)
else
(
Char (Lexing.lexeme lexbuf)
)
}
| begin_att_ref
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or !open_brackets >= 1 then
Char (Lexing.lexeme lexbuf)
else
if not !ele_ref_mode then
(
ele_ref_mode := true;
ATT_REF
)
else
(
Char (Lexing.lexeme lexbuf)
)
}
| begin_met_ref
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or !open_brackets >= 1 then
Char (Lexing.lexeme lexbuf)
else
if not !ele_ref_mode then
(
ele_ref_mode := true;
MET_REF
)
else
(
Char (Lexing.lexeme lexbuf)
)
}
| begin_sec_ref
{
incr_cpts lexbuf ;
if !verb_mode or !latex_mode or !code_pre_mode or !open_brackets >= 1 then
Char (Lexing.lexeme lexbuf)
else
if not !ele_ref_mode then
(
ele_ref_mode := true;
SEC_REF
)
else
(
Char (Lexing.lexeme lexbuf)
)
}
| begin_verb
{
incr_cpts lexbuf ;
if !latex_mode or (!open_brackets >= 1) or !code_pre_mode or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
(
verb_mode := true;
VERB
)
}
| end_verb
{
incr_cpts lexbuf ;
if !latex_mode or (!open_brackets >= 1) or !code_pre_mode or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
(
verb_mode := false;
END_VERB
)
}
| shortcut_list_item
{
incr_cpts lexbuf ;
if !shortcut_list_mode then
(
SHORTCUT_LIST_ITEM
)
else
(
shortcut_list_mode := true;
BEGIN_SHORTCUT_LIST_ITEM
)
}
| shortcut_enum_item
{
incr_cpts lexbuf ;
if !shortcut_list_mode then
SHORTCUT_ENUM_ITEM
else
(
shortcut_list_mode := true;
BEGIN_SHORTCUT_ENUM_ITEM
)
}
| end_shortcut_list
{
incr_cpts lexbuf ;
lexbuf.Lexing.lex_curr_pos <- lexbuf.Lexing.lex_curr_pos - 1;
lexbuf.Lexing.lex_curr_p <-
{ lexbuf.Lexing.lex_curr_p with
pos_cnum = lexbuf.Lexing.lex_curr_p.pos_cnum - 1 ;
} ;
decr line_number ;
if !shortcut_list_mode then
(
shortcut_list_mode := false;
(* go back one char to re-use the last '\n', so we can
restart another shortcut-list with a single blank line,
and not two.*)
END_SHORTCUT_LIST
)
else
if !latex_mode or (!open_brackets >= 1) or !code_pre_mode or !ele_ref_mode or !verb_mode then
Char (Lexing.lexeme lexbuf)
else
BLANK_LINE
}
| eof { EOF }
| "{"
{
incr_cpts lexbuf ;
if !latex_mode or (!open_brackets >= 1) or !code_pre_mode or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
ERROR
}
| _
{
incr_cpts lexbuf ;
Char (Lexing.lexeme lexbuf)
}
|