forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction_helpers.c
1663 lines (1499 loc) · 51 KB
/
action_helpers.c
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
#include <Python.h>
#include "pegen.h"
#include "string_parser.h"
#include "pycore_runtime.h" // _PyRuntime
void *
_PyPegen_dummy_name(Parser *p, ...)
{
return &_PyRuntime.parser.dummy_name;
}
/* Creates a single-element asdl_seq* that contains a */
asdl_seq *
_PyPegen_singleton_seq(Parser *p, void *a)
{
assert(a != NULL);
asdl_seq *seq = (asdl_seq*)_Py_asdl_generic_seq_new(1, p->arena);
if (!seq) {
return NULL;
}
asdl_seq_SET_UNTYPED(seq, 0, a);
return seq;
}
/* Creates a copy of seq and prepends a to it */
asdl_seq *
_PyPegen_seq_insert_in_front(Parser *p, void *a, asdl_seq *seq)
{
assert(a != NULL);
if (!seq) {
return _PyPegen_singleton_seq(p, a);
}
asdl_seq *new_seq = (asdl_seq*)_Py_asdl_generic_seq_new(asdl_seq_LEN(seq) + 1, p->arena);
if (!new_seq) {
return NULL;
}
asdl_seq_SET_UNTYPED(new_seq, 0, a);
for (Py_ssize_t i = 1, l = asdl_seq_LEN(new_seq); i < l; i++) {
asdl_seq_SET_UNTYPED(new_seq, i, asdl_seq_GET_UNTYPED(seq, i - 1));
}
return new_seq;
}
/* Creates a copy of seq and appends a to it */
asdl_seq *
_PyPegen_seq_append_to_end(Parser *p, asdl_seq *seq, void *a)
{
assert(a != NULL);
if (!seq) {
return _PyPegen_singleton_seq(p, a);
}
asdl_seq *new_seq = (asdl_seq*)_Py_asdl_generic_seq_new(asdl_seq_LEN(seq) + 1, p->arena);
if (!new_seq) {
return NULL;
}
for (Py_ssize_t i = 0, l = asdl_seq_LEN(new_seq); i + 1 < l; i++) {
asdl_seq_SET_UNTYPED(new_seq, i, asdl_seq_GET_UNTYPED(seq, i));
}
asdl_seq_SET_UNTYPED(new_seq, asdl_seq_LEN(new_seq) - 1, a);
return new_seq;
}
static Py_ssize_t
_get_flattened_seq_size(asdl_seq *seqs)
{
Py_ssize_t size = 0;
for (Py_ssize_t i = 0, l = asdl_seq_LEN(seqs); i < l; i++) {
asdl_seq *inner_seq = asdl_seq_GET_UNTYPED(seqs, i);
size += asdl_seq_LEN(inner_seq);
}
return size;
}
/* Flattens an asdl_seq* of asdl_seq*s */
asdl_seq *
_PyPegen_seq_flatten(Parser *p, asdl_seq *seqs)
{
Py_ssize_t flattened_seq_size = _get_flattened_seq_size(seqs);
assert(flattened_seq_size > 0);
asdl_seq *flattened_seq = (asdl_seq*)_Py_asdl_generic_seq_new(flattened_seq_size, p->arena);
if (!flattened_seq) {
return NULL;
}
int flattened_seq_idx = 0;
for (Py_ssize_t i = 0, l = asdl_seq_LEN(seqs); i < l; i++) {
asdl_seq *inner_seq = asdl_seq_GET_UNTYPED(seqs, i);
for (Py_ssize_t j = 0, li = asdl_seq_LEN(inner_seq); j < li; j++) {
asdl_seq_SET_UNTYPED(flattened_seq, flattened_seq_idx++, asdl_seq_GET_UNTYPED(inner_seq, j));
}
}
assert(flattened_seq_idx == flattened_seq_size);
return flattened_seq;
}
void *
_PyPegen_seq_last_item(asdl_seq *seq)
{
Py_ssize_t len = asdl_seq_LEN(seq);
return asdl_seq_GET_UNTYPED(seq, len - 1);
}
void *
_PyPegen_seq_first_item(asdl_seq *seq)
{
return asdl_seq_GET_UNTYPED(seq, 0);
}
/* Creates a new name of the form <first_name>.<second_name> */
expr_ty
_PyPegen_join_names_with_dot(Parser *p, expr_ty first_name, expr_ty second_name)
{
assert(first_name != NULL && second_name != NULL);
PyObject *uni = PyUnicode_FromFormat("%U.%U",
first_name->v.Name.id, second_name->v.Name.id);
if (!uni) {
return NULL;
}
PyUnicode_InternInPlace(&uni);
if (_PyArena_AddPyObject(p->arena, uni) < 0) {
Py_DECREF(uni);
return NULL;
}
return _PyAST_Name(uni, Load, EXTRA_EXPR(first_name, second_name));
}
/* Counts the total number of dots in seq's tokens */
int
_PyPegen_seq_count_dots(asdl_seq *seq)
{
int number_of_dots = 0;
for (Py_ssize_t i = 0, l = asdl_seq_LEN(seq); i < l; i++) {
Token *current_expr = asdl_seq_GET_UNTYPED(seq, i);
switch (current_expr->type) {
case ELLIPSIS:
number_of_dots += 3;
break;
case DOT:
number_of_dots += 1;
break;
default:
Py_UNREACHABLE();
}
}
return number_of_dots;
}
/* Creates an alias with '*' as the identifier name */
alias_ty
_PyPegen_alias_for_star(Parser *p, int lineno, int col_offset, int end_lineno,
int end_col_offset, PyArena *arena) {
PyObject *str = PyUnicode_InternFromString("*");
if (!str) {
return NULL;
}
if (_PyArena_AddPyObject(p->arena, str) < 0) {
Py_DECREF(str);
return NULL;
}
return _PyAST_alias(str, NULL, lineno, col_offset, end_lineno, end_col_offset, arena);
}
/* Creates a new asdl_seq* with the identifiers of all the names in seq */
asdl_identifier_seq *
_PyPegen_map_names_to_ids(Parser *p, asdl_expr_seq *seq)
{
Py_ssize_t len = asdl_seq_LEN(seq);
assert(len > 0);
asdl_identifier_seq *new_seq = _Py_asdl_identifier_seq_new(len, p->arena);
if (!new_seq) {
return NULL;
}
for (Py_ssize_t i = 0; i < len; i++) {
expr_ty e = asdl_seq_GET(seq, i);
asdl_seq_SET(new_seq, i, e->v.Name.id);
}
return new_seq;
}
/* Constructs a CmpopExprPair */
CmpopExprPair *
_PyPegen_cmpop_expr_pair(Parser *p, cmpop_ty cmpop, expr_ty expr)
{
assert(expr != NULL);
CmpopExprPair *a = _PyArena_Malloc(p->arena, sizeof(CmpopExprPair));
if (!a) {
return NULL;
}
a->cmpop = cmpop;
a->expr = expr;
return a;
}
asdl_int_seq *
_PyPegen_get_cmpops(Parser *p, asdl_seq *seq)
{
Py_ssize_t len = asdl_seq_LEN(seq);
assert(len > 0);
asdl_int_seq *new_seq = _Py_asdl_int_seq_new(len, p->arena);
if (!new_seq) {
return NULL;
}
for (Py_ssize_t i = 0; i < len; i++) {
CmpopExprPair *pair = asdl_seq_GET_UNTYPED(seq, i);
asdl_seq_SET(new_seq, i, pair->cmpop);
}
return new_seq;
}
asdl_expr_seq *
_PyPegen_get_exprs(Parser *p, asdl_seq *seq)
{
Py_ssize_t len = asdl_seq_LEN(seq);
assert(len > 0);
asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
if (!new_seq) {
return NULL;
}
for (Py_ssize_t i = 0; i < len; i++) {
CmpopExprPair *pair = asdl_seq_GET_UNTYPED(seq, i);
asdl_seq_SET(new_seq, i, pair->expr);
}
return new_seq;
}
/* Creates an asdl_seq* where all the elements have been changed to have ctx as context */
static asdl_expr_seq *
_set_seq_context(Parser *p, asdl_expr_seq *seq, expr_context_ty ctx)
{
Py_ssize_t len = asdl_seq_LEN(seq);
if (len == 0) {
return NULL;
}
asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
if (!new_seq) {
return NULL;
}
for (Py_ssize_t i = 0; i < len; i++) {
expr_ty e = asdl_seq_GET(seq, i);
asdl_seq_SET(new_seq, i, _PyPegen_set_expr_context(p, e, ctx));
}
return new_seq;
}
static expr_ty
_set_name_context(Parser *p, expr_ty e, expr_context_ty ctx)
{
return _PyAST_Name(e->v.Name.id, ctx, EXTRA_EXPR(e, e));
}
static expr_ty
_set_tuple_context(Parser *p, expr_ty e, expr_context_ty ctx)
{
return _PyAST_Tuple(
_set_seq_context(p, e->v.Tuple.elts, ctx),
ctx,
EXTRA_EXPR(e, e));
}
static expr_ty
_set_list_context(Parser *p, expr_ty e, expr_context_ty ctx)
{
return _PyAST_List(
_set_seq_context(p, e->v.List.elts, ctx),
ctx,
EXTRA_EXPR(e, e));
}
static expr_ty
_set_subscript_context(Parser *p, expr_ty e, expr_context_ty ctx)
{
return _PyAST_Subscript(e->v.Subscript.value, e->v.Subscript.slice,
ctx, EXTRA_EXPR(e, e));
}
static expr_ty
_set_attribute_context(Parser *p, expr_ty e, expr_context_ty ctx)
{
return _PyAST_Attribute(e->v.Attribute.value, e->v.Attribute.attr,
ctx, EXTRA_EXPR(e, e));
}
static expr_ty
_set_starred_context(Parser *p, expr_ty e, expr_context_ty ctx)
{
return _PyAST_Starred(_PyPegen_set_expr_context(p, e->v.Starred.value, ctx),
ctx, EXTRA_EXPR(e, e));
}
/* Creates an `expr_ty` equivalent to `expr` but with `ctx` as context */
expr_ty
_PyPegen_set_expr_context(Parser *p, expr_ty expr, expr_context_ty ctx)
{
assert(expr != NULL);
expr_ty new = NULL;
switch (expr->kind) {
case Name_kind:
new = _set_name_context(p, expr, ctx);
break;
case Tuple_kind:
new = _set_tuple_context(p, expr, ctx);
break;
case List_kind:
new = _set_list_context(p, expr, ctx);
break;
case Subscript_kind:
new = _set_subscript_context(p, expr, ctx);
break;
case Attribute_kind:
new = _set_attribute_context(p, expr, ctx);
break;
case Starred_kind:
new = _set_starred_context(p, expr, ctx);
break;
default:
new = expr;
}
return new;
}
/* Constructs a KeyValuePair that is used when parsing a dict's key value pairs */
KeyValuePair *
_PyPegen_key_value_pair(Parser *p, expr_ty key, expr_ty value)
{
KeyValuePair *a = _PyArena_Malloc(p->arena, sizeof(KeyValuePair));
if (!a) {
return NULL;
}
a->key = key;
a->value = value;
return a;
}
/* Extracts all keys from an asdl_seq* of KeyValuePair*'s */
asdl_expr_seq *
_PyPegen_get_keys(Parser *p, asdl_seq *seq)
{
Py_ssize_t len = asdl_seq_LEN(seq);
asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
if (!new_seq) {
return NULL;
}
for (Py_ssize_t i = 0; i < len; i++) {
KeyValuePair *pair = asdl_seq_GET_UNTYPED(seq, i);
asdl_seq_SET(new_seq, i, pair->key);
}
return new_seq;
}
/* Extracts all values from an asdl_seq* of KeyValuePair*'s */
asdl_expr_seq *
_PyPegen_get_values(Parser *p, asdl_seq *seq)
{
Py_ssize_t len = asdl_seq_LEN(seq);
asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
if (!new_seq) {
return NULL;
}
for (Py_ssize_t i = 0; i < len; i++) {
KeyValuePair *pair = asdl_seq_GET_UNTYPED(seq, i);
asdl_seq_SET(new_seq, i, pair->value);
}
return new_seq;
}
/* Constructs a KeyPatternPair that is used when parsing mapping & class patterns */
KeyPatternPair *
_PyPegen_key_pattern_pair(Parser *p, expr_ty key, pattern_ty pattern)
{
KeyPatternPair *a = _PyArena_Malloc(p->arena, sizeof(KeyPatternPair));
if (!a) {
return NULL;
}
a->key = key;
a->pattern = pattern;
return a;
}
/* Extracts all keys from an asdl_seq* of KeyPatternPair*'s */
asdl_expr_seq *
_PyPegen_get_pattern_keys(Parser *p, asdl_seq *seq)
{
Py_ssize_t len = asdl_seq_LEN(seq);
asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
if (!new_seq) {
return NULL;
}
for (Py_ssize_t i = 0; i < len; i++) {
KeyPatternPair *pair = asdl_seq_GET_UNTYPED(seq, i);
asdl_seq_SET(new_seq, i, pair->key);
}
return new_seq;
}
/* Extracts all patterns from an asdl_seq* of KeyPatternPair*'s */
asdl_pattern_seq *
_PyPegen_get_patterns(Parser *p, asdl_seq *seq)
{
Py_ssize_t len = asdl_seq_LEN(seq);
asdl_pattern_seq *new_seq = _Py_asdl_pattern_seq_new(len, p->arena);
if (!new_seq) {
return NULL;
}
for (Py_ssize_t i = 0; i < len; i++) {
KeyPatternPair *pair = asdl_seq_GET_UNTYPED(seq, i);
asdl_seq_SET(new_seq, i, pair->pattern);
}
return new_seq;
}
/* Constructs a NameDefaultPair */
NameDefaultPair *
_PyPegen_name_default_pair(Parser *p, arg_ty arg, expr_ty value, Token *tc)
{
NameDefaultPair *a = _PyArena_Malloc(p->arena, sizeof(NameDefaultPair));
if (!a) {
return NULL;
}
a->arg = _PyPegen_add_type_comment_to_arg(p, arg, tc);
a->value = value;
return a;
}
/* Constructs a SlashWithDefault */
SlashWithDefault *
_PyPegen_slash_with_default(Parser *p, asdl_arg_seq *plain_names, asdl_seq *names_with_defaults)
{
SlashWithDefault *a = _PyArena_Malloc(p->arena, sizeof(SlashWithDefault));
if (!a) {
return NULL;
}
a->plain_names = plain_names;
a->names_with_defaults = names_with_defaults;
return a;
}
/* Constructs a StarEtc */
StarEtc *
_PyPegen_star_etc(Parser *p, arg_ty vararg, asdl_seq *kwonlyargs, arg_ty kwarg)
{
StarEtc *a = _PyArena_Malloc(p->arena, sizeof(StarEtc));
if (!a) {
return NULL;
}
a->vararg = vararg;
a->kwonlyargs = kwonlyargs;
a->kwarg = kwarg;
return a;
}
asdl_seq *
_PyPegen_join_sequences(Parser *p, asdl_seq *a, asdl_seq *b)
{
Py_ssize_t first_len = asdl_seq_LEN(a);
Py_ssize_t second_len = asdl_seq_LEN(b);
asdl_seq *new_seq = (asdl_seq*)_Py_asdl_generic_seq_new(first_len + second_len, p->arena);
if (!new_seq) {
return NULL;
}
int k = 0;
for (Py_ssize_t i = 0; i < first_len; i++) {
asdl_seq_SET_UNTYPED(new_seq, k++, asdl_seq_GET_UNTYPED(a, i));
}
for (Py_ssize_t i = 0; i < second_len; i++) {
asdl_seq_SET_UNTYPED(new_seq, k++, asdl_seq_GET_UNTYPED(b, i));
}
return new_seq;
}
static asdl_arg_seq*
_get_names(Parser *p, asdl_seq *names_with_defaults)
{
Py_ssize_t len = asdl_seq_LEN(names_with_defaults);
asdl_arg_seq *seq = _Py_asdl_arg_seq_new(len, p->arena);
if (!seq) {
return NULL;
}
for (Py_ssize_t i = 0; i < len; i++) {
NameDefaultPair *pair = asdl_seq_GET_UNTYPED(names_with_defaults, i);
asdl_seq_SET(seq, i, pair->arg);
}
return seq;
}
static asdl_expr_seq *
_get_defaults(Parser *p, asdl_seq *names_with_defaults)
{
Py_ssize_t len = asdl_seq_LEN(names_with_defaults);
asdl_expr_seq *seq = _Py_asdl_expr_seq_new(len, p->arena);
if (!seq) {
return NULL;
}
for (Py_ssize_t i = 0; i < len; i++) {
NameDefaultPair *pair = asdl_seq_GET_UNTYPED(names_with_defaults, i);
asdl_seq_SET(seq, i, pair->value);
}
return seq;
}
static int
_make_posonlyargs(Parser *p,
asdl_arg_seq *slash_without_default,
SlashWithDefault *slash_with_default,
asdl_arg_seq **posonlyargs) {
if (slash_without_default != NULL) {
*posonlyargs = slash_without_default;
}
else if (slash_with_default != NULL) {
asdl_arg_seq *slash_with_default_names =
_get_names(p, slash_with_default->names_with_defaults);
if (!slash_with_default_names) {
return -1;
}
*posonlyargs = (asdl_arg_seq*)_PyPegen_join_sequences(
p,
(asdl_seq*)slash_with_default->plain_names,
(asdl_seq*)slash_with_default_names);
}
else {
*posonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
}
return *posonlyargs == NULL ? -1 : 0;
}
static int
_make_posargs(Parser *p,
asdl_arg_seq *plain_names,
asdl_seq *names_with_default,
asdl_arg_seq **posargs) {
if (plain_names != NULL && names_with_default != NULL) {
asdl_arg_seq *names_with_default_names = _get_names(p, names_with_default);
if (!names_with_default_names) {
return -1;
}
*posargs = (asdl_arg_seq*)_PyPegen_join_sequences(
p,(asdl_seq*)plain_names, (asdl_seq*)names_with_default_names);
}
else if (plain_names == NULL && names_with_default != NULL) {
*posargs = _get_names(p, names_with_default);
}
else if (plain_names != NULL && names_with_default == NULL) {
*posargs = plain_names;
}
else {
*posargs = _Py_asdl_arg_seq_new(0, p->arena);
}
return *posargs == NULL ? -1 : 0;
}
static int
_make_posdefaults(Parser *p,
SlashWithDefault *slash_with_default,
asdl_seq *names_with_default,
asdl_expr_seq **posdefaults) {
if (slash_with_default != NULL && names_with_default != NULL) {
asdl_expr_seq *slash_with_default_values =
_get_defaults(p, slash_with_default->names_with_defaults);
if (!slash_with_default_values) {
return -1;
}
asdl_expr_seq *names_with_default_values = _get_defaults(p, names_with_default);
if (!names_with_default_values) {
return -1;
}
*posdefaults = (asdl_expr_seq*)_PyPegen_join_sequences(
p,
(asdl_seq*)slash_with_default_values,
(asdl_seq*)names_with_default_values);
}
else if (slash_with_default == NULL && names_with_default != NULL) {
*posdefaults = _get_defaults(p, names_with_default);
}
else if (slash_with_default != NULL && names_with_default == NULL) {
*posdefaults = _get_defaults(p, slash_with_default->names_with_defaults);
}
else {
*posdefaults = _Py_asdl_expr_seq_new(0, p->arena);
}
return *posdefaults == NULL ? -1 : 0;
}
static int
_make_kwargs(Parser *p, StarEtc *star_etc,
asdl_arg_seq **kwonlyargs,
asdl_expr_seq **kwdefaults) {
if (star_etc != NULL && star_etc->kwonlyargs != NULL) {
*kwonlyargs = _get_names(p, star_etc->kwonlyargs);
}
else {
*kwonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
}
if (*kwonlyargs == NULL) {
return -1;
}
if (star_etc != NULL && star_etc->kwonlyargs != NULL) {
*kwdefaults = _get_defaults(p, star_etc->kwonlyargs);
}
else {
*kwdefaults = _Py_asdl_expr_seq_new(0, p->arena);
}
if (*kwdefaults == NULL) {
return -1;
}
return 0;
}
/* Constructs an arguments_ty object out of all the parsed constructs in the parameters rule */
arguments_ty
_PyPegen_make_arguments(Parser *p, asdl_arg_seq *slash_without_default,
SlashWithDefault *slash_with_default, asdl_arg_seq *plain_names,
asdl_seq *names_with_default, StarEtc *star_etc)
{
asdl_arg_seq *posonlyargs;
if (_make_posonlyargs(p, slash_without_default, slash_with_default, &posonlyargs) == -1) {
return NULL;
}
asdl_arg_seq *posargs;
if (_make_posargs(p, plain_names, names_with_default, &posargs) == -1) {
return NULL;
}
asdl_expr_seq *posdefaults;
if (_make_posdefaults(p,slash_with_default, names_with_default, &posdefaults) == -1) {
return NULL;
}
arg_ty vararg = NULL;
if (star_etc != NULL && star_etc->vararg != NULL) {
vararg = star_etc->vararg;
}
asdl_arg_seq *kwonlyargs;
asdl_expr_seq *kwdefaults;
if (_make_kwargs(p, star_etc, &kwonlyargs, &kwdefaults) == -1) {
return NULL;
}
arg_ty kwarg = NULL;
if (star_etc != NULL && star_etc->kwarg != NULL) {
kwarg = star_etc->kwarg;
}
return _PyAST_arguments(posonlyargs, posargs, vararg, kwonlyargs,
kwdefaults, kwarg, posdefaults, p->arena);
}
/* Constructs an empty arguments_ty object, that gets used when a function accepts no
* arguments. */
arguments_ty
_PyPegen_empty_arguments(Parser *p)
{
asdl_arg_seq *posonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
if (!posonlyargs) {
return NULL;
}
asdl_arg_seq *posargs = _Py_asdl_arg_seq_new(0, p->arena);
if (!posargs) {
return NULL;
}
asdl_expr_seq *posdefaults = _Py_asdl_expr_seq_new(0, p->arena);
if (!posdefaults) {
return NULL;
}
asdl_arg_seq *kwonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
if (!kwonlyargs) {
return NULL;
}
asdl_expr_seq *kwdefaults = _Py_asdl_expr_seq_new(0, p->arena);
if (!kwdefaults) {
return NULL;
}
return _PyAST_arguments(posonlyargs, posargs, NULL, kwonlyargs,
kwdefaults, NULL, posdefaults, p->arena);
}
/* Encapsulates the value of an operator_ty into an AugOperator struct */
AugOperator *
_PyPegen_augoperator(Parser *p, operator_ty kind)
{
AugOperator *a = _PyArena_Malloc(p->arena, sizeof(AugOperator));
if (!a) {
return NULL;
}
a->kind = kind;
return a;
}
/* Construct a FunctionDef equivalent to function_def, but with decorators */
stmt_ty
_PyPegen_function_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty function_def)
{
assert(function_def != NULL);
if (function_def->kind == AsyncFunctionDef_kind) {
return _PyAST_AsyncFunctionDef(
function_def->v.AsyncFunctionDef.name,
function_def->v.AsyncFunctionDef.args,
function_def->v.AsyncFunctionDef.body, decorators,
function_def->v.AsyncFunctionDef.returns,
function_def->v.AsyncFunctionDef.type_comment,
function_def->v.AsyncFunctionDef.type_params,
function_def->lineno, function_def->col_offset,
function_def->end_lineno, function_def->end_col_offset, p->arena);
}
return _PyAST_FunctionDef(
function_def->v.FunctionDef.name,
function_def->v.FunctionDef.args,
function_def->v.FunctionDef.body, decorators,
function_def->v.FunctionDef.returns,
function_def->v.FunctionDef.type_comment,
function_def->v.FunctionDef.type_params,
function_def->lineno, function_def->col_offset,
function_def->end_lineno, function_def->end_col_offset, p->arena);
}
/* Construct a ClassDef equivalent to class_def, but with decorators */
stmt_ty
_PyPegen_class_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty class_def)
{
assert(class_def != NULL);
return _PyAST_ClassDef(
class_def->v.ClassDef.name,
class_def->v.ClassDef.bases, class_def->v.ClassDef.keywords,
class_def->v.ClassDef.body, decorators,
class_def->v.ClassDef.type_params,
class_def->lineno, class_def->col_offset, class_def->end_lineno,
class_def->end_col_offset, p->arena);
}
/* Construct a KeywordOrStarred */
KeywordOrStarred *
_PyPegen_keyword_or_starred(Parser *p, void *element, int is_keyword)
{
KeywordOrStarred *a = _PyArena_Malloc(p->arena, sizeof(KeywordOrStarred));
if (!a) {
return NULL;
}
a->element = element;
a->is_keyword = is_keyword;
return a;
}
/* Get the number of starred expressions in an asdl_seq* of KeywordOrStarred*s */
static int
_seq_number_of_starred_exprs(asdl_seq *seq)
{
int n = 0;
for (Py_ssize_t i = 0, l = asdl_seq_LEN(seq); i < l; i++) {
KeywordOrStarred *k = asdl_seq_GET_UNTYPED(seq, i);
if (!k->is_keyword) {
n++;
}
}
return n;
}
/* Extract the starred expressions of an asdl_seq* of KeywordOrStarred*s */
asdl_expr_seq *
_PyPegen_seq_extract_starred_exprs(Parser *p, asdl_seq *kwargs)
{
int new_len = _seq_number_of_starred_exprs(kwargs);
if (new_len == 0) {
return NULL;
}
asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(new_len, p->arena);
if (!new_seq) {
return NULL;
}
int idx = 0;
for (Py_ssize_t i = 0, len = asdl_seq_LEN(kwargs); i < len; i++) {
KeywordOrStarred *k = asdl_seq_GET_UNTYPED(kwargs, i);
if (!k->is_keyword) {
asdl_seq_SET(new_seq, idx++, k->element);
}
}
return new_seq;
}
/* Return a new asdl_seq* with only the keywords in kwargs */
asdl_keyword_seq*
_PyPegen_seq_delete_starred_exprs(Parser *p, asdl_seq *kwargs)
{
Py_ssize_t len = asdl_seq_LEN(kwargs);
Py_ssize_t new_len = len - _seq_number_of_starred_exprs(kwargs);
if (new_len == 0) {
return NULL;
}
asdl_keyword_seq *new_seq = _Py_asdl_keyword_seq_new(new_len, p->arena);
if (!new_seq) {
return NULL;
}
int idx = 0;
for (Py_ssize_t i = 0; i < len; i++) {
KeywordOrStarred *k = asdl_seq_GET_UNTYPED(kwargs, i);
if (k->is_keyword) {
asdl_seq_SET(new_seq, idx++, k->element);
}
}
return new_seq;
}
expr_ty
_PyPegen_ensure_imaginary(Parser *p, expr_ty exp)
{
if (exp->kind != Constant_kind || !PyComplex_CheckExact(exp->v.Constant.value)) {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(exp, "imaginary number required in complex literal");
return NULL;
}
return exp;
}
expr_ty
_PyPegen_ensure_real(Parser *p, expr_ty exp)
{
if (exp->kind != Constant_kind || PyComplex_CheckExact(exp->v.Constant.value)) {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(exp, "real number required in complex literal");
return NULL;
}
return exp;
}
mod_ty
_PyPegen_make_module(Parser *p, asdl_stmt_seq *a) {
asdl_type_ignore_seq *type_ignores = NULL;
Py_ssize_t num = p->type_ignore_comments.num_items;
if (num > 0) {
// Turn the raw (comment, lineno) pairs into TypeIgnore objects in the arena
type_ignores = _Py_asdl_type_ignore_seq_new(num, p->arena);
if (type_ignores == NULL) {
return NULL;
}
for (int i = 0; i < num; i++) {
PyObject *tag = _PyPegen_new_type_comment(p, p->type_ignore_comments.items[i].comment);
if (tag == NULL) {
return NULL;
}
type_ignore_ty ti = _PyAST_TypeIgnore(p->type_ignore_comments.items[i].lineno,
tag, p->arena);
if (ti == NULL) {
return NULL;
}
asdl_seq_SET(type_ignores, i, ti);
}
}
return _PyAST_Module(a, type_ignores, p->arena);
}
PyObject *
_PyPegen_new_type_comment(Parser *p, const char *s)
{
PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
if (res == NULL) {
return NULL;
}
if (_PyArena_AddPyObject(p->arena, res) < 0) {
Py_DECREF(res);
return NULL;
}
return res;
}
arg_ty
_PyPegen_add_type_comment_to_arg(Parser *p, arg_ty a, Token *tc)
{
if (tc == NULL) {
return a;
}
const char *bytes = PyBytes_AsString(tc->bytes);
if (bytes == NULL) {
return NULL;
}
PyObject *tco = _PyPegen_new_type_comment(p, bytes);
if (tco == NULL) {
return NULL;
}
return _PyAST_arg(a->arg, a->annotation, tco,
a->lineno, a->col_offset, a->end_lineno, a->end_col_offset,
p->arena);
}
/* Checks if the NOTEQUAL token is valid given the current parser flags
0 indicates success and nonzero indicates failure (an exception may be set) */
int
_PyPegen_check_barry_as_flufl(Parser *p, Token* t) {
assert(t->bytes != NULL);
assert(t->type == NOTEQUAL);
const char* tok_str = PyBytes_AS_STRING(t->bytes);
if (p->flags & PyPARSE_BARRY_AS_BDFL && strcmp(tok_str, "<>") != 0) {
RAISE_SYNTAX_ERROR("with Barry as BDFL, use '<>' instead of '!='");
return -1;
}
if (!(p->flags & PyPARSE_BARRY_AS_BDFL)) {
return strcmp(tok_str, "!=");
}
return 0;
}
int
_PyPegen_check_legacy_stmt(Parser *p, expr_ty name) {
if (name->kind != Name_kind) {
return 0;
}
const char* candidates[2] = {"print", "exec"};
for (int i=0; i<2; i++) {
if (PyUnicode_CompareWithASCIIString(name->v.Name.id, candidates[i]) == 0) {
return 1;
}
}
return 0;
}
static ResultTokenWithMetadata *
result_token_with_metadata(Parser *p, void *result, PyObject *metadata)
{
ResultTokenWithMetadata *res = _PyArena_Malloc(p->arena, sizeof(ResultTokenWithMetadata));
if (res == NULL) {
return NULL;
}
res->metadata = metadata;
res->result = result;
return res;
}
ResultTokenWithMetadata *
_PyPegen_check_fstring_conversion(Parser *p, Token* conv_token, expr_ty conv)
{
if (conv_token->lineno != conv->lineno || conv_token->end_col_offset != conv->col_offset) {
return RAISE_SYNTAX_ERROR_KNOWN_RANGE(
conv_token, conv,
"f-string: conversion type must come right after the exclamanation mark"
);
}
return result_token_with_metadata(p, conv, conv_token->metadata);
}
ResultTokenWithMetadata *
_PyPegen_setup_full_format_spec(Parser *p, Token *colon, asdl_expr_seq *spec, int lineno, int col_offset,
int end_lineno, int end_col_offset, PyArena *arena)
{
if (!spec) {
return NULL;
}
// This is needed to keep compatibility with 3.11, where an empty format
// spec is parsed as an *empty* JoinedStr node, instead of having an empty
// constant in it.
Py_ssize_t n_items = asdl_seq_LEN(spec);
Py_ssize_t non_empty_count = 0;
for (Py_ssize_t i = 0; i < n_items; i++) {
expr_ty item = asdl_seq_GET(spec, i);
non_empty_count += !(item->kind == Constant_kind &&
PyUnicode_CheckExact(item->v.Constant.value) &&
PyUnicode_GET_LENGTH(item->v.Constant.value) == 0);
}
if (non_empty_count != n_items) {
asdl_expr_seq *resized_spec =
_Py_asdl_expr_seq_new(non_empty_count, p->arena);
if (resized_spec == NULL) {
return NULL;
}
Py_ssize_t j = 0;
for (Py_ssize_t i = 0; i < n_items; i++) {
expr_ty item = asdl_seq_GET(spec, i);
if (item->kind == Constant_kind &&
PyUnicode_CheckExact(item->v.Constant.value) &&
PyUnicode_GET_LENGTH(item->v.Constant.value) == 0) {
continue;
}
asdl_seq_SET(resized_spec, j++, item);
}
assert(j == non_empty_count);
spec = resized_spec;
}
expr_ty res = _PyAST_JoinedStr(spec, lineno, col_offset, end_lineno,