-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathparser.v
More file actions
1429 lines (1351 loc) · 33.1 KB
/
parser.v
File metadata and controls
1429 lines (1351 loc) · 33.1 KB
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 main
import x.json2
// Extract type annotation from v_annotation or inferred_annotation
fn parse_type_annotation(m map[string]json2.Any) ?string {
// First try v_annotation (string)
if raw_ann := m['v_annotation'] {
return ?string(raw_ann.str())
}
// Then try inferred_annotation (object with id field)
if raw_inf := m['inferred_annotation'] {
inf_map := raw_inf.as_map()
typ := inf_map['_type'] or { json2.Any('') }.str()
// Handle Name annotation (simple types like 'int', 'str')
if typ == 'Name' {
if id := inf_map['id'] {
name := id.str()
// Map Python types to V types using the v_type_map from types.v
return ?string(map_type(name))
}
}
// Handle Subscript annotation (generic types like 'List[int]', 'Dict[str, int]')
if typ == 'Subscript' {
value_map := (inf_map['value'] or { json2.Any('') }).as_map()
value_type := (value_map['_type'] or { json2.Any('') }).str()
if value_type == 'Name' {
container := (value_map['id'] or { json2.Any('') }).str()
// Parse the slice to get the element type
slice_map := (inf_map['slice'] or { json2.Any('') }).as_map()
slice_type := (slice_map['_type'] or { json2.Any('') }).str()
mut elem_type := 'Any'
if slice_type == 'Name' {
elem_name := (slice_map['id'] or { json2.Any('') }).str()
elem_type = match elem_name {
'int' { 'int' }
'float' { 'f64' }
'str' { 'string' }
'bool' { 'bool' }
else { elem_name }
}
}
// Map container types
return ?string(match container {
'list', 'List' { '[]${elem_type}' }
'tuple', 'Tuple' { '[]${elem_type}' }
'set', 'Set' { '[]${elem_type}' }
'dict', 'Dict' { 'map[string]${elem_type}' }
else { '[]${elem_type}' }
})
}
}
}
return none
}
// Parse JSON AST string into Module
pub fn parse_ast(json_str string) !Module {
raw := json2.decode[json2.Any](json_str)!
return parse_module(raw.as_map())
}
// Safely read a nested JSON map field.
fn map_field(m map[string]json2.Any, key string) map[string]json2.Any {
return (m[key] or { json2.Any(map[string]json2.Any{}) }).as_map()
}
// Parse a Module node
fn parse_module(m map[string]json2.Any) !Module {
mut body := []Stmt{}
if raw_body := m['body'] {
for item in raw_body.as_array() {
if stmt := parse_stmt(item.as_map()) {
body << stmt
}
}
}
mut docstring := ?string(none)
if raw_doc := m['docstring_comment'] {
s := raw_doc.str()
if s.len > 0 {
docstring = s
}
}
return Module{
body: body
loc: parse_location(m)
docstring_comment: docstring
}
}
// Parse location from a node map
fn parse_location(m map[string]json2.Any) Location {
return Location{
lineno: m['lineno'] or { json2.Any(0) }.int()
col_offset: m['col_offset'] or { json2.Any(0) }.int()
end_lineno: m['end_lineno'] or { json2.Any(0) }.int()
end_col_offset: m['end_col_offset'] or { json2.Any(0) }.int()
}
}
// Parse a statement node
fn parse_stmt(m map[string]json2.Any) ?Stmt {
node_type := m['_type'] or { return none }.str()
match node_type {
'FunctionDef' {
return Stmt(parse_function_def(m))
}
'AsyncFunctionDef' {
return Stmt(parse_async_function_def(m))
}
'ClassDef' {
return Stmt(parse_class_def(m))
}
'Return' {
return Stmt(parse_return(m))
}
'Delete' {
return Stmt(parse_delete(m))
}
'Assign' {
return Stmt(parse_assign(m))
}
'AugAssign' {
return Stmt(parse_aug_assign(m))
}
'AnnAssign' {
return Stmt(parse_ann_assign(m))
}
'For' {
return Stmt(parse_for(m))
}
'AsyncFor' {
return Stmt(parse_async_for(m))
}
'While' {
return Stmt(parse_while(m))
}
'If' {
return Stmt(parse_if(m))
}
'With' {
return Stmt(parse_with(m))
}
'AsyncWith' {
return Stmt(parse_async_with(m))
}
'Raise' {
return Stmt(parse_raise(m))
}
'Try' {
return Stmt(parse_try(m))
}
'Assert' {
return Stmt(parse_assert(m))
}
'Import' {
return Stmt(parse_import(m))
}
'ImportFrom' {
return Stmt(parse_import_from(m))
}
'Global' {
return Stmt(parse_global(m))
}
'Nonlocal' {
return Stmt(parse_nonlocal(m))
}
'Expr' {
return Stmt(parse_expr_stmt(m))
}
'Pass' {
return Stmt(Pass{
loc: parse_location(m)
})
}
'Break' {
return Stmt(Break{
loc: parse_location(m)
})
}
'Continue' {
return Stmt(Continue{
loc: parse_location(m)
})
}
else {
return none
}
}
}
// Parse an expression node
fn parse_expr(m map[string]json2.Any) ?Expr {
node_type := m['_type'] or { return none }.str()
match node_type {
'Constant' { return Expr(parse_constant(m)) }
'Name' { return Expr(parse_name(m)) }
'BinOp' { return Expr(parse_binop(m)) }
'UnaryOp' { return Expr(parse_unaryop(m)) }
'BoolOp' { return Expr(parse_boolop(m)) }
'Compare' { return Expr(parse_compare(m)) }
'Call' { return Expr(parse_call(m)) }
'Attribute' { return Expr(parse_attribute(m)) }
'Subscript' { return Expr(parse_subscript(m)) }
'Slice' { return Expr(parse_slice(m)) }
'List' { return Expr(parse_list(m)) }
'Tuple' { return Expr(parse_tuple(m)) }
'Dict' { return Expr(parse_dict(m)) }
'Set' { return Expr(parse_set(m)) }
'IfExp' { return Expr(parse_ifexp(m)) }
'Lambda' { return Expr(parse_lambda(m)) }
'ListComp' { return Expr(parse_list_comp(m)) }
'SetComp' { return Expr(parse_set_comp(m)) }
'DictComp' { return Expr(parse_dict_comp(m)) }
'GeneratorExp' { return Expr(parse_generator_exp(m)) }
'Await' { return Expr(parse_await(m)) }
'Yield' { return Expr(parse_yield(m)) }
'YieldFrom' { return Expr(parse_yield_from(m)) }
'FormattedValue' { return Expr(parse_formatted_value(m)) }
'JoinedStr' { return Expr(parse_joined_str(m)) }
'NamedExpr' { return Expr(parse_named_expr(m)) }
'Starred' { return Expr(parse_starred(m)) }
else { return none }
}
}
// Parse FunctionDef
fn parse_function_def(m map[string]json2.Any) FunctionDef {
mut body := []Stmt{}
if raw_body := m['body'] {
for item in raw_body.as_array() {
if stmt := parse_stmt(item.as_map()) {
body << stmt
}
}
}
mut decorators := []Expr{}
if raw_decs := m['decorator_list'] {
for item in raw_decs.as_array() {
if expr := parse_expr(item.as_map()) {
decorators << expr
}
}
}
mut mutable_vars := []string{}
if raw_vars := m['mutable_vars'] {
for item in raw_vars.as_array() {
mutable_vars << item.str()
}
}
return FunctionDef{
name: m['name'] or { json2.Any('') }.str()
args: parse_arguments(m['args'] or { json2.Any(map[string]json2.Any{}) })
body: body
decorator_list: decorators
returns: parse_optional_expr(m['returns'] or { json2.Any(json2.Null{}) })
type_comment: parse_optional_string(m['type_comment'] or { json2.Any(json2.Null{}) })
loc: parse_location(m)
is_generator: m['is_generator'] or { json2.Any(false) }.bool()
is_void: m['is_void'] or { json2.Any(false) }.bool()
mutable_vars: mutable_vars
is_class_method: m['is_class_method'] or { json2.Any(false) }.bool()
class_name: m['class_name'] or { json2.Any('') }.str()
}
}
// Parse AsyncFunctionDef
fn parse_async_function_def(m map[string]json2.Any) AsyncFunctionDef {
fd := parse_function_def(m)
return AsyncFunctionDef{
name: fd.name
args: fd.args
body: fd.body
decorator_list: fd.decorator_list
returns: fd.returns
type_comment: fd.type_comment
loc: fd.loc
is_generator: fd.is_generator
is_void: fd.is_void
mutable_vars: fd.mutable_vars
is_class_method: fd.is_class_method
class_name: fd.class_name
}
}
// Parse ClassDef
fn parse_class_def(m map[string]json2.Any) ClassDef {
mut body := []Stmt{}
if raw_body := m['body'] {
for item in raw_body.as_array() {
if stmt := parse_stmt(item.as_map()) {
body << stmt
}
}
}
mut bases := []Expr{}
if raw_bases := m['bases'] {
for item in raw_bases.as_array() {
if expr := parse_expr(item.as_map()) {
bases << expr
}
}
}
mut keywords := []Keyword{}
if raw_kws := m['keywords'] {
for item in raw_kws.as_array() {
keywords << parse_keyword(item.as_map())
}
}
mut decorators := []Expr{}
if raw_decs := m['decorator_list'] {
for item in raw_decs.as_array() {
if expr := parse_expr(item.as_map()) {
decorators << expr
}
}
}
mut declarations := map[string]string{}
if raw_decls := m['declarations'] {
raw_map := raw_decls.as_map()
for key, val in raw_map {
declarations[key] = val.str()
}
}
mut class_defaults := map[string]Expr{}
if raw_defaults := m['class_defaults'] {
raw_map := raw_defaults.as_map()
for key, val in raw_map {
if expr := parse_expr(val.as_map()) {
class_defaults[key] = expr
}
}
}
mut docstring := ?string(none)
if raw_doc := m['docstring_comment'] {
s := raw_doc.str()
if s.len > 0 {
docstring = s
}
}
return ClassDef{
name: m['name'] or { json2.Any('') }.str()
bases: bases
keywords: keywords
body: body
decorator_list: decorators
loc: parse_location(m)
declarations: declarations
class_defaults: class_defaults
docstring_comment: docstring
}
}
// Parse Arguments
fn parse_arguments(raw json2.Any) Arguments {
m := raw.as_map()
mut args := []Arg{}
if raw_args := m['args'] {
for item in raw_args.as_array() {
args << parse_arg(item.as_map())
}
}
mut posonlyargs := []Arg{}
if raw_pos := m['posonlyargs'] {
for item in raw_pos.as_array() {
posonlyargs << parse_arg(item.as_map())
}
}
mut kwonlyargs := []Arg{}
if raw_kw := m['kwonlyargs'] {
for item in raw_kw.as_array() {
kwonlyargs << parse_arg(item.as_map())
}
}
mut defaults := []Expr{}
if raw_defs := m['defaults'] {
for item in raw_defs.as_array() {
if expr := parse_expr(item.as_map()) {
defaults << expr
}
}
}
mut kw_defaults := []?Expr{}
if raw_kw_defs := m['kw_defaults'] {
for item in raw_kw_defs.as_array() {
kw_defaults << parse_optional_expr(item)
}
}
return Arguments{
posonlyargs: posonlyargs
args: args
vararg: parse_optional_arg(m['vararg'] or { json2.Any(json2.Null{}) })
kwonlyargs: kwonlyargs
kw_defaults: kw_defaults
kwarg: parse_optional_arg(m['kwarg'] or { json2.Any(json2.Null{}) })
defaults: defaults
}
}
// Parse Arg
fn parse_arg(m map[string]json2.Any) Arg {
return Arg{
arg: m['arg'] or { json2.Any('') }.str()
annotation: parse_optional_expr(m['annotation'] or { json2.Any(json2.Null{}) })
type_comment: parse_optional_string(m['type_comment'] or { json2.Any(json2.Null{}) })
loc: parse_location(m)
}
}
fn parse_optional_arg(raw json2.Any) ?Arg {
if raw is json2.Null {
return none
}
m := raw.as_map()
if m.len == 0 {
return none
}
return parse_arg(m)
}
// Parse Keyword
fn parse_keyword(m map[string]json2.Any) Keyword {
return Keyword{
arg: parse_optional_string(m['arg'] or { json2.Any(json2.Null{}) })
value: parse_expr(map_field(m, 'value')) or { Expr(Constant{
value: NoneValue{}
}) }
loc: parse_location(m)
}
}
// Parse Return
fn parse_return(m map[string]json2.Any) Return {
return Return{
value: parse_optional_expr(m['value'] or { json2.Any(json2.Null{}) })
loc: parse_location(m)
}
}
// Parse Delete
fn parse_delete(m map[string]json2.Any) Delete {
mut targets := []Expr{}
if raw_targets := m['targets'] {
for item in raw_targets.as_array() {
if expr := parse_expr(item.as_map()) {
targets << expr
}
}
}
return Delete{
targets: targets
loc: parse_location(m)
}
}
// Parse Assign
fn parse_assign(m map[string]json2.Any) Assign {
mut targets := []Expr{}
if raw_targets := m['targets'] {
for item in raw_targets.as_array() {
if expr := parse_expr(item.as_map()) {
targets << expr
}
}
}
mut redefined := []string{}
if raw_redef := m['redefined_targets'] {
for item in raw_redef.as_array() {
redefined << item.str()
}
}
return Assign{
targets: targets
value: parse_expr(map_field(m, 'value')) or {
Expr(Constant{
value: NoneValue{}
})
}
type_comment: parse_optional_string(m['type_comment'] or { json2.Any(json2.Null{}) })
loc: parse_location(m)
redefined_targets: redefined
}
}
// Parse AugAssign
fn parse_aug_assign(m map[string]json2.Any) AugAssign {
return AugAssign{
target: parse_expr(map_field(m, 'target')) or { Expr(Constant{
value: NoneValue{}
}) }
op: parse_operator(map_field(m, 'op'))
value: parse_expr(map_field(m, 'value')) or { Expr(Constant{
value: NoneValue{}
}) }
loc: parse_location(m)
}
}
// Parse AnnAssign
fn parse_ann_assign(m map[string]json2.Any) AnnAssign {
return AnnAssign{
target: parse_expr(map_field(m, 'target')) or {
Expr(Constant{
value: NoneValue{}
})
}
annotation: parse_expr(map_field(m, 'annotation')) or {
Expr(Constant{
value: NoneValue{}
})
}
value: parse_optional_expr(m['value'] or { json2.Any(json2.Null{}) })
simple: m['simple'] or { json2.Any(0) }.int()
loc: parse_location(m)
}
}
// Parse For
fn parse_for(m map[string]json2.Any) For {
mut body := []Stmt{}
if raw_body := m['body'] {
for item in raw_body.as_array() {
if stmt := parse_stmt(item.as_map()) {
body << stmt
}
}
}
mut orelse := []Stmt{}
if raw_else := m['orelse'] {
for item in raw_else.as_array() {
if stmt := parse_stmt(item.as_map()) {
orelse << stmt
}
}
}
return For{
target: parse_expr(map_field(m, 'target')) or {
Expr(Constant{
value: NoneValue{}
})
}
iter: parse_expr(map_field(m, 'iter')) or {
Expr(Constant{
value: NoneValue{}
})
}
body: body
orelse: orelse
type_comment: parse_optional_string(m['type_comment'] or { json2.Any(json2.Null{}) })
loc: parse_location(m)
level: m['level'] or { json2.Any(0) }.int()
}
}
// Parse AsyncFor
fn parse_async_for(m map[string]json2.Any) AsyncFor {
f := parse_for(m)
return AsyncFor{
target: f.target
iter: f.iter
body: f.body
orelse: f.orelse
type_comment: f.type_comment
loc: f.loc
level: f.level
}
}
// Parse While
fn parse_while(m map[string]json2.Any) While {
mut body := []Stmt{}
if raw_body := m['body'] {
for item in raw_body.as_array() {
if stmt := parse_stmt(item.as_map()) {
body << stmt
}
}
}
mut orelse := []Stmt{}
if raw_else := m['orelse'] {
for item in raw_else.as_array() {
if stmt := parse_stmt(item.as_map()) {
orelse << stmt
}
}
}
return While{
test: parse_expr(map_field(m, 'test')) or { Expr(Constant{
value: NoneValue{}
}) }
body: body
orelse: orelse
loc: parse_location(m)
level: m['level'] or { json2.Any(0) }.int()
}
}
// Parse If
fn parse_if(m map[string]json2.Any) If {
mut body := []Stmt{}
if raw_body := m['body'] {
for item in raw_body.as_array() {
if stmt := parse_stmt(item.as_map()) {
body << stmt
}
}
}
mut orelse := []Stmt{}
if raw_else := m['orelse'] {
for item in raw_else.as_array() {
if stmt := parse_stmt(item.as_map()) {
orelse << stmt
}
}
}
return If{
test: parse_expr(map_field(m, 'test')) or { Expr(Constant{
value: NoneValue{}
}) }
body: body
orelse: orelse
loc: parse_location(m)
level: m['level'] or { json2.Any(0) }.int()
}
}
// Parse With
fn parse_with(m map[string]json2.Any) With {
mut items := []WithItem{}
if raw_items := m['items'] {
for item in raw_items.as_array() {
items << parse_with_item(item.as_map())
}
}
mut body := []Stmt{}
if raw_body := m['body'] {
for item in raw_body.as_array() {
if stmt := parse_stmt(item.as_map()) {
body << stmt
}
}
}
return With{
items: items
body: body
type_comment: parse_optional_string(m['type_comment'] or { json2.Any(json2.Null{}) })
loc: parse_location(m)
}
}
fn parse_with_item(m map[string]json2.Any) WithItem {
return WithItem{
context_expr: parse_expr(map_field(m, 'context_expr')) or {
Expr(Constant{
value: NoneValue{}
})
}
optional_vars: parse_optional_expr(m['optional_vars'] or { json2.Any(json2.Null{}) })
}
}
// Parse AsyncWith
fn parse_async_with(m map[string]json2.Any) AsyncWith {
w := parse_with(m)
return AsyncWith{
items: w.items
body: w.body
type_comment: w.type_comment
loc: w.loc
}
}
// Parse Raise
fn parse_raise(m map[string]json2.Any) Raise {
return Raise{
exc: parse_optional_expr(m['exc'] or { json2.Any(json2.Null{}) })
cause: parse_optional_expr(m['cause'] or { json2.Any(json2.Null{}) })
loc: parse_location(m)
}
}
// Parse Try
fn parse_try(m map[string]json2.Any) Try {
mut body := []Stmt{}
if raw_body := m['body'] {
for item in raw_body.as_array() {
if stmt := parse_stmt(item.as_map()) {
body << stmt
}
}
}
mut handlers := []ExceptHandler{}
if raw_handlers := m['handlers'] {
for item in raw_handlers.as_array() {
handlers << parse_except_handler(item.as_map())
}
}
mut orelse := []Stmt{}
if raw_else := m['orelse'] {
for item in raw_else.as_array() {
if stmt := parse_stmt(item.as_map()) {
orelse << stmt
}
}
}
mut finalbody := []Stmt{}
if raw_final := m['finalbody'] {
for item in raw_final.as_array() {
if stmt := parse_stmt(item.as_map()) {
finalbody << stmt
}
}
}
return Try{
body: body
handlers: handlers
orelse: orelse
finalbody: finalbody
loc: parse_location(m)
}
}
fn parse_except_handler(m map[string]json2.Any) ExceptHandler {
mut body := []Stmt{}
if raw_body := m['body'] {
for item in raw_body.as_array() {
if stmt := parse_stmt(item.as_map()) {
body << stmt
}
}
}
return ExceptHandler{
typ: parse_optional_expr(m['type'] or { json2.Any(json2.Null{}) })
name: parse_optional_string(m['name'] or { json2.Any(json2.Null{}) })
body: body
loc: parse_location(m)
}
}
// Parse Assert
fn parse_assert(m map[string]json2.Any) Assert {
return Assert{
test: parse_expr(map_field(m, 'test')) or { Expr(Constant{
value: NoneValue{}
}) }
msg: parse_optional_expr(m['msg'] or { json2.Any(json2.Null{}) })
loc: parse_location(m)
}
}
// Parse Import
fn parse_import(m map[string]json2.Any) Import {
mut names := []Alias{}
if raw_names := m['names'] {
for item in raw_names.as_array() {
names << parse_alias(item.as_map())
}
}
return Import{
names: names
loc: parse_location(m)
}
}
// Parse ImportFrom
fn parse_import_from(m map[string]json2.Any) ImportFrom {
mut names := []Alias{}
if raw_names := m['names'] {
for item in raw_names.as_array() {
names << parse_alias(item.as_map())
}
}
return ImportFrom{
mod: parse_optional_string(m['module'] or { json2.Any(json2.Null{}) })
names: names
level: m['level'] or { json2.Any(0) }.int()
loc: parse_location(m)
}
}
fn parse_alias(m map[string]json2.Any) Alias {
return Alias{
name: m['name'] or { json2.Any('') }.str()
asname: parse_optional_string(m['asname'] or { json2.Any(json2.Null{}) })
loc: parse_location(m)
}
}
// Parse Global
fn parse_global(m map[string]json2.Any) Global {
mut names := []string{}
if raw_names := m['names'] {
for item in raw_names.as_array() {
names << item.str()
}
}
return Global{
names: names
loc: parse_location(m)
}
}
// Parse Nonlocal
fn parse_nonlocal(m map[string]json2.Any) Nonlocal {
mut names := []string{}
if raw_names := m['names'] {
for item in raw_names.as_array() {
names << item.str()
}
}
return Nonlocal{
names: names
loc: parse_location(m)
}
}
// Parse ExprStmt
fn parse_expr_stmt(m map[string]json2.Any) ExprStmt {
return ExprStmt{
value: parse_expr(map_field(m, 'value')) or { Expr(Constant{
value: NoneValue{}
}) }
loc: parse_location(m)
}
}
// Parse Constant
fn parse_constant(m map[string]json2.Any) Constant {
raw_val := m['value'] or { json2.Any(json2.Null{}) }
value := parse_constant_value(raw_val)
return Constant{
value: value
kind: parse_optional_string(m['kind'] or { json2.Any(json2.Null{}) })
loc: parse_location(m)
v_annotation: parse_type_annotation(m)
}
}
fn parse_constant_value(raw json2.Any) ConstantValue {
if raw is json2.Null {
return NoneValue{}
}
if raw is map[string]json2.Any {
m := raw as map[string]json2.Any
if typ := m['_type'] {
typ_str := typ.str()
if typ_str == 'Ellipsis' {
return EllipsisValue{}
}
if typ_str == 'bytes' {
mut data := []u8{}
if val := m['value'] {
for b in val.as_array() {
data << u8(b.int())
}
}
return BytesValue{
data: data
}
}
}
}
match raw {
bool {
return ConstantValue(raw as bool)
}
i64 {
val := raw as i64
if val >= i64(-2147483647) - 1 && val <= i64(2147483647) {
return ConstantValue(int(val))
}
return ConstantValue(val)
}
f64 {
val := raw as f64
int_val := i64(val)
if f64(int_val) == val && int_val >= i64(-2147483647) - 1 && int_val <= i64(2147483647) {
return ConstantValue(int(int_val))
}
return ConstantValue(val)
}
string {
return ConstantValue(raw as string)
}
else {
return NoneValue{}
}
}
}
// Parse Name
fn parse_name(m map[string]json2.Any) Name {
return Name{
id: m['id'] or { json2.Any('') }.str()
ctx: parse_context(map_field(m, 'ctx'))
loc: parse_location(m)
is_mutable: m['is_mutable'] or { json2.Any(false) }.bool()
v_annotation: parse_type_annotation(m)
}
}
// Parse context
fn parse_context(m map[string]json2.Any) ExprContext {
ctx_type := m['_type'] or { json2.Any('Load') }.str()
return match ctx_type {
'Store' { ExprContext(Store{}) }
'Del' { ExprContext(Del{}) }
else { ExprContext(Load{}) }
}
}
// Parse BinOp
fn parse_binop(m map[string]json2.Any) BinOp {
return BinOp{
left: parse_expr(map_field(m, 'left')) or {
Expr(Constant{
value: NoneValue{}
})
}
op: parse_operator(map_field(m, 'op'))
right: parse_expr(map_field(m, 'right')) or {
Expr(Constant{
value: NoneValue{}
})
}
loc: parse_location(m)
v_annotation: parse_type_annotation(m)
}
}
// Parse Operator
fn parse_operator(m map[string]json2.Any) Operator {
op_type := m['_type'] or { json2.Any('Add') }.str()
return match op_type {
'Sub' { Operator(Sub{}) }
'Mult' { Operator(Mult{}) }
'MatMult' { Operator(MatMult{}) }
'Div' { Operator(Div{}) }
'Mod' { Operator(Mod{}) }
'Pow' { Operator(Pow{}) }
'LShift' { Operator(LShift{}) }
'RShift' { Operator(RShift{}) }
'BitOr' { Operator(BitOr{}) }
'BitXor' { Operator(BitXor{}) }
'BitAnd' { Operator(BitAnd{}) }
'FloorDiv' { Operator(FloorDiv{}) }
else { Operator(Add{}) }
}
}
// Parse UnaryOp
fn parse_unaryop(m map[string]json2.Any) UnaryOp {
return UnaryOp{
op: parse_unary_operator(map_field(m, 'op'))
operand: parse_expr(map_field(m, 'operand')) or {
Expr(Constant{
value: NoneValue{}
})
}
loc: parse_location(m)
v_annotation: parse_type_annotation(m)
}
}
fn parse_unary_operator(m map[string]json2.Any) UnaryOperator {
op_type := m['_type'] or { json2.Any('Not') }.str()
return match op_type {
'Invert' { UnaryOperator(Invert{}) }
'UAdd' { UnaryOperator(UAdd{}) }
'USub' { UnaryOperator(USub{}) }
else { UnaryOperator(Not{}) }
}
}
// Parse BoolOp
fn parse_boolop(m map[string]json2.Any) BoolOp {
mut values := []Expr{}
if raw_values := m['values'] {
for item in raw_values.as_array() {
if expr := parse_expr(item.as_map()) {
values << expr
}
}
}
return BoolOp{
op: parse_bool_operator(map_field(m, 'op'))
values: values
loc: parse_location(m)
v_annotation: parse_type_annotation(m)
}
}
fn parse_bool_operator(m map[string]json2.Any) BoolOperator {
op_type := m['_type'] or { json2.Any('And') }.str()