-
Notifications
You must be signed in to change notification settings - Fork 1
/
as3parser.ml
1240 lines (1128 loc) · 40.4 KB
/
as3parser.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
(* * Haxe Compiler * Copyright (c)2005 Nicolas Cannasse * * This program *)
(* is free software; you can redistribute it and/or modify * it under the *)
(* terms of the GNU General Public License as published by * the Free *)
(* Software Foundation; either version 2 of the License, or * (at your *)
(* option) any later version. * * This program is distributed in the hope *)
(* that it will be useful, * but WITHOUT ANY WARRANTY; without even the *)
(* implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR *)
(* PURPOSE. See the * GNU General Public License for more details. * * You *)
(* should have received a copy of the GNU General Public License * along *)
(* with this program; if not, write to the Free Software * Foundation, *)
(* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *)
(* Patrick Le Clec'h trying to implement a mixed as3 parser and haxe *)
(* done parse package XXX {} syntax *)
(* done insert super call *)
(* done variable hoisting *)
(* done check duplicate var definition *)
(* done check constant reassignment *)
(* done add public keyword, private by default *)
(* done add metadata parsing [] *)
open Ast
open Common
type error_msg =
| Unexpected of token
| Duplicate_default
| Missing_semicolon
| Unclosed_macro
| Unimplemented
| Missing_type
| Expecting of token
| Custom of string
exception Error of error_msg * pos
exception TypePath of string list * string option
exception Display of expr
let error_msg = function
| Unexpected t -> "Unexpected "^(s_token t)
| Duplicate_default -> "Duplicate default"
| Missing_semicolon -> "Missing ;"
| Unclosed_macro -> "Unclosed macro"
| Unimplemented -> "Not implemented for current platform"
| Missing_type -> "Missing type declaration"
| Expecting t -> "Expecting "^(s_token t)
| Custom s -> s
let warning:(string -> pos -> unit) ref = ref (fun _ _ -> assert false)
let error m p = raise (Error (m, p))
let display_error : (error_msg -> pos -> unit) ref =
ref (fun _ _ -> assert false)
let quoted_ident_prefix = "@$__hx__"
let quote_ident s =
try
for i = 0 to String.length s - 1 do
match String.unsafe_get s i with
| 'a'..'z' | 'A'..'Z' | '_' -> ()
| '0'..'9' when i > 0 -> ()
| _ -> raise Exit
done;
if Hashtbl.mem Lexer.keywords s then raise Exit;
s
with Exit ->
quoted_ident_prefix ^ s
let cache = ref (DynArray.create())
let doc = ref None
let use_doc = ref false
let resume_display = ref null_pos
let last_token s =
let n = Stream.count s in
DynArray.get (!cache) (if n = 0 then 0 else n - 1)
let expect_char_pos t =
let pos = snd t in { pfile = pos.pfile; pmin = pos.pmax; pmax = pos.pmax }
let serror() = raise (Stream.Error "")
let do_resume() = !resume_display <> null_pos
let display e = raise (Display e)
let is_resuming p =
let p2 = !resume_display in
p.pmax = p2.pmin && String.lowercase (Common.get_full_path p.pfile) = String.lowercase p2.pfile
let precedence op =
let left = true and right = false in
match op with
| OpMod -> 0, left
| OpMult | OpDiv -> 1, left
| OpAdd | OpSub -> 2, left
| OpShl | OpShr | OpUShr -> 3, left
| OpOr | OpAnd | OpXor -> 4, left
| OpEq | OpNotEq | OpGt | OpLt | OpGte | OpLte -> 5, left
| OpInterval -> 6, left
| OpBoolAnd -> 7, left
| OpBoolOr -> 8, left
| OpAssign | OpAssignOp _ -> 9, right
let is_not_assign = function
| OpAssign | OpAssignOp _ -> false
| _ -> true
let swap op1 op2 =
let p1, left1 = precedence op1 in
let p2, _ = precedence op2 in
left1 && p1 <= p2
let rec make_binop op e ((v, p2) as e2) =
match v with
| EBinop (_op, _e, _e2) when swap op _op ->
let _e = make_binop op e _e in
EBinop (_op, _e, _e2) , punion (pos _e) (pos _e2)
| ETernary (e1, e2, e3) when is_not_assign op ->
let e = make_binop op e e1 in
ETernary (e, e2, e3) , punion (pos e) (pos e3)
| _ ->
EBinop (op, e, e2) , punion (pos e) (pos e2)
let rec make_unop op ((v, p2) as e) p1 =
match v with
| EBinop (bop, e, e2) -> EBinop (bop, make_unop op e p1 , e2) , (punion p1 p2)
| ETernary (e1, e2, e3) -> ETernary (make_unop op e1 p1 , e2, e3), punion p1 p2
| _ ->
EUnop (op, Prefix, e), punion p1 p2
let popt f = parser
| [< v = f >] -> Some v
| [< >] -> None
let rec plist f = parser
| [< v = f; l = plist f >] -> v :: l
| [< >] -> []
let rec psep sep f = parser
| [< v = f; s >] ->
let rec loop = parser
| [< '(sep2, _) when sep2 = sep; v = f; l = loop >] -> v :: l
| [< >] -> []
in
v :: loop s
| [< >] -> []
let ident = parser
| [< '(Const (Ident i), _) >] -> i
let any_ident = parser
| [< '(Const (Ident i), p) >] -> i, p
| [< '(Const (Type t), p) >] -> t, p
let property_ident = parser
| [< i, _ = any_ident >] -> i
| [< '(Kwd Dynamic, _) >] -> "dynamic"
| [< '(Kwd Default, _) >] -> "default"
let log m s =
prerr_endline m
let get_doc s =
let d = !doc in
doc := None;
d
let comma = parser
| [< '(Comma, _) >] -> ()
let semicolon s =
if fst (last_token s) = BrClose then
match s with parser
| [< '(Semicolon, p) >] -> p
| [< >] -> snd (last_token s)
else
match s with parser
| [< '(Semicolon, p) >] -> p
| [< s >] ->
let pos = snd (last_token s) in
if do_resume() then pos else error Missing_semicolon pos
let optsemicolon s =
if fst (last_token s) = BrClose then
match s with parser
| [< '(Semicolon, p) >] -> p
| [< >] -> snd (last_token s)
else
match s with parser
| [< '(Semicolon, p) >] -> p
| [< '(Eof, p) >] -> p
| [< s >] -> match Stream.peek s with
| Some(tk) ->
print_endline(s_token (fst tk));
let pos = snd (last_token s) in
if fst tk = BrClose then
pos
else begin
let next_pos = snd tk in
let file = Lexer.get_file_from_pos pos in
let line, _ = Lexer.find_line pos.pmax file in
let next_line, _ = Lexer.find_line next_pos.pmin file in
if (line == next_line) then
if do_resume() then pos else error Missing_semicolon pos
else
pos
end
| None ->
let pos = snd (last_token s) in
if do_resume() then pos else error Missing_semicolon pos
let expect_token t = parser
| [< '(v, p) when v == t >] -> p
| [< s >] ->
let pos = expect_char_pos (last_token s) in
if do_resume() then pos else error (Expecting t) pos
let rename_constructor cl_name fn_name =
if fn_name = cl_name then "new"
else fn_name
let make_ident n p = (EConst(Ident n), p)
let make_asn i v p = (EBinop(OpAssign, i, v), p)
let make_call n a p = (ECall(make_ident n p, a), p)
let make_type_path s =
{
tpackage = [];
tname = s;
tparams = [];
tsub = None;
}
let need_super hl = match hl with
| HExtends _ :: _ -> true
| _ -> false
let set_const i = i lor 1
let is_const i = (i land 1) = 1
let defined i = (i land 6) lsr 1
let inc_defined i = i lor (((i lsl 1) lor 2) land 6)
let assigned i = (i land 24) lsr 3
let inc_assigned i = i lor (((i lsl 1) lor 8) land 24)
let default_value ht k v = try Hashtbl.find ht k with Not_found -> v
let split_def_asn ?(const=false) lv p ht =
let rec loop lv def asn = match lv with
| [] -> def, asn
| (s, t, e) as x :: tl ->
let i = inc_defined(default_value ht s 0) in
match e with
| None ->
let def =
if (defined i) > 1 then begin
if (const || is_const i) then
error (Custom ("Constant redefinition " ^ s)) p
else
!warning ("Duplicate definition " ^ s) p;
def
end else
x :: def
in
Hashtbl.add ht s (if const then set_const i else i);
loop tl def asn
| Some(v) ->
let def =
if (defined i) > 1 then begin
if (const || is_const i) then
error (Custom ("Constant redefinition " ^ s)) p
else
!warning ("Duplicate definition " ^ s) p;
def
end else
(s, t, None) :: def
in
let i = inc_assigned i in
Hashtbl.add ht s (if const then set_const i else i);
loop tl def ((make_asn (make_ident s p) v p) :: asn)
in loop lv [] []
let hoist ?(need_super=false) e =
let ht = Hashtbl.create 0 in
match e with
| (EBlock l,p) ->
let le,v,hs =
(let rec loop le acc h_acc super = match le with
| (EBlock l,p) :: tl ->
let le2, v2, hs2 = loop l [] h_acc super in
loop tl ((EBlock le2, p) :: acc) v2 hs2
| (EVars evl, p) :: tl ->
let d,a = split_def_asn evl p ht in
loop tl (a@acc) ( (EVars d,p) :: h_acc) super
| (EVals evl, p) :: tl ->
let d,a = split_def_asn ~const:true evl p ht in
loop tl (a@acc) ( (EVars d,p) :: h_acc) super
| (EBinop(OpAssign, (EConst(Ident n), p2) , v), p1) as x :: tl ->
let i = default_value ht n 0 in
if (is_const i) then error (Custom ("Constant already setted " ^ n)) p1;
loop tl (x :: acc) h_acc super
| (ECall( (EConst(Ident "super"), _) , _), _) as x :: tl ->
loop tl (x :: acc) h_acc true
| hl :: tl -> loop tl (hl :: acc) h_acc super
| [] -> (List.rev acc), h_acc, super
in loop l [] [] (not need_super))
in
if (hs) then
(EBlock (List.rev_append v le), p)
else
(EBlock ((make_call "super" [] p) :: (List.rev_append v le)), p)
| _ -> e
let rec parse_file s =
doc := None;
let compat = {
tpackage = ["asx"];
tname = "Compat";
tparams = [];
tsub = Some "UsesCompat";
}
in
match s with parser
| [< '(Kwd Package, _); p = parse_package; bp = expect_token BrOpen; l = parse_type_decls [(EUsing compat, bp)]; _ = expect_token BrClose; '(Eof, _) >] -> p , l
| [< l = parse_type_decls []; '(Eof, _) >] -> [] , l
and parse_type_decls acc s =
try
match s with parser
| [< v = parse_type_decl; l = parse_type_decls (v :: acc) >] -> l
| [< >] -> List.rev acc
with (TypePath ([], Some name)) as e ->
(* resolve imports *)
List.iter (fun d ->
match fst d with
| EImport t when (t.tsub = None && t.tname = name) -> raise (TypePath (t.tpackage, Some t.tname))
| _ -> ()
) acc;
raise e
and parse_type_decl s =
match s with parser
| [< '(Kwd Import, p1); t = parse_type_path; p2 = optsemicolon >] -> EImport t, punion p1 p2
| [< '(Kwd Using, p1); t = parse_type_path; p2 = optsemicolon >] -> EUsing t, punion p1 p2
| [< meta = parse_meta; c = parse_common_flags; s >] ->
match s with parser
| [< n , p1 = parse_enum_flags; doc = get_doc; name = type_name; tl = parse_constraint_params; '(BrOpen, _); l = plist parse_enum; '(BrClose, p2) >] ->
(EEnum {
d_name = name;
d_doc = doc;
d_meta = meta;
d_params = tl;
d_flags = List.map snd c @ n;
d_data = l
}, punion p1 p2)
| [< n , p1 = parse_class_flags; doc = get_doc; name = type_name; tl = parse_constraint_params; hl = psep Comma parse_class_herit; '(BrOpen, _); fl, p2 = parse_class_fields ~cl_name: name ~need_super: (need_super hl) false p1 >] ->
let rec loop l acc ns_def =
match l with
| [] -> List.rev acc, ns_def
| (HImplements {tname="ns::public"}, EExtern) :: tl -> loop tl acc true
| (HPrivate, _) as x :: tl -> loop tl (x :: acc) true
| x :: tl -> loop tl (x :: acc) ns_def
in let c, nd = loop c [] false in
let c =
if (nd) then c
else (HPrivate, EPrivate) :: c
in
(EClass {
d_name = name;
d_doc = doc;
d_meta = meta;
d_params = tl;
d_flags = List.map fst c @ n @ hl;
d_data = fl;
}, punion p1 p2)
| [< '(Kwd Typedef, p1); doc = get_doc; name = type_name; tl = parse_constraint_params; '(Binop OpAssign, p2); t = parse_complex_type; s >] ->
(match s with parser
| [< '(Semicolon, _) >] -> ()
| [< >] -> ());
(ETypedef {
d_name = name;
d_doc = doc;
d_meta = meta;
d_params = tl;
d_flags = List.map snd c;
d_data = t;
}, punion p1 p2)
and parse_package s = psep Dot ident s
and parse_class_fields ?(cl_name ="") ?(need_super=false) tdecl p1 s =
let l = parse_class_field_resume ~cl_name: cl_name ~need_super: need_super tdecl s in
let p2 = (match s with parser
| [< '(BrClose, p2) >] -> p2
| [< >] ->
if do_resume() then p1 else serror()
) in
l, p2
and parse_class_field ?(cl_name ="") ?(need_super=false) s =
doc := None;
match s with parser
| [< meta = parse_meta; al = parse_cf_rights true []; doc = get_doc; s >] ->
let name, pos, k = (match s with parser
| [< '(Kwd Var, p1); name, _ = any_ident; s >] ->
(match s with parser
| [< '(POpen, _); i1 = property_ident; '(Comma, _); i2 = property_ident; '(PClose, _); '(DblDot, _); t = parse_complex_type >] ->
let e , p2 = (match s with parser
| [< '(Binop OpAssign, _); e = toplevel_expr; p2 = semicolon >] -> Some e , p2
| [< '(Semicolon, p2) >] -> None , p2
| [< >] -> serror()
) in
name, punion p1 p2, FProp (i1, i2, t, e)
| [< t = parse_type_opt; s >] ->
let e , p2 = (match s with parser
| [< '(Binop OpAssign, _); e = toplevel_expr; p2 = semicolon >] -> Some e , p2
| [< '(Semicolon, p2) >] -> None , p2
| [< >] -> serror()
) in
name, punion p1 p2, FVar (t, e))
| [< '(Kwd Function, p1); name = parse_fun_name; pl = parse_constraint_params; '(POpen, _); al = psep Comma parse_fun_param; '(PClose, _); t = parse_type_opt; s >] ->
let e, p2, name = (match s with parser
| [< e = toplevel_expr >] ->
let name = rename_constructor cl_name name in
let p = pos e in
let e =
if name ="new" && need_super then
hoist ~need_super:true e
else
hoist e
in Some e, p, name
| [< '(Semicolon, p) >] ->
None, p, rename_constructor cl_name name
| [< >] -> serror()
) in
let f = {
f_params = pl;
f_args = al;
f_type = t;
f_expr = e;
} in
name, punion p1 p2, FFun f
| [< >] ->
if al = [] then raise Stream.Failure else serror()
) in let al = (match k with
| FFun _ when (al =[] && name ="new") -> [APublic]
| _ -> al
)
in
{
cff_name = name;
cff_doc = doc;
cff_meta = meta;
cff_access = al;
cff_pos = pos;
cff_kind = k;
}
and parse_class_field_resume ?(cl_name ="") ?(need_super = false) tdecl s =
if not (do_resume()) then
plist (parse_class_field ~cl_name: cl_name ~need_super: need_super) s
else try
let c = parse_class_field ~cl_name: cl_name ~need_super: need_super s in
c :: parse_class_field_resume ~cl_name: cl_name ~need_super: need_super tdecl s
with Stream.Error _ | Stream.Failure ->
(* look for next variable/function or next type declaration *)
let rec junk k =
if k <= 0 then () else begin
Stream.junk s;
junk (k - 1);
end
in
(* walk back tokens which are prefixing a type/field declaration *)
let rec junk_tokens k =
if k = 0 then
()
else match List.rev_map fst (Stream.npeek k s) with
| Kwd Private :: _ -> junk_tokens (k - 1)
| (Const (Ident _ | Type _) | Kwd _) :: DblDot :: At :: l
| (Const (Ident _ | Type _) | Kwd _) :: At :: l ->
junk_tokens (List.length l)
| PClose :: l ->
(* count matching parenthesises for metadata call *)
let rec loop n = function
| [] -> []
| POpen :: l -> if n = 0 then l else loop (n - 1) l
| PClose :: l -> loop (n + 1) l
| _ :: l -> loop n l
in
(match loop 0 l with
| (Const (Ident _ | Type _) | Kwd _) :: At :: l
| (Const (Ident _ | Type _) | Kwd _) :: DblDot :: At :: l -> junk_tokens (List.length l)
| _ ->
junk k)
| _ ->
junk k
in
let rec loop k =
match List.rev_map fst (Stream.npeek k s) with
(* field declaration *)
| Const _ :: Kwd Function :: _
| Kwd New :: Kwd Function :: _ ->
junk_tokens (k - 2);
parse_class_field_resume ~cl_name: cl_name tdecl ~need_super: need_super s
| Kwd Public :: _ | Kwd Static :: _ | Kwd Var :: _ | Kwd Override :: _ | Kwd Dynamic :: _ | Kwd Inline :: _ ->
junk_tokens (k - 1);
parse_class_field_resume ~cl_name: cl_name ~need_super: need_super tdecl s
| BrClose :: _ when tdecl ->
junk_tokens (k - 1);
[]
(* type declaration *)
| Eof :: _ | Kwd Import :: _ | Kwd Using :: _ | Kwd Extern :: _ | Kwd Class :: _ | Kwd Interface :: _ | Kwd Enum :: _ | Kwd Typedef :: _ ->
junk_tokens (k - 1);
[]
| [] ->
[]
| _ ->
loop (k + 1)
in
loop 1
and parse_common_flags = parser
| [< '(Kwd Private, _); l = parse_common_flags >] -> (HPrivate, EPrivate) :: l
| [< '(Kwd Extern, _); l = parse_common_flags >] -> (HExtern, EExtern) :: l
| [< '(Kwd Public, _); l = parse_common_flags >] -> (HImplements (make_type_path "ns::public"), EExtern) :: l
| [< >] -> []
and parse_meta = parser
| [< '(At, _); name, p = meta_name; s >] ->
(match s with parser
| [< '(POpen, _); params = psep Comma expr; '(PClose, _); s >] -> (name, params, p) :: parse_meta s
| [< >] -> (name,[], p) :: parse_meta s)
| [< '(BkOpen, p); params = psep Comma expr; '(BkClose, _); s >] -> (List.map (fun x-> (":meta", [x], p)) params) @ parse_meta s
| [< >] -> []
and meta_name = parser
| [< '(Const (Ident i), p) >] -> i, p
| [< '(Const (Type t), p) >] -> t, p
| [< '(Kwd k, p) >] -> s_keyword k, p
| [< '(DblDot, _); s >] -> let n, p = meta_name s in ":" ^ n, p
and parse_enum_flags = parser
| [< '(Kwd Enum, p) >] -> [] , p
and parse_class_flags = parser
| [< '(Kwd Class, p) >] -> [] , p
| [< '(Kwd Interface, p) >] -> [HInterface] , p
and parse_type_opt = parser
| [< '(DblDot, _); t = parse_complex_type >] -> Some t
| [< >] -> None
and parse_complex_type s =
let t = parse_complex_type_inner s in
parse_complex_type_next t s
and parse_complex_type_inner = parser
| [< '(POpen, _); t = parse_complex_type; '(PClose, _) >] -> CTParent t
| [< '(BrOpen, p1); s >] ->
(match s with parser
| [< l = parse_type_anonymous false >] -> CTAnonymous l
| [< '(Binop OpGt, _); t = parse_type_path; '(Comma, _); s >] ->
(match s with parser
| [< l = parse_type_anonymous false >] -> CTExtend (t, l)
| [< l, _ = parse_class_fields true p1 >] -> CTExtend (t, l)
| [< >] -> serror())
| [< l, _ = parse_class_fields true p1 >] -> CTAnonymous l
| [< >] -> serror())
| [< '(Question, _); t = parse_complex_type_inner >] ->
CTOptional t
| [< t = parse_type_path >] ->
CTPath t
and parse_type_path s = parse_type_path1 [] s
(*
and parse_type_path1_hx pack = parser
| [< '(Const (Ident name),p); s >] ->
(match s with parser
| [< '(Dot,p) >] ->
if is_resuming p then
raise (TypePath (List.rev (name :: pack),None))
else
parse_type_path1_hx (name :: pack) s
| [< '(Semicolon,_) >] ->
error (Custom "Type name should start with an uppercase letter") p
| [< >] -> serror());
| [< '(Const (Type name),_); s >] ->
let sub = (match s with parser
| [< '(Dot,p); s >] ->
(if is_resuming p then
raise (TypePath (List.rev pack,Some name))
else match s with parser
| [< '(Const (Type name),_) >] -> Some name
| [< >] -> serror())
| [< >] -> None
) in
let params = (match s with parser
| [< '(Binop OpLt,_); l = psep Comma parse_type_path_or_const; '(Binop OpGt,_) >] -> l
| [< >] -> []
) in
{
tpackage = List.rev pack;
tname = name;
tparams = params;
tsub = sub;
}
*)
(* since as3 class name can be written in lowercase just use a : to mark a subclass *)
(* as subclass is not supported it's not a problem, and allow to use haxe subclass feature *)
(* import zzz.yyy.myclass:subclass *)
and parse_type_dot_path1 pack s = match s with parser
| [< '(Dot, p); s >] ->
if is_resuming p then
raise (TypePath (List.rev pack, None))
else
(match s with parser
| [< '(Const (Type name), _); s >] -> parse_type_dot_path1 (name :: pack) s
| [< '(Const (Ident name),_); s >] -> parse_type_dot_path1 (name :: pack) s
| [< s >] -> pack, None, s)
| [< '(DblDot, p); s >] ->
if is_resuming p then
raise (TypePath (List.rev pack, None))
else
(match s with parser
| [< '(Const (Type name), _); s >] -> pack, Some name, s
| [< '(Const (Ident name),_); s >] -> pack, Some name, s
| [< s >] -> pack, None, s)
| [< >] -> pack, None, s
and parse_type_path1 pack = parser
| [< '(Const (Type name), p); s >] ->
let pack, sub, s = parse_type_dot_path1 (name::pack) s in
let pack, name = match pack with
| hd::tl -> tl, hd
| _ -> serror()
in
let name = match name with
| "int" -> "Int"
| "uint" -> "UInt"
| "float" ->"Float"
| "void" -> "Void"
| _ -> name
in
let params = (match s with parser
| [< '(Binop OpLt, _); l = psep Comma parse_type_path_or_const; '(Binop OpGt, _) >] -> l
| [< >] -> []
) in
{
tpackage = List.rev pack;
tname = name;
tparams = params;
tsub = sub;
}
| [< '(Const (Ident name), p); pack, sub, s = parse_type_dot_path1 (name::pack) >] ->
let pack, name = match pack with
| hd::tl -> tl, hd
| _ -> serror()
in
let name = match name with
| "int" -> "Int"
| "uint" -> "UInt"
| "float" -> "Float"
| "void" -> "Void"
| _ -> name
in
let params = (match s with parser
| [< '(Binop OpLt, _); l = psep Comma parse_type_path_or_const; '(Binop OpGt, _) >] -> l
| [< >] -> []
) in
{
tpackage = List.rev pack;
tname = name;
tparams = params;
tsub = sub;
}
and type_name = parser
| [< '(Const (Type name), _) >] -> name
| [< '(Const (Ident name), p) >] ->
error (Custom "Type name should start with an uppercase letter") p
and parse_type_path_or_const = parser
| [< '(POpen, _); e = expr; '(PClose, _) >] -> TPExpr e
| [< t = parse_complex_type >] -> TPType t
| [< '(Const c, p) >] -> TPExpr (EConst c, p)
| [< e = expr >] -> TPExpr e
and parse_complex_type_next t = parser
| [< '(Arrow, _); t2 = parse_complex_type >] ->
(match t2 with
| CTFunction (args, r) ->
CTFunction (t :: args, r)
| _ ->
CTFunction ([t] , t2))
| [< >] -> t
and parse_type_anonymous opt = parser
| [< '(Question, _) when not opt; s >] -> parse_type_anonymous true s
| [< name, p1 = any_ident; '(DblDot, _); t = parse_complex_type; s >] ->
let next p2 acc =
let t = if not opt then t else (match t with
| CTPath { tpackage = []; tname = "Null" } -> t
| _ -> CTPath { tpackage = []; tname = "Null"; tsub = None; tparams = [TPType t] }
) in
{
cff_name = name;
cff_meta = if opt then [":optional",[], p1] else [];
cff_access = [];
cff_doc = None;
cff_kind = FVar (Some t, None);
cff_pos = punion p1 p2;
} :: acc
in
match s with parser
| [< '(BrClose, p2) >] -> next p2 []
| [< '(Comma, p2) >] ->
(match s with parser
| [< '(BrClose, _) >] -> next p2 []
| [< l = parse_type_anonymous false >] -> next p2 l
| [< >] -> serror());
| [< >] -> serror()
and parse_enum s =
doc := None;
let meta = parse_meta s in
match s with parser
| [< name, p1 = any_ident; doc = get_doc; s >] ->
match s with parser
| [< '(POpen, _); l = psep Comma parse_enum_param; '(PClose, _); p = semicolon; >] -> (name, doc, meta, l, punion p1 p)
| [< '(Semicolon, p) >] -> (name, doc, meta,[], punion p1 p)
| [< >] -> serror()
and parse_enum_param = parser
| [< '(Question, _); name, _ = any_ident; '(DblDot, _); t = parse_complex_type >] -> (name, true, t)
| [< name, _ = any_ident; '(DblDot, _); t = parse_complex_type >] -> (name, false, t)
and parse_cf_rights allow_static l = parser
| [< '(Kwd Static, _) when allow_static; l = parse_cf_rights false (AStatic :: l) >] -> l
| [< '(Kwd Public, _) when not(List.mem APublic l || List.mem APrivate l); l = parse_cf_rights allow_static (APublic :: l) >] -> l
| [< '(Kwd Private, _) when not(List.mem APublic l || List.mem APrivate l); l = parse_cf_rights allow_static (APrivate :: l) >] -> l
| [< '(Kwd Override, _) when not (List.mem AOverride l); l = parse_cf_rights false (AOverride :: l) >] -> l
| [< '(Kwd Dynamic, _) when not (List.mem ADynamic l); l = parse_cf_rights allow_static (ADynamic :: l) >] -> l
| [< '(Kwd Inline, _); l = parse_cf_rights allow_static (AInline :: l) >] -> l
| [< >] -> l
and parse_fun_name = parser
| [< '(Const (Ident name), _) >] -> name
| [< '(Const (Type name), _) >] -> name
| [< '(Kwd New, _) >] -> "new"
and parse_fun_param = parser
| [< '(Question, _); name, _ = any_ident; t = parse_type_opt; c = parse_fun_param_value >] -> (name, true, t, c)
| [< name, _ = any_ident; t = parse_type_opt; c = parse_fun_param_value >] -> (name, false, t, c)
and parse_fun_param_value = parser
| [< '(Binop OpAssign, _); e = expr >] -> Some e
| [< >] -> None
and parse_fun_param_type = parser
| [< '(Question, _); name = any_ident; '(DblDot, _); t = parse_complex_type >] -> (name, true, t)
| [< name = any_ident; '(DblDot, _); t = parse_complex_type >] -> (name, false, t)
and parse_constraint_params = parser
| [< '(Binop OpLt, _); l = psep Comma parse_constraint_param; '(Binop OpGt, _) >] -> l
| [< >] -> []
and parse_constraint_param = parser
| [< name = type_name; s >] ->
match s with parser
| [< '(DblDot, _); s >] ->
(match s with parser
| [< '(POpen, _); l = psep Comma parse_complex_type; '(PClose, _) >] -> (name, l)
| [< t = parse_complex_type >] -> (name,[t])
| [< >] -> serror())
| [< >] -> (name,[])
and parse_class_herit = parser
| [< '(Kwd Extends, _); t = parse_type_path >] -> HExtends t
| [< '(Kwd Implements, _); t = parse_type_path >] -> HImplements t
and block1 = parser
| [< '(Const (Ident "const"), p1); vl = psep Comma parse_var_decl; p2 = optsemicolon; b = block [(EVals vl, punion p1 p2)] >] -> EBlock b
| [< '(Const (Ident name), p); s >] -> block2 name (Ident name) p s
| [< '(Const (Type name), p); s >] -> block2 name (Type name) p s
| [< '(Const (String name), p); s >] -> block2 (quote_ident name) (String name) p s
| [< b = block [] >] -> EBlock b
and block2 name ident p = parser
| [< '(DblDot, _); e = expr; l = parse_obj_decl >] -> EObjectDecl ((name, e) :: l)
| [< e = expr_next (EConst ident, p); s >] ->
try
let _ = optsemicolon s in
let b = block [e] s in
EBlock b
with
| Error (err, p) ->
(!display_error) err p;
EBlock (block [e] s)
and block acc s =
try
(* because of inner recursion, we can't put Display handling in errors *)
(* below *)
let e = try parse_block_elt s with Display e -> display (EBlock (List.rev (e :: acc)), snd e) in
block (e :: acc) s
with
| Stream.Failure ->
List.rev acc
| Stream.Error _ ->
let tk , pos = (match Stream.peek s with None -> last_token s | Some t -> t) in
(!display_error) (Unexpected tk) pos;
block acc s
| Error (e, p) ->
(!display_error) e p;
block acc s
and parse_block_elt = parser
| [< '(Kwd Var, p1); vl = psep Comma parse_var_decl; p2 = optsemicolon >] -> (EVars vl, punion p1 p2)
| [< '(Const (Ident "const"), p1); vl = psep Comma parse_var_decl; p2 = optsemicolon >] -> (EVals vl, punion p1 p2)
| [< e = expr; _ = optsemicolon >] -> e
and parse_init_for_block_elt = parser
| [< '(Kwd Var, p1); vl = psep Comma parse_var_decl; _ = semicolon >] -> Some (EVars vl, p1)
| [< '(Const (Ident "const"), p1); vl = psep Comma parse_var_decl; _ = semicolon >] -> Some (EVals vl, p1)
| [< e = expr ; _ = semicolon>] -> Some e
| [< _ = semicolon >] -> None
and parse_for_expr = parser
| [< e = expr; _ = semicolon >] -> Some e
| [<_ = semicolon >] -> None
and parse_last_for_expr = parser
| [< e = expr; '(PClose, _) >] -> Some e
| [< '(PClose, _) >] -> None
and parse_obj_decl = parser
| [< '(Comma, _); s >] ->
(match s with parser
| [< name, _ = any_ident; '(DblDot, _); e = expr; l = parse_obj_decl >] -> (name, e) :: l
| [< '(Const (String name), _); '(DblDot, _); e = expr; l = parse_obj_decl >] -> (quote_ident name, e) :: l
| [< >] -> [])
| [< >] -> []
and parse_array_decl = parser
| [< e = expr; s >] ->
(match s with parser
| [< '(Comma, _); l = parse_array_decl >] -> e :: l
| [< >] -> [e])
| [< >] ->
[]
and parse_var_decl ?(is_const=false) = parser
| [< name, _ = any_ident; t = parse_type_opt; s >] ->
match s with parser
| [< '(Binop OpAssign, _); e = expr >] -> (name, t, Some e)
| [< >] -> (name, t, None)
and expr = parser
| [< '(BrOpen, p1); b = block1; '(BrClose, p2); s >] ->
let e = (b, punion p1 p2) in
(match b with
| EObjectDecl _ -> expr_next e s
| _ -> e)
| [< '(Const c, p); s >] -> expr_next (EConst c, p) s
| [< '(Kwd This, p); s >] -> expr_next (EConst (Ident "this"), p) s
| [< '(Kwd Callback, p); s >] -> expr_next (EConst (Ident "callback"), p) s
| [< '(Kwd Cast, p1); s >] ->
(match s with parser
| [< '(POpen, _); e = expr; s >] ->
(match s with parser
| [< '(Comma, _); t = parse_complex_type; '(PClose, p2); s >] -> expr_next (ECast (e, Some t), punion p1 p2) s
| [< '(PClose, p2); s >] -> expr_next (ECast (e, None), punion p1 (pos e)) s
| [< >] -> serror())
| [< e = secure_expr >] -> expr_next (ECast (e, None), punion p1 (pos e)) s)
| [< '(Kwd Throw, p); e = expr >] -> (EThrow e, p)
| [< '(Kwd New, p1); t = parse_type_path; '(POpen, p); s >] ->
if is_resuming p then display (EDisplayNew t, punion p1 p);
(match s with parser
| [< al = psep Comma expr; '(PClose, p2); s >] -> expr_next (ENew (t, al), punion p1 p2) s
| [< >] -> serror())
| [< '(POpen, p1); e = expr; '(PClose, p2); s >] -> expr_next (EParenthesis e, punion p1 p2) s
| [< '(BkOpen, p1); l = parse_array_decl; '(BkClose, p2); s >] -> expr_next (EArrayDecl l, punion p1 p2) s
| [< '(Kwd Function, p1); name = popt any_ident; pl = parse_constraint_params; '(POpen, _); al = psep Comma parse_fun_param; '(PClose, _); t = parse_type_opt; s >] ->
let make e =
let f = {
f_params = pl;
f_type = t;
f_args = al;
f_expr = Some e;
} in
EFunction ((match name with None -> None | Some (name, _) -> Some name), f), punion p1 (pos e)
in
(try
expr_next (make (secure_expr s)) s
with
Display e -> display (make e))
| [< '(Unop op, p1) when is_prefix op; e = expr >] -> make_unop op e p1
| [< '(Binop OpSub, p1); e = expr >] ->
let neg s =
if s.[0] = '-' then String.sub s 1 (String.length s - 1) else "-" ^ s
in
(match make_unop Neg e p1 with
| EUnop (Neg, Prefix, (EConst (Int i), pc)), p -> EConst (Int (neg i)), p
| EUnop (Neg, Prefix, (EConst (Float j), pc)), p -> EConst (Float (neg j)), p
| e -> e)
(* /* removed unary + : this cause too much syntax errors go unnoticed, *)
(* such as "a + + 1" (missing 'b') without adding anything to the language *)
(* | [< '(Binop OpAdd,p1); s >] -> (match s with parser | [< '(Const (Int *)
(* i),p); e = expr_next (EConst (Int i),p) >] -> e | [< '(Const (Float *)
(* f),p); e = expr_next (EConst (Float f),p) >] -> e | [< >] -> serror()) *)
(* */ *)
| [< '(Kwd For, p); '(POpen, _); ie = parse_init_for_block_elt ; cond = parse_for_expr; last = parse_last_for_expr; s >] ->
let cond = match cond with
| Some e -> e
| None -> (EConst (Ident "true") , p)
in
(try
let e = secure_expr s in
let be = match last with
| Some c -> EBlock [e;c], pos c
| None -> e
in let w = (EWhile (cond, be, NormalWhile), (pos e)) in
let b = match ie with
| Some e -> [e;w]
| None -> [w]
in
(EBlock b, punion p (pos e))
with
Display e -> display (
let be = match last with
| Some c -> EBlock [e;c], pos c
| None -> e
in let w = (EWhile (cond, be, NormalWhile), (pos e)) in
let b = match ie with
| Some e -> [e;w]
| None -> [w]
in
(EBlock b, punion p (pos e)))
)
(*
| [< '(Kwd For, p); '(POpen, _); it = expr; '(PClose, _); s >] ->
(try
let e = secure_expr s in
(EFor (it, e), punion p (pos e))
with
Display e -> display (EFor (it, e), punion p (pos e)))
*)
| [< '(Kwd If, p); '(POpen, _); cond = expr; '(PClose, _); e1 = expr; s >] ->
let e2 = (match s with parser
| [< '(Kwd Else, _); e2 = expr; s >] -> Some e2
| [< >] ->
(* we can't directly npeek 2 elements because this might remove some *)
(* documentation tag. *)
match Stream.npeek 1 s with
| [(Semicolon, _)] ->
(match Stream.npeek 2 s with
| [(Semicolon, _); (Kwd Else, _)] ->
Stream.junk s;
Stream.junk s;
Some (secure_expr s)
| _ -> None)
| _ ->
None
) in
(EIf (cond, e1, e2), punion p (match e2 with None -> pos e1 | Some e -> pos e))
| [< '(Kwd Return, p); e = popt expr >] -> (EReturn e, match e with None -> p | Some e -> punion p (pos e))
| [< '(Kwd Break, p) >] -> (EBreak, p)
| [< '(Kwd Continue, p) >] -> (EContinue, p)
| [< '(Kwd While, p1); '(POpen, _); cond = expr; '(PClose, _); s >] ->
(try
let e = secure_expr s in
(EWhile (cond, e, NormalWhile), punion p1 (pos e))
with
Display e -> display (EWhile (cond, e, NormalWhile), punion p1 (pos e)))
| [< '(Kwd Do, p1); e = expr; '(Kwd While, _); '(POpen, _); cond = expr; '(PClose, _); s >] -> (EWhile (cond, e, DoWhile), punion p1 (pos e))
| [< '(Kwd Switch, p1); e = expr; '(BrOpen, _); cases , def = parse_switch_cases e []; '(BrClose, p2); s >] -> (ESwitch (e, cases, def), punion p1 p2)
| [< '(Kwd Try, p1); e = expr; cl = plist (parse_catch e); s >] -> (ETry (e, cl), p1)
| [< '(IntInterval i, p1); e2 = expr >] -> make_binop OpInterval (EConst (Int i), p1) e2
| [< '(Kwd Untyped, p1); e = expr >] -> (EUntyped e, punion p1 (pos e))
| [< '(Dollar v, p); s >] -> expr_next (EConst (Ident ("$"^v)), p) s
and expr_next e1 = parser
| [< '(Dot, p); s >] ->
if is_resuming p then display (EDisplay (e1, false), p);