forked from jrh13/hol-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pa_j_4.xx_8.00.ml
executable file
·3557 lines (3303 loc) · 134 KB
/
pa_j_4.xx_8.00.ml
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
(* camlp5r *)
(* pa_o.ml,v *)
(* Copyright (c) INRIA 2007-2017 *)
#load "pa_extend.cmo";
#load "q_MLast.cmo";
#load "pa_macro.cmo";
#load "pa_macro_gram.cmo";
open Pcaml;
open Mlsyntax.Original;
Pcaml.syntax_name.val := "HOL-Light";
Pcaml.no_constructors_arity.val := True;
(* ------------------------------------------------------------------------- *)
(* plexer.ml *)
(* ------------------------------------------------------------------------- *)
(* camlp5r *)
(* plexer.ml,v *)
(* Copyright (c) INRIA 2007-2017 *)
#load "pa_lexer.cmo";
open Versdep;
value simplest_raw_strings = ref False ;
value no_quotations = ref False;
value error_on_unknown_keywords = ref False;
value dollar_for_antiquotation = ref True;
value specific_space_dot = ref False;
value force_antiquot_loc = ref False;
type context =
{ after_space : mutable bool;
simplest_raw_strings : bool ;
dollar_for_antiquotation : bool;
specific_space_dot : bool;
find_kwd : string -> string;
line_cnt : int -> char -> unit;
set_line_nb : unit -> unit;
make_lined_loc : (int * int) -> string -> Ploc.t }
;
value err ctx loc msg =
Ploc.raise (ctx.make_lined_loc loc "") (Plexing.Error msg)
;
(* ------------------------------------------------------------------------- *)
(* JRH's hack to make the case distinction "unmixed" versus "mixed" *)
(* ------------------------------------------------------------------------- *)
value is_uppercase s = String.uppercase_ascii s = s;
value is_only_lowercase s = String.lowercase_ascii s = s && not(is_uppercase s);
value jrh_lexer = ref False;
value jrh_identifier find_kwd id =
let jflag = jrh_lexer.val in
if id = "set_jrh_lexer" then
(let _ = jrh_lexer.val := True in ("",find_kwd "true"))
else if id = "unset_jrh_lexer" then
(let _ = jrh_lexer.val := False in ("",find_kwd "false"))
else
try ("", find_kwd id) with
[ Not_found ->
if not(jflag) then
if is_uppercase (String.sub id 0 1) then ("UIDENT", id)
else ("LIDENT", id)
else if is_uppercase (String.sub id 0 1) &&
is_only_lowercase (String.sub id 1 (String.length id - 1))
(***** JRH: Carl's alternative version
then ("UIDENT", id)
else if is_uppercase (String.sub id 0 1) then ("LIDENT", "__uc_"^id)
else ("LIDENT", id)];
*****)
then ("UIDENT", id) else ("LIDENT", id)];
value keyword_or_error ctx loc s =
try ("", ctx.find_kwd s) with
[ Not_found ->
if error_on_unknown_keywords.val then
err ctx loc ("illegal token: " ^ s)
else ("", s) ]
;
value rev_implode l =
let s = string_create (List.length l) in
bytes_to_string (loop (string_length s - 1) l) where rec loop i =
fun
[ [c :: l] -> do { string_unsafe_set s i c; loop (i - 1) l }
| [] -> s ]
;
value implode l = rev_implode (List.rev l);
value stream_peek_nth n strm =
loop n (Stream.npeek n strm) where rec loop n =
fun
[ [] -> None
| [x] -> if n == 1 then Some x else None
| [_ :: l] -> loop (n - 1) l ]
;
value utf8_lexing = ref False;
value greek_tab =
["α"; "β"; "γ"; "δ"; "ε"; "ζ"; "η"; "θ"; "ι"; "κ"; "λ"; "μ"; "ν"; "ξ";
"ο"; "π"; "ρ"; "σ"; "τ"; "υ"; "φ"; "χ"; "ψ"; "ω"]
;
value greek_letter buf strm =
if utf8_lexing.val then
match Stream.peek strm with
[ Some c ->
if Char.code c >= 128 then
let x = implode (Stream.npeek 2 strm) in
if List.mem x greek_tab then do { Stream.junk strm; $add c }
else raise Stream.Failure
else raise Stream.Failure
| None -> raise Stream.Failure ]
else
raise Stream.Failure
;
value misc_letter buf strm =
if utf8_lexing.val then
match Stream.peek strm with
[ Some c ->
if Char.code c >= 128 then
match implode (Stream.npeek 3 strm) with
[ "→" | "≤" | "≥" -> raise Stream.Failure
| _ -> do { Stream.junk strm; $add c } ]
else raise Stream.Failure
| None -> raise Stream.Failure ]
else
match strm with lexer [ '\128'-'\225' | '\227'-'\255' ]
;
value misc_punct buf strm =
if utf8_lexing.val then
match strm with lexer [ '\226' _ _ ]
else
match strm with parser []
;
value utf8_equiv ctx bp buf strm =
if utf8_lexing.val then
match strm with lexer
[ "→" -> keyword_or_error ctx (bp, $pos) "->"
| "≤" -> keyword_or_error ctx (bp, $pos) "<="
| "≥" -> keyword_or_error ctx (bp, $pos) ">=" ]
else
match strm with parser []
;
value rec ident =
lexer
[ [ 'A'-'Z' | 'a'-'z' | '0'-'9' | '_' | ''' | misc_letter ] ident! | ]
;
value rec ident2_or other =
lexer
[ [ '!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' |
'%' | '.' | ':' | '<' | '>' | '|' | '$' | other | misc_punct ]
(ident2_or other)!
| ]
;
value ident2 = ident2_or (fun buf strm -> raise Stream.Failure)
;
value hash_follower_chars = ident2_or (lexer [ '#' ])
;
value rec ident3 =
lexer
[ [ '0'-'9' | 'A'-'Z' | 'a'-'z' | '_' | '!' | '%' | '&' | '*' | '+' | '-' |
'.' | '/' | ':' | '<' | '=' | '>' | '?' | '@' | '^' | '|' | '~' | ''' |
'$' | '\128'-'\255' ] ident3!
| ]
;
value binary = lexer [ '0' | '1' ];
value octal = lexer [ '0'-'7' ];
value decimal = lexer [ '0'-'9' ];
value hexa = lexer [ '0'-'9' | 'a'-'f' | 'A'-'F' ];
value end_integer =
lexer
[ "l"/ -> ("INT_l", $buf)
| "L"/ -> ("INT_L", $buf)
| "n"/ -> ("INT_n", $buf)
| -> ("INT", $buf) ]
;
value rec digits_under kind =
lexer
[ kind (digits_under kind)!
| "_" (digits_under kind)!
| end_integer ]
;
value digits kind =
lexer
[ kind (digits_under kind)!
| -> raise (Stream.Error "ill-formed integer constant") ]
;
value rec decimal_digits_under =
lexer [ [ '0'-'9' | '_' ] decimal_digits_under! | ]
;
value exponent_part =
lexer
[ [ 'e' | 'E' ] [ '+' | '-' | ]
'0'-'9' ? "ill-formed floating-point constant"
decimal_digits_under! ]
;
value number =
lexer
[ decimal_digits_under "." decimal_digits_under! exponent_part ->
("FLOAT", $buf)
| decimal_digits_under "." decimal_digits_under! -> ("FLOAT", $buf)
| decimal_digits_under exponent_part -> ("FLOAT", $buf)
| decimal_digits_under end_integer! ]
;
(*
let hex_float_literal =
'0' ['x' 'X']
['0'-'9' 'A'-'F' 'a'-'f'] ['0'-'9' 'A'-'F' 'a'-'f' '_']*
('.' ['0'-'9' 'A'-'F' 'a'-'f' '_']* )?
(['p' 'P'] ['+' '-']? ['0'-'9'] ['0'-'9' '_']* )?
let literal_modifier = ['G'-'Z' 'g'-'z']
let hex_float_literal =
'0' ['x' 'X']
['0'-'9' 'A'-'F' 'a'-'f'] ['0'-'9' 'A'-'F' 'a'-'f' '_']*
('.' ['0'-'9' 'A'-'F' 'a'-'f' '_']* )?
(['p' 'P'] ['+' '-']? ['0'-'9'] ['0'-'9' '_']* )?
let literal_modifier = ['G'-'Z' 'g'-'z']
*)
(* hex_digits* *)
value rec hex_digits_under_star =
lexer [ [ '0'-'9' | 'a'-'f' | 'A'-'F' | '_' ] hex_digits_under_star! | ]
;
value rec hex_under_integer =
lexer [ [ '0'-'9' | 'a'-'f' | 'A'-'F' ] hex_digits_under_star ]
;
value rec decimal_under_integer =
lexer [ [ '0'-'9' | ] decimal_digits_under! ]
;
value hex_exponent_part =
lexer
[ [ 'p' | 'P' ] [ '+' | '-' | ]
decimal_under_integer! ]
;
value hex_number =
lexer
[ hex_under_integer '.' hex_digits_under_star! hex_exponent_part -> ("FLOAT", $buf)
| hex_under_integer '.' hex_digits_under_star! -> ("FLOAT", $buf)
| hex_under_integer hex_exponent_part -> ("FLOAT", $buf)
| hex_under_integer exponent_part -> ("FLOAT", $buf)
| hex_under_integer end_integer! ]
;
value char_after_bslash =
lexer
[ "'"/
| _ [ "'"/ | _ [ "'"/ | ] ] ]
;
value char ctx bp =
lexer
[ "\\" _ char_after_bslash!
| "\\" -> err ctx (bp, $pos) "char not terminated"
| ?= [ _ '''] _! "'"/ ]
;
value any ctx buf =
parser bp [: `c :] -> do { ctx.line_cnt bp c; $add c }
;
value rec skiplws = lexer [
' '/ skiplws!
| '\t'/ skiplws!
|
]
;
value rec string ctx bp =
lexer
[ "\""/
| "\\"/ ?= [ "\n" ] "\n"/ skiplws! (string ctx bp)!
| "\\"/ ?= [ "\n" | " " ] (any ctx) (string ctx bp)!
| "\\" (any ctx) (string ctx bp)!
| (any ctx) (string ctx bp)!
| -> err ctx (bp, $pos) "string not terminated" ]
;
value rec qstring ctx bp =
lexer
[ "`"/
| (any ctx) (qstring ctx bp)!
| -> err ctx (bp, $pos) "quotation not terminated" ]
;
value rec quotation ctx bp =
lexer
[ ">>"/
| ">" (quotation ctx bp)!
| "<<" (quotation ctx bp)! [ -> $add ">>" ]! (quotation ctx bp)!
| "<:" ident! "<" (quotation ctx bp)! [ -> $add ">>" ]! (quotation ctx bp)!
| "<:" ident! (quotation ctx bp)!
| "<" (quotation ctx bp)!
| "\\"/ [ '>' | '<' | '\\' ] (quotation ctx bp)!
| "\\" (quotation ctx bp)!
| (any ctx) (quotation ctx bp)!
| -> err ctx (bp, $pos) "quotation not terminated" ]
;
value less_expected = "character '<' expected";
value less ctx bp buf strm =
if no_quotations.val then
match strm with lexer
[ [ -> $add "<" ] ident2! -> keyword_or_error ctx (bp, $pos) $buf ]
else
match strm with lexer
[ "<"/ (quotation ctx bp) -> ("QUOTATION", ":" ^ $buf)
| ":"/ ident! "<"/ ? less_expected [ -> $add ":" ]! (quotation ctx bp) ->
("QUOTATION", $buf)
| ":"/ ident! ":<"/ ? less_expected [ -> $add "@" ]! (quotation ctx bp) ->
("QUOTATION", $buf)
| [ -> $add "<" ] ident2! -> keyword_or_error ctx (bp, $pos) $buf ]
;
value rec antiquot_rest ctx bp =
lexer
[ "$"/
| "\\"/ (any ctx) (antiquot_rest ctx bp)!
| (any ctx) (antiquot_rest ctx bp)!
| -> err ctx (bp, $pos) "antiquotation not terminated" ]
;
value rec antiquot ctx bp =
lexer
[ "$"/ -> ":" ^ $buf
| [ 'a'-'z' | 'A'-'Z' | '0'-'9' | '!' | '_' ] (antiquot ctx bp)!
| ":" (antiquot_rest ctx bp)! -> $buf
| "\\"/ (any ctx) (antiquot_rest ctx bp)! -> ":" ^ $buf
| (any ctx) (antiquot_rest ctx bp)! -> ":" ^ $buf
| -> err ctx (bp, $pos) "antiquotation not terminated" ]
;
value antiloc bp ep s = Printf.sprintf "%d,%d:%s" bp ep s;
value skip_to_next_colon s i =
loop (i + 1) where rec loop j =
if j = String.length s then (i, 0)
else
match s.[j] with
[ ':' -> (j, j - i - 1)
| 'a'..'z' | 'A'..'Z' | '0'..'9' | '!' | '_' -> loop (j + 1)
| _ -> (i, 0) ]
;
value parse_antiloc s =
try
let i = String.index s ':' in
let (j, len) = skip_to_next_colon s i in
let kind = String.sub s (i + 1) len in
let loc =
let k = String.index s ',' in
let bp = int_of_string (String.sub s 0 k) in
let ep = int_of_string (String.sub s (k + 1) (i - k - 1)) in
Ploc.make_unlined (bp, ep)
in
Some (loc, kind, String.sub s (j + 1) (String.length s - j - 1))
with
[ Not_found | Failure _ -> None ]
;
value rec antiquot_loc ctx bp =
lexer
[ "$"/ -> antiloc bp $pos (":" ^ $buf)
| [ 'a'-'z' | 'A'-'Z' | '0'-'9' | '!' | '_' ] (antiquot_loc ctx bp)!
| ":" (antiquot_rest ctx bp)! -> antiloc bp $pos $buf
| "\\"/ (any ctx) (antiquot_rest ctx bp)! -> antiloc bp $pos (":" ^ $buf)
| (any ctx) (antiquot_rest ctx bp)! -> antiloc bp $pos (":" ^ $buf)
| -> err ctx (bp, $pos) "antiquotation not terminated" ]
;
value dollar ctx bp buf strm =
if not no_quotations.val && ctx.dollar_for_antiquotation then
("ANTIQUOT", antiquot ctx bp buf strm)
else if force_antiquot_loc.val then
("ANTIQUOT_LOC", antiquot_loc ctx bp buf strm)
else
match strm with lexer
[ [ -> $add "$" ] ident2! -> ("", $buf) ]
;
(* ANTIQUOT - specific case for QUESTIONIDENT and QUESTIONIDENTCOLON
input expr patt
----- ---- ----
?$abc:d$ ?abc:d ?abc
?$abc:d$: ?abc:d: ?abc:
?$d$ ?:d ?
?$d$: ?:d: ?:
*)
(* ANTIQUOT_LOC - specific case for QUESTIONIDENT and QUESTIONIDENTCOLON
input expr patt
----- ---- ----
?$abc:d$ ?8,13:abc:d ?abc
?$abc:d$: ?8,13:abc:d: ?abc:
?$d$ ?8,9::d ?
?$d$: ?8,9::d: ?:
*)
value question ctx bp buf strm =
if ctx.dollar_for_antiquotation then
match strm with parser
[ [: `'$'; s = antiquot ctx bp $empty; `':' :] ->
("ANTIQUOT", "?" ^ s ^ ":")
| [: `'$'; s = antiquot ctx bp $empty :] ->
("ANTIQUOT", "?" ^ s)
| [: :] ->
match strm with lexer
[ hash_follower_chars! -> keyword_or_error ctx (bp, $pos) $buf ] ]
else if force_antiquot_loc.val then
match strm with parser
[ [: `'$'; s = antiquot_loc ctx bp $empty; `':' :] ->
("ANTIQUOT_LOC", "?" ^ s ^ ":")
| [: `'$'; s = antiquot_loc ctx bp $empty :] ->
("ANTIQUOT_LOC", "?" ^ s)
| [: :] ->
match strm with lexer
[ hash_follower_chars! -> keyword_or_error ctx (bp, $pos) $buf ] ]
else
match strm with lexer
[ hash_follower_chars! -> keyword_or_error ctx (bp, $pos) $buf ]
;
value tilde ctx bp buf strm =
if ctx.dollar_for_antiquotation then
match strm with parser
[ [: `'$'; s = antiquot ctx bp $empty; `':' :] ->
("ANTIQUOT", "~" ^ s ^ ":")
| [: `'$'; s = antiquot ctx bp $empty :] ->
("ANTIQUOT", "~" ^ s)
| [: :] ->
match strm with lexer
[ hash_follower_chars! -> keyword_or_error ctx (bp, $pos) $buf ] ]
else if force_antiquot_loc.val then
match strm with parser
[ [: `'$'; s = antiquot_loc ctx bp $empty; `':' :] ->
("ANTIQUOT_LOC", "~" ^ s ^ ":")
| [: `'$'; s = antiquot_loc ctx bp $empty :] ->
("ANTIQUOT_LOC", "~" ^ s)
| [: :] ->
match strm with lexer
[ hash_follower_chars! -> keyword_or_error ctx (bp, $pos) $buf ] ]
else
match strm with lexer
[ hash_follower_chars! -> keyword_or_error ctx (bp, $pos) $buf ]
;
value tildeident =
lexer
[ ":"/ -> ("TILDEIDENTCOLON", $buf)
| -> ("TILDEIDENT", $buf) ]
;
value questionident =
lexer
[ ":"/ -> ("QUESTIONIDENTCOLON", $buf)
| -> ("QUESTIONIDENT", $buf) ]
;
value rec linedir n s =
match stream_peek_nth n s with
[ Some (' ' | '\t') -> linedir (n + 1) s
| Some ('0'..'9') -> linedir_digits (n + 1) s
| _ -> False ]
and linedir_digits n s =
match stream_peek_nth n s with
[ Some ('0'..'9') -> linedir_digits (n + 1) s
| _ -> linedir_quote n s ]
and linedir_quote n s =
match stream_peek_nth n s with
[ Some (' ' | '\t') -> linedir_quote (n + 1) s
| Some '"' -> True
| _ -> False ]
;
value rec any_to_nl =
lexer
[ "\r" | "\n"
| _ any_to_nl!
| ]
;
value rec rawstring1 delimtok (ofs, delim) ctx buf =
parser bp [: `c ; strm :] -> do {
ctx.line_cnt bp c;
let buf = $add c in
if String.get delim ofs <> c then
if String.get delim 0 = c then
rawstring1 delimtok (1, delim) ctx buf strm
else
rawstring1 delimtok (0, delim) ctx buf strm
else if ofs+1 < String.length delim then
rawstring1 delimtok (ofs+1, delim) ctx buf strm
else
let s = $buf in
let slen = String.length s in do {
(delimtok, String.sub s 0 (slen - (String.length delim)))
}
}
;
value rec rawstring0 ctx bp buf =
parser bp [
[: `'|' ; strm :] -> do {
rawstring1 $buf (0, "|" ^ $buf ^ "}") ctx $empty strm
}
| [: `('a'..'z' | '_' as c) ; strm :] -> do {
rawstring0 ctx bp ($add c) strm
}
]
;
value add_string buf s =
let slen = String.length s in
let rec addrec buf i =
if i = slen then buf
else addrec ($add (String.get s i)) (i+1)
in addrec buf 0
;
(*
* This predicate checks that the stream contains a valid raw-string starter.
* The definition of "valid raw string starter" depends on the value of
* the variable [simplest_raw_strings]: if it is [False], then a valid
* raw-string starter is "[:alpha:]+|"; if it is [True], a valid raw-string
* starter is "[:alpha:]*|". [simplest_raw_strings] is set to True in
* original syntax.
* This predicate gets called when the main lexer has already seen a "{".
*)
value raw_string_starter_p ctx strm =
let rec predrec n =
match stream_peek_nth n strm with
[ None -> False
| Some ('a'..'z' | '_') ->
predrec (n+1)
| Some '|' when ctx.simplest_raw_strings || n > 1 -> True
| Some _ -> False ]
in predrec 1
;
value comment_rawstring ctx bp (buf : Plexing.Lexbuf.t) strm =
if not (raw_string_starter_p ctx strm) then
buf
else
let (delim, s) = rawstring0 ctx bp $empty strm in
let rs = Printf.sprintf "{%s|%s|%s}" delim s delim in
add_string buf rs
;
value comment ctx bp =
comment where rec comment =
lexer
[ "*)"
| "*" comment!
| "{" (comment_rawstring ctx bp)! comment!
| "(*" comment! comment!
| "(" comment!
| "\"" (string ctx bp)! [ -> $add "\"" ] comment!
| "'*)"
| "'*" comment!
| "'" (any ctx) comment!
| (any ctx) comment!
| -> err ctx (bp, $pos) "comment not terminated" ]
;
value keyword_or_error_or_rawstring ctx bp (loc,s) buf strm =
if not (raw_string_starter_p ctx strm) then
keyword_or_error ctx loc "{"
else
let (delim, s) = rawstring0 ctx bp $empty strm in
("STRING", String.escaped s)
;
value zerobuf f buf strm =
f $empty strm
;
value rec ws =
lexer
[ [' '/ | '\t'/ | '\n'/] [ ws | ]
]
;
value rec extattrident =
lexer
[ ident [ "." extattrident | ] ]
;
value quoted_extension1 ctx (bp, _) extid buf strm =
let (delim, s) = rawstring0 ctx bp $empty strm in
("QUOTEDEXTENSION", extid^":"^(String.escaped s))
;
value quoted_extension0 ctx (bp, _) extid =
lexer
[ ws (zerobuf (quoted_extension1 ctx (bp, $pos) extid))
| (zerobuf (quoted_extension1 ctx (bp, $pos) extid))
]
;
value quoted_extension ctx (bp, _) =
lexer [
extattrident (zerobuf (quoted_extension0 ctx (bp, $pos) $buf))
]
;
value dotsymbolchar = lexer
[ '!' | '$' | '%' | '&' | '*' | '+' | '-' | '/' | ':' | '=' | '>' | '?' | '@' | '^' | '|' ]
;
value rec dotsymbolchar_star = lexer
[ dotsymbolchar dotsymbolchar_star | ]
;
value kwdopchar = lexer
[ '$' | '&' | '*' | '+' | '-' | '/' | '<' | '=' | '>' | '@' | '^' | '|' ]
;
value symbolchar = lexer
['!' | '$' | '%' | '&' | '*' | '+' | '-' | '.' | '/' | ':' | '<' | '=' | '>' | '?' | '@' | '^' | '|' | '~']
;
value rec symbolchar_star = lexer
[ symbolchar symbolchar_star | ]
;
value word_operators ctx id = lexer
[ [ kwdopchar dotsymbolchar_star ->
("", id ^ $buf)
| -> try ("", ctx.find_kwd id) with [ Not_found -> ("LIDENT", id) ]
] ]
;
value keyword = fun ctx buf strm ->
let id = $buf in
if id = "let" || id = "and" then word_operators ctx id $empty strm
else
jrh_identifier ctx.find_kwd id
(*** JRH: original was
try ("", ctx.find_kwd id) with [ Not_found -> ("LIDENT", id) ]
*)
;
value dot ctx (bp, pos) buf strm =
match Stream.peek strm with [
None ->
let id =
if ctx.specific_space_dot && ctx.after_space then " ." else "."
in
keyword_or_error ctx (bp, pos) id
| _ -> match strm with lexer [ [ -> $add "." ] dotsymbolchar_star! -> keyword_or_error ctx (bp, $pos) $buf ]
]
;
value next_token_after_spaces ctx bp =
lexer
[ 'A'-'Z' ident! ->
let id = $buf in
jrh_identifier ctx.find_kwd id
(*** JRH: original was
try ("", ctx.find_kwd id) with [ Not_found -> ("UIDENT", id) ]
*)
| greek_letter ident! -> ("GIDENT", $buf)
| [ 'a'-'z' | '_' | misc_letter ] ident! (keyword ctx)
| '1'-'9' number!
| "0" [ 'o' | 'O' ] (digits octal)!
| "0" [ 'x' | 'X' ] (hex_number)!
| "0" [ 'b' | 'B' ] (digits binary)!
| "0" number!
| "'"/ ?= [ '\\' 'a'-'z' 'a'-'z' ] -> keyword_or_error ctx (bp, $pos) "'"
| "'"/ (char ctx bp) -> ("CHAR", $buf)
| "'" -> keyword_or_error ctx (bp, $pos) "'"
| "\""/ (string ctx bp)! -> ("STRING", $buf)
(*** Line added by JRH ***)
| "`"/ (qstring ctx bp)! -> ("QUOTATION", "tot:" ^ $buf)
| "$"/ (dollar ctx bp)!
| [ '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' | '%' ] ident2! ->
keyword_or_error ctx (bp, $pos) $buf
| '!' hash_follower_chars! -> keyword_or_error ctx (bp, $pos) $buf
| "~"/ 'a'-'z' ident! tildeident!
| "~"/ '_' ident! tildeident!
| "~" (tilde ctx bp)
| "?"/ 'a'-'z' ident! questionident!
| "?" (question ctx bp)!
| "<"/ (less ctx bp)!
| ":]" -> keyword_or_error ctx (bp, $pos) $buf
| "::" -> keyword_or_error ctx (bp, $pos) $buf
| ":=" -> keyword_or_error ctx (bp, $pos) $buf
| ":>" -> keyword_or_error ctx (bp, $pos) $buf
| ":" -> keyword_or_error ctx (bp, $pos) $buf
| ">]" -> keyword_or_error ctx (bp, $pos) $buf
| ">}" -> keyword_or_error ctx (bp, $pos) $buf
| ">" ident2! -> keyword_or_error ctx (bp, $pos) $buf
| "|]" -> keyword_or_error ctx (bp, $pos) $buf
| "|}" -> keyword_or_error ctx (bp, $pos) $buf
| "|" ident2! -> keyword_or_error ctx (bp, $pos) $buf
| "[" ?= [ "<<" | "<:" ] -> keyword_or_error ctx (bp, $pos) $buf
| "[@" -> keyword_or_error ctx (bp, $pos) $buf
| "[@@" -> keyword_or_error ctx (bp, $pos) $buf
| "[@@@" -> keyword_or_error ctx (bp, $pos) $buf
| "[%" -> keyword_or_error ctx (bp, $pos) $buf
| "[%%" -> keyword_or_error ctx (bp, $pos) $buf
| "[|" -> keyword_or_error ctx (bp, $pos) $buf
| "[<" -> keyword_or_error ctx (bp, $pos) $buf
| "[:" -> keyword_or_error ctx (bp, $pos) $buf
| "[" -> keyword_or_error ctx (bp, $pos) $buf
| "{" ?= [ "<<" | "<:" ] -> keyword_or_error ctx (bp, $pos) $buf
| "{<" -> keyword_or_error ctx (bp, $pos) $buf
| "{%"/ (zerobuf (quoted_extension ctx (bp, $pos)))
| "{:" -> keyword_or_error ctx (bp, $pos) $buf
| "{" (keyword_or_error_or_rawstring ctx bp ((bp, $pos),$buf))
| ".." -> keyword_or_error ctx (bp, $pos) ".."
| "." dotsymbolchar symbolchar_star ->
keyword_or_error ctx (bp, $pos) $buf
| "." ->
let id =
if ctx.specific_space_dot && ctx.after_space then " ." else "."
in
keyword_or_error ctx (bp, $pos) id
| ";;" -> keyword_or_error ctx (bp, $pos) ";;"
| ";" -> keyword_or_error ctx (bp, $pos) ";"
| (utf8_equiv ctx bp)
| misc_punct ident2! -> keyword_or_error ctx (bp, $pos) $buf
| "\\"/ ident3! -> ("LIDENT", $buf)
| "#" hash_follower_chars! -> keyword_or_error ctx (bp, $pos) $buf
| (any ctx) -> keyword_or_error ctx (bp, $pos) $buf ]
;
value get_comment buf strm = $buf;
value rec next_token ctx buf =
parser bp
[ [: `('\n' | '\r' as c); s :] ep -> do {
if c = '\n' then incr Plexing.line_nb.val else ();
Plexing.bol_pos.val.val := ep;
ctx.set_line_nb ();
ctx.after_space := True;
next_token ctx ($add c) s
}
| [: `(' ' | '\t' | '\026' | '\012' as c); s :] -> do {
ctx.after_space := True;
next_token ctx ($add c) s
}
| [: `'#' when bp = Plexing.bol_pos.val.val; s :] ->
let comm = get_comment buf () in
if linedir 1 s then do {
let buf = any_to_nl ($add '#') s in
incr Plexing.line_nb.val;
Plexing.bol_pos.val.val := Stream.count s;
ctx.set_line_nb ();
ctx.after_space := True;
next_token ctx buf s
}
else
let loc = ctx.make_lined_loc (bp, bp + 1) comm in
(keyword_or_error ctx (bp, bp + 1) "#", loc)
| [: `'(';
a =
parser
[ [: `'*'; buf = comment ctx bp ($add "(*") !; s :] -> do {
ctx.set_line_nb ();
ctx.after_space := True;
next_token ctx buf s
}
| [: :] ep ->
let loc = ctx.make_lined_loc (bp, ep) $buf in
(keyword_or_error ctx (bp, ep) "(", loc) ] ! :] -> a
| [: comm = get_comment buf;
tok = next_token_after_spaces ctx bp $empty :] ep ->
let loc = ctx.make_lined_loc (bp, max (bp + 1) ep) comm in
(tok, loc)
| [: comm = get_comment buf; _ = Stream.empty :] ->
let loc = ctx.make_lined_loc (bp, bp + 1) comm in
(("EOI", ""), loc) ]
;
value next_token_fun ctx glexr (cstrm, s_line_nb, s_bol_pos) =
try do {
match Plexing.restore_lexing_info.val with
[ Some (line_nb, bol_pos) -> do {
s_line_nb.val := line_nb;
s_bol_pos.val := bol_pos;
Plexing.restore_lexing_info.val := None;
}
| None -> () ];
Plexing.line_nb.val := s_line_nb;
Plexing.bol_pos.val := s_bol_pos;
let comm_bp = Stream.count cstrm in
ctx.set_line_nb ();
ctx.after_space := False;
let (r, loc) = next_token ctx $empty cstrm in
match glexr.val.Plexing.tok_comm with
[ Some list ->
if Ploc.first_pos loc > comm_bp then
let comm_loc = Ploc.make_unlined (comm_bp, Ploc.last_pos loc) in
glexr.val.Plexing.tok_comm := Some [comm_loc :: list]
else ()
| None -> () ];
(r, loc)
}
with
[ Stream.Error str ->
err ctx (Stream.count cstrm, Stream.count cstrm + 1) str ]
;
value make_ctx kwd_table =
let line_nb = ref 0 in
let bol_pos = ref 0 in
{after_space = False;
dollar_for_antiquotation = dollar_for_antiquotation.val;
simplest_raw_strings = simplest_raw_strings.val ;
specific_space_dot = specific_space_dot.val;
find_kwd = Hashtbl.find kwd_table;
line_cnt bp1 c =
match c with
[ '\n' | '\r' -> do {
if c = '\n' then incr Plexing.line_nb.val else ();
Plexing.bol_pos.val.val := bp1 + 1;
}
| c -> () ];
set_line_nb () = do {
line_nb.val := Plexing.line_nb.val.val;
bol_pos.val := Plexing.bol_pos.val.val;
};
make_lined_loc loc comm =
Ploc.make_loc Plexing.input_file.val line_nb.val bol_pos.val loc comm}
;
value func ctx kwd_table glexr =
Plexing.lexer_func_of_parser (next_token_fun ctx glexr)
;
value rec check_keyword_stream =
parser [: _ = check $empty; _ = Stream.empty :] -> True
and check =
lexer
[ [ 'A'-'Z' | 'a'-'z' | misc_letter ] check_ident!
| [ '!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' | '%' |
'.' ]
check_ident2!
| "$" check_ident2!
| "<" ?= [ ":" | "<" ]
| "<" check_ident2!
| ":]"
| "::"
| ":="
| ":>"
| ":"
| ">]"
| ">}"
| ">" check_ident2!
| "|]"
| "|}"
| "|" check_ident2!
| "[" ?= [ "<<" | "<:" ]
| "[@"
| "[@@"
| "[@@@"
| "[%"
| "[%%"
| "[|"
| "[<"
| "[:"
| "["
| "{" ?= [ "<<" | "<:" ]
| "{|"
| "{<"
| "{:"
| "{"
| ";;"
| ";"
| misc_punct check_ident2!
| _ ]
and check_ident =
lexer
[ [ 'A'-'Z' | 'a'-'z' | '0'-'9' | '_' | ''' | misc_letter ]
check_ident! | ]
and check_ident2 =
lexer
[ [ '!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' | '%' |
'.' | ':' | '<' | '>' | '|' | misc_punct ]
check_ident2! | ]
;
value check_keyword ctx s =
if ctx.simplest_raw_strings && (s = "{|" || s = "|}") then False
else
try check_keyword_stream (Stream.of_string s) with _ -> False
;
value error_no_respect_rules p_con p_prm =
raise
(Plexing.Error
("the token " ^
(if p_con = "" then "\"" ^ p_prm ^ "\""
else if p_prm = "" then p_con
else p_con ^ " \"" ^ p_prm ^ "\"") ^
" does not respect Plexer rules"))
;
value using_token ctx kwd_table (p_con, p_prm) =
match p_con with
[ "" ->
if not (hashtbl_mem kwd_table p_prm) then
if check_keyword ctx p_prm then Hashtbl.add kwd_table p_prm p_prm
else error_no_respect_rules p_con p_prm
else ()
| "LIDENT" ->
if p_prm = "" then ()
else
match p_prm.[0] with
[ 'A'..'Z' -> error_no_respect_rules p_con p_prm
| _ -> () ]
| "UIDENT" ->
if p_prm = "" then ()
else
match p_prm.[0] with
[ 'a'..'z' -> error_no_respect_rules p_con p_prm
| _ -> () ]
| "TILDEIDENT" | "TILDEIDENTCOLON" | "QUESTIONIDENT" |
"QUESTIONIDENTCOLON" | "INT" | "INT_l" | "INT_L" | "INT_n" | "FLOAT" |
"QUOTEDEXTENSION" |
"CHAR" | "STRING" | "QUOTATION" | "GIDENT" |
"ANTIQUOT" | "ANTIQUOT_LOC" | "EOI" ->
()
| _ ->
raise
(Plexing.Error
("the constructor \"" ^ p_con ^
"\" is not recognized by Plexer")) ]
;
value removing_token kwd_table (p_con, p_prm) =
match p_con with
[ "" -> Hashtbl.remove kwd_table p_prm
| _ -> () ]
;
value text =
fun
[ ("", t) -> "'" ^ t ^ "'"
| ("LIDENT", "") -> "lowercase identifier"
| ("LIDENT", t) -> "'" ^ t ^ "'"
| ("UIDENT", "") -> "uppercase identifier"
| ("UIDENT", t) -> "'" ^ t ^ "'"
| ("INT", "") -> "integer"
| ("INT", s) -> "'" ^ s ^ "'"
| ("FLOAT", "") -> "float"
| ("STRING", "") -> "string"
| ("CHAR", "") -> "char"
| ("QUOTATION", "") -> "quotation"
| ("ANTIQUOT", k) -> "antiquot \"" ^ k ^ "\""
| ("EOI", "") -> "end of input"
| (con, "") -> con
| (con, prm) -> con ^ " \"" ^ prm ^ "\"" ]
;
value eq_before_colon p e =
loop 0 where rec loop i =
if i == String.length e then
failwith "Internal error in Plexer: incorrect ANTIQUOT"
else if i == String.length p then e.[i] == ':'
else if p.[i] == e.[i] then loop (i + 1)
else False
;
value after_colon e =
try
let i = String.index e ':' in
String.sub e (i + 1) (String.length e - i - 1)
with