forked from CakeML/candle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pa_j_3.1x_5.xx.ml
2027 lines (1919 loc) · 67.5 KB
/
pa_j_3.1x_5.xx.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_extend.cmo q_MLast.cmo *)
(* $Id: pa_o.ml 1271 2007-10-01 08:22:47Z deraugla $ *)
(* Copyright (c) INRIA 2007 *)
open Pcaml;
Pcaml.syntax_name.val := "OCaml";
Pcaml.no_constructors_arity.val := True;
(* camlp5r pa_lexer.cmo *)
(* $Id: plexer.ml 1402 2007-10-14 02:50:31Z deraugla $ *)
(* Copyright (c) INRIA 2007 *)
(* ------------------------------------------------------------------------- *)
(* Added by JRH as a backdoor to change lexical conventions. *)
(* ------------------------------------------------------------------------- *)
value jrh_lexer = 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;
(* The string buffering machinery *)
value rev_implode l =
let s = String.create (List.length l) in
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 ]
;
(* The lexer *)
type context =
{ after_space : mutable 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 s = s;
value is_only_lowercase s = String.lowercase s = s && not(is_uppercase s);
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)];
(* ------------------------------------------------------------------------- *)
(* Back to original file with the mod of using the above. *)
(* ------------------------------------------------------------------------- *)
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 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 rec ident =
lexer
[ [ 'A'-'Z' | 'a'-'z' | '0'-'9' | '_' | ''' | '\128'-'\255' ] ident! | ]
;
value rec ident2 =
lexer
[ [ '!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' |
'%' | '.' | ':' | '<' | '>' | '|' | '$' ]
ident2!
| ]
;
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! ]
;
value rec char_aux ctx bp =
lexer
[ "'"/
| _ (char_aux ctx bp)!
| -> err ctx (bp, $pos) "char not terminated" ]
;
value char ctx bp =
lexer
[ "\\" _ (char_aux ctx bp)!
| "\\" -> err ctx (bp, $pos) "char not terminated"
| ?= [ _ '''] _! "'"/ ]
;
value any ctx buf =
parser bp [: `c :] -> do { ctx.line_cnt bp c; $add c }
;
value rec string ctx bp =
lexer
[ "\""/
| "\\" (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 comment ctx bp =
comment where rec comment =
lexer
[ "*)"
| "*" comment!
| "(*" comment! comment!
| "(" comment!
| "\"" (string ctx bp)! [ -> $add "\"" ] comment!
| "'" (char ctx bp) comment!
| "'" comment!
| (any ctx) comment!
| -> err ctx (bp, $pos) "comment 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 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! [ -> $add ":" ]! "<"/ ? "character '<' expected"
(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 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 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
[ ident2! -> 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
[ ident2! -> keyword_or_error ctx (bp, $pos) $buf ] ]
else
match strm with lexer
[ ident2! -> 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
[ ident2! -> 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
[ ident2! -> keyword_or_error ctx (bp, $pos) $buf ] ]
else
match strm with lexer
[ ident2! -> 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 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) ]
*********)
| [ 'a'-'z' | '_' | '\128'-'\255' ] ident! ->
let id = $buf in
jrh_identifier ctx.find_kwd id
(********** JRH: original was
try ("", ctx.find_kwd id) with [ Not_found -> ("LIDENT", id) ]
*********)
| '1'-'9' number!
| "0" [ 'o' | 'O' ] (digits octal)!
| "0" [ 'x' | 'X' ] (digits hexa)!
| "0" [ 'b' | 'B' ] (digits binary)!
| "0" number!
| "'"/ (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
| "~"/ 'a'-'z' 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) ".."
| "." ->
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) ";"
| "\\"/ ident3! -> ("LIDENT", $buf)
| (any ctx) -> keyword_or_error ctx (bp, $pos) $buf ]
;
value rec next_token ctx buf =
parser bp
[ [: `('\n' | '\r' as c); s :] ep -> do {
incr Plexing.line_nb.val;
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 :] ->
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) $buf 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
| [: tok = next_token_after_spaces ctx bp $empty :] ep ->
let loc = ctx.make_lined_loc (bp, max (bp + 1) ep) $buf in
(tok, loc)
| [: _ = Stream.empty :] ->
let loc = ctx.make_lined_loc (bp, bp + 1) $buf 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 func kwd_table glexr =
let ctx =
let line_nb = ref 0 in
let bol_pos = ref 0 in
{after_space = False;
dollar_for_antiquotation = dollar_for_antiquotation.val;
specific_space_dot = specific_space_dot.val;
find_kwd = Hashtbl.find kwd_table;
line_cnt bp1 c =
match c with
[ '\n' | '\r' -> do {
incr Plexing.line_nb.val;
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 line_nb.val bol_pos.val loc}
in
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' | '\128'-'\255' ] check_ident!
| [ '!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' | '%' |
'.' ]
check_ident2!
| "$" check_ident2!
| "<" ?= [ ":" | "<" ]
| "<" check_ident2!
| ":]"
| "::"
| ":="
| ":>"
| ":"
| ">]"
| ">}"
| ">" check_ident2!
| "|]"
| "|}"
| "|" check_ident2!
| "[" ?= [ "<<" | "<:" ]
| "[|"
| "[<"
| "[:"
| "["
| "{" ?= [ "<<" | "<:" ]
| "{|"
| "{<"
| "{:"
| "{"
| ";;"
| ";"
| _ ]
and check_ident =
lexer
[ [ 'A'-'Z' | 'a'-'z' | '0'-'9' | '_' | ''' | '\128'-'\255' ]
check_ident! | ]
and check_ident2 =
lexer
[ [ '!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' | '%' |
'.' | ':' | '<' | '>' | '|' ]
check_ident2! | ]
;
value check_keyword s =
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 error_ident_and_keyword p_con p_prm =
raise
(Plexing.Error
("the token \"" ^ p_prm ^ "\" is used as " ^ p_con ^
" and as keyword"))
;
value using_token kwd_table ident_table (p_con, p_prm) =
match p_con with
[ "" ->
if not (Hashtbl.mem kwd_table p_prm) then
if check_keyword p_prm then
if Hashtbl.mem ident_table p_prm then
error_ident_and_keyword (Hashtbl.find ident_table p_prm) p_prm
else 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
| _ ->
if Hashtbl.mem kwd_table p_prm then
error_ident_and_keyword p_con p_prm
else Hashtbl.add ident_table p_prm p_con ]
| "UIDENT" ->
if p_prm = "" then ()
else
match p_prm.[0] with
[ 'a'..'z' -> error_no_respect_rules p_con p_prm
| _ ->
if Hashtbl.mem kwd_table p_prm then
error_ident_and_keyword p_con p_prm
else Hashtbl.add ident_table p_prm p_con ]
| "TILDEIDENT" | "TILDEIDENTCOLON" | "QUESTIONIDENT" |
"QUESTIONIDENTCOLON" | "INT" | "INT_l" | "INT_L" | "INT_n" | "FLOAT" |
"CHAR" | "STRING" | "QUOTATION" |
"ANTIQUOT" | "ANTIQUOT_LOC" | "EOI" ->
()
| _ ->
raise
(Plexing.Error
("the constructor \"" ^ p_con ^
"\" is not recognized by Plexer")) ]
;
value removing_token kwd_table ident_table (p_con, p_prm) =
match p_con with
[ "" -> Hashtbl.remove kwd_table p_prm
| "LIDENT" | "UIDENT" ->
if p_prm <> "" then Hashtbl.remove ident_table p_prm else ()
| _ -> () ]
;
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
[ Not_found -> "" ]
;
value after_colon_except_last e =
try
let i = String.index e ':' in
String.sub e (i + 1) (String.length e - i - 2)
with
[ Not_found -> "" ]
;
value tok_match =
fun
[ ("ANTIQUOT", p_prm) ->
if p_prm <> "" && (p_prm.[0] = '~' || p_prm.[0] = '?') then
if p_prm.[String.length p_prm - 1] = ':' then
let p_prm = String.sub p_prm 0 (String.length p_prm - 1) in
fun
[ ("ANTIQUOT", prm) ->
if prm <> "" && prm.[String.length prm - 1] = ':' then
if eq_before_colon p_prm prm then after_colon_except_last prm
else raise Stream.Failure
else raise Stream.Failure
| _ -> raise Stream.Failure ]
else
fun
[ ("ANTIQUOT", prm) ->
if prm <> "" && prm.[String.length prm - 1] = ':' then
raise Stream.Failure
else if eq_before_colon p_prm prm then after_colon prm
else raise Stream.Failure
| _ -> raise Stream.Failure ]
else
fun
[ ("ANTIQUOT", prm) when eq_before_colon p_prm prm -> after_colon prm
| _ -> raise Stream.Failure ]
| tok -> Plexing.default_match tok ]
;
value gmake () =
let kwd_table = Hashtbl.create 301 in
let id_table = Hashtbl.create 301 in
let glexr =
ref
{Plexing.tok_func = fun []; tok_using = fun []; tok_removing = fun [];
tok_match = fun []; tok_text = fun []; tok_comm = None}
in
let glex =
{Plexing.tok_func = func kwd_table glexr;
tok_using = using_token kwd_table id_table;
tok_removing = removing_token kwd_table id_table; tok_match = tok_match;
tok_text = text; tok_comm = None}
in
do { glexr.val := glex; glex }
;
do {
let odfa = dollar_for_antiquotation.val in
dollar_for_antiquotation.val := False;
Grammar.Unsafe.gram_reinit gram (gmake ());
dollar_for_antiquotation.val := odfa;
Grammar.Unsafe.clear_entry interf;
Grammar.Unsafe.clear_entry implem;
Grammar.Unsafe.clear_entry top_phrase;
Grammar.Unsafe.clear_entry use_file;
Grammar.Unsafe.clear_entry module_type;
Grammar.Unsafe.clear_entry module_expr;
Grammar.Unsafe.clear_entry sig_item;
Grammar.Unsafe.clear_entry str_item;
Grammar.Unsafe.clear_entry expr;
Grammar.Unsafe.clear_entry patt;
Grammar.Unsafe.clear_entry ctyp;
Grammar.Unsafe.clear_entry let_binding;
Grammar.Unsafe.clear_entry type_declaration;
Grammar.Unsafe.clear_entry constructor_declaration;
Grammar.Unsafe.clear_entry match_case;
Grammar.Unsafe.clear_entry with_constr;
Grammar.Unsafe.clear_entry poly_variant;
Grammar.Unsafe.clear_entry class_type;
Grammar.Unsafe.clear_entry class_expr;
Grammar.Unsafe.clear_entry class_sig_item;
Grammar.Unsafe.clear_entry class_str_item
};
Pcaml.parse_interf.val := Grammar.Entry.parse interf;
Pcaml.parse_implem.val := Grammar.Entry.parse implem;
value neg_string n =
let len = String.length n in
if len > 0 && n.[0] = '-' then String.sub n 1 (len - 1) else "-" ^ n
;
value mkumin loc f arg =
match arg with
[ <:expr< $int:n$ >> -> <:expr< $int:neg_string n$ >>
| <:expr< $flo:n$ >> -> <:expr< $flo:neg_string n$ >>
| _ ->
let f = "~" ^ f in
<:expr< $lid:f$ $arg$ >> ]
;
value mklistexp loc last =
loop True where rec loop top =
fun
[ [] ->
match last with
[ Some e -> e
| None -> <:expr< [] >> ]
| [e1 :: el] ->
let loc =
if top then loc else Ploc.encl (MLast.loc_of_expr e1) loc
in
<:expr< [$e1$ :: $loop False el$] >> ]
;
value mklistpat loc last =
loop True where rec loop top =
fun
[ [] ->
match last with
[ Some p -> p
| None -> <:patt< [] >> ]
| [p1 :: pl] ->
let loc =
if top then loc else Ploc.encl (MLast.loc_of_patt p1) loc
in
<:patt< [$p1$ :: $loop False pl$] >> ]
;
(*** JRH pulled this outside so user can add new infixes here too ***)
value ht = Hashtbl.create 73;
(*** And JRH added all the new HOL Light infixes here already ***)
value is_operator = do {
let ct = Hashtbl.create 73 in
List.iter (fun x -> Hashtbl.add ht x True)
["asr"; "land"; "lor"; "lsl"; "lsr"; "lxor"; "mod"; "or"; "o"; "upto";
"F_F"; "THENC"; "THEN"; "THENL"; "ORELSE"; "ORELSEC";
"THEN_TCL"; "ORELSE_TCL"];
List.iter (fun x -> Hashtbl.add ct x True)
['!'; '&'; '*'; '+'; '-'; '/'; ':'; '<'; '='; '>'; '@'; '^'; '|'; '~';
'?'; '%'; '.'; '$'];
fun x ->
try Hashtbl.find ht x with
[ Not_found -> try Hashtbl.find ct x.[0] with _ -> False ]
};
(*** JRH added this so parenthesised operators undergo same mapping ***)
value translate_operator =
fun s ->
match s with
[ "THEN" -> "then_"
| "THENC" -> "thenc_"
| "THENL" -> "thenl_"
| "ORELSE" -> "orelse_"
| "ORELSEC" -> "orelsec_"
| "THEN_TCL" -> "then_tcl_"
| "ORELSE_TCL" -> "orelse_tcl_"
| "F_F" -> "f_f_"
| _ -> s];
(*** And JRH inserted it in here ***)
value operator_rparen =
Grammar.Entry.of_parser gram "operator_rparen"
(fun strm ->
match Stream.npeek 2 strm with
[ [("", s); ("", ")")] when is_operator s -> do {
Stream.junk strm;
Stream.junk strm;
translate_operator s
}
| _ -> raise Stream.Failure ])
;
value check_not_part_of_patt =
Grammar.Entry.of_parser gram "check_not_part_of_patt"
(fun strm ->
let tok =
match Stream.npeek 4 strm with
[ [("LIDENT", _); tok :: _] -> tok
| [("", "("); ("", s); ("", ")"); tok] when is_operator s -> tok
| _ -> raise Stream.Failure ]
in
match tok with
[ ("", "," | "as" | "|" | "::") -> raise Stream.Failure
| _ -> () ])
;
value symbolchar =
let list =
['!'; '$'; '%'; '&'; '*'; '+'; '-'; '.'; '/'; ':'; '<'; '='; '>'; '?';
'@'; '^'; '|'; '~']
in
loop where rec loop s i =
if i == String.length s then True
else if List.mem s.[i] list then loop s (i + 1)
else False
;
value prefixop =
let list = ['!'; '?'; '~'] in
let excl = ["!="; "??"; "?!"] in
Grammar.Entry.of_parser gram "prefixop"
(parser
[: `("", x)
when
not (List.mem x excl) && String.length x >= 2 &&
List.mem x.[0] list && symbolchar x 1 :] ->
x)
;
value infixop0 =
let list = ['='; '<'; '>'; '|'; '&'; '$'] in
let excl = ["<-"; "||"; "&&"] in
Grammar.Entry.of_parser gram "infixop0"
(parser
[: `("", x)
when
not (List.mem x excl) && String.length x >= 2 &&
List.mem x.[0] list && symbolchar x 1 :] ->
x)
;
value infixop1 =
let list = ['@'; '^'] in
Grammar.Entry.of_parser gram "infixop1"
(parser
[: `("", x)
when
String.length x >= 2 && List.mem x.[0] list &&
symbolchar x 1 :] ->
x)
;
value infixop2 =
let list = ['+'; '-'] in
Grammar.Entry.of_parser gram "infixop2"
(parser
[: `("", x)
when
x <> "->" && String.length x >= 2 && List.mem x.[0] list &&
symbolchar x 1 :] ->
x)
;
value infixop3 =
let list = ['*'; '/'; '%'] in
Grammar.Entry.of_parser gram "infixop3"
(parser
[: `("", x)
when
String.length x >= 2 && List.mem x.[0] list &&
symbolchar x 1 :] ->
x)
;
value infixop4 =
Grammar.Entry.of_parser gram "infixop4"
(parser
[: `("", x)
when
String.length x >= 3 && x.[0] == '*' && x.[1] == '*' &&
symbolchar x 2 :] ->
x)
;
value test_constr_decl =
Grammar.Entry.of_parser gram "test_constr_decl"
(fun strm ->
match Stream.npeek 1 strm with
[ [("UIDENT", _)] ->
match Stream.npeek 2 strm with
[ [_; ("", ".")] -> raise Stream.Failure
| [_; ("", "(")] -> raise Stream.Failure
| [_ :: _] -> ()
| _ -> raise Stream.Failure ]
| [("", "|")] -> ()
| _ -> raise Stream.Failure ])
;
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 ]
;
(* horrible hack to be able to parse class_types *)
value test_ctyp_minusgreater =
Grammar.Entry.of_parser gram "test_ctyp_minusgreater"
(fun strm ->
let rec skip_simple_ctyp n =
match stream_peek_nth n strm with
[ Some ("", "->") -> n
| Some ("", "[" | "[<") ->
skip_simple_ctyp (ignore_upto "]" (n + 1) + 1)
| Some ("", "(") -> skip_simple_ctyp (ignore_upto ")" (n + 1) + 1)
| Some
("",
"as" | "'" | ":" | "*" | "." | "#" | "<" | ">" | ".." | ";" |
"_") ->