-
Notifications
You must be signed in to change notification settings - Fork 11
/
verb.mll
1793 lines (1572 loc) · 49.2 KB
/
verb.mll
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(***********************************************************************)
(* *)
(* HEVEA *)
(* *)
(* Luc Maranget, projet Moscova, INRIA Rocquencourt *)
(* *)
(* Copyright 2001 Institut National de Recherche en Informatique et *)
(* Automatique. Distributed only by permission. *)
(* *)
(* $Id: verb.mll,v 1.85 2006-02-03 12:25:49 maranget Exp $ *)
(***********************************************************************)
{
exception VError of string
module type S = sig end
;;
module Make
(Dest : OutManager.S) (Image : ImageManager.S)
(Scan : Latexscan.S) : S =
struct
open Printf
open Misc
open Lexing
open Save
open Lexstate
open Latexmacros
open Stack
open Scan
open Subst
exception Eof of string
exception ParseError
(* For file verbatim scanning *)
let input_verb = ref false
;;
(* For scanning by line *)
let line_buff = Out.create_buff ()
and process = ref (fun () -> ())
and finish = ref (fun () -> ())
;;
let env_extract s =
let i = String.index s '{'
and j = String.rindex s '}' in
String.sub s (i+1) (j-i-1)
and newlines_extract s =
let rec do_rec i =
if i < String.length s then begin
if s.[i] = '\n' then
1+do_rec (i+1)
else
0
end else
0 in
do_rec 0
(* For scanning the ``listings'' way *)
let lst_process_error _ lxm =
warning ("listings, unknown character: ``"^Char.escaped lxm^"''")
let lst_char_table = Array.create 256 lst_process_error
;;
let lst_init_char c f =
lst_char_table.(Char.code c) <- f
let lst_init_chars s f =
let last = String.length s - 1 in
for i = 0 to last do
lst_char_table.(Char.code s.[i]) <- f
done
let lst_init_save_char c f =
let old = lst_char_table.(Char.code c) in
lst_char_table.(Char.code c) <- f old
let lst_init_save_chars s f =
let last = String.length s - 1 in
for i = 0 to last do
lst_init_save_char s.[i] f
done
(* Parameters *)
type line_limit =
| LineNumber of int
| Marker of string
let pmark = function
| LineNumber x -> string_of_int x
| Marker tok -> "«"^tok^"»"
let lst_gobble = ref 0
and lst_nlines = ref 0
and lst_first = ref 1
and lst_last = ref 99999 (* infinity *)
and lst_linerange = ref []
and lst_print = ref true
and lst_includerangemarker = ref true
and lst_string_spaces = ref true
and lst_texcl = ref false
and lst_extended = ref false
and lst_sensitive = ref true
and lst_mathescape = ref false
and lst_directives = ref false
and lst_showlines = ref false
and lst_tabsize = ref 8
and lst_col = ref 0
let lst_showspaces = ref false (* false => spaces are spaces *)
and lst_showtabs = ref false
and lst_save_spaces = ref false
(* Output functions *)
let lst_buff = Out.create_buff ()
let lst_last_char = ref ' '
and lst_finish_comment = ref 0
let lst_put c =
incr lst_col ;
lst_last_char := c ;
Out.put_char lst_buff c
and lst_direct_put c =
incr lst_col ;
lst_last_char := c ;
Dest.put_char c
type lst_scan_mode =
| Letter | Other | Empty | Start
| Directive of bool (* bool flags some letter read *)
let show_mode = function
| Letter -> "Letter" | Other -> "Other" | Empty -> "Empty" | Start -> "Start"
| Directive _ -> "Directive"
let lst_scan_mode = ref Empty
type comment_type =
| Nested of int
| Balanced of (char -> string -> bool)
| Line
type lst_top_mode =
| Skip of lst_top_mode
| StartNextLine of lst_top_mode * (bool ref) | EndNextLine of lst_top_mode
| String of (char * (char * (Lexing.lexbuf -> char -> unit)) list)
| Normal | Comment of string * comment_type
| Delim of int * (char * (Lexing.lexbuf -> char -> unit)) list
| Gobble of lst_top_mode * int
| Escape of lst_top_mode * char * bool (* bool flags mathescape *)
let is_normal = function
| Normal -> true
| _ -> false
let is_outputing = function
| Skip _ -> false
| _ -> true
let rec string_of_top_mode = function
| Delim (i,_) -> "Delim: "^string_of_int i
| Skip _ -> "Skip"
| StartNextLine (_,_) -> "StartNextLine"
| EndNextLine (mode) ->
sprintf "EndNextLine (%s)" (string_of_top_mode mode)
| Comment (_, Balanced _) -> "Balanced"
| Comment (_, Nested n) -> "(Nested "^string_of_int n^")"
| Comment (_, Line) -> "Line"
| String _ -> "String"
| Normal -> "Normal"
| Gobble (_,_) -> "Gobble"
| Escape (_,_,_) -> "Escape"
let lst_top_mode = ref (Skip Normal)
let lst_ptok s = prerr_endline (s^": "^Out.to_string lst_buff)
(* Final ouput, with transformations *)
let dest_string s =
for i = 0 to String.length s - 1 do
Dest.put (Dest.iso s.[i])
done
(* Echo, with case change *)
let dest_case s =
Dest.put
(match !case with
| Upper -> String.uppercase s
| Lower -> String.lowercase s
| _ -> s)
(* Keywords *)
let def_print s =
Latexmacros.def "\\@tmp@lst" zero_pat
(CamlCode (fun _ -> dest_case s)) ;
Latexmacros.def "\\@tmp@lst@print" zero_pat
(CamlCode (fun _ -> dest_string s))
;;
let lst_output_com com =
let com = com^"{\\@tmp@lst}{\\@tmp@lst@print}" in
fun () ->
if not (Out.is_empty lst_buff) then begin
let arg = Out.to_string lst_buff in
match !lst_top_mode with
| Normal ->
def_print arg ;
scan_this Scan.main com
| Skip _|StartNextLine (_,_)
| Gobble (_,_)
| Escape (_,_,_) ->
assert false
| Delim (_, _)
| Comment (_,_)|String _
| EndNextLine _ ->
scan_this main "\\@NewLine" ;
dest_string arg
end
let lst_output_other = lst_output_com "\\lst@output@other"
and lst_output_letter = lst_output_com "\\lst@output"
and lst_output_directive = lst_output_com "\\lst@output@directive"
let lst_output_token () =
match !lst_scan_mode with
| Letter -> lst_output_letter ()
| Other -> lst_output_other ()
| Directive _ -> lst_output_directive ()
| Empty|Start -> scan_this main "\\@NewLine"
(*********************)
(* Delay some action *)
(*********************)
let chars_string c s =
let rec do_rec r i =
if i < String.length s then
if List.mem s.[i] r then
do_rec r (i+1)
else
do_rec (s.[i]::r) (i+1)
else
r in
do_rec [c] 0
let init_char_table_delim chars wrapper =
List.map
(fun c ->
let old_process = lst_char_table.(Char.code c) in
lst_init_save_char c wrapper ;
(c,old_process))
chars
let rec restore_char_table to_restore =
let rec do_rec = function
| [] -> ()
| (c,f)::rest ->
lst_init_char c f ;
do_rec rest in
do_rec to_restore
let eat_delim k new_mode old_process lb c s =
let chars = chars_string c s in
let wrapper old_process lb c = match !lst_top_mode with
| Delim (n,to_restore) ->
old_process lb c ;
if n = 1 then begin
lst_output_token () ;
lst_top_mode := new_mode ;
restore_char_table to_restore ;
k ()
end else
lst_top_mode := Delim (n-1,to_restore)
| _ -> assert false in
let to_restore = init_char_table_delim chars wrapper in
lst_top_mode := Delim (1+String.length s, to_restore) ;
wrapper old_process lb c
let delay_action k mode old_process lb c s =
let chars = chars_string c s in
let wrapper old_process lb c = match !lst_top_mode with
| Delim (n,to_restore) ->
lst_top_mode := mode ;
old_process lb c ;
if n = 1 then begin
restore_char_table to_restore ;
k ()
end else
lst_top_mode := Delim (n-1,to_restore)
| _ -> assert false in
let to_restore = init_char_table_delim chars wrapper in
lst_top_mode := Delim (1+String.length s, to_restore) ;
wrapper old_process lb c
(* Process functions *)
let lst_do_gobble mode n =
if n > 1 then
lst_top_mode := Gobble (mode,n-1)
else
lst_top_mode := mode
let lst_do_escape mode endchar math _lb lxm =
if lxm = endchar then begin
scan_this main "\\begingroup\\lst@escapebegin" ;
if math then scan_this main "$" ;
scan_this main (Out.to_string lst_buff) ;
if math then scan_this main "$" ;
scan_this main "\\lst@escapeend\\endgroup" ;
lst_top_mode := mode
end else
Out.put_char lst_buff lxm
let debug_curline msg =
eprintf "%s: %s\n" msg (Scan.get_this_main "\\usebox{\\@curline}")
(* We put those here since newlines may terminate comments *)
let begin_comment () =
lst_output_token () ;
scan_this Scan.main "\\begin{lrbox}{\\lst@box}"
and end_comment sty () =
scan_this Scan.main
(Printf.sprintf "\\end{lrbox}{%s{\\lst@box}}" sty)
let end_string to_restore =
scan_this Scan.main "\\end{lrbox}{\\lst@stringstyle{\\lst@box}}" ;
restore_char_table to_restore ;
lst_showspaces := !lst_save_spaces
let rec end_mode mode = match mode with
| Comment (style,_) -> end_comment style ()
| String (_,to_restore) -> end_string to_restore
| Skip m|StartNextLine (m,_) -> end_mode m
| _ -> ()
(* Number of (printed) source blocks in a listing *)
let lst_nblocks = ref 0
(* Note, at the moment lst_process_newline is the only
processor that can start/stop output,
1. Process_newline is fooled by using lst_first/lst_last variables
2. This process_newlines must sometime not increase line numbers,
ie, when there is in fact no eol *)
let is_comment_line = function
| Comment (_, Line) -> true
| _ -> false
let rec lst_process_newline real_eol lb c =
if !verbose > 1 then
fprintf stderr "lst_process_newline: mode=%s first=%i last=%i nlines=%i\n"
(string_of_top_mode !lst_top_mode)
!lst_first !lst_last !lst_nlines ;
match !lst_top_mode with
| Skip newmode ->
if !lst_nlines = !lst_first - 1 then begin
lst_top_mode := newmode ;
lst_process_newline real_eol lb c ;
if !lst_nblocks = 0 then
scan_this Scan.main
(if !lst_first=1 then "\\let\\lst@br\\lst@@@br"
else "\\let\\lst@br\\lst@@br")
end else begin
if real_eol then begin
incr lst_nlines ;
scan_this Scan.main "\\lsthk@InitVarEOL"
end
end
| StartNextLine (newmode,to_activate) ->
lst_first := !lst_nlines+1 ;
lst_top_mode := Skip newmode ;
lst_process_newline real_eol lb c ;
to_activate := true
| EndNextLine (mode) ->
lst_last := !lst_nlines ;
lst_top_mode := mode ;
lst_process_newline real_eol lb c
| Gobble (mode,_) ->
lst_top_mode := mode ;
lst_process_newline real_eol lb c
| Escape (_mode,cc,math) ->
lst_do_escape (Comment ("\\@empty",Line)) cc math lb c ;
if is_comment_line !lst_top_mode then
lst_process_newline real_eol lb c
| Comment (style, Line) ->
lst_output_token () ;
end_comment style () ;
lst_top_mode := Normal ;
lst_process_newline real_eol lb c
| Delim (_, _) -> assert false
| String _|Normal|Comment (_,(Balanced _|Nested _)) as mode ->
if real_eol then
scan_this Scan.main "\\lsthk@InitVarEOL\\lsthk@EOL" ;
begin match !lst_scan_mode with
| Empty -> lst_scan_mode := Start
| Start -> ()
| _ ->
lst_output_token () ;
lst_scan_mode := Start
end ;
let next_line = !lst_nlines+1 in
if next_line <= !lst_last then begin
lst_col := 0 ;
scan_this Scan.main
"\\lsthk@InitVarBOL\\lsthk@EveryLine" ;
if !lst_gobble > 0 then
lst_top_mode := Gobble (mode,!lst_gobble)
end else begin
incr lst_nblocks ;
lst_col := 0 ;
scan_this Scan.main
"\\lsthk@InitVarBOL\\lsthk@LastLine" ;
set_next_linerange mode
end ;
if real_eol then lst_nlines := next_line
(***************)
(* Line ranges *)
(***************)
and process_EMark active rest_E old_process lb c =
if !active && if_next_string rest_E lb then begin
active := false ;
let zyva () =
lst_last := !lst_nlines-1 ;
lst_process_newline false lb c in
if !lst_includerangemarker then begin
delay_action zyva !lst_top_mode old_process lb c rest_E
end else begin
zyva () ;
old_process lb c
end
end else
old_process lb c
and lst_process_BMark active to_activate mark_start old_process lb c =
if !active then begin
match !lst_top_mode with
| Skip newmode when if_next_string mark_start lb ->
active := false ; lst_last := 99999 ;
if !lst_includerangemarker then begin
lst_first := !lst_nlines+1 ;
lst_process_newline false lb c ;
delay_action
(fun () -> to_activate := true)
!lst_top_mode old_process lb c mark_start
end else begin
lst_top_mode := StartNextLine (newmode, to_activate) ;
old_process lb c
end
| Escape (_, _, _)|Gobble (_, _)|Delim (_, _)|
Comment (_,_)|String _|EndNextLine _|
StartNextLine (_,_)|Skip _|Normal ->
old_process lb c
end else
old_process lb c
and init_marker mark_start activate_end =
let head_S = mark_start.[0]
and rest_S = String.sub mark_start 1 (String.length mark_start-1) in
lst_init_save_char head_S
(lst_process_BMark (ref true) activate_end rest_S)
and set_next_linerange mode = match !lst_linerange with
| [] ->
lst_first := 99999 ;
lst_top_mode := Skip mode
| (fst, lst)::rem ->
if !verbose > 1 then
eprintf "SET LINERANGE: %s %s\n" (pmark fst) (pmark lst) ;
lst_linerange := rem ;
let activate_end = ref false in
begin match lst with
| LineNumber x ->
lst_last := x
| Marker all_E ->
lst_last := 99999 ;
let head_E = all_E.[0]
and rest_E =
String.sub all_E 1 (String.length all_E-1) in
lst_init_save_char head_E (process_EMark activate_end rest_E)
end ;
begin match fst with
| LineNumber x ->
if !verbose > 1 then eprintf "fst=%i, nlines=%i\n" x !lst_nlines ;
lst_first := x ;
if !lst_nlines+1 < x then begin
lst_top_mode := Skip mode
end else begin
if !lst_nblocks = 0 then
scan_this Scan.main "\\let\\lst@br\\lst@@@br" ;
lst_top_mode := mode
end
| Marker tok ->
lst_first := -1 ;
lst_top_mode := Skip mode ;
init_marker tok activate_end
end
let lst_process_letter lb lxm =
if !verbose > 1 then fprintf stderr "lst_process_letter: %c\n" lxm ;
match !lst_top_mode with
| Skip _|StartNextLine (_,_) -> ()
| Gobble (mode,n) -> lst_do_gobble mode n
| Escape (mode,c,math) -> lst_do_escape mode c math lb lxm
| _ -> match !lst_scan_mode with
| Letter -> lst_put lxm
| Directive true ->
lst_put lxm
| Directive false ->
lst_scan_mode := Directive true ;
lst_put lxm
| Empty|Start ->
lst_scan_mode := Letter ;
lst_put lxm
| Other ->
lst_output_other () ;
lst_scan_mode := Letter ;
lst_put lxm
let lst_process_digit lb lxm =
if !verbose > 1 then
fprintf stderr "lst_process_digit: %c\n" lxm ;
match !lst_top_mode with
| Skip _|StartNextLine (_,_) -> ()
| Gobble (mode,n) -> lst_do_gobble mode n
| Escape (mode,c,math) -> lst_do_escape mode c math lb lxm
| _ -> match !lst_scan_mode with
| Letter|Other -> lst_put lxm
| Directive _ ->
lst_output_directive () ;
lst_scan_mode := Other ;
lst_put lxm
| Empty|Start ->
lst_scan_mode := Other ;
lst_put lxm
let lst_process_other lb lxm =
if !verbose > 1 then
fprintf stderr "process_other: %c\n" lxm ;
match !lst_top_mode with
| Skip _|StartNextLine (_,_) -> ()
| Gobble (mode,n) -> lst_do_gobble mode n
| Escape (mode,c,math) -> lst_do_escape mode c math lb lxm
| _ -> match !lst_scan_mode with
| Other -> lst_put lxm
| Empty|Start ->
lst_scan_mode := Other ;
lst_put lxm
| Directive _ ->
lst_output_directive () ;
lst_scan_mode := Other ;
lst_put lxm
| Letter ->
lst_output_letter () ;
lst_scan_mode := Other ;
lst_put lxm
(* Caml code for \stepcounter{lst@space} *)
let lst_output_space () =
incr lst_col ;
Counter.step_counter "lst@spaces"
let lst_process_space lb lxm =
if !verbose > 1 then
fprintf stderr "process_space: '%s'\n" (Char.escaped lxm) ;
match !lst_top_mode with
| Skip _ |StartNextLine (_,_)-> ()
| Gobble (mode,n) -> lst_do_gobble mode n
| Escape (mode,c,math) -> lst_do_escape mode c math lb lxm
| _ ->
begin match !lst_scan_mode with
| Other ->
lst_output_other () ;
lst_scan_mode := Empty
| Letter|Directive true ->
lst_output_token () ;
lst_scan_mode := Empty
| Empty|Directive false -> ()
| Start ->
lst_scan_mode := Empty
end ;
lst_output_space ()
let lst_process_tab lb lxm =
if !verbose > 1 then
fprintf stderr "process_tab: '%s'\n" (Char.escaped lxm) ;
let n = !lst_tabsize - (!lst_col mod !lst_tabsize) in
if !lst_showtabs && is_normal !lst_top_mode then begin
lst_output_token () ;
if Latexmacros.exists "\\lst@tab" then begin
for _i = 1 to n-1 do
lst_process_space lb lxm
done ;
let save = !lst_showspaces in
lst_showspaces := false ;
lst_output_token () ;
lst_showspaces := save ;
(* Assumes that the tab equivalent is one-char wide *)
incr lst_col ;
scan_this main "{\\lst@tab}"
end else begin
for _i = 1 to n do
lst_process_space lb lxm
done ;
let save = !lst_showspaces in
lst_showspaces := true ;
lst_output_token () ;
lst_showspaces := save
end
end else begin
for _i = 1 to n do
lst_process_space lb lxm
done
end
let lst_process_start_directive old_process lb lxm =
match !lst_top_mode with
| Normal -> begin match !lst_scan_mode with
| Start ->
lst_scan_mode := Directive false
| _ -> old_process lb lxm
end
| _ -> old_process lb lxm
exception EndVerb
let lst_process_end endstring old_process lb lxm =
if !verbose > 1 then
fprintf stderr "process_end: «%c»\n" lxm ;
if
(not !input_verb || Stack.empty stack_lexbuf)
&& if_next_string endstring lb then begin
Save.skip_delim endstring lb ;
raise EndVerb
end else
old_process lb lxm
let lst_init_char_table inline =
lst_init_chars
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@_$"
lst_process_letter ;
lst_init_chars "!\"#%&'()*+,-./:;<=>?[\\]^{}|`~" lst_process_other ;
lst_init_chars "0123456789" lst_process_digit ;
lst_init_char ' ' lst_process_space ;
lst_init_char '\t' lst_process_tab ;
if inline then
lst_init_char '\n' lst_process_space
else
lst_init_char '\n' (lst_process_newline true)
;;
(* TeX escapes *)
let start_escape mode endchar math =
lst_output_token () ;
lst_top_mode := Escape (mode, endchar, math)
let lst_process_escape math ec old_process lb lxm =
if !verbose > 1 then
fprintf stderr "lst_process_escape: %c\n" lxm ;
match !lst_top_mode with
| Skip _|StartNextLine (_,_) -> ()
| Gobble (mode,n) -> lst_do_gobble mode n
| Escape _ -> old_process lb lxm
| mode -> start_escape mode ec math
(* Strings *)
let lst_bs_string process_c c old_process lb lxm =
if !verbose > 1 then begin
eprintf "lst_bs_string: «%c»\n" lxm
end ;
(* Processs backslash, as usual *)
old_process lb lxm ;
(* Change char_tables for backshalsh and c *)
let saved = Array.copy lst_char_table in
let restore_lst_char_table () =
Array.blit saved 0 lst_char_table 0 (Array.length saved) in
lst_char_table.(Char.code lxm) <- old_process ;
lst_char_table.(Char.code c) <- process_c ;
(* Change all entries in char_table, -> restore table once the
backslashed char is processed *)
for i = 0 to Array.length lst_char_table-1 do
let do_process = lst_char_table.(i) in
lst_char_table.(i) <-
(fun lb lxm ->
do_process lb lxm ;
restore_lst_char_table ())
done
(* c is the character to be quoted *)
let lst_init_quote old_process c s =
let r = ref [] in
for i = 0 to String.length s-1 do
if s.[i] = 'b' then begin
r := ('\\',lst_char_table.(Char.code '\\')) :: !r ;
lst_init_save_char '\\' (lst_bs_string old_process c)
end
done ;
!r
let lst_process_stringizer quote old_process lb lxm = match !lst_top_mode with
| Normal ->
lst_output_token () ;
let to_restore = lst_init_quote old_process lxm quote in
lst_top_mode := String (lxm, to_restore) ;
lst_save_spaces := !lst_showspaces ;
lst_showspaces := !lst_string_spaces ;
scan_this Scan.main "\\begin{lrbox}{\\lst@box}" ;
old_process lb lxm
| String (c,to_restore) when lxm = c ->
old_process lb lxm ;
lst_output_token () ;
end_string to_restore ;
lst_top_mode := Normal
| _ -> old_process lb lxm
(* Comment *)
let lst_process_BNC sty _ s old_process lb c = match !lst_top_mode with
| Normal when if_next_string s lb ->
begin_comment () ;
eat_delim (fun () -> ()) (Comment (sty,Nested 0)) old_process lb c s
| Comment (sty,Nested n) when if_next_string s lb ->
eat_delim (fun () -> ()) (Comment (sty,Nested (n+1))) old_process lb c s
| _ -> old_process lb c
and lst_process_ENC s old_process lb c = match !lst_top_mode with
| Comment (sty,Nested 0) when if_next_string s lb ->
eat_delim
(end_comment sty)
Normal
old_process
lb c s
| Comment (sty,Nested n) when if_next_string s lb ->
eat_delim
(fun () -> ())
(Comment (sty,Nested (n-1)))
old_process lb c s
| _ -> old_process lb c
let lst_process_BBC sty check s old_process lb c = match !lst_top_mode with
| Normal when if_next_string s lb ->
begin_comment () ;
eat_delim
(fun () -> ())
(Comment (sty, Balanced check))
old_process lb c s
| _ -> old_process lb c
and lst_process_EBC s old_process lb c = match !lst_top_mode with
| Comment (sty,Balanced check) when
check c s && if_next_string s lb ->
eat_delim
(end_comment sty)
Normal
old_process
lb c s
| _ -> old_process lb c
let lst_process_LC sty s old_process lb c = match !lst_top_mode with
| Normal when if_next_string s lb ->
begin_comment () ;
eat_delim
(fun () -> ())
(if !lst_texcl then Escape (Normal,'\n', false) else Comment (sty,Line))
old_process lb c s
| _ -> old_process lb c
}
let command_name =
'\\' (( ['@''A'-'Z' 'a'-'z']+ '*'?) | [^ 'A'-'Z' 'a'-'z'] | "\\*")
rule inverb verb_delim put = parse
| (_ as c)
{if c = verb_delim then begin
Dest.close_group () ;
end else begin
put c ;
inverb verb_delim put lexbuf
end}
| eof
{if not (empty stack_lexbuf) then
let lexbuf = previous_lexbuf () in
inverb verb_delim put lexbuf
else
raise (VError ("End of file after \\verb"))}
and start_inverb put = parse
| (_ as c) {inverb c put lexbuf}
| eof
{if not (empty stack_lexbuf) then
let lexbuf = previous_lexbuf () in
start_inverb put lexbuf
else
raise (VError ("End of file after \\verb"))}
and scan_byline = parse
| "\\end" [' ''\t']* '{' [^'}']+ '}'
{let lxm = lexeme lexbuf in
let env = env_extract lxm in
if
(not !input_verb || Stack.empty stack_lexbuf)
&& env = !Scan.cur_env then begin
!finish () ;
scan_this Scan.main ("\\end"^env) ;
Scan.top_close_block "" ;
Scan.close_env !Scan.cur_env ;
Scan.check_alltt_skip lexbuf
end else begin
Out.put line_buff lxm ;
scan_byline lexbuf
end}
| '\n'
{!process () ; scan_byline lexbuf}
| _
{let lxm = lexeme_char lexbuf 0 in
Out.put_char line_buff lxm ;
scan_byline lexbuf}
| eof
{if not (Stack.empty stack_lexbuf) then begin
let lexbuf = previous_lexbuf () in
scan_byline lexbuf
end else begin
!finish () ;
raise
(Eof "scan_byline")
end}
and scan_bycommand out is_cmd = parse
| "\\end" [' ''\t']* '{' [^'}']+ '}' as lxm
{let env = env_extract lxm in
if env = !Scan.cur_env then begin
Latexmacros.def "\\@tmp@scanned"
zero_pat (Toks [Out.to_string out]) ;
lxm
end else begin
Out.blit out lexbuf ;
scan_bycommand out is_cmd lexbuf
end}
| "\\verb"
{Out.blit out lexbuf ;
Save.start_echo () ;
ignore (arg_verbatim lexbuf) ;
let a = Save.get_echo () in
Out.put out a ;
scan_bycommand out is_cmd lexbuf}
| command_name as lxm
{if is_cmd lxm then begin
Latexmacros.def "\\@tmp@scanned" zero_pat (Toks [Out.to_string out]) ;
Scan.expand_command lxm lexbuf ;
Out.reset out
end else begin
Out.blit out lexbuf ;
end ;
scan_bycommand out is_cmd lexbuf}
| ('%' [^'\n']* '\n'?) | _
{ Out.blit out lexbuf ;
scan_bycommand out is_cmd lexbuf }
| eof
{if not (Stack.empty stack_lexbuf) then begin
let lexbuf = previous_lexbuf () in
scan_bycommand out is_cmd lexbuf
end else begin
raise
(Eof "scan_bycommand")
end}
and listings = parse
| eof
{if not (Stack.empty stack_lexbuf) then begin
let lexbuf = previous_lexbuf () in
listings lexbuf
end else begin
raise
(Eof "listings")
end}
| (_ as lxm)
{lst_char_table.(Char.code lxm) lexbuf lxm ;
listings lexbuf}
and eat_line = parse
| eof
{if not (Stack.empty stack_lexbuf) then begin
let lexbuf = previous_lexbuf () in
eat_line lexbuf
end else begin
raise
(Eof "eat_line")
end}
| [^'\n'] {eat_line lexbuf}
| '\n' {lst_process_newline true lexbuf '\n'}
and get_line = parse
| eof
{if not (Stack.empty stack_lexbuf) then begin
let lexbuf = previous_lexbuf () in
get_line lexbuf
end else begin
raise
(Eof "get_line")
end}
| [^'\n']
{let lxm = lexeme_char lexbuf 0 in
Out.put_char line_buff lxm ;
get_line lexbuf}
| '\n' {Out.to_string line_buff}
and do_escape = parse
| eof {()}
| "\\esc"
{let arg = save_arg lexbuf in
scan_this main "\\mbox{" ;
scan_this_arg Scan.main arg ;
scan_this main "}" ;
do_escape lexbuf}
| _ as lxm
{Dest.put (Dest.iso lxm) ;
do_escape lexbuf}
and lst_linearg = parse
| '-'? (['0'-'9']+ as n) {LineNumber (int_of_string n)}
| '-'? ([^'-'',']+ as tok) {Marker tok}
| "" {raise ParseError}
and lst_parse_linerange = parse
| eof {[]}
| ","?
{let fst = lst_linearg lexbuf in
let lst = lst_linearg lexbuf in
(fst,lst)::lst_parse_linerange lexbuf}
{
let _ = ()
;;
let put_char_star = function
| ' '|'\t' -> Dest.put_char '_' ;
| c -> Dest.put (Dest.iso c)
and put_char = function
| '\t' -> Dest.put_char ' '
| c -> Dest.put (Dest.iso c)
;;
let open_verb put lexbuf =
Dest.open_group "CODE" ;
start_inverb put lexbuf
;;
def_code "\\verb" (open_verb (fun c -> Dest.put (Dest.iso c)));
def_code "\\verb*" (open_verb put_char_star);
();;
let put_line_buff_verb () =
Out.iter put_char line_buff ;
Out.reset line_buff
and put_line_buff_verb_star () =
Out.iter put_char_star line_buff ;
Out.reset line_buff
;;
let noeof lexer lexbuf =