-
Notifications
You must be signed in to change notification settings - Fork 8
/
parser.sml
executable file
·6849 lines (6257 loc) · 240 KB
/
parser.sml
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
(* -*- mode: sml; mode: font-lock; tab-width: 4; insert-tabs-mode: nil; indent-tabs-mode: nil -*- *)
structure Parser = struct
(* Local tracing machinery *)
val doTrace = ref false
fun trace ss = if (!doTrace) then LogErr.log ("[parse] " :: ss) else ()
fun error ss = LogErr.parseError ss
exception ParseError = LogErr.ParseError
exception LexError = LogErr.LexError
open Token
datatype alpha =
ALLOWLIST
| NOLIST
datatype beta =
ALLOWIN
| NOIN
datatype gamma =
ALLOWEXPR
| NOEXPR
datatype omega =
ABBREV
| NOSHORTIF
| FULL
datatype tau =
GLOBAL
| CLASS
| INTERFACE
| LOCAL
type ATTRS = {ns: Ast.EXPR option,
override: bool,
static: bool,
final: bool,
dynamic: bool,
prototype: bool,
native: bool,
rest: bool}
(*
PATTERNS to BINDINGS to FIXTURES
EXPRS to INITS
*)
datatype PATTERN =
ObjectPattern of FIELD_PATTERN list
| ArrayPattern of PATTERN list
| SimplePattern of Ast.EXPR
| IdentifierPattern of Ast.IDENT
withtype FIELD_PATTERN =
{ ident: Ast.IDENT_EXPR,
pattern : PATTERN }
type PATTERN_BINDING_PART =
{ kind:Ast.VAR_DEFN_TAG,
ty:Ast.TYPE_EXPR,
prototype:bool,
static:bool }
val currentClassName : Ast.IDENT ref = ref ""
fun newline (ts : (TOKEN * Ast.POS) list) =
(* fun newline ts = Lexer.UserDeclarations.followsLineBreak ts *)
let
val (_, {file, span, sm, post_newline}) = hd ts
in
post_newline
end
fun posOf ts =
case ts of
(_, pos) :: _ => (SOME pos)
| _ => (NONE)
fun setPos ts =
LogErr.setPos (posOf ts)
val defaultAttrs : ATTRS =
{
ns = NONE,
override = false,
static = false,
final = false,
dynamic = false,
prototype = false,
native = false,
rest = false }
val defaultRestAttrs : ATTRS =
{
ns = NONE,
override = false,
static = false,
final = false,
dynamic = false,
prototype = false,
native = false,
rest = true }
(*
PATTERN
Patterns in binding contexts (e.g. after 'var') cause fixtures
to be created. Other patterns de-sugar into assignment expressions
with the value of the right side stored in a temporary to avoid
multiple evaluation.
When the definition phase is complete, the only kind of patterns
that remain are SimplePatterns which are wrappers for the property
references that are the targets of assignments
Example:
ns var {i:x,j:y} : {i:int,j:string} = o
gets rewritten by the parser as,
ns var {i:x,j:y} : {i:int,j:string}
ns <init> {i:x,j:y} = o
which gets rewritten by 'defBinding' and 'defInit' as,
ns var x:int
ns var y:string
and
<temp> t = o
ns::x = t["i"]
ns::y = t["j"]
respectively, where the name shown as 't' is guaranteed not to
conflict with or shadow any other name in scope.
Example:
[ns::x, ns::y] = o
gets rewriten by 'defAssignment' as,
<temp> t = o
ns::x = t[0]
ns::y = t[1]
Note: the difference between defineAssignment and defInit is that
defInit creates qualified identifiers using the namespace and the
identifiers on the left side of each assignment.
*)
(*
* Patterns get desugared into FIXTURES (covering all temporaries) and
* lists of INIT_STEPs.
*
* Depending on context, the INIT_STEPs might be restricted to
* initializing single idents, thus of the form "Init (...)". This is
* true of patterns in 4 contexts:
*
* - function parameters
* - class member initializers (including proto and static inits)
* - class constructor settings
* - variable binding forms like "var" and "let" in imperative blocks
*
* In other contexts, a pattern represents just a sequence of general
* assignments, and any expression that can evaluate to an object ref
* is valid for the pattern LHS. In these cases the desugared form is
* Assign (e1,e2) and e1 must be a "ref expr" (see eval.sml).
*
* INIT_STEP is required as input to the definer so that the target of
* the initialisation can be known. The definer turns on INIT_STEPS into
* INITS or STMTS.
*)
(*
var x : int = 10
Binding {ident="x",ty=int}
InitStep ("x",10)
Turn a pattern, type and initialiser into lists of bindings, inits, and statements.
The outer defn will wrap both with the attributes
*)
fun desugarPattern (pos:Ast.POS option)
(pattern:PATTERN)
(ty:Ast.TYPE_EXPR)
(expr:Ast.EXPR option)
(nesting:int)
: (Ast.BINDING list * Ast.INIT_STEP list) =
let
fun desugarIdentifierPattern (id:Ast.IDENT)
: (Ast.BINDING list * Ast.INIT_STEP list) =
(*
Binding (id,ty)
InitStep (id,expr)
*)
let
val ident = Ast.PropIdent id
val bind = Ast.Binding {ident=ident,ty=ty}
in case expr of
SOME e => ([bind],[Ast.InitStep (ident,e)])
| NONE => ([bind],[])
end
fun desugarSimplePattern (patternExpr:Ast.EXPR)
: (Ast.BINDING list * Ast.INIT_STEP list) =
(*
[],
AssignStep (patternExpr,expr)
*)
let
in case expr of
SOME e => ([],[Ast.AssignStep (patternExpr,e)])
| NONE => error ["simple pattern without initialiser"]
end
(*
[x,y] = o
let [i,s]:[int,String] = o
ISSUE: Are partial type annotations allowed? let [i,s]:[int]=...
If so, what is the type of 's' here? int or *?
*)
fun desugarArrayPattern (element_ptrns: PATTERN list)
(element_types: Ast.TYPE_EXPR)
(temp:Ast.EXPR)
(n:int)
: (Ast.BINDING list * Ast.INIT_STEP list) =
let
in case element_ptrns of
p::plist =>
let
val id = Int.toString n
val str = Ast.LiteralString id
val ident = Ast.ExpressionIdentifier { expr = (Ast.LiteralExpr (str)),
openNamespaces = [] }
val e = SOME (Ast.ObjectRef {base=temp, ident=ident, pos=pos})
val t = Ast.ElementTypeRef (element_types,n)
val (binds, inits) = desugarPattern pos p t e (nesting+1)
val (binds', inits') = desugarArrayPattern plist element_types temp (n+1)
in
((binds @ binds'), (inits @ inits'))
end
| [] => ([], [])
end
(*
{i:x,j:y}:{i:t,j:u} = o
becomes
<temp> tmp = o
Bind x,t
Init x = tmp["i"]
Bind y,u
Init y = tmp["j"]
*)
fun desugarObjectPattern (field_ptrns:FIELD_PATTERN list)
(field_types:Ast.TYPE_EXPR)
(temp:Ast.EXPR)
: (Ast.BINDING list * Ast.INIT_STEP list) =
let
in
case field_ptrns of
fp::fps =>
let
val (binds,inits) = desugarFieldPattern fp field_types temp
val (binds',inits') = desugarObjectPattern fps field_types temp
in
((binds @ binds'), (inits @ inits'))
end
| [] => ([],[])
end
(*
Use the field name to get the field type and associate that field type
with the field's pattern. Deference the given expression with the field
name to get the value of the field.
*)
and desugarFieldPattern (field_pattern: FIELD_PATTERN)
(field_types: Ast.TYPE_EXPR)
(temp: Ast.EXPR)
: (Ast.BINDING list * Ast.INIT_STEP list) =
let
val {ident,pattern=p} = field_pattern
in
case (field_types,ident) of
(ty, Ast.Identifier {ident=id,...}) =>
(* if the field pattern is typed, it must have a identifier for
its name so we can do the mapping to its field type *)
let
val t = Ast.FieldTypeRef (ty,id)
val e = SOME (Ast.ObjectRef {base=temp, ident=ident, pos=pos})
in
desugarPattern pos p t e (nesting+1)
end
| (_,_) =>
let
val t = Ast.SpecialType Ast.Any
val e = SOME (Ast.ObjectRef {base=temp, ident=ident, pos=pos})
in
desugarPattern pos p t e (nesting+1)
end
end
in
case (pattern,expr) of
(SimplePattern expr,_) => desugarSimplePattern expr
| (IdentifierPattern id,_) => desugarIdentifierPattern id
| (_,_) =>
let
val e = case expr of SOME e => e | _ => (Ast.GetTemp 0)
val temp_n = nesting
val temp_id = Ast.TempIdent temp_n
val temp_binding = Ast.Binding {ident=temp_id,ty=ty}
val temp_step = Ast.InitStep (temp_id, e)
val temp_expr = Ast.GetTemp temp_n
in case pattern of
ObjectPattern fields =>
let
val (bindings, steps) = desugarObjectPattern fields ty temp_expr
in
(temp_binding::bindings, temp_step::steps)
end
| ArrayPattern elements =>
let
val temp_index = 0
val (bindings, steps) = desugarArrayPattern elements ty temp_expr temp_index
in
(temp_binding::bindings, temp_step::steps)
end
| _ => error ["won't get here"]
end
end
(*
Identifier
Identifier
ContextuallyReservedIdentifier
*)
and identifier ts =
let
in case ts of
(Identifier(str), _) :: tr => (tr,str)
| (Call, _) :: tr => (tr,"call")
| (Debugger, _) :: tr => (tr,"debugger")
| (Decimal, _) :: tr => (tr,"decimal")
| (Double, _) :: tr => (tr,"double")
| (Dynamic, _) :: tr => (tr,"dynamic")
| (Each, _) :: tr => (tr,"each")
| (Final, _) :: tr => (tr,"final")
| (Get, _) :: tr => (tr,"get")
| (Goto, _) :: tr => (tr,"goto")
| (Has, _) :: tr => (tr,"has")
| (Include, _) :: tr => (tr,"include")
| (Int, _) :: tr => (tr,"int")
| (Namespace, _) :: tr => (tr,"namespace")
| (Native, _) :: tr => (tr,"native")
| (Number, _) :: tr => (tr,"number")
| (Override, _) :: tr => (tr,"override")
| (Precision, _) :: tr => (tr,"precision")
| (Prototype, _) :: tr => (tr,"prototype")
| (Rounding, _) :: tr => (tr,"rounding")
| (Standard, _) :: tr => (tr,"standard")
| (Strict, _) :: tr => (tr,"strict")
| (UInt, _) :: tr => (tr,"uint")
| (Set, _) :: tr => (tr,"set")
| (Static, _) :: tr => (tr,"static")
| (Type, _) :: tr => (tr,"type")
| (Undefined, _) :: tr => (tr,"undefined")
| (Xml, _) :: tr => (tr,"xml")
| (Yield, _) :: tr => (tr,"yield")
| _ => error(["expecting 'identifier' before '",tokenname(hd ts),"'"])
end
(*
Qualifier
ReservedNamespace
PropertyIdentifier
*)
and qualifier ts =
let
in case ts of
((Internal, _) :: _ | (Intrinsic, _) :: _ | (Private, _) :: _ | (Protected, _) :: _ | (Public, _) :: _) =>
let
val (ts1,nd1) = reservedNamespace ts
in
(ts1,Ast.LiteralExpr(Ast.LiteralNamespace nd1))
end
| (Mult, _) :: _ =>
let
in
(tl ts,Ast.LexicalRef{ident=Ast.WildcardIdentifier, pos=posOf ts})
end
| _ =>
let
val (ts1,nd1) = identifier ts
in
(ts1,Ast.LexicalRef{ident=Ast.Identifier {ident=nd1, openNamespaces=[]}, pos=posOf ts})
end
end
and reservedNamespace ts =
let val _ = trace([">> reservedNamespace with next=",tokenname(hd(ts))])
in case ts of
(Internal, _) :: tr =>
(tr, Ast.Internal "")
| (Intrinsic, _) :: tr =>
(tr, Ast.Intrinsic)
| (Private, _) :: tr =>
(tr, Ast.Private "class name here")
| (Protected, _) :: tr =>
(tr, Ast.Protected "class name here")
| (Public, _) :: tr =>
(tr, Ast.Public "")
| _ => error ["unknown reserved namespace"]
end
(*
SimpleQualifiedIdentifier
PropertyIdentifier
Qualifier :: PropertyIdentifier
Qualifier :: ReservedIdentifier
Qualifier :: Brackets
ExpressionQualifiedIdentifer
ParenListExpression :: PropertyIdentifier
ParenListExpression :: ReservedIdentifier
ParenListExpression :: Brackets
left factored:
SimpleQualifiedIdentifier
ReservedNamespace :: QualifiedIdentifierPrime
PropertyIdentifier :: QualifiedIdentifierPrime
PropertyIdentifier
ExpressionQualifiedIdentifer
ParenListExpression :: QualifiedIdentifierPrime
QualifiedIdentifierPrime
PropertyIdentifier
ReservedIdentifier
Brackets
*)
and simpleQualifiedIdentifier ts =
let val _ = trace([">> simpleQualifiedIdentifier with next=",tokenname(hd(ts))])
in case ts of
((Internal, _) :: _ | (Intrinsic, _) :: _ | (Private, _) :: _ | (Protected, _) :: _ | (Public, _) :: _) =>
let
val (ts1, nd1) = reservedNamespace(ts)
in case ts1 of
(DoubleColon, _) :: ts2 => qualifiedIdentifier'(ts2,Ast.LiteralExpr(Ast.LiteralNamespace nd1))
| _ => error ["qualified namespace without double colon"]
end
| (Mult,_) :: _ =>
let
val (ts1, nd1) = (tl ts, Ast.WildcardIdentifier)
in case ts1 of
(DoubleColon, _) :: _ =>
qualifiedIdentifier'(tl ts1,Ast.LexicalRef ({ident=nd1, pos=posOf ts1}))
| _ =>
(trace(["<< simpleQualifiedIdentifier with next=",tokenname(hd(ts1))]);
(ts1,nd1))
end
| _ =>
let
val (ts1, nd1) = identifier(ts)
val id = Ast.Identifier {ident=nd1, openNamespaces=[]}
in case ts1 of
(DoubleColon, _) :: _ =>
qualifiedIdentifier'(tl ts1,Ast.LexicalRef ({ident=id, pos=posOf ts}))
| _ =>
(trace(["<< simpleQualifiedIdentifier with next=",tokenname(hd(ts1))]);
(ts1,id))
end
end
and expressionQualifiedIdentifier (ts) =
let
val (ts1,nd1) = parenListExpression(ts)
in case ts1 of
(DoubleColon, _) :: _ =>
let
val (ts2,nd2) = qualifiedIdentifier'(tl ts1,nd1)
(* todo: make qualifier be an EXPR list *)
in
(ts2,nd2)
end
| _ => error ["unknown form of expression-qualified identifier"]
end
and reservedOrOrdinaryIdentifier ts =
case isreserved(hd ts) of
true => (tl ts, tokenname(hd ts))
| false =>
case ts of
(Mult, _) :: _ => (tl ts, "*")
| _ => identifier(ts)
and reservedIdentifier ts =
case isreserved(hd ts) of
true => (tl ts, tokenname(hd ts))
| false => error ["non-reserved identifier"]
and qualifiedIdentifier' (ts1, nd1) : ((TOKEN * Ast.POS) list * Ast.IDENT_EXPR) =
let val _ = trace([">> qualifiedIdentifier' with next=",tokenname(hd(ts1))])
in case ts1 of
(LeftBracket, _) :: ts =>
let
val (ts2,nd2) = brackets (ts1)
val (ts3,nd3) = (ts2,Ast.QualifiedExpression({qual=nd1,expr=nd2}))
in
(ts3,nd3)
end
| tk :: ts =>
let
val (ts2,nd2) = reservedOrOrdinaryIdentifier(ts1)
val qid = Ast.QualifiedIdentifier({qual=nd1, ident=nd2})
val (ts3,nd3) = (ts2,qid)
in
(ts3,nd3)
end
| _ => error ["empty token stream for qualified identifier"]
end
(*
NonAttributeQualifiedIdentifier
SimpleQualifiedIdentifier
ExpressionQualifiedIdentifier
*)
and nonAttributeQualifiedIdentifier ts =
let val _ = trace([">> nonAttributeQualifiedIdentifier with next=",tokenname(hd(ts))])
in case ts of
(LeftParen, _) :: _ => expressionQualifiedIdentifier(ts)
| _ => simpleQualifiedIdentifier(ts)
end
(*
AttributeIdentifier
@ Brackets
@ NonAttributeQualifiedIdentifier
*)
and attributeIdentifier ts =
let val _ = trace([">> attributeIdentifier with next=",tokenname(hd(ts))])
in case ts of
(At, _) :: (LeftBracket, _) :: _ =>
let
val (ts1,nd1) = brackets(tl ts)
in
(ts1,Ast.AttributeIdentifier (Ast.ExpressionIdentifier { expr = nd1,
openNamespaces = [] }))
end
| (At, _) :: _ =>
let
val (ts1,nd1) = nonAttributeQualifiedIdentifier(tl ts)
in
(ts1,Ast.AttributeIdentifier nd1)
end
| _ =>
error ["unknown form of attribute identifier"]
end
(*
QualifiedIdentifier
AttributeIdentifier
NonAttributeQualifiedIdentifier
*)
and qualifiedIdentifier ts =
let val _ = trace([">> qualifiedIdentifier with next=",tokenname(hd(ts))])
in case ts of
(At, _) :: _ => attributeIdentifier(ts)
| _ => nonAttributeQualifiedIdentifier(ts)
end
(*
TypeIdentifier
SimpleTypeIdentifier
SimpleTypeIdentifier .< TypeExpressionList >
*)
and propertyIdentifier ts =
let val _ = trace([">> propertyIdentifier with next=",tokenname(hd(ts))])
val (ts1,nd1) = nonAttributeQualifiedIdentifier ts
in case ts1 of
(LeftDotAngle, _) :: _ =>
let
val (ts2,nd2) = typeExpressionList (tl ts1)
in case ts2 of
(GreaterThan, _) :: _ =>
(trace(["<< propertyIdentifier with next=",tokenname(hd(tl ts2))]);
(tl ts2,Ast.TypeIdentifier {ident=nd1,typeArgs=nd2}))
| _ => error ["unknown final token of parametric type expression"]
end
| _ =>
(trace(["<< propertyIdentifier with next=",tokenname(hd(ts1))]);
(ts1, nd1))
end
and primaryIdentifier ts =
let val _ = trace([">> primaryIdentifier with next=",tokenname(hd(ts))])
in case ts of
(Identifier _, _) :: (Dot, _) :: _ =>
let
val (ts1,nd1) = path ts
in case ts1 of
(Dot, _) :: _ =>
let
val (ts2,nd2) = propertyIdentifier (tl ts1)
in
(ts2,Ast.UnresolvedPath (nd1,nd2))
end
| _ => LogErr.internalError ["primaryIdentifier"]
end
| _ => propertyIdentifier(ts)
end
and path (ts) : (TOKEN * Ast.POS) list * Ast.IDENT list =
let val _ = trace([">> path with next=", tokenname(hd ts)])
val (ts1,nd1) = identifier ts
in case ts1 of
(Dot, _) :: (Identifier _, _) :: (Dot, _) :: (Identifier _, _) :: _ =>
let
val (ts2,nd2) = path (tl ts1)
in
(ts2,nd1::nd2)
end
| _ =>
let
in
(ts1,nd1::[])
end
end
(*
ParenExpression
( AssignmentExpressionallowLet, allowIn )
*)
and parenExpression ts =
let val _ = trace([">> parenExpression with next=",tokenname(hd(ts))])
in case ts of
(LeftParen, _) :: ts1 =>
let
val (ts2,nd2:Ast.EXPR) = assignmentExpression (ts1,ALLOWLIST,ALLOWIN)
in case ts2 of
(RightParen, _) :: ts3 => (ts3,nd2)
| _ => error ["unknown final token of paren expression"]
end
| _ => error ["unknown initial token of paren expression"]
end
(*
ParenListExpression
( ListExpression(ALLOWIN) )
*)
and parenListExpression (ts) : ((TOKEN * Ast.POS) list * Ast.EXPR) =
let val _ = trace([">> parenListExpression with next=",tokenname(hd(ts))])
in case ts of
(LeftParen, _) :: _ =>
let
val (ts1,nd1) = listExpression (tl ts,ALLOWIN)
val nd1 = case nd1 of
Ast.ListExpr [x] => x
| x => x
in case ts1 of
(RightParen, _) :: _ =>
(trace(["<< parenListExpression with next=",tokenname(hd(ts1))]);
(tl ts1,nd1))
| _ => error ["unknown final token of paren list expression"]
end
| _ => error ["unknown initial token of paren list expression"]
end
(*
FunctionExpression(allowList, b)
function FunctionSignature Block
function FunctionSignature ListExpressionb
function Identifier FunctionSignature Block
function Identifier FunctionSignature ListExpressionb
FunctionExpression(noList, b)
function FunctionSignature Block
function Identifier FunctionSignature Block
*)
and functionExpression (ts,a:alpha,b:beta) =
let val _ = trace([">> functionExpression with next=",tokenname(hd(ts))])
in case ts of
(Function, _) :: ts1 =>
let
in case ts1 of
((LeftDotAngle | LeftParen),_) :: _ =>
let
val (ts3,nd3) = functionSignature ts1
in case (ts3,a) of
((LeftBrace, _) :: _,_) =>
let
val (ts4,nd4) = block (ts3,LOCAL)
in
(ts4,Ast.LiteralExpr
(Ast.LiteralFunction
(Ast.Func {name={kind=Ast.Ordinary,ident=""},
fsig=nd3,
block=nd4,
isNative=false,
defaults=[],
param=([],[]),
ty=functionTypeFromSignature nd3})))
end
| (_,ALLOWLIST) =>
let
val (ts4,nd4) = listExpression (ts3,b)
in
(ts4,Ast.LiteralExpr
(Ast.LiteralFunction
(Ast.Func {name={kind=Ast.Ordinary,ident=""},
fsig=nd3,
block=Ast.Block {pragmas=[],
defns=[],
body=[Ast.ReturnStmt nd4],
head=NONE,
pos=posOf ts3},
isNative=false,
param=([],[]),
defaults=[],
ty=functionTypeFromSignature nd3})))
end
| _ => error ["unknown body form in anonymous function expression"]
end
| _ =>
let
val (ts2,nd2) = identifier ts1
val (ts3,nd3) = functionSignature ts2
in case (ts3,a) of
((LeftBrace, _) :: _,_) =>
let
val (ts4,nd4) = block (ts3,LOCAL)
in
(ts4,Ast.LiteralExpr
(Ast.LiteralFunction
(Ast.Func {name={kind=Ast.Ordinary,ident=nd2},
fsig=nd3,
block=nd4,
isNative=false,
param=([],[]),
defaults=[],
ty=functionTypeFromSignature nd3})))
end
| (_,ALLOWLIST) =>
let
val (ts4,nd4) = listExpression (ts3,b)
in
(ts4,Ast.LiteralExpr
(Ast.LiteralFunction
(Ast.Func
{name={kind=Ast.Ordinary,ident=nd2},
fsig=nd3,
block=Ast.Block
{pragmas=[],
defns=[],
body=[Ast.ReturnStmt nd4],
head=NONE,
pos=posOf ts3},
isNative=false,
param=([],[]),
defaults=[],
ty=functionTypeFromSignature nd3})))
end
| _ => error ["unknown body form in named function expression"]
end
end
| _ => error ["unknown form of function expression"]
end
(*
FunctionSignature
TypeParameters ( Parameters ) ResultType
TypeParameters ( this : TypeIdentifier , Parameters ) ResultType
*)
and needType (nd:Ast.IDENT_EXPR,nullable:bool option) =
case nd of
Ast.Identifier {ident,...} =>
if (ident="*")
then Ast.SpecialType Ast.Any
else if( ident="Object" ) (* FIXME: check for *the* object name *)
then Ast.TypeName nd
else Ast.TypeName nd
| _ => Ast.TypeName nd
and functionSignature (ts) : ((TOKEN * Ast.POS) list * Ast.FUNC_SIG) =
let val _ = trace([">> functionSignature with next=",tokenname(hd(ts))])
val (ts1,nd1) = typeParameters ts
in case ts1 of
(LeftParen, _) :: (This, _) :: (Colon, _) :: _ =>
let
val (ts2,nd2) = primaryIdentifier (tl (tl (tl ts1)))
val temp = Ast.Binding {ident=Ast.ParamIdent 0, ty=Ast.SpecialType Ast.Any}
(* FIXME: what is the type of this? *)
in case ts2 of
(Comma, _) :: _ =>
let
val (ts3,((b,i),e,t),hasRest) = nonemptyParameters (tl ts2) 1 false
in case ts3 of
(RightParen, _) :: _ =>
let
val (ts4,nd4) = resultType (tl ts3)
val thisType = SOME (needType (nd2,SOME false))
in
trace(["<< functionSignature with next=",tokenname(hd ts4)]);
(ts4,Ast.FunctionSignature
{typeParams=nd1,
thisType=thisType,
params=(temp::b,i),
paramTypes=t,
defaults=e,
returnType=nd4,
ctorInits=NONE,
hasRest=hasRest })
end
| _ => error ["unknown token in functionSignature"]
end
| (RightParen, _) :: _ =>
let
val (ts3,nd3) = resultType (tl ts2)
in
trace ["<< functionSignature with next=",tokenname(hd ts3)];
(ts3,Ast.FunctionSignature
{ typeParams=nd1,
thisType=SOME (needType (nd2,SOME false)),
params=([],[]),
paramTypes=[],
defaults=[],
returnType=nd3,
ctorInits=NONE,
hasRest=false})
end
| _ => error ["unknown final token of this-qualified function signature"]
end
| (LeftParen, _) :: _ =>
let
val (ts2,((b,i),e,t),hasRest) = parameters (tl ts1)
in case ts2 of
(RightParen, _) :: _ =>
let
val (ts3,nd3) = resultType (tl ts2)
in
trace ["<< functionSignature with next=",tokenname(hd ts3)];
(ts3,Ast.FunctionSignature
{typeParams=nd1,
params=(b,i),
paramTypes=t,
defaults=e,
returnType=nd3,
ctorInits=NONE,
thisType=NONE, (* todo *)
hasRest=hasRest })
end
| _ => error ["unknown final token of function signature"]
end
| _ => error ["unknown initial token of function signature"]
end
and functionSignatureType (ts) =
let val _ = trace([">> functionSignatureType with next=",tokenname(hd(ts))])
val (ts1,nd1) = typeParameters ts
in case ts1 of
(LeftParen, _) :: (This, _) :: (Colon, _) :: _ =>
let
val (ts2,nd2) = primaryIdentifier (tl (tl (tl ts1)))
in case ts2 of
(Comma, _) :: _ =>
let
val (ts3,(d,t)) = nonemptyParametersType (tl ts2)
in case ts3 of
(RightParen, _) :: _ =>
let
val (ts4,nd4) = resultType (tl ts3)
in
trace ["<< functionSignature with next=",tokenname(hd ts4)];
(ts4,Ast.FunctionSignature
{ typeParams=nd1,
thisType=SOME (needType (nd2,SOME false)),
params=([],[]),
paramTypes=t,
defaults=d,
returnType=nd4,
ctorInits=NONE,
hasRest=false }) (* do we need this *)
end
| _ => error ["unknown token in functionSignatureType"]
end
| (RightParen, _) :: _ =>
let
val (ts3,nd3) = resultType (tl ts2)
in
trace ["<< functionSignature with next=",tokenname(hd ts3)];
(ts3,Ast.FunctionSignature
{ typeParams=nd1,
thisType=SOME (needType (nd2,SOME false)),
params=([],[]),
paramTypes=[],
defaults=[],
returnType=nd3,
ctorInits=NONE,
hasRest=false }) (* do we need this *)
end
| _ => error ["unknown token in functionSignatureType"]
end
| (LeftParen, _) :: _ =>
let
val (ts2, (d,t)) = parametersType (tl ts1)
in case ts2 of
(RightParen, _) :: _ =>
let
val (ts3,nd3) = resultType (tl ts2)
in
trace ["<< functionSignature with next=",tokenname(hd ts3)];
(ts3,Ast.FunctionSignature
{ typeParams=nd1,
params=([],[]),
paramTypes=t,
defaults=d,
returnType=nd3,
ctorInits=NONE,
thisType=NONE, (* todo *)
hasRest=false }) (* do we need this *)
end
| _ => error ["unknown token in functionSignatureType"]
end
| _ => error ["unknown token in functionSignatureType"]
end
(*
TypeParameters
empty
.< TypeParameterList >
*)
and typeParameters ts =
let val _ = trace([">> typeParameters with next=",tokenname(hd(ts))])
in case ts of
(LeftDotAngle, _) :: _ =>
let
val (ts1,nd1) = typeParameterList (tl ts)
in case ts1 of
(GreaterThan, _) :: _ =>
let
in
trace(["<< typeParameters with next=",tokenname(hd(tl ts1))]);
(tl ts1,nd1)
end
| _ => error ["unknown token in typeParameters"]
end
| _ =>
(trace(["<< typeParameters with next=",tokenname(hd(ts))]);
(ts,[]))
end
(*
TypeParametersList
Identifier
Identifier , TypeParameterList