This repository has been archived by the owner on Dec 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdparser.ml
1052 lines (974 loc) · 32.5 KB
/
rdparser.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
(** Recursive descent parser for dill *)
open Common
open Slexer
open Syntax
open Ast
(* for now we get these from the Menhir parser, so it's compatible with the
lexer. *)
(* type token =
| I_LIT of Int64.t
| F_LIT of float
| S_LIT of string
| B_LIT of char
| LC_IDENT of string
| UC_IDENT of string
| VLABEL of string
| LPAREN | RPAREN | LBRACE | RBRACE | LSQRB | RSQRB
| PLUS | MINUS | TIMES | DIV | MOD
(* | UMINUS (* not lexed *) *)
| AMP | PIPE | CARAT | TILDE (* bitwise operators *)
| SHL | SHR (* logical shift *)
| EQ | NE | LT | GT | LE | GE
| AND | OR | NOT (* Which ones overloadable? Maybe not these. *)
| TRUE | FALSE | NULL (* | VAL *)
| COLON | DCOLON | SEMI | DOT | COMMA
| HASH | DOLLAR | QMARK | ARROW | DARROW
| ASSIGN | NULLASSIGN
| VAR | REF | IMMREF
| NOP
| IF | THEN | ELSIF | ELSE | ENDIF
| WHILE (* | LOOP *) | ENDWHILE
| CASE | OF | ENDCASE
| IS | DO
| PRIVATE | PROC | ENDPROC | RETURN
| MODULE (* | BEGIN *) | ENDMODULE | MODSPEC | ENDMODSPEC
| IMPORT | AS | OPEN (* | EXPORT *) | REQUIRE
| TYPE | ENDTYPE | OPAQUE | REC | RECORD | VARIANT | MUT
| EOF
*)
type tlocated = {
ttype: Parser.token;
loc: Lexing.position * Lexing.position
}
(* Maybe put this stuff in its own module, or even in the lexer *)
(** Type for simple token buffering/lookahead implementation. *)
type tbuf = {
buf: tlocated array;
sbuf: string array; (* Store lexemes of tokens in parallel array *)
lexbuf: Sedlexing.lexbuf;
mutable bpos: int;
lookahead: int;
(* Info about last lexeme looked at (even if not consumed) *)
mutable last_lexeme: string;
mutable last_loc: (Lexing.position * Lexing.position)
}
(** Refill token buffer's lookahead in-place from lexer. *)
let _refill_tbuf tbuf =
let rec tloop i =
if i == tbuf.lookahead then ()
else if tbuf.bpos < tbuf.lookahead then (
(* If not at end, copy from back of buffer. *)
Array.set tbuf.buf i tbuf.buf.(tbuf.bpos);
Array.set tbuf.sbuf i tbuf.sbuf.(tbuf.bpos);
tbuf.bpos <- tbuf.bpos + 1;
tloop (i+1))
else
(* Otherwise pull from the lexer. *)
let nexttok = Slexer.token tbuf.lexbuf in
Array.set tbuf.buf i {ttype=nexttok; loc=Sedlexing.lexing_positions tbuf.lexbuf};
Array.set tbuf.sbuf i (Enc.lexeme tbuf.lexbuf);
if nexttok = EOF then () (* leave old tokens *)
else tloop (i+1)
in
if tbuf.bpos = 0 then () (* no-op case *)
else tloop 0; tbuf.bpos <- 0
(** Initialize token buffer with specified lookahead size. *)
let init_tbuf lexbuf lookahead =
let startloc = Sedlexing.lexing_positions lexbuf in
let tbuf = {
buf = Array.init lookahead (fun _ ->
{ttype = EOF; loc=startloc});
sbuf = Array.init lookahead (fun _ -> "");
lexbuf = lexbuf;
bpos = lookahead; (* marks as empty so it can refill *)
lookahead = lookahead;
last_lexeme = "";
last_loc = startloc
} in
_refill_tbuf tbuf;
tbuf
(** Return current token in buffer without moving the pointer. *)
let peek tbuf =
(* For lookahead 1, this is all we have to check. *)
if tbuf.bpos = tbuf.lookahead then _refill_tbuf tbuf;
let i = tbuf.bpos in
tbuf.last_lexeme <- tbuf.sbuf.(i);
tbuf.last_loc <- tbuf.buf.(i).loc;
tbuf.buf.(i)
(** LL(2) lookahead *)
let peek2 tbuf =
if tbuf.bpos >= tbuf.lookahead - 1 then _refill_tbuf tbuf;
let i = tbuf.bpos + 1 in
tbuf.last_lexeme <- tbuf.sbuf.(i);
tbuf.last_loc <- tbuf.buf.(i).loc;
tbuf.buf.(i)
(** Attempt to eat one token of given type from token buffer. *)
let consume tbuf (ttype: Parser.token) =
let tok = peek tbuf in
let samettype = match (ttype, tok.ttype) with
(* Could save typing with Obj.tag but I'd feel dirty :) *)
| I_LIT _, I_LIT _ -> true
| F_LIT _, F_LIT _ -> true
| S_LIT _, S_LIT _ -> true
| B_LIT _, B_LIT _ -> true
| LC_IDENT _, LC_IDENT _ -> true
| UC_IDENT _, UC_IDENT _ -> true
| _, _ -> ttype = tok.ttype
in
if samettype then (
tbuf.bpos <- tbuf.bpos + 1;
Some tok)
else
(* Oh wait, have to set prev_pos here too. It's really last_pos *)
None
(** Consume the next token of any type. Should be only for testing? *)
let consume_any tbuf =
let tok = peek tbuf in
tbuf.bpos <- tbuf.bpos + 1;
tok
let last_lexeme tbuf = tbuf.last_lexeme
let last_loc tbuf = tbuf.last_loc
(* -------------------- Parser Helper Functions ------------------ *)
(* helpers to extract token values. Maybe not needed now. *)
let tok_ival tok = match tok.ttype with
| I_LIT i -> i
| _ -> failwith "can't get integer from non-int token "
let tok_fval tok = match tok.ttype with
| F_LIT x -> x
| _ -> failwith "can't get float from non-float token "
let tok_cval tok = match tok.ttype with
| B_LIT c -> c
| _ -> failwith "can't get char from non-char token "
let tok_sval tok = match tok.ttype with
| S_LIT s | LC_IDENT s | UC_IDENT s -> s
| _ -> failwith "can't get string from non-string token "
(** Helper for general parse error messages. *)
let parse_error msg tbuf =
SyntaxError {
msg = msg;
loc = last_loc tbuf
}
(** Helper for "expected this, got that" error messages. *)
let expect_error estr tbuf =
SyntaxError {
msg = "Expected \"" ^ estr ^ "\", found \"" ^ last_lexeme tbuf ^ "\"";
loc = last_loc tbuf
}
(** Helper for "unexpected token" messages *)
let unexpect_error tbuf =
SyntaxError {
msg = "Unexpected token \"" ^ last_lexeme tbuf ^ "\"";
loc = last_loc tbuf
}
(** Make location out of a start and end position. *)
let make_location (t1spos, _) (_, t2epos) =
(t1spos, t2epos)
(** Helper to add location to an AST object. *)
let make_located node (t1spos, _) (_, t2epos) = {
value = node;
loc = (t1spos, t2epos)
}
(** Parse a token-separated list on an object given by rule. *)
let separated_list_of rule sep follow tbuf =
let sloc = (peek tbuf).loc in
let rec loop eloc =
if (peek tbuf).ttype = follow then
([], eloc)
else
let (parsed, ploc) = rule tbuf in
if (peek tbuf).ttype = sep then
let (rest, eloc) = loop ploc in
(parsed::rest, eloc)
else
([], eloc)
in
let (plist, eloc) = loop sloc in
(List.rev plist, make_location sloc eloc)
(*
(** Maybe parse a given object, but OK if not (catch exception) *)
let option rule tbuf =
try (rule tbuf) with
| SyntaxError _ -> None (* no it hides errors *)
| res -> Some res
*)
(* ----------------- rule functions begin ---------------- *)
(* "Token parsers" seem to have special status. They are the only ones
that call "consume" directly and use the coercion functions. *)
(** Consume token of given type, emitting error message with given description if not *)
let tok_val (ttype: Parser.token) descrip tbuf =
match (consume tbuf ttype) with
| None -> raise (expect_error descrip tbuf)
| Some tok -> tok
(** Parse a given token type without data, returning only location *)
let tok_only ttype tbuf =
(tok_val ttype (string_of_ttype ttype) tbuf).loc
(** Parse unqualified lowercase name. Takes a description string from context
so errors can say what the value is for. *)
let uqname descrip tbuf =
let tok = tok_val (LC_IDENT "") descrip tbuf in
(tok_sval tok, tok.loc)
let qname descrip tbuf =
let name1 = tok_val (LC_IDENT "") ("module name or " ^ descrip) tbuf in
match (peek tbuf).ttype with
| DCOLON ->
let _ = tok_only DCOLON tbuf in
let name2 = tok_val (LC_IDENT "") descrip tbuf in
(tok_sval name1, tok_sval name2, make_location name1.loc name2.loc)
| _ ->
("", tok_sval name1, name1.loc)
let typename tbuf =
let tok = tok_val (UC_IDENT "") "class name" tbuf in
(tok_sval tok, tok.loc)
(* finish me! or is it already done in typeexps. Anyway need generics *)
(* let qtypename tbuf = *)
(* match (peek tbuf).ttype with *)
(* | LC_IDENT _ -> (\* actually could be typevar *\) *)
(* let mname = tok_val (LC_IDENT "") ("module name ") tbuf in *)
(* let _ = tok_only DCOLON tbuf in *)
(* let tname = tok_val (UC_IDENT "") ("type name ") tbuf in *)
let iconst tbuf =
let tok = tok_val (I_LIT Int64.zero) "integer constant" tbuf in
(tok_ival tok, tok.loc)
let fconst tbuf =
let tok = tok_val (F_LIT 0.0) "float constant" tbuf
in (tok_fval tok, tok.loc)
let bconst tbuf =
let tok = tok_val (B_LIT '@') "byte constant" tbuf
in (tok_cval tok, tok.loc)
let sconst tbuf =
let tok = tok_val (S_LIT "") "string constant" tbuf
in (tok_sval tok, tok.loc)
(** Convert a token to its operator type *)
let binop_tok: (Parser.token -> binary_op) = function
| PLUS -> OpPlus
| MINUS -> OpMinus
| TIMES -> OpTimes
| DIV -> OpDiv
| MOD -> OpMod
| AMP -> OpBitAnd
| PIPE -> OpBitOr
| CARAT -> OpBitXor
| SHL -> OpShl
| SHR -> OpShr
| AND -> OpAnd
| OR -> OpOr
| EQ -> OpEq
| NE -> OpNe
| LT -> OpLt
| LE -> OpLe
| GT -> OpGt
| GE -> OpGe
| _ -> failwith "BUG: Token does not correspond do a binary operator"
let binop_prec = function
| OpTimes | OpDiv | OpMod -> 1
| OpPlus | OpMinus -> 2
| OpShl | OpShr -> 3
| OpBitAnd -> 4 (* Break with C, put these higher than comparison *)
| OpBitXor -> 5
| OpBitOr -> 6
| OpLt | OpLe | OpGt | OpGe -> 7
| OpEq | OpNe -> 8
| OpAnd -> 9
| OpOr -> 10
(* ---- end of single-token parsers ---- *)
(** typeExpr doesn't return separate location, it's in the struct. *)
let typeExpr tbuf =
let stok = peek tbuf in
match stok.ttype with
(* Can't just do "qtypename" because an lc_ident could be a type
variable also. *)
| LC_IDENT _ ->
(* maybe move this out to qualTypename or just typename? *)
let (n1, _) = uqname "module name or type variable" tbuf in
(match (peek tbuf).ttype with
| DCOLON ->
ignore (tok_only DCOLON tbuf);
let (cn, el1) = typename tbuf in
(* TODO: parse type args/nullable/array markers *)
{ texpkind=(
Concrete {
modname=n1; classname=cn;
typeargs=[];
});
(* nullable becomes 'option' class in analysis? *)
nullable=false; array=false;
loc=make_location stok.loc el1
}
| _ -> failwith "todo: type var parsing"
)
| UC_IDENT _ ->
let (cn, el1) = typename tbuf in
(* TODO: parse type args/nullable/array markers *)
{ texpkind=(
Concrete {
modname=""; classname=cn;
typeargs=[];
});
(* nullable should be 'option' *)
nullable=false; array=false;
loc=make_location stok.loc el1
}
| _ -> raise (expect_error "type expression" tbuf)
(** Void typeExpr for when it's implicit.
Still want to think about removing this from AST too. *)
let voidTypeExpr loc =
{ texpkind = (Concrete {
modname = "";
classname = "Void";
typeargs = [] });
nullable = false;
array = false;
loc = loc
}
(** Parse a token for a literal and extract its value for an ExpLiteral. *)
let literal_val tbuf =
match (peek tbuf).ttype with
| I_LIT _ ->
let (n, tloc) = iconst tbuf in (IntVal n, tloc)
| F_LIT _ ->
let (x, tloc) = fconst tbuf in (FloatVal x, tloc)
| B_LIT _ ->
let (c, tloc) = bconst tbuf in (ByteVal c, tloc)
| S_LIT _ ->
let (s, tloc) = sconst tbuf in (StringVal s, tloc)
| TRUE ->
let tloc = tok_only TRUE tbuf in (BoolVal true, tloc)
| FALSE ->
let tloc = tok_only FALSE tbuf in (BoolVal false, tloc)
| NULL ->
let tloc = tok_only NULL tbuf in (NullVal, tloc)
| _ -> failwith "BUG: literal_val called with wrong token type"
let unop_val tbuf =
match (peek tbuf).ttype with
| MINUS ->
let tloc = tok_only MINUS tbuf in (OpNeg, tloc)
| BANG ->
let tloc = tok_only BANG tbuf in (OpNot, tloc)
| TILDE ->
let tloc = tok_only TILDE tbuf in (OpBitNot, tloc)
| _ -> failwith "BUG: unop_val called with wrong token type"
let rec expr tbuf =
(* oh wait! record exps don't work with operators. Do I need to
catch that here? Nah. *)
(* Get a single expression, then look for operators, and finally
compute precedence *)
let rec base_expr () =
match (peek tbuf).ttype with
| LPAREN ->
let sloc = tok_only LPAREN tbuf in
let e = expr tbuf in (* exprs already have location *)
let eloc = tok_only RPAREN tbuf in
{ e=e.e; decor=make_location sloc eloc }
| I_LIT _ | F_LIT _ | B_LIT _ | S_LIT _ | TRUE | FALSE | NULL ->
let (v, tloc) = literal_val tbuf in
{ e=ExpLiteral v; decor=make_location tloc tloc }
| MINUS | BANG | TILDE ->
let oper, sloc = unop_val tbuf in
let e = base_expr () in
{ e=ExpUnop (oper, e); decor=make_location sloc e.decor }
| LSQRB ->
let sloc = tok_only LSQRB tbuf in
let rec seq_loop () =
(* Revisit: should an empty sequence be allowed? *)
let e = expr tbuf in
match (peek tbuf).ttype with
| COMMA ->
let _ = tok_only COMMA tbuf in e :: (seq_loop ())
| _ -> [e]
in
let seq = seq_loop () in
let eloc = tok_only RSQRB tbuf in
{ e=ExpSeq seq; decor=make_location sloc eloc }
| LBRACE ->
let sloc = tok_only LBRACE tbuf in
let rec record_loop () =
let fname, _ = uqname "field name" tbuf in
let _ = tok_only ASSIGN tbuf in
let fexp = expr tbuf in
match (peek tbuf).ttype with
| COMMA ->
let _ = tok_only COMMA tbuf in (fname, fexp) :: (record_loop ())
| _ -> [(fname, fexp)]
in
let fields = record_loop() in
let eloc = tok_only RBRACE tbuf in
{ e=ExpRecord fields; decor=make_location sloc eloc }
| VLABEL _ -> variant_exp tbuf
| _ -> var_or_call_expr tbuf
in
let e1 = base_expr () in
let rec oper_tail () =
match (peek tbuf).ttype with
| PLUS | MINUS | TIMES | DIV | MOD | AND | OR | AMP | PIPE | CARAT
| SHL | SHR | EQ | NE | LT | LE | GT | GE ->
let optok = consume_any tbuf in
let oper = binop_tok optok.ttype in
let e = base_expr () in
(oper, e) :: (oper_tail ())
| _ -> []
in
let tail = oper_tail () in
if tail = [] then e1
else
let opers, exprs = List.split tail in
expr_tree (e1 :: exprs) opers
and variant_exp tbuf =
let lbltok = tok_val (VLABEL "") "variant label" tbuf in
match (peek tbuf).ttype with
| LPAREN ->
let _ = tok_only LPAREN tbuf in
let rec varvals_loop () =
let e = expr tbuf in
match (peek tbuf).ttype with
| COMMA -> e :: (varvals_loop ())
| _ -> [e]
in
let varvals = varvals_loop() in
let eloc = tok_only RPAREN tbuf in
{ e=ExpVariant(tok_sval lbltok, varvals);
decor=make_location lbltok.loc eloc }
| _ ->
{ e=ExpVariant(tok_sval lbltok, []); decor=lbltok.loc }
(** Generate an expression tree from lists, based on precedence *)
and expr_tree elist oplist =
let rec max_prec maxi max li ri =
if li = ri then maxi
else
let prec = binop_prec (List.nth oplist li) in
if prec >= max (* greater or equal to find rightmost max *)
then max_prec li prec (li+1) ri
else max_prec maxi max (li+1) ri
in
let rec build li ri =
if li = ri then List.nth elist li
(* if li = ri-1 then
let e1, e2 = List.nth elist li, List.nth elist ri in
{ e=ExpBinop(e1, List.nth oplist li, e2);
decor=make_location e1.decor e2.decor } *)
else
let maxi = max_prec 0 0 li ri in
let e1, e2 = build li maxi, build (maxi+1) ri in
{ e=ExpBinop(e1, List.nth oplist maxi, e2);
decor=make_location e1.decor e2.decor }
in
build 0 (List.length oplist)
(* and operTail tbuf = (\* look for operators following *\) *)
(** Parse a string with idents and see if it's a varexpr or callexpr later *)
(* something with a call in it can still be a varexpr but not lvalue. *)
and var_or_call_expr tbuf =
let mname, id, sloc = qname "identifier" tbuf in
let qident = if mname = "" then id else mname ^ "::" ^ id in
match (peek tbuf).ttype with
| LPAREN -> (* CallExpr *)
let _ = tok_only LPAREN tbuf in
let args = arg_list tbuf in
let eloc = tok_only RPAREN tbuf in
{ e=ExpCall (qident, args);
decor=make_location sloc eloc}
| _ -> (* loop for varexpr tail (starting with possible indexes) *)
let ix1, eloc =
if (peek tbuf).ttype = LSQRB then
index_exprs tbuf
else ([], sloc)
in
let rec ve_tail eloc =
match (peek tbuf).ttype with
| DOT ->
let ident, eloc = uqname "field identifier" tbuf in
let ixs, eloc =
if (peek tbuf).ttype = LSQRB then
index_exprs tbuf
else ([], eloc)
in
let rest, eloc = ve_tail eloc in
((ident, ixs)::rest, eloc)
| _ -> ([], eloc)
in
let fields, eloc = ve_tail eloc in
{ e=ExpVar ((qident, ix1), fields);
decor=make_location sloc eloc }
(** Sequence of square-bracketed index expressions *)
and index_exprs tbuf =
let sloc = tok_only LSQRB tbuf in
let rec loop () =
let iexpr = expr tbuf in
let eloc = tok_only RSQRB tbuf in
match (peek tbuf).ttype with
| LSQRB ->
let _ = tok_only LSQRB tbuf in
let tail, eloc = loop () in
(iexpr::tail, make_location sloc eloc)
| _ -> ([iexpr], make_location sloc eloc)
in loop ()
(** List of arguments to a function call. *)
and arg_list tbuf =
(* Don't need overall location, each expr has its own *)
let rec loop () =
if (peek tbuf).ttype = RPAREN then []
else
let mutmark = match (peek tbuf).ttype with
| DOLLAR -> (ignore (tok_only DOLLAR tbuf); true)
| _ -> false
in
let e = expr tbuf in
if (peek tbuf).ttype = COMMA then
let rest = loop () in
((mutmark, e)::rest)
else
[(mutmark, e)]
in
loop ()
(* ------------------- Statement parsers ------------------- *)
(* if initializer is required, the caller will check; this rule is used
for both globals and locals that may not require initializers. *)
let rec decl_stmt tbuf =
let stok = peek tbuf in
match stok.ttype with
| VAR ->
let sloc = tok_only VAR tbuf in
let (vname, _) = uqname "lvalue" tbuf in
let tyopt =
(match (peek tbuf).ttype with
| COLON -> (* has type expression *)
ignore (tok_only COLON tbuf);
Some (typeExpr tbuf)
| _ -> None
) in
let initopt =
match (peek tbuf).ttype with
| ASSIGN ->
let _ = tok_only ASSIGN tbuf in
Some (expr tbuf)
| _ -> None
in
let eloc = tok_only SEMI tbuf in
{ st=StmtDecl (vname, tyopt, initopt);
decor=make_location sloc eloc }
| _ -> failwith "decl_stmt invalid state"
and if_stmt tbuf =
let sloc = tok_only IF tbuf in
let cond = expr tbuf in
let _ = tok_only THEN tbuf in
let then_block = stmt_seq tbuf in
let elsif_blocks = match (peek tbuf).ttype with
| ELSIF ->
let rec elsif_loop () =
(* have to do (optional) else in here too? *)
let _ = tok_only ELSIF tbuf in
let cond = expr tbuf in
let _ = tok_only THEN tbuf in
let block = stmt_seq tbuf in
match (peek tbuf).ttype with
| ELSIF -> (cond, block) :: (elsif_loop ())
| _ -> [(cond, block)]
in elsif_loop ()
| _ -> []
in
let elseopt = match (peek tbuf).ttype with
| ELSE ->
let _ = tok_only ELSE tbuf in
let else_block = stmt_seq tbuf in
Some else_block
| _ -> None
in
let eloc = tok_only ENDIF tbuf in
{ st=StmtIf (cond, then_block, elsif_blocks, elseopt);
decor=make_location sloc eloc }
and while_stmt tbuf =
let sloc = tok_only WHILE tbuf in
let cond = expr tbuf in
let _ = tok_only DO tbuf in
let body = stmt_seq tbuf in
let eloc = tok_only ENDWHILE tbuf in
{ st=StmtWhile (cond, body); decor=make_location sloc eloc }
and case_stmt tbuf =
let sloc = tok_only CASE tbuf in
let matchexp = expr tbuf in
let caseblocks =
let rec case_loop () =
let _ = tok_only OF tbuf in
let patexp = expr tbuf in
let _ = tok_only THEN tbuf in
let body = stmt_seq tbuf in
match (peek tbuf).ttype with
| OF -> (patexp, body) :: (case_loop ())
| _ -> [(patexp, body)]
in case_loop()
in
let elseopt = match (peek tbuf).ttype with
| ELSE ->
let _ = tok_only ELSE tbuf in
Some (stmt_seq tbuf)
| _ -> None
in
let eloc = tok_only ENDCASE tbuf in
{ st=StmtCase (matchexp, caseblocks, elseopt);
decor=make_location sloc eloc }
and nop_stmt tbuf =
let sloc = tok_only NOP tbuf in
let eloc = tok_only SEMI tbuf in
{ st=StmtNop; decor=make_location sloc eloc }
and call_stmt tbuf =
let e = expr tbuf in
match e.e with
| ExpCall _ ->
let eloc = tok_only SEMI tbuf in
{ st=StmtCall e; decor=(make_location e.decor eloc) }
| _ ->
raise (parse_error "Expression cannot serve as a statement" tbuf)
(** Both call and assign statement start with an expression *)
and call_or_assign_stmt tbuf =
let e = expr tbuf in
match (peek tbuf).ttype with
| ASSIGN ->
(match e.e with
(* Lvalues are somewhat defined syntactically by the AST: only var_expr. *)
| ExpVar vexp ->
let _ = tok_only ASSIGN tbuf in
let rvalue = expr tbuf in
let eloc = tok_only SEMI tbuf in
{ st=StmtAssign (vexp, rvalue); decor=make_location e.decor eloc }
| _ -> raise (parse_error "This expression type cannot be assigned to" tbuf)
)
| SEMI ->
(match e.e with
(* Then I could have had StmtCall be just name and args too, not expr *)
| ExpCall _ ->
let eloc = tok_only SEMI tbuf in
{ st=StmtCall e; decor=make_location e.decor eloc }
| _ ->
raise (parse_error "Expression cannot serve as a statement" tbuf)
)
| _ -> raise (unexpect_error tbuf)
and return_stmt tbuf =
let sloc = tok_only RETURN tbuf in
match (peek tbuf).ttype with
| SEMI ->
let eloc = tok_only SEMI tbuf in
{ st=StmtReturn None; decor=make_location sloc eloc }
| _ ->
let retexp = expr tbuf in
let eloc = tok_only SEMI tbuf in
{ st=StmtReturn (Some retexp); decor=make_location sloc eloc }
(** At least one statement, possibly more *)
and stmt_seq tbuf =
let rec loop () =
let st = stmt tbuf in
match (peek tbuf).ttype with
| ELSE | ELSIF | ENDIF | ENDCASE | ENDPROC | ENDTYPE | ENDWHILE -> [st]
| _ -> st :: (loop ())
in
loop ()
and stmt tbuf =
match (peek tbuf).ttype with
| VAR -> decl_stmt tbuf (* | REF *)
| IF -> if_stmt tbuf
| WHILE -> while_stmt tbuf
| CASE -> case_stmt tbuf
| RETURN -> return_stmt tbuf
| NOP -> nop_stmt tbuf
(* | LC_IDENT _ -> call_stmt tbuf (* call_or_assign *) *)
| _ -> call_or_assign_stmt tbuf
(* ------------------------ top-level parsers ------------------------ *)
(** Parse a single import statement or nothing. *)
let import tbuf =
print_string "* import...\n";
let stok = peek tbuf in
match stok.ttype with
| IMPORT ->
let sloc = tok_only IMPORT tbuf in
let (mname, _) = uqname "module name" tbuf in
print_string mname;
let alias = match (peek tbuf).ttype with
| AS ->
let _ = tok_only AS tbuf in
let (malias, _) = uqname "module alias" tbuf in
Some malias
| _ -> None
in
let eloc = tok_only SEMI tbuf in
(make_located (Import (mname, alias)) sloc eloc)
| OPEN ->
let sloc = tok_only OPEN tbuf in
let (mname, _) = uqname "module name" tbuf in
let eloc = tok_only SEMI tbuf in
(make_located (Open mname) sloc eloc)
| _ -> failwith "BUG: import called with non-import token"
let typedef_body tbuf =
let _ = tok_only IS tbuf in
(* oh, have to look for "rec" too *)
match (peek tbuf).ttype with
| STRUCT ->
let _ = tok_only STRUCT tbuf in
let rec fields_loop () =
let fpriv = match (peek tbuf).ttype with
| PRIVATE -> ignore (tok_only PRIVATE tbuf); true
| _ -> false
in
let fmut = match (peek tbuf).ttype with
| MUT -> ignore (tok_only MUT tbuf); true
| _ -> false
in
let fname, floc = uqname "field name" tbuf in
let _ = tok_only COLON tbuf in
let ftype = typeExpr tbuf in
let finfo = { fieldname=fname; priv=fpriv; mut=fmut; fieldtype=ftype;
decor=make_location floc ftype.loc} in
match (peek tbuf).ttype with
| COMMA -> finfo :: (fields_loop ())
| _ -> [finfo]
in fields_loop()
| VARIANT -> failwith "in progress" (* will return "kindinfo" when done *)
| UC_IDENT _ -> failwith "in progress" (* newtype *)
| _ -> raise (parse_error ("expected type or type kind specifier, found " ^
string_of_token (peek tbuf).ttype) tbuf)
let typedef tbuf =
let (vis, sloc) = match (peek tbuf).ttype with
| OPAQUE ->
let sloc = tok_only OPAQUE tbuf in
let _ = tok_only TYPE tbuf in
(Opaque, sloc)
| PRIVATE ->
let sloc = tok_only PRIVATE tbuf in
let _ = tok_only TYPE tbuf in
(Private, sloc)
| _ ->
let sloc = tok_only TYPE tbuf in
(Open, sloc)
in
(* todo: type variables for generics *)
let tname, _ = typename tbuf in
let fields = typedef_body tbuf in
let _ = tok_only SEMI tbuf in
let eloc = tok_only ENDTYPE tbuf in
{ typename=tname;
rectype=false;
typeparams=[];
kindinfo = Fields fields;
visibility = vis;
decor = make_location sloc eloc
}
(** Type declaration and possible definition for modspec. *)
let type_decl tbuf =
(* Private types don't exist in modspecs, and Opaque is shown by having no body *)
let sloc = tok_only TYPE tbuf in
let tname, _ = typename tbuf in
match (peek tbuf).ttype with
| SEMI -> (* opaque type *)
let eloc = tok_only SEMI tbuf in
{ typename=tname;
rectype=false;
typeparams=[];
kindinfo=Hidden;
visibility=Opaque;
decor=make_location sloc eloc
}
| IS ->
let fields = typedef_body tbuf in
let _ = tok_only SEMI tbuf in
let eloc = tok_only ENDTYPE tbuf in
{ typename=tname;
rectype=false;
typeparams=[];
kindinfo=Fields fields;
visibility=Open;
decor=make_location sloc eloc
}
| _ -> raise (unexpect_error tbuf)
let param_info tbuf =
let sloc = (peek tbuf).loc in
let mutmark = match (peek tbuf).ttype with
| DOLLAR ->
ignore (tok_only DOLLAR tbuf); true
| _ -> false
in
let (varname, _) = uqname "parameter name" tbuf in
let _ = tok_only COLON tbuf in
let texp = typeExpr tbuf in
((mutmark, varname, texp), make_location sloc texp.loc)
let param_list tbuf =
separated_list_of param_info COMMA RPAREN tbuf
let visibility tbuf =
let stok = peek tbuf in
let vis: visibility = match stok.ttype with
| PRIVATE -> let _ = tok_only PRIVATE tbuf in Private
| _ -> Public
in
(vis, stok.loc)
let proc_header tbuf =
let vis, sloc = visibility tbuf in
let _ = tok_only PROC tbuf in
let (pname, _) = uqname "procedure name" tbuf in
(* TODO: generic params *)
let _ = tok_only LPAREN tbuf in
let (params, _) = param_list tbuf in
let eloc = tok_only RPAREN tbuf in
let rettype = match (peek tbuf).ttype with
| ARROW ->
let _ = tok_only ARROW tbuf in
typeExpr tbuf
| _ -> voidTypeExpr eloc
in
{ name=pname;
typeparams = [];
params = params;
visibility = vis;
rettype = rettype;
decor = make_location sloc rettype.loc
}
let proc tbuf =
let decl = proc_header tbuf in
let _ = tok_only DO tbuf in
let stmts = stmt_seq tbuf in
let eloc = tok_only ENDPROC tbuf in
{ decl=decl; body=stmts; decor=make_location decl.decor eloc }
let module_body mname tbuf =
(* I can make imports come first, yay. *)
let imports =
let rec imploop () =
match (peek tbuf).ttype with
| IMPORT | OPEN ->
(* must be sure the statement is parsed first or infinite loop! *)
let istmt = import tbuf in istmt :: (imploop ())
| _ -> []
in imploop ()
in
let typedefs =
let rec typedefs_loop () =
if (peek tbuf).ttype = TYPE || (peek2 tbuf).ttype = TYPE then
let ty = typedef tbuf in
ty :: (typedefs_loop ())
else []
in typedefs_loop ()
in
let globals =
let rec globals_loop () =
if (peek tbuf).ttype = VAR || (peek2 tbuf).ttype = VAR then
let vis: visibility = match (peek tbuf).ttype with
| PRIVATE -> Private
| _ -> Public in
let gdecl = match decl_stmt tbuf with
| { st=StmtDecl (vname, tyexp, init); decor=stloc } ->
{ visibility=vis; varname=vname; typeexp=tyexp;
init=init; decor=stloc }
| _ -> failwith "BUG: didn't get StmtDecl for global statement"
in gdecl :: (globals_loop ())
else []
in globals_loop ()
in
let procs =
let rec procloop () =
if (peek tbuf).ttype = PROC || (peek2 tbuf).ttype = PROC then
let p = proc tbuf in p :: (procloop ())
else []
in procloop ()
in
{ name = mname;
imports = imports;
typedefs = typedefs;
globals = globals;
procs = procs;
}
(** Separate parser for global variable decl in a modspec only. *)
let global_decl tbuf =
let sloc = tok_only VAR tbuf in
let (vname, _) = uqname "global variable name" tbuf in
let ty =
(match (peek tbuf).ttype with
| COLON -> (* has type expression *)
ignore (tok_only COLON tbuf);
typeExpr tbuf
| _ -> (* give more detailed error message *)
raise (parse_error ("(modspec): Global variable declaration must "
^ "include a type annotation") tbuf)
) in
let eloc = tok_only SEMI tbuf in
{ varname=vname; typeexp=ty; decor=make_location sloc eloc }
let modspec tbuf =
let _ = tok_only MODSPEC tbuf in
let (mname, _) = uqname "module name" tbuf in
let _ = tok_only IS tbuf in
let requires =
let rec requires_loop () =
if (peek tbuf).ttype = REQUIRE then
let sloc = tok_only REQUIRE tbuf in
let (req, _) = uqname "module name" tbuf in
let eloc = tok_only SEMI tbuf in
(make_located req sloc eloc)::(requires_loop ())
else []
in requires_loop() in
let typedefs =
let rec typedecls_loop () =
if (peek tbuf).ttype = TYPE then (* no qualifier *)
(type_decl tbuf)::(typedecls_loop ())
else []
in typedecls_loop () in
let globals =
let rec globals_loop () =
if (peek tbuf).ttype = VAR then
let gd = global_decl tbuf in gd :: (globals_loop ())
else []
in globals_loop () in
let procdecls =
let rec procs_loop () =
if (peek tbuf).ttype = PROC || (peek2 tbuf).ttype = PROC then
let ph = proc_header tbuf in
let _ = tok_only SEMI tbuf in
ph :: (procs_loop ())
else []
in procs_loop () in
{ name=mname;
alias=mname; (* replaced at higher level *)
requires=requires;
typedefs=typedefs;
globals=globals;
procdecls=procdecls