forked from jrh13/hol-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pa_j_3.07.ml
2409 lines (2318 loc) · 83.3 KB
/
pa_j_3.07.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
(* camlp4r pa_extend.cmo q_MLast.cmo *)
(***********************************************************************)
(* *)
(* Camlp4 *)
(* *)
(* Daniel de Rauglaudre, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 2002 Institut National de Recherche en Informatique et *)
(* Automatique. Distributed only by permission. *)
(* *)
(***********************************************************************)
(* $Id: pa_o.ml,v 1.54 2003/09/30 14:39:38 mauny Exp $ *)
open Stdpp;
open Pcaml;
Pcaml.syntax_name.val := "OCaml";
Pcaml.no_constructors_arity.val := True;
(* ------------------------------------------------------------------------- *)
(* Hacked version of the lexer. *)
(* ------------------------------------------------------------------------- *)
open Token;
value jrh_lexer = ref False;
value no_quotations = ref False;
(* The string buffering machinery *)
value buff = ref (String.create 80);
value store len x =
do {
if len >= String.length buff.val then
buff.val := buff.val ^ String.create (String.length buff.val)
else ();
buff.val.[len] := x;
succ len
}
;
value mstore len s =
add_rec len 0 where rec add_rec len i =
if i == String.length s then len else add_rec (store len s.[i]) (succ i)
;
value get_buff len = String.sub buff.val 0 len;
(* The lexer *)
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 len =
parser
[ [: `('A'..'Z' | 'a'..'z' | '\192'..'\214' | '\216'..'\246' |
'\248'..'\255' | '0'..'9' | '_' | ''' as
c)
;
s :] ->
ident (store len c) s
| [: :] -> len ]
and ident2 len =
parser
[ [: `('!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' |
'%' | '.' | ':' | '<' | '>' | '|' | '$' as
c)
;
s :] ->
ident2 (store len c) s
| [: :] -> len ]
and ident3 len =
parser
[ [: `('0'..'9' | 'A'..'Z' | 'a'..'z' | '\192'..'\214' | '\216'..'\246' |
'\248'..'\255' | '_' | '!' | '%' | '&' | '*' | '+' | '-' | '.' |
'/' | ':' | '<' | '=' | '>' | '?' | '@' | '^' | '|' | '~' | ''' |
'$' as
c)
;
s :] ->
ident3 (store len c) s
| [: :] -> len ]
and base_number len =
parser
[ [: `'o' | 'O'; s :] -> digits octal (store len 'o') s
| [: `'x' | 'X'; s :] -> digits hexa (store len 'x') s
| [: `'b' | 'B'; s :] -> digits binary (store len 'b') s
| [: a = number len :] -> a ]
and digits kind len =
parser
[ [: d = kind; s :] -> digits_under kind (store len d) s
| [: :] -> raise (Stream.Error "ill-formed integer constant") ]
and digits_under kind len =
parser
[ [: d = kind; s :] -> digits_under kind (store len d) s
| [: `'_'; s :] -> digits_under kind len s
| [: :] -> ("INT", get_buff len) ]
and octal = parser [ [: `('0'..'7' as d) :] -> d ]
and hexa = parser [ [: `('0'..'9' | 'a'..'f' | 'A'..'F' as d) :] -> d ]
and binary = parser [ [: `('0'..'1' as d) :] -> d ]
and number len =
parser
[ [: `('0'..'9' as c); s :] -> number (store len c) s
| [: `'_'; s :] -> number len s
| [: `'.'; s :] -> decimal_part (store len '.') s
| [: `'e' | 'E'; s :] -> exponent_part (store len 'E') s
| [: `'l' :] -> ("INT32", get_buff len)
| [: `'L' :] -> ("INT64", get_buff len)
| [: `'n' :] -> ("NATIVEINT", get_buff len)
| [: :] -> ("INT", get_buff len) ]
and decimal_part len =
parser
[ [: `('0'..'9' as c); s :] -> decimal_part (store len c) s
| [: `'_'; s :] -> decimal_part len s
| [: `'e' | 'E'; s :] -> exponent_part (store len 'E') s
| [: :] -> ("FLOAT", get_buff len) ]
and exponent_part len =
parser
[ [: `('+' | '-' as c); s :] -> end_exponent_part (store len c) s
| [: a = end_exponent_part len :] -> a ]
and end_exponent_part len =
parser
[ [: `('0'..'9' as c); s :] -> end_exponent_part_under (store len c) s
| [: :] -> raise (Stream.Error "ill-formed floating-point constant") ]
and end_exponent_part_under len =
parser
[ [: `('0'..'9' as c); s :] -> end_exponent_part_under (store len c) s
| [: `'_'; s :] -> end_exponent_part_under len s
| [: :] -> ("FLOAT", get_buff len) ]
;
value error_on_unknown_keywords = ref False;
value err loc msg = raise_with_loc loc (Token.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))
then ("UIDENT", id) else ("LIDENT", id)];
(* ------------------------------------------------------------------------- *)
(* Back to original file with the mod of using the above. *)
(* ------------------------------------------------------------------------- *)
(*
value next_token_fun dfa find_kwd =
let keyword_or_error loc s =
try (("", find_kwd s), loc) with
[ Not_found ->
if error_on_unknown_keywords.val then err loc ("illegal token: " ^ s)
else (("", s), loc) ]
in
let rec next_token =
parser bp
[ [: `' ' | '\010' | '\013' | '\t' | '\026' | '\012'; s :] ->
next_token s
| [: `'('; s :] -> left_paren bp s
| [: `'#'; s :] -> do { spaces_tabs s; linenum bp s }
| [: `('A'..'Z' | '\192'..'\214' | '\216'..'\222' as c); s :] ->
let id = get_buff (ident (store 0 c) s) in
let loc = (bp, Stream.count s) in
(jrh_identifier find_kwd id, loc)
(********** original
(try ("", find_kwd id) with [ Not_found -> ("UIDENT", id) ], loc)
***********)
| [: `('a'..'z' | '\223'..'\246' | '\248'..'\255' | '_' as c); s :] ->
let id = get_buff (ident (store 0 c) s) in
let loc = (bp, Stream.count s) in
(jrh_identifier find_kwd id, loc)
(********** original
(try ("", find_kwd id) with [ Not_found -> ("LIDENT", id) ], loc)
**********)
| [: `('1'..'9' as c); s :] ->
let tok = number (store 0 c) s in
let loc = (bp, Stream.count s) in
(tok, loc)
| [: `'0'; s :] ->
let tok = base_number (store 0 '0') s in
let loc = (bp, Stream.count s) in
(tok, loc)
| [: `'''; s :] ->
match Stream.npeek 3 s with
[ [_; '''; _] | ['\\'; _; _] | ['\x0D'; '\x0A'; '''] ->
let tok = ("CHAR", get_buff (char bp 0 s)) in
let loc = (bp, Stream.count s) in
(tok, loc)
| _ -> keyword_or_error (bp, Stream.count s) "'" ]
| [: `'"'; s :] ->
let tok = ("STRING", get_buff (string bp 0 s)) in
let loc = (bp, Stream.count s) in
(tok, loc)
| [: `'`'; s :] ->
let tok = ("QUOTATION", "tot:"^(qstring bp 0 s)) in
let loc = (bp, Stream.count s) in
(tok, loc)
| [: `'$'; s :] ->
let tok = dollar bp 0 s in
let loc = (bp, Stream.count s) in
(tok, loc)
| [: `('!' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' | '%' as c);
s :] ->
let id = get_buff (ident2 (store 0 c) s) in
keyword_or_error (bp, Stream.count s) id
| [: `('~' as c);
a =
parser
[ [: `('a'..'z' as c); len = ident (store 0 c) :] ep ->
(("TILDEIDENT", get_buff len), (bp, ep))
| [: s :] ->
let id = get_buff (ident2 (store 0 c) s) in
keyword_or_error (bp, Stream.count s) id ] :] ->
a
| [: `('?' as c);
a =
parser
[ [: `('a'..'z' as c); len = ident (store 0 c) :] ep ->
(("QUESTIONIDENT", get_buff len), (bp, ep))
| [: s :] ->
let id = get_buff (ident2 (store 0 c) s) in
keyword_or_error (bp, Stream.count s) id ] :] ->
a
| [: `'<'; s :] -> less bp s
| [: `(':' as c1);
len =
parser
[ [: `(']' | ':' | '=' | '>' as c2) :] -> store (store 0 c1) c2
| [: :] -> store 0 c1 ] :] ep ->
let id = get_buff len in
keyword_or_error (bp, ep) id
| [: `('>' | '|' as c1);
len =
parser
[ [: `(']' | '}' as c2) :] -> store (store 0 c1) c2
| [: a = ident2 (store 0 c1) :] -> a ] :] ep ->
let id = get_buff len in
keyword_or_error (bp, ep) id
| [: `('[' | '{' as c1); s :] ->
let len =
match Stream.npeek 2 s with
[ ['<'; '<' | ':'] -> store 0 c1
| _ ->
match s with parser
[ [: `('|' | '<' | ':' as c2) :] -> store (store 0 c1) c2
| [: :] -> store 0 c1 ] ]
in
let ep = Stream.count s in
let id = get_buff len in
keyword_or_error (bp, ep) id
| [: `'.';
id =
parser
[ [: `'.' :] -> ".."
| [: :] -> if ssd && after_space then " ." else "." ] :] ep ->
keyword_or_error (bp, ep) id
| [: `';';
id =
parser
[ [: `';' :] -> ";;"
| [: :] -> ";" ] :] ep ->
keyword_or_error (bp, ep) id
| [: `'\\'; s :] ep -> (("LIDENT", get_buff (ident3 0 s)), (bp, ep))
| [: `c :] ep -> keyword_or_error (bp, ep) (String.make 1 c)
| [: _ = Stream.empty :] -> (("EOI", ""), (bp, succ bp)) ]
and less bp strm =
if no_quotations.val then
match strm with parser
[ [: len = ident2 (store 0 '<') :] ep ->
let id = get_buff len in
keyword_or_error (bp, ep) id ]
else
match strm with parser
[ [: `'<'; len = quotation bp 0 :] ep ->
(("QUOTATION", ":" ^ get_buff len), (bp, ep))
| [: `':'; i = parser [: len = ident 0 :] -> get_buff len;
`'<' ? "character '<' expected"; len = quotation bp 0 :] ep ->
(("QUOTATION", i ^ ":" ^ get_buff len), (bp, ep))
| [: len = ident2 (store 0 '<') :] ep ->
let id = get_buff len in
keyword_or_error (bp, ep) id ]
and string bp len =
parser
[ [: `'"' :] -> len
| [: `'\\'; `c; s :] -> string bp (store (store len '\\') c) s
| [: `c; s :] -> string bp (store len c) s
| [: :] ep -> err (bp, ep) "string not terminated" ]
and qstring bp len =
parser
[ [: `'`' :] -> get_buff len
| [: `c; s :] -> qstring bp (store len c) s
| [: :] ep -> err (bp, ep) "quotation not terminated" ]
and char bp len =
parser
[ [: `'''; s :] -> if len = 0 then char bp (store len ''') s else len
| [: `'\\'; `c; s :] -> char bp (store (store len '\\') c) s
| [: `c; s :] -> char bp (store len c) s
| [: :] ep -> err (bp, ep) "char not terminated" ]
and dollar bp len =
parser
[ [: `'$' :] -> ("ANTIQUOT", ":" ^ get_buff len)
| [: `('a'..'z' | 'A'..'Z' as c); s :] -> antiquot bp (store len c) s
| [: `('0'..'9' as c); s :] -> maybe_locate bp (store len c) s
| [: `':'; s :] ->
let k = get_buff len in
("ANTIQUOT", k ^ ":" ^ locate_or_antiquot_rest bp 0 s)
| [: `'\\'; `c; s :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: s :] ->
if dfa then
match s with parser
[ [: `c :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: :] ep -> err (bp, ep) "antiquotation not terminated" ]
else ("", get_buff (ident2 (store 0 '$') s)) ]
and maybe_locate bp len =
parser
[ [: `'$' :] -> ("ANTIQUOT", ":" ^ get_buff len)
| [: `('0'..'9' as c); s :] -> maybe_locate bp (store len c) s
| [: `':'; s :] ->
("LOCATE", get_buff len ^ ":" ^ locate_or_antiquot_rest bp 0 s)
| [: `'\\'; `c; s :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: `c; s :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: :] ep -> err (bp, ep) "antiquotation not terminated" ]
and antiquot bp len =
parser
[ [: `'$' :] -> ("ANTIQUOT", ":" ^ get_buff len)
| [: `('a'..'z' | 'A'..'Z' | '0'..'9' as c); s :] ->
antiquot bp (store len c) s
| [: `':'; s :] ->
let k = get_buff len in
("ANTIQUOT", k ^ ":" ^ locate_or_antiquot_rest bp 0 s)
| [: `'\\'; `c; s :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: `c; s :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: :] ep -> err (bp, ep) "antiquotation not terminated" ]
and locate_or_antiquot_rest bp len =
parser
[ [: `'$' :] -> get_buff len
| [: `'\\'; `c; s :] -> locate_or_antiquot_rest bp (store len c) s
| [: `c; s :] -> locate_or_antiquot_rest bp (store len c) s
| [: :] ep -> err (bp, ep) "antiquotation not terminated" ]
and quotation bp len =
parser
[ [: `'>'; s :] -> maybe_end_quotation bp len s
| [: `'<'; s :] ->
quotation bp (maybe_nested_quotation bp (store len '<') s) s
| [: `'\\';
len =
parser
[ [: `('>' | '<' | '\\' as c) :] -> store len c
| [: :] -> store len '\\' ];
s :] ->
quotation bp len s
| [: `c; s :] -> quotation bp (store len c) s
| [: :] ep -> err (bp, ep) "quotation not terminated" ]
and maybe_nested_quotation bp len =
parser
[ [: `'<'; s :] -> mstore (quotation bp (store len '<') s) ">>"
| [: `':'; len = ident (store len ':');
a =
parser
[ [: `'<'; s :] -> mstore (quotation bp (store len '<') s) ">>"
| [: :] -> len ] :] ->
a
| [: :] -> len ]
and maybe_end_quotation bp len =
parser
[ [: `'>' :] -> len
| [: a = quotation bp (store len '>') :] -> a ]
and left_paren bp =
parser
[ [: `'*'; _ = comment bp; a = next_token True :] -> a
| [: :] ep -> keyword_or_error (bp, ep) "(" ]
and comment bp =
parser
[ [: `'('; s :] -> left_paren_in_comment bp s
| [: `'*'; s :] -> star_in_comment bp s
| [: `'"'; _ = string bp 0; s :] -> comment bp s
| [: `'''; s :] -> quote_in_comment bp s
| [: `c; s :] -> comment bp s
| [: :] ep -> err (bp, ep) "comment not terminated" ]
and quote_in_comment bp =
parser
[ [: `'''; s :] -> comment bp s
| [: `'\013'; s :] -> quote_cr_in_comment bp s
| [: `'\\'; s :] -> quote_antislash_in_comment bp s
| [: `'('; s :] -> quote_left_paren_in_comment bp s
| [: `'*'; s :] -> quote_star_in_comment bp s
| [: `'"'; s :] -> quote_doublequote_in_comment bp s
| [: `_; s :] -> quote_any_in_comment bp s
| [: s :] -> comment bp s ]
and quote_any_in_comment bp =
parser
[ [: `'''; s :] -> comment bp s
| [: s :] -> comment bp s ]
and quote_cr_in_comment bp =
parser
[ [: `'\010'; s :] -> quote_any_in_comment bp s
| [: s :] -> quote_any_in_comment bp s ]
and quote_left_paren_in_comment bp =
parser
[ [: `'''; s :] -> comment bp s
| [: s :] -> left_paren_in_comment bp s ]
and quote_star_in_comment bp =
parser
[ [: `'''; s :] -> comment bp s
| [: s :] -> star_in_comment bp s ]
and quote_doublequote_in_comment bp =
parser
[ [: `'''; s :] -> comment bp s
| [: _ = string bp 0; s :] -> comment bp s ]
and quote_antislash_in_comment bp =
parser
[ [: `'''; s :] -> quote_antislash_quote_in_comment bp s
| [: `('\\' | '"' | 'n' | 't' | 'b' | 'r'); s :] ->
quote_any_in_comment bp s
| [: `('0'..'9'); s :] -> quote_antislash_digit_in_comment bp s
| [: `'x'; s :] -> quote_antislash_x_in_comment bp s
| [: s :] -> comment bp s ]
and quote_antislash_quote_in_comment bp =
parser
[ [: `'''; s :] -> comment bp s
| [: s :] -> quote_in_comment bp s ]
and quote_antislash_digit_in_comment bp =
parser
[ [: `('0'..'9'); s :] -> quote_antislash_digit2_in_comment bp s
| [: s :] -> comment bp s ]
and quote_antislash_digit2_in_comment bp =
parser
[ [: `('0'..'9'); s :] -> quote_any_in_comment bp s
| [: s :] -> comment bp s ]
and quote_antislash_x_in_comment bp =
parser
[ [: _ = hexa; s :] -> quote_antislash_x_digit_in_comment bp s
| [: s :] -> comment bp s ]
and quote_antislash_x_digit_in_comment bp =
parser
[ [: _ = hexa; s :] -> quote_any_in_comment bp s
| [: s :] -> comment bp s ]
and left_paren_in_comment bp =
parser
[ [: `'*'; s :] -> do { comment bp s; comment bp s }
| [: a = comment bp :] -> a ]
and star_in_comment bp =
parser
[ [: `')' :] -> ()
| [: a = comment bp :] -> a ]
and 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 ]
and any_to_nl =
parser
[ [: `'\013' | '\010' :] ep -> bolpos.val := ep
| [: `_; s :] -> any_to_nl s
| [: :] -> () ]
in
fun cstrm ->
try
let glex = glexr.val in
let comm_bp = Stream.count cstrm in
let r = next_token False cstrm in
do {
match glex.tok_comm with
[ Some list ->
if fst (snd r) > comm_bp then
let comm_loc = (comm_bp, fst (snd r)) in
glex.tok_comm := Some [comm_loc :: list]
else ()
| None -> () ];
r
}
with
[ Stream.Error str ->
err (Stream.count cstrm, Stream.count cstrm + 1) str ]
;
*)
value next_token_fun dfa ssd find_kwd bolpos glexr =
let keyword_or_error loc s =
try (("", find_kwd s), loc) with
[ Not_found ->
if error_on_unknown_keywords.val then err loc ("illegal token: " ^ s)
else (("", s), loc) ] in
let error_if_keyword ( ((_,id), loc) as a) =
try do {
ignore(find_kwd id);
err loc ("illegal use of a keyword as a label: " ^ id) }
with [ Not_found -> a ]
in
let rec next_token after_space =
parser bp
[ [: `'\010' | '\013'; s :] ep ->
do { bolpos.val := ep; next_token True s }
| [: `' ' | '\t' | '\026' | '\012'; s :] -> next_token True s
| [: `'#' when bp = bolpos.val; s :] ->
if linedir 1 s then do { any_to_nl s; next_token True s }
else keyword_or_error (bp, bp + 1) "#"
| [: `'('; s :] -> left_paren bp s
| [: `('A'..'Z' | '\192'..'\214' | '\216'..'\222' as c); s :] ->
let id = get_buff (ident (store 0 c) s) in
let loc = (bp, Stream.count s) in
(jrh_identifier find_kwd id, loc)
(********** original
(try ("", find_kwd id) with [ Not_found -> ("UIDENT", id) ], loc)
***********)
| [: `('a'..'z' | '\223'..'\246' | '\248'..'\255' | '_' as c); s :] ->
let id = get_buff (ident (store 0 c) s) in
let loc = (bp, Stream.count s) in
(jrh_identifier find_kwd id, loc)
(********** original
(try ("", find_kwd id) with [ Not_found -> ("LIDENT", id) ], loc)
**********)
| [: `('1'..'9' as c); s :] ->
let tok = number (store 0 c) s in
let loc = (bp, Stream.count s) in
(tok, loc)
| [: `'0'; s :] ->
let tok = base_number (store 0 '0') s in
let loc = (bp, Stream.count s) in
(tok, loc)
| [: `'''; s :] ->
match Stream.npeek 2 s with
[ [_; '''] | ['\\'; _] ->
let tok = ("CHAR", get_buff (char bp 0 s)) in
let loc = (bp, Stream.count s) in
(tok, loc)
| _ -> keyword_or_error (bp, Stream.count s) "'" ]
| [: `'"'; s :] ->
let tok = ("STRING", get_buff (string bp 0 s)) in
let loc = (bp, Stream.count s) in
(tok, loc)
| [: `'`'; s :] ->
let tok = ("QUOTATION", "tot:"^(qstring bp 0 s)) in
let loc = (bp, Stream.count s) in
(tok, loc)
| [: `'$'; s :] ->
let tok = dollar bp 0 s in
let loc = (bp, Stream.count s) in
(tok, loc)
| [: `('!' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' | '%' as c);
s :] ->
let id = get_buff (ident2 (store 0 c) s) in
keyword_or_error (bp, Stream.count s) id
| [: `('~' as c);
a =
parser
[ [: `('a'..'z' as c); len = ident (store 0 c); s :] ep ->
let id = get_buff len in
match s with parser
[ [: `':' :] eb -> error_if_keyword (("LABEL", id), (bp,ep))
| [: :] -> error_if_keyword (("TILDEIDENT", id), (bp, ep)) ]
| [: s :] ->
let id = get_buff (ident2 (store 0 c) s) in
keyword_or_error (bp, Stream.count s) id ] :] ->
a
| [: `('?' as c);
a =
parser
[ [: `('a'..'z' as c); len = ident (store 0 c); s :] ep ->
let id = get_buff len in
match s with parser
[ [: `':' :] eb -> error_if_keyword (("OPTLABEL", id), (bp,ep))
| [: :] -> error_if_keyword (("QUESTIONIDENT", id), (bp, ep)) ]
| [: s :] ->
let id = get_buff (ident2 (store 0 c) s) in
keyword_or_error (bp, Stream.count s) id ] :] ->
a
| [: `'<'; s :] -> less bp s
| [: `(':' as c1);
len =
parser
[ [: `(']' | ':' | '=' | '>' as c2) :] -> store (store 0 c1) c2
| [: :] -> store 0 c1 ] :] ep ->
let id = get_buff len in
keyword_or_error (bp, ep) id
| [: `('>' | '|' as c1);
len =
parser
[ [: `(']' | '}' as c2) :] -> store (store 0 c1) c2
| [: a = ident2 (store 0 c1) :] -> a ] :] ep ->
let id = get_buff len in
keyword_or_error (bp, ep) id
| [: `('[' | '{' as c1); s :] ->
let len =
match Stream.npeek 2 s with
[ ['<'; '<' | ':'] -> store 0 c1
| _ ->
match s with parser
[ [: `('|' | '<' | ':' as c2) :] -> store (store 0 c1) c2
| [: :] -> store 0 c1 ] ]
in
let ep = Stream.count s in
let id = get_buff len in
keyword_or_error (bp, ep) id
| [: `'.';
id =
parser
[ [: `'.' :] -> ".."
| [: :] -> if ssd && after_space then " ." else "." ] :] ep ->
keyword_or_error (bp, ep) id
| [: `';';
id =
parser
[ [: `';' :] -> ";;"
| [: :] -> ";" ] :] ep ->
keyword_or_error (bp, ep) id
| [: `'\\'; s :] ep -> (("LIDENT", get_buff (ident3 0 s)), (bp, ep))
| [: `c :] ep -> keyword_or_error (bp, ep) (String.make 1 c)
| [: _ = Stream.empty :] -> (("EOI", ""), (bp, succ bp)) ]
and less bp strm =
if no_quotations.val then
match strm with parser
[ [: len = ident2 (store 0 '<') :] ep ->
let id = get_buff len in
keyword_or_error (bp, ep) id ]
else
match strm with parser
[ [: `'<'; len = quotation bp 0 :] ep ->
(("QUOTATION", ":" ^ get_buff len), (bp, ep))
| [: `':'; i = parser [: len = ident 0 :] -> get_buff len;
`'<' ? "character '<' expected"; len = quotation bp 0 :] ep ->
(("QUOTATION", i ^ ":" ^ get_buff len), (bp, ep))
| [: len = ident2 (store 0 '<') :] ep ->
let id = get_buff len in
keyword_or_error (bp, ep) id ]
and string bp len =
parser
[ [: `'"' :] -> len
| [: `'\\'; `c; s :] -> string bp (store (store len '\\') c) s
| [: `c; s :] -> string bp (store len c) s
| [: :] ep -> err (bp, ep) "string not terminated" ]
and qstring bp len =
parser
[ [: `'`' :] -> get_buff len
| [: `c; s :] -> qstring bp (store len c) s
| [: :] ep -> err (bp, ep) "quotation not terminated" ]
and char bp len =
parser
[ [: `'''; s :] -> if len = 0 then char bp (store len ''') s else len
| [: `'\\'; `c; s :] -> char bp (store (store len '\\') c) s
| [: `c; s :] -> char bp (store len c) s
| [: :] ep -> err (bp, ep) "char not terminated" ]
and dollar bp len =
parser
[ [: `'$' :] -> ("ANTIQUOT", ":" ^ get_buff len)
| [: `('a'..'z' | 'A'..'Z' as c); s :] -> antiquot bp (store len c) s
| [: `('0'..'9' as c); s :] -> maybe_locate bp (store len c) s
| [: `':'; s :] ->
let k = get_buff len in
("ANTIQUOT", k ^ ":" ^ locate_or_antiquot_rest bp 0 s)
| [: `'\\'; `c; s :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: s :] ->
if dfa then
match s with parser
[ [: `c :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: :] ep -> err (bp, ep) "antiquotation not terminated" ]
else ("", get_buff (ident2 (store 0 '$') s)) ]
and maybe_locate bp len =
parser
[ [: `'$' :] -> ("ANTIQUOT", ":" ^ get_buff len)
| [: `('0'..'9' as c); s :] -> maybe_locate bp (store len c) s
| [: `':'; s :] ->
("LOCATE", get_buff len ^ ":" ^ locate_or_antiquot_rest bp 0 s)
| [: `'\\'; `c; s :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: `c; s :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: :] ep -> err (bp, ep) "antiquotation not terminated" ]
and antiquot bp len =
parser
[ [: `'$' :] -> ("ANTIQUOT", ":" ^ get_buff len)
| [: `('a'..'z' | 'A'..'Z' | '0'..'9' as c); s :] ->
antiquot bp (store len c) s
| [: `':'; s :] ->
let k = get_buff len in
("ANTIQUOT", k ^ ":" ^ locate_or_antiquot_rest bp 0 s)
| [: `'\\'; `c; s :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: `c; s :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: :] ep -> err (bp, ep) "antiquotation not terminated" ]
and locate_or_antiquot_rest bp len =
parser
[ [: `'$' :] -> get_buff len
| [: `'\\'; `c; s :] -> locate_or_antiquot_rest bp (store len c) s
| [: `c; s :] -> locate_or_antiquot_rest bp (store len c) s
| [: :] ep -> err (bp, ep) "antiquotation not terminated" ]
and quotation bp len =
parser
[ [: `'>'; s :] -> maybe_end_quotation bp len s
| [: `'<'; s :] ->
quotation bp (maybe_nested_quotation bp (store len '<') s) s
| [: `'\\';
len =
parser
[ [: `('>' | '<' | '\\' as c) :] -> store len c
| [: :] -> store len '\\' ];
s :] ->
quotation bp len s
| [: `c; s :] -> quotation bp (store len c) s
| [: :] ep -> err (bp, ep) "quotation not terminated" ]
and maybe_nested_quotation bp len =
parser
[ [: `'<'; s :] -> mstore (quotation bp (store len '<') s) ">>"
| [: `':'; len = ident (store len ':');
a =
parser
[ [: `'<'; s :] -> mstore (quotation bp (store len '<') s) ">>"
| [: :] -> len ] :] ->
a
| [: :] -> len ]
and maybe_end_quotation bp len =
parser
[ [: `'>' :] -> len
| [: a = quotation bp (store len '>') :] -> a ]
and left_paren bp =
parser
[ [: `'*'; _ = comment bp; a = next_token True :] -> a
| [: :] ep -> keyword_or_error (bp, ep) "(" ]
and comment bp =
parser
[ [: `'('; s :] -> left_paren_in_comment bp s
| [: `'*'; s :] -> star_in_comment bp s
| [: `'"'; _ = string bp 0; s :] -> comment bp s
| [: `'''; s :] -> quote_in_comment bp s
| [: `c; s :] -> comment bp s
| [: :] ep -> err (bp, ep) "comment not terminated" ]
and quote_in_comment bp =
parser
[ [: `'''; s :] -> comment bp s
| [: `'\\'; s :] -> quote_antislash_in_comment bp 0 s
| [: s :] ->
do {
match Stream.npeek 2 s with
[ [_; '''] -> do { Stream.junk s; Stream.junk s }
| _ -> () ];
comment bp s
} ]
and quote_any_in_comment bp =
parser
[ [: `'''; s :] -> comment bp s
| [: a = comment bp :] -> a ]
and quote_antislash_in_comment bp len =
parser
[ [: `'''; s :] -> comment bp s
| [: `'\\' | '"' | 'n' | 't' | 'b' | 'r'; s :] ->
quote_any_in_comment bp s
| [: `'0'..'9'; s :] -> quote_antislash_digit_in_comment bp s
| [: a = comment bp :] -> a ]
and quote_antislash_digit_in_comment bp =
parser
[ [: `'0'..'9'; s :] -> quote_antislash_digit2_in_comment bp s
| [: a = comment bp :] -> a ]
and quote_antislash_digit2_in_comment bp =
parser
[ [: `'0'..'9'; s :] -> quote_any_in_comment bp s
| [: a = comment bp :] -> a ]
and left_paren_in_comment bp =
parser
[ [: `'*'; s :] -> do { comment bp s; comment bp s }
| [: a = comment bp :] -> a ]
and star_in_comment bp =
parser
[ [: `')' :] -> ()
| [: a = comment bp :] -> a ]
and 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 ]
and any_to_nl =
parser
[ [: `'\013' | '\010' :] ep -> bolpos.val := ep
| [: `_; s :] -> any_to_nl s
| [: :] -> () ]
in
fun cstrm ->
try
let glex = glexr.val in
let comm_bp = Stream.count cstrm in
let r = next_token False cstrm in
do {
match glex.tok_comm with
[ Some list ->
if fst (snd r) > comm_bp then
let comm_loc = (comm_bp, fst (snd r)) in
glex.tok_comm := Some [comm_loc :: list]
else ()
| None -> () ];
r
}
with
[ Stream.Error str ->
err (Stream.count cstrm, Stream.count cstrm + 1) str ]
;
value dollar_for_antiquotation = ref True;
value specific_space_dot = ref False;
value func kwd_table glexr =
let bolpos = ref 0 in
let find = Hashtbl.find kwd_table in
let dfa = dollar_for_antiquotation.val in
let ssd = specific_space_dot.val in
Token.lexer_func_of_parser (next_token_fun dfa ssd find bolpos glexr)
;
value rec check_keyword_stream =
parser [: _ = check; _ = Stream.empty :] -> True
and check =
parser
[ [: `'A'..'Z' | 'a'..'z' | '\192'..'\214' | '\216'..'\246' | '\248'..'\255'
;
s :] ->
check_ident s
| [: `'!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' |
'%' | '.'
;
s :] ->
check_ident2 s
| [: `'<'; s :] ->
match Stream.npeek 1 s with
[ [':' | '<'] -> ()
| _ -> check_ident2 s ]
| [: `':';
_ =
parser
[ [: `']' | ':' | '=' | '>' :] -> ()
| [: :] -> () ] :] ep ->
()
| [: `'>' | '|';
_ =
parser
[ [: `']' | '}' :] -> ()
| [: a = check_ident2 :] -> a ] :] ->
()
| [: `'[' | '{'; s :] ->
match Stream.npeek 2 s with
[ ['<'; '<' | ':'] -> ()
| _ ->
match s with parser
[ [: `'|' | '<' | ':' :] -> ()
| [: :] -> () ] ]
| [: `';';
_ =
parser
[ [: `';' :] -> ()
| [: :] -> () ] :] ->
()
| [: `_ :] -> () ]
and check_ident =
parser
[ [: `'A'..'Z' | 'a'..'z' | '\192'..'\214' | '\216'..'\246' |
'\248'..'\255' | '0'..'9' | '_' | '''
;
s :] ->
check_ident s
| [: :] -> () ]
and check_ident2 =
parser
[ [: `'!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' |
'%' | '.' | ':' | '<' | '>' | '|'
;
s :] ->
check_ident2 s
| [: :] -> () ]
;
value check_keyword s =
try check_keyword_stream (Stream.of_string s) with _ -> False
;
value error_no_respect_rules p_con p_prm =
raise
(Token.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
(Token.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 ]
| "INT" | "INT32" | "INT64" | "NATIVEINT"
| "FLOAT" | "CHAR" | "STRING"
| "TILDEIDENT" | "QUESTIONIDENT" | "LABEL" | "OPTLABEL"
| "QUOTATION" | "ANTIQUOT" | "LOCATE" | "EOI" ->
()
| _ ->
raise
(Token.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"