-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.fs
1692 lines (1261 loc) · 51.5 KB
/
Parser.fs
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
module Syntax.Parser
open System.Collections.Generic
open Common.Span
open Common.Util.Util
open Syntax.AST
open Lexer
open Syntax.Util
type internal Parser(lexer: Lexer, error: ResizeArray<Error>, ctx: Context) =
let ctx = ctx
member _.WithCtx ctx = Parser(lexer, error, ctx)
member _.FreeExpr() =
Parser(
lexer,
error,
{ ctx with
InCond = false
InUnary = false }
)
member _.InCond() =
Parser(
lexer,
error,
{ ctx with
InCond = true
InUnary = false }
)
member _.LtGt parser error =
let res = ResizeArray()
let rec repeat () =
match lexer.Peek() with
| Some { Data = Operator(Cmp Gt); Span = span } ->
lexer.Consume()
res.ToArray(), span
| _ ->
let item = parser ()
res.Add item
match lexer.Peek() with
| Some { Data = Comma } ->
lexer.Consume()
repeat ()
| Some { Data = Operator(Cmp Gt); Span = span } ->
lexer.Consume()
res.ToArray(), span
| Some token -> raise (ParserExp(UnexpectedToken(token, error)))
| None -> raise (ParserExp(IncompleteAtEnd error))
repeat ()
member _.CommaSeq parser limiter =
let res = ResizeArray()
let rec repeat () =
match lexer.Peek() with
| Some { Data = data; Span = span } when data = limiter ->
lexer.Consume()
res.ToArray(), span
| _ ->
let item = parser ()
res.Add item
let next = lexer.Read "comma sequence"
match next.Data with
| Comma -> repeat ()
| data when data = limiter -> res.ToArray(), next.Span
| _ -> raise (ParserExp(NeedDelimiter(next.Span)))
repeat ()
member _.StructLike parser =
let res = ResizeArray()
let rec skipDelimiter () =
lexer.Consume()
match lexer.PeekInline() with
| Some { Data = Delimiter _ } -> skipDelimiter ()
| _ -> ()
let rec repeat () =
match lexer.Peek() with
| Some { Data = data; Span = span } when data = Close Curly ->
lexer.Consume()
res.ToArray(), span
| _ ->
let item = parser ()
res.Add item
match lexer.PeekInline() with
| Some { Data = Delimiter _ } ->
skipDelimiter ()
repeat ()
| Some { Data = Comma } ->
lexer.Consume()
repeat ()
| Some { Data = data; Span = span } when data = Close Curly ->
lexer.Consume()
res.ToArray(), span
| Some token -> raise (ParserExp(UnexpectedToken(token, "Struct Like")))
| None -> raise (ParserExp(IncompleteAtEnd("Struct Like")))
repeat ()
member _.ManyItem parser limiter =
let res = ResizeArray()
let rec skipDelimiter () =
match lexer.PeekInline() with
| Some { Data = Delimiter _ } ->
lexer.Consume()
skipDelimiter ()
| _ -> ()
let rec repeat () =
match lexer.Peek() with
| Some { Data = data; Span = span } when Some data = limiter ->
lexer.Consume()
res.ToArray(), span
| Some _ ->
let item = parser ()
res.Add item
skipDelimiter ()
repeat ()
| None ->
match limiter with
| Some _ -> raise (ParserExp(IncompleteAtEnd("Many Item")))
| None -> res.ToArray(), Span.dummy
repeat ()
member this.PatPath path =
let rec repeat (path: PathPat) =
match lexer.Peek() with
| Some { Data = ColonColon } ->
lexer.Consume()
let id = lexer.ReadId "Path Pattern Segments"
let path: PathPat =
{ Prefix = path.Prefix
Seg = Array.append path.Seg [| id |]
Span = path.Span.WithLast id.Span }
repeat path
| _ -> path
let rec field () =
match lexer.Read "Struct Pattern Field" with
| { Data = Identifier sym; Span = span } ->
let id = { Sym = sym; Span = span }
match lexer.Peek() with
| Some { Data = Colon } ->
lexer.Consume()
let pat = this.Pat()
KeyValuePat
{ Id = id
Pat = pat
Span = id.Span.WithLast pat.Span }
| _ -> ShorthandPat id
| token -> raise (ParserExp(UnexpectedToken(token, "Struct Pattern Field")))
let path = repeat path
match lexer.Peek() with
| Some { Data = Open Paren } ->
lexer.Consume()
if path.Seg.Length = 0 then
error.Add(IncompletePath path.Span)
let payload, last = this.CommaSeq this.Pat (Close Paren)
EnumPat
{ Variant = path
Payload = payload
Span = path.Span.WithLast last }
| Some { Data = Open Curly } ->
lexer.Consume()
if path.Prefix <> Some Self && path.Seg.Length = 0 then
error.Add(IncompletePath path.Span)
let field, last = this.StructLike field
StructPat
{ Struct = path
Field = field
Span = path.Span.WithLast last }
| _ ->
if path.Prefix = None && path.Seg.Length = 1 then
IdPat
{ Id = path.Seg[0]
Mut = false
Span = path.Span }
else if path.Prefix = Some LowSelf && path.Seg.Length = 0 then
SelfPat path.Span
else
if path.Seg.Length = 0 then
error.Add(IncompletePath path.Span)
PathPat path
member this.PatPrefix() =
let { Data = data; Span = span } as token = lexer.Read "Pattern"
match data with
| Reserved MUTABLE ->
let id = lexer.ReadId "mutable id pattern"
IdPat
{ Id = id
Mut = true
Span = span.WithLast id.Span }
| Identifier sym ->
let id = { Sym = sym; Span = span }
let path: PathPat =
{ Prefix = None
Seg = [| id |]
Span = span }
this.PatPath path
| Reserved(SELF | LOWSELF | PACKAGE as kw) ->
if not ctx.InDecl && kw = SELF then
error.Add(SelfOutOfDecl span)
let path: PathPat =
{ Prefix = Some(toPrefix kw)
Seg = [||]
Span = span }
this.PatPath path
| Lit l -> LitPat { Value = l; Span = span }
| DotDot ->
let to_, span =
match lexer.Peek() with
| Some { Data = data } when canStartPat data ->
let pat = this.PatPrefix()
Some pat, span.WithLast pat.Span
| _ -> None, span
RangePat { From = None; To = to_; Span = span }
| UnderLine -> CatchAllPat span
| Open Paren ->
let ele, last = this.CommaSeq this.Pat (Close Paren)
let span = span.WithLast last
if ele.Length = 1 then
ele[0].WithSpan span
else
TuplePat { Ele = ele; Span = span }
| Open Bracket ->
let ele, last = this.CommaSeq this.Pat (Close Paren)
let span = span.WithLast last
let rest = Array.filter isRestPat ele
if rest.Length > 1 then
error.Add(rest |> Array.map _.Span |> TooManyRestPat)
ArrayPat { Ele = ele; Span = span }
| Operator(Arith BitAnd) ->
let self = lexer.Expect (Reserved LOWSELF) "ref self"
RefSelfPat(span.WithLast self)
| _ -> raise (ParserExp(UnexpectedToken(token, "Pattern")))
member this.PatRange pat =
match lexer.Peek() with
| Some { Data = DotDot; Span = span } ->
lexer.Consume()
let (to_: Option<Pat>), span =
match lexer.Peek() with
| Some { Data = data } when canStartPat data ->
let to_ = this.PatPrefix()
let to_ = this.PatRange to_
Some to_, pat.Span.WithLast to_.Span
| _ -> None, pat.Span.WithLast span
RangePat
{ From = Some pat
To = to_
Span = span }
| _ -> pat
member this.PatAs pat =
match lexer.Peek() with
| Some { Data = Reserved AS } ->
lexer.Consume()
let mut =
match lexer.Peek() with
| Some { Data = Reserved MUTABLE } ->
lexer.Consume()
true
| _ -> false
let id = lexer.ReadId "As Pattern Name"
let pat =
AsPat
{ Pat = pat
Id = id
Mut = mut
Span = pat.Span.WithLast id.Span }
this.PatAs pat
| _ -> pat
member this.PatOr pat =
let res = ResizeArray()
res.Add pat
let rec repeat () =
match lexer.Peek() with
| Some { Data = Operator(Arith BitOr) } ->
lexer.Consume()
let pat = this.PatPrefix()
let pat = this.PatRange pat
let pat = this.PatAs pat
res.Add pat
repeat ()
| _ -> ()
repeat ()
if res.Count = 1 then
res[0]
else
let last = res[res.Count - 1]
OrPat
{ Pat = res.ToArray()
Span = pat.Span.WithLast last.Span }
member this.PatNoAlt() =
let pat = this.PatPrefix()
let pat = this.PatRange pat
this.PatAs pat
member this.Pat() =
let pat = this.PatPrefix()
let pat = this.PatRange pat
let pat = this.PatAs pat
this.PatOr pat
member this.Path (first: Either<Id, PathPrefix * Span>) turbofish =
let tryInst (span: Span) =
lexer.Consume()
let child = this.WithCtx { ctx with InTypeInst = true }
let g, last = child.LtGt child.Type "Generic Parameter"
if g.Length = 0 then
error.Add(EmptyTypeInst(span.WithLast last))
g, last
let seg = ResizeArray()
let rec tryInstTurboFish (id: Id) =
if turbofish then
match lexer.PeekInline() with
| Some({ Data = ColonColon }) ->
lexer.Consume()
match lexer.Peek() with
| Some { Data = Operator(Cmp Lt); Span = span } ->
let inst, last = tryInst span
seg.Add(id, inst)
last
| Some { Data = Identifier sym; Span = span } ->
lexer.Consume()
let newId = { Sym = sym; Span = span }
seg.Add(id, [||])
tryInstTurboFish newId
| Some token -> raise (ParserExp(UnexpectedToken(token, "Path Segment")))
| None -> raise (ParserExp(IncompleteAtEnd "Path Segment"))
| _ ->
seg.Add(id, [||])
id.Span
else
match lexer.Peek() with
| Some { Data = Operator(Cmp Lt); Span = span } ->
let inst, last = tryInst span
seg.Add(id, inst)
last
| _ ->
seg.Add(id, [||])
id.Span
let prefix, span =
match first with
| Left id -> None, id.Span.WithLast(tryInstTurboFish id)
| Right(prefix, span) -> Some prefix, span
let rec follow (span: Span) =
match lexer.PeekInline() with
| Some { Data = ColonColon } ->
lexer.Consume()
let id =
match lexer.Read "Path Segment" with
| { Data = Identifier sym; Span = span } -> { Sym = sym; Span = span }
| token -> raise (ParserExp(UnexpectedToken(token, "Path Segment")))
let span = span.WithLast(tryInstTurboFish id)
follow span
| _ -> span
let span = follow span
{ Prefix = prefix
Seg = seg.ToArray()
Span = span }
member this.Type() =
let { Data = data; Span = span } as token = lexer.Read "Type"
match data with
| Identifier sym ->
let id = { Sym = sym; Span = span }
let path = this.Path (Left id) false
if path.Prefix = None && path.Seg.Length = 1 && (snd path.Seg[0]).Length = 0 then
IdType id
else
PathType path
| Reserved(SELF | LOWSELF | PACKAGE as kw) ->
if not ctx.InDecl && kw = SELF then
error.Add(SelfOutOfDecl span)
let prefix = toPrefix kw
let path = this.Path (Right(prefix, span)) false
if path.Seg.Length = 0 && prefix <> Self then
error.Add(IncompletePath path.Span)
PathType path
| Lit l ->
if not ctx.InTypeInst then
error.Add(UnexpectedConstType token)
LitType { Value = l; Span = span }
| Not -> NeverType span
| UnderLine -> InferedType span
| Operator(Arith Sub) ->
if not ctx.InTypeInst then
error.Add(UnexpectedConstType token)
let ty = this.Type()
NegType
{ Ty = ty
Span = span.WithLast ty.Span }
| Operator(Arith BitAnd) ->
let ty = this.Type()
RefType
{ Ty = ty
Span = span.WithLast ty.Span }
| Open Paren ->
let ele, paren = this.CommaSeq this.Type (Close Paren)
let span = span.WithLast paren
if ele.Length = 1 then
ele[0].WithSpan span
else
TupleType { Ele = ele; Span = span }
| Open Bracket ->
let ele = this.Type()
match lexer.Peek() with
| Some { Data = Delimiter Semi } ->
lexer.Consume()
let len = this.Type()
let bracket = lexer.Expect (Close Bracket) "Array type"
ArrayType
{ Ele = ele
Len = Some len
Span = span.WithLast bracket }
| None -> raise (ParserExp(IncompleteAtEnd "Array Type"))
| _ ->
let bracket = lexer.Expect (Close Bracket) "Array type"
ArrayType
{ Ele = ele
Len = None
Span = span.WithLast bracket }
| Operator(Arith BitOr) ->
let param, _ = this.CommaSeq this.Type data
let _ = lexer.Expect Arrow "Function Type"
let ret = this.Type()
FnType
{ Param = param
Ret = ret
Span = span.WithLast ret.Span }
| _ -> raise (ParserExp(UnexpectedToken(token, "Type")))
member this.Param closure =
let pat =
// closure param cannot contain or pattern because of |
if closure then this.PatNoAlt() else this.Pat()
match lexer.Peek() with
| Some { Data = Colon } ->
lexer.Consume()
let ty = this.Type()
{ Pat = pat
Ty = Some ty
Span = pat.Span.WithLast ty.Span }
| _ ->
{ Pat = pat
Ty = None
Span = pat.Span }
member this.ExprPath(first: Either<Id, PathPrefix * Span>) =
let path = this.Path first true
let field () =
let { Data = data; Span = span } as token = lexer.Read "Struct Field"
match data with
| Identifier sym ->
let id = { Sym = sym; Span = span }
match lexer.Peek() with
| Some { Data = Colon } ->
lexer.Consume()
let value = this.Expr()
KeyValueField
{ Name = id
Value = value
Span = span.WithLast value.Span }
| _ -> ShorthandField id
| DotDot ->
let value = this.Expr()
RestField
{ Value = value
Span = span.WithLast value.Span }
| _ -> raise (ParserExp(UnexpectedToken(token, "Struct Field")))
match lexer.PeekInline() with
| Some { Data = Open Curly } when not ctx.InCond ->
lexer.Consume()
let field, last = this.FreeExpr().StructLike field
StructLit
{ Struct = path
Field = field
Span = path.Span.WithLast last }
| _ ->
if path.Prefix = None && path.Seg.Length = 1 && (snd path.Seg[0]).Length = 0 then
Id(fst path.Seg[0])
else if path.Prefix = Some LowSelf && path.Seg.Length = 0 then
SelfExpr path.Span
else
if path.Seg.Length = 0 then
error.Add(IncompletePath path.Span)
Path path
member this.ExprRange (token: Token) (from: Option<Expr>) =
let exclusive =
match token.Data with
| DotDot -> false
| DotDotCaret -> true
| _ -> failwith "Unreachable"
let first = from |> Option.map _.Span |> Option.defaultValue token.Span
let (to_: Expr option), last =
match lexer.Peek() with
| Some { Data = data } when canStartExpr data ->
let to_ = this.ExprPrefix()
let to_ = this.ExprRepeat -1 to_
Some to_, to_.Span
| _ -> None, token.Span
Range
{ From = from
Exclusive = exclusive
To = to_
Span = first.WithLast last }
member this.Cond() =
match lexer.Peek() with
| Some { Data = Reserved LET; Span = span } ->
lexer.Consume()
let pat = this.Pat()
let _ = lexer.Expect Eq "Let Condition"
let child = this.InCond()
let value = child.ExprPrefix()
let value = child.ExprRepeat -100 value
LetCond
{ Pat = pat
Value = value
Span = span.WithLast value.Span }
| Some { Data = data } when canStartExpr data ->
let child = this.InCond()
let value = child.ExprPrefix()
let value = child.ExprRepeat -100 value
BoolCond value
| Some token -> raise (ParserExp(UnexpectedToken(token, "Condition")))
| None -> raise (ParserExp(IncompleteAtEnd "Condition"))
member this.ExprPrefix() =
let { Data = data; Span = span } as token = lexer.Read "expression prefix"
let expr =
match data with
| Lit l -> LitExpr { Value = l; Span = span }
| Identifier i ->
let id = { Sym = i; Span = span }
this.ExprPath(Left id)
| Reserved(PACKAGE | SELF | LOWSELF as kw) ->
if not ctx.InDecl && kw = SELF then
error.Add(SelfOutOfDecl span)
let prefix = toPrefix kw
this.ExprPath(Right(prefix, span))
| Open Paren ->
let child = this.FreeExpr()
let (ele: Expr[]), paren = child.CommaSeq (child.Expr) (Close Paren)
let span = span.WithLast paren
if ele.Length = 1 then
ele[0].WithSpan span
else
Tuple { Ele = ele; Span = span }
| Open Bracket ->
let child = this.FreeExpr()
match lexer.Peek() with
| None -> raise (ParserExp(IncompletePair token))
| Some { Data = Close Bracket; Span = last } ->
lexer.Consume()
Array
{ Ele = [||]
Span = span.WithLast last }
| _ ->
let first = child.Expr()
let next = lexer.Read "array expression"
match next.Data with
| Close Bracket ->
Array
{ Ele = [| first |]
Span = span.WithLast next.Span }
| Comma ->
let rest, last = child.CommaSeq child.Expr (Close Bracket)
let ele = Array.append [| first |] rest
Array { Ele = ele; Span = span.WithLast last }
| Delimiter Semi ->
let count = child.Expr()
let last = lexer.Expect (Close Bracket) "array expression"
ArrayRepeat
{ Ele = first
Len = count
Span = span.WithLast last }
| _ -> raise (ParserExp(UnexpectedToken(next, "array expression")))
| Open Curly -> Block(this.Block span)
| Operator(Arith(Sub | Mul | BitAnd))
| Not ->
let value = this.WithCtx({ ctx with InUnary = true }).ExprPrefix()
let op =
match data with
| Operator(Arith Sub) -> Neg
| Operator(Arith Mul) -> Deref
| Operator(Arith BitAnd) -> Ref
| Not -> Syntax.AST.Not
| _ -> failwith "Unreachable"
Unary
{ Op = op
Value = value
Span = span.WithLast value.Span }
| DotDot
| DotDotCaret -> this.ExprRange token None
| Operator(Arith BitOr) ->
let child = this.WithCtx(ctx.EnterFn())
let param, _ = child.CommaSeq (fun () -> child.Param true) (Operator(Arith BitOr))
let ret = child.RetTy()
let body = child.Expr()
Closure
{ Param = param
Ret = ret
Body = body
Span = span.WithLast body.Span }
| Reserved BREAK ->
if not ctx.InLoop then
error.Add(OutOfLoop token)
Break span
| Reserved CONTINUE ->
if not ctx.InLoop then
error.Add(OutOfLoop token)
Continue span
| Reserved RETURN ->
if not ctx.InFn then
error.Add(OutOfFn span)
match lexer.Peek() with
| Some { Data = data } when canStartExpr data ->
let value = this.FreeExpr().Expr()
Return { Value = Some value; Span = span }
| _ -> Return { Value = None; Span = span }
| Reserved IF ->
let cond = this.Cond()
let curly = lexer.Expect (Open Curly) "If Expression Body"
let then_ = this.Block curly
let elseIf = ResizeArray()
let rec repeat (else_) =
match lexer.Peek() with
| Some { Data = Reserved ELSE; Span = span } ->
lexer.Consume()
match lexer.Peek() with
| Some { Data = Reserved IF } ->
lexer.Consume()
let cond = this.Cond()
let curly = lexer.Expect (Open Curly) "If Expression Body"
let block = this.Block curly
elseIf.Add
{ Cond = cond
Block = block
Span = span.WithLast block.Span }
repeat else_
| Some { Data = Open Curly; Span = span } ->
lexer.Consume()
this.Block span |> Some
| Some token -> raise (ParserExp(UnexpectedToken(token, "If Else")))
| None -> else_
| _ -> else_
let else_ = repeat None
let last =
match else_ with
| Some else_ -> else_.Span
| None ->
if elseIf.Count > 0 then
elseIf[elseIf.Count - 1].Span
else
then_.Span
If
{ Cond = cond
ElseIf = elseIf.ToArray()
Else = else_
Then = then_
Span = span.WithLast last }
| Reserved MATCH ->
let child = this.InCond()
let value = child.ExprPrefix()
let value = child.ExprRepeat -100 value
let _ = lexer.Expect (Open Curly) "Match Body"
let branch () =
let pat = this.Pat()
let guard =
match lexer.Peek() with
| Some { Data = Reserved IF } ->
lexer.Consume()
this.Expr() |> Some
| _ -> None
let _ = lexer.Expect FatArrow "Match Branch"
let expr = this.Expr()
{ Pat = pat
Guard = guard
Expr = expr
Span = pat.Span.WithLast expr.Span }
let branch, last = this.StructLike branch
Match
{ Value = value
Branch = branch
Span = span.WithLast last }
| Reserved WHILE ->
let cond = this.Cond()
let curly = lexer.Expect (Open Curly) "While Expression Body"
let body = this.WithCtx({ ctx with InLoop = true }).Block curly
While
{ Cond = cond
Body = body
Span = span.WithLast body.Span }
| Reserved FOR ->
let pat = this.Pat()
let _ = lexer.Expect (Reserved IN) "For In"
let child = this.InCond()
let iter = child.ExprPrefix()
let iter = child.ExprRepeat -100 iter
let curly = lexer.Expect (Open Curly) "For Expression Body"
let body = this.WithCtx({ ctx with InLoop = true }).Block curly
For
{ Pat = pat
Iter = iter
Body = body
Span = span.WithLast body.Span }
| _ -> raise (ParserExp(UnexpectedToken(token, "expression prefix")))
this.ExprPostfix expr
member this.ExprPostfix expr =
match lexer.PeekPost() with
| Some { Data = Dot } ->
lexer.Consume()
match lexer.Read "field access expression" with
| { Data = Identifier sym; Span = span } ->
let field =
Field
{ Receiver = expr
Field = { Sym = sym; Span = span }
Span = expr.Span.WithLast span }
this.ExprPostfix field
| { Data = Lit(Int i); Span = span } ->
let access =
TupleAccess
{ Receiver = expr
Index = (i, span)
Span = expr.Span.WithLast span }
this.ExprPostfix access
| token -> raise (ParserExp(UnexpectedToken(token, "field access expression")))
| Some { Data = Open Paren } ->
lexer.Consume()
let child = this.FreeExpr()
let arg, paren = child.CommaSeq child.Expr (Close Paren)
let call =
Call
{ Callee = expr
Arg = arg
Span = expr.Span.WithLast paren }
this.ExprPostfix call
| Some { Data = Open Bracket } ->
lexer.Consume()
let child = this.FreeExpr()
let index = child.Expr()
let bracket = lexer.Expect (Close Bracket) "index expression"
let index =
Index
{ Container = expr
Index = index
Span = expr.Span.WithLast bracket }
this.ExprPostfix index
| Some { Data = Question; Span = span } ->
lexer.Consume()
let tryReturn =
TryReturn
{ Base = expr
Span = expr.Span.WithLast span }
this.ExprPostfix tryReturn
| Some { Data = Reserved AS } when not ctx.InUnary ->
lexer.Consume()
let ty = this.Type()
let as_ =
As
{ Value = expr
Ty = ty
Span = expr.Span.WithLast ty.Span }