Coverage Report

Created: 2026-04-20 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Parser/action_helpers.c
Line
Count
Source
1
#include <Python.h>
2
#include "pycore_pystate.h"         // _PyInterpreterState_GET()
3
#include "pycore_runtime.h"         // _PyRuntime
4
#include "pycore_unicodeobject.h"   // _PyUnicode_InternImmortal()
5
6
#include "pegen.h"
7
#include "string_parser.h"          // _PyPegen_decode_string()
8
9
10
void *
11
_PyPegen_dummy_name(Parser *p, ...)
12
57.7k
{
13
57.7k
    return &_PyRuntime.parser.dummy_name;
14
57.7k
}
15
16
/* Creates a single-element asdl_seq* that contains a */
17
asdl_seq *
18
_PyPegen_singleton_seq(Parser *p, void *a)
19
132k
{
20
132k
    assert(a != NULL);
21
132k
    asdl_seq *seq = (asdl_seq*)_Py_asdl_generic_seq_new(1, p->arena);
22
132k
    if (!seq) {
23
0
        return NULL;
24
0
    }
25
132k
    asdl_seq_SET_UNTYPED(seq, 0, a);
26
132k
    return seq;
27
132k
}
28
29
/* Creates a copy of seq and prepends a to it */
30
asdl_seq *
31
_PyPegen_seq_insert_in_front(Parser *p, void *a, asdl_seq *seq)
32
298k
{
33
298k
    assert(a != NULL);
34
298k
    if (!seq) {
35
8.51k
        return _PyPegen_singleton_seq(p, a);
36
8.51k
    }
37
38
290k
    asdl_seq *new_seq = (asdl_seq*)_Py_asdl_generic_seq_new(asdl_seq_LEN(seq) + 1, p->arena);
39
290k
    if (!new_seq) {
40
0
        return NULL;
41
0
    }
42
43
290k
    asdl_seq_SET_UNTYPED(new_seq, 0, a);
44
550k
    for (Py_ssize_t i = 1, l = asdl_seq_LEN(new_seq); i < l; i++) {
45
259k
        asdl_seq_SET_UNTYPED(new_seq, i, asdl_seq_GET_UNTYPED(seq, i - 1));
46
259k
    }
47
290k
    return new_seq;
48
290k
}
49
50
/* Creates a copy of seq and appends a to it */
51
asdl_seq *
52
_PyPegen_seq_append_to_end(Parser *p, asdl_seq *seq, void *a)
53
0
{
54
0
    assert(a != NULL);
55
0
    if (!seq) {
56
0
        return _PyPegen_singleton_seq(p, a);
57
0
    }
58
59
0
    asdl_seq *new_seq = (asdl_seq*)_Py_asdl_generic_seq_new(asdl_seq_LEN(seq) + 1, p->arena);
60
0
    if (!new_seq) {
61
0
        return NULL;
62
0
    }
63
64
0
    for (Py_ssize_t i = 0, l = asdl_seq_LEN(new_seq); i + 1 < l; i++) {
65
0
        asdl_seq_SET_UNTYPED(new_seq, i, asdl_seq_GET_UNTYPED(seq, i));
66
0
    }
67
0
    asdl_seq_SET_UNTYPED(new_seq, asdl_seq_LEN(new_seq) - 1, a);
68
0
    return new_seq;
69
0
}
70
71
static Py_ssize_t
72
_get_flattened_seq_size(asdl_seq *seqs)
73
20.2k
{
74
20.2k
    Py_ssize_t size = 0;
75
104k
    for (Py_ssize_t i = 0, l = asdl_seq_LEN(seqs); i < l; i++) {
76
83.7k
        asdl_seq *inner_seq = asdl_seq_GET_UNTYPED(seqs, i);
77
83.7k
        size += asdl_seq_LEN(inner_seq);
78
83.7k
    }
79
20.2k
    return size;
80
20.2k
}
81
82
/* Flattens an asdl_seq* of asdl_seq*s */
83
asdl_seq *
84
_PyPegen_seq_flatten(Parser *p, asdl_seq *seqs)
85
20.2k
{
86
20.2k
    Py_ssize_t flattened_seq_size = _get_flattened_seq_size(seqs);
87
20.2k
    assert(flattened_seq_size > 0);
88
89
20.2k
    asdl_seq *flattened_seq = (asdl_seq*)_Py_asdl_generic_seq_new(flattened_seq_size, p->arena);
90
20.2k
    if (!flattened_seq) {
91
0
        return NULL;
92
0
    }
93
94
20.2k
    int flattened_seq_idx = 0;
95
104k
    for (Py_ssize_t i = 0, l = asdl_seq_LEN(seqs); i < l; i++) {
96
83.7k
        asdl_seq *inner_seq = asdl_seq_GET_UNTYPED(seqs, i);
97
175k
        for (Py_ssize_t j = 0, li = asdl_seq_LEN(inner_seq); j < li; j++) {
98
91.3k
            asdl_seq_SET_UNTYPED(flattened_seq, flattened_seq_idx++, asdl_seq_GET_UNTYPED(inner_seq, j));
99
91.3k
        }
100
83.7k
    }
101
20.2k
    assert(flattened_seq_idx == flattened_seq_size);
102
103
20.2k
    return flattened_seq;
104
20.2k
}
105
106
void *
107
_PyPegen_seq_last_item(asdl_seq *seq)
108
82
{
109
82
    Py_ssize_t len = asdl_seq_LEN(seq);
110
82
    return asdl_seq_GET_UNTYPED(seq, len - 1);
111
82
}
112
113
void *
114
_PyPegen_seq_first_item(asdl_seq *seq)
115
38
{
116
38
    return asdl_seq_GET_UNTYPED(seq, 0);
117
38
}
118
119
/* Creates a new name of the form <first_name>.<second_name> */
120
expr_ty
121
_PyPegen_join_names_with_dot(Parser *p, expr_ty first_name, expr_ty second_name)
122
6.42k
{
123
6.42k
    assert(first_name != NULL && second_name != NULL);
124
6.42k
    PyObject *uni = PyUnicode_FromFormat("%U.%U",
125
6.42k
            first_name->v.Name.id, second_name->v.Name.id);
126
6.42k
    if (!uni) {
127
0
        return NULL;
128
0
    }
129
6.42k
    PyInterpreterState *interp = _PyInterpreterState_GET();
130
6.42k
    _PyUnicode_InternImmortal(interp, &uni);
131
6.42k
    if (_PyArena_AddPyObject(p->arena, uni) < 0) {
132
0
        Py_DECREF(uni);
133
0
        return NULL;
134
0
    }
135
136
6.42k
    return _PyAST_Name(uni, Load, EXTRA_EXPR(first_name, second_name));
137
6.42k
}
138
139
/* Counts the total number of dots in seq's tokens */
140
int
141
_PyPegen_seq_count_dots(asdl_seq *seq)
142
2.95k
{
143
2.95k
    int number_of_dots = 0;
144
5.65k
    for (Py_ssize_t i = 0, l = asdl_seq_LEN(seq); i < l; i++) {
145
2.70k
        Token *current_expr = asdl_seq_GET_UNTYPED(seq, i);
146
2.70k
        switch (current_expr->type) {
147
585
            case ELLIPSIS:
148
585
                number_of_dots += 3;
149
585
                break;
150
2.11k
            case DOT:
151
2.11k
                number_of_dots += 1;
152
2.11k
                break;
153
0
            default:
154
0
                Py_UNREACHABLE();
155
2.70k
        }
156
2.70k
    }
157
158
2.95k
    return number_of_dots;
159
2.95k
}
160
161
/* Creates an alias with '*' as the identifier name */
162
alias_ty
163
_PyPegen_alias_for_star(Parser *p, int lineno, int col_offset, int end_lineno,
164
180
                        int end_col_offset, PyArena *arena) {
165
180
    PyObject *str = PyUnicode_InternFromString("*");
166
180
    if (!str) {
167
0
        return NULL;
168
0
    }
169
180
    if (_PyArena_AddPyObject(p->arena, str) < 0) {
170
0
        Py_DECREF(str);
171
0
        return NULL;
172
0
    }
173
180
    return _PyAST_alias(str, NULL, lineno, col_offset, end_lineno, end_col_offset, arena);
174
180
}
175
176
/* Creates a new asdl_seq* with the identifiers of all the names in seq */
177
asdl_identifier_seq *
178
_PyPegen_map_names_to_ids(Parser *p, asdl_expr_seq *seq)
179
1.59k
{
180
1.59k
    Py_ssize_t len = asdl_seq_LEN(seq);
181
1.59k
    assert(len > 0);
182
183
1.59k
    asdl_identifier_seq *new_seq = _Py_asdl_identifier_seq_new(len, p->arena);
184
1.59k
    if (!new_seq) {
185
0
        return NULL;
186
0
    }
187
4.49k
    for (Py_ssize_t i = 0; i < len; i++) {
188
2.90k
        expr_ty e = asdl_seq_GET(seq, i);
189
2.90k
        asdl_seq_SET(new_seq, i, e->v.Name.id);
190
2.90k
    }
191
1.59k
    return new_seq;
192
1.59k
}
193
194
/* Constructs a CmpopExprPair */
195
CmpopExprPair *
196
_PyPegen_cmpop_expr_pair(Parser *p, cmpop_ty cmpop, expr_ty expr)
197
17.1k
{
198
17.1k
    assert(expr != NULL);
199
17.1k
    CmpopExprPair *a = _PyArena_Malloc(p->arena, sizeof(CmpopExprPair));
200
17.1k
    if (!a) {
201
0
        return NULL;
202
0
    }
203
17.1k
    a->cmpop = cmpop;
204
17.1k
    a->expr = expr;
205
17.1k
    return a;
206
17.1k
}
207
208
asdl_int_seq *
209
_PyPegen_get_cmpops(Parser *p, asdl_seq *seq)
210
6.53k
{
211
6.53k
    Py_ssize_t len = asdl_seq_LEN(seq);
212
6.53k
    assert(len > 0);
213
214
6.53k
    asdl_int_seq *new_seq = _Py_asdl_int_seq_new(len, p->arena);
215
6.53k
    if (!new_seq) {
216
0
        return NULL;
217
0
    }
218
23.1k
    for (Py_ssize_t i = 0; i < len; i++) {
219
16.5k
        CmpopExprPair *pair = asdl_seq_GET_UNTYPED(seq, i);
220
16.5k
        asdl_seq_SET(new_seq, i, pair->cmpop);
221
16.5k
    }
222
6.53k
    return new_seq;
223
6.53k
}
224
225
asdl_expr_seq *
226
_PyPegen_get_exprs(Parser *p, asdl_seq *seq)
227
6.53k
{
228
6.53k
    Py_ssize_t len = asdl_seq_LEN(seq);
229
6.53k
    assert(len > 0);
230
231
6.53k
    asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
232
6.53k
    if (!new_seq) {
233
0
        return NULL;
234
0
    }
235
23.1k
    for (Py_ssize_t i = 0; i < len; i++) {
236
16.5k
        CmpopExprPair *pair = asdl_seq_GET_UNTYPED(seq, i);
237
16.5k
        asdl_seq_SET(new_seq, i, pair->expr);
238
16.5k
    }
239
6.53k
    return new_seq;
240
6.53k
}
241
242
/* Creates an asdl_seq* where all the elements have been changed to have ctx as context */
243
static asdl_expr_seq *
244
_set_seq_context(Parser *p, asdl_expr_seq *seq, expr_context_ty ctx)
245
3.38k
{
246
3.38k
    Py_ssize_t len = asdl_seq_LEN(seq);
247
3.38k
    if (len == 0) {
248
1.55k
        return NULL;
249
1.55k
    }
250
251
1.83k
    asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
252
1.83k
    if (!new_seq) {
253
0
        return NULL;
254
0
    }
255
5.13k
    for (Py_ssize_t i = 0; i < len; i++) {
256
3.30k
        expr_ty e = asdl_seq_GET(seq, i);
257
3.30k
        asdl_seq_SET(new_seq, i, _PyPegen_set_expr_context(p, e, ctx));
258
3.30k
    }
259
1.83k
    return new_seq;
260
1.83k
}
261
262
static expr_ty
263
_set_name_context(Parser *p, expr_ty e, expr_context_ty ctx)
264
175k
{
265
175k
    return _PyAST_Name(e->v.Name.id, ctx, EXTRA_EXPR(e, e));
266
175k
}
267
268
static expr_ty
269
_set_tuple_context(Parser *p, expr_ty e, expr_context_ty ctx)
270
2.80k
{
271
2.80k
    return _PyAST_Tuple(
272
2.80k
            _set_seq_context(p, e->v.Tuple.elts, ctx),
273
2.80k
            ctx,
274
2.80k
            EXTRA_EXPR(e, e));
275
2.80k
}
276
277
static expr_ty
278
_set_list_context(Parser *p, expr_ty e, expr_context_ty ctx)
279
588
{
280
588
    return _PyAST_List(
281
588
            _set_seq_context(p, e->v.List.elts, ctx),
282
588
            ctx,
283
588
            EXTRA_EXPR(e, e));
284
588
}
285
286
static expr_ty
287
_set_subscript_context(Parser *p, expr_ty e, expr_context_ty ctx)
288
83
{
289
83
    return _PyAST_Subscript(e->v.Subscript.value, e->v.Subscript.slice,
290
83
                            ctx, EXTRA_EXPR(e, e));
291
83
}
292
293
static expr_ty
294
_set_attribute_context(Parser *p, expr_ty e, expr_context_ty ctx)
295
218
{
296
218
    return _PyAST_Attribute(e->v.Attribute.value, e->v.Attribute.attr,
297
218
                            ctx, EXTRA_EXPR(e, e));
298
218
}
299
300
static expr_ty
301
_set_starred_context(Parser *p, expr_ty e, expr_context_ty ctx)
302
271
{
303
271
    return _PyAST_Starred(_PyPegen_set_expr_context(p, e->v.Starred.value, ctx),
304
271
                          ctx, EXTRA_EXPR(e, e));
305
271
}
306
307
/* Creates an `expr_ty` equivalent to `expr` but with `ctx` as context */
308
expr_ty
309
_PyPegen_set_expr_context(Parser *p, expr_ty expr, expr_context_ty ctx)
310
179k
{
311
179k
    assert(expr != NULL);
312
313
179k
    expr_ty new = NULL;
314
179k
    switch (expr->kind) {
315
175k
        case Name_kind:
316
175k
            new = _set_name_context(p, expr, ctx);
317
175k
            break;
318
2.80k
        case Tuple_kind:
319
2.80k
            new = _set_tuple_context(p, expr, ctx);
320
2.80k
            break;
321
588
        case List_kind:
322
588
            new = _set_list_context(p, expr, ctx);
323
588
            break;
324
83
        case Subscript_kind:
325
83
            new = _set_subscript_context(p, expr, ctx);
326
83
            break;
327
218
        case Attribute_kind:
328
218
            new = _set_attribute_context(p, expr, ctx);
329
218
            break;
330
271
        case Starred_kind:
331
271
            new = _set_starred_context(p, expr, ctx);
332
271
            break;
333
0
        default:
334
0
            new = expr;
335
179k
    }
336
179k
    return new;
337
179k
}
338
339
/* Constructs a KeyValuePair that is used when parsing a dict's key value pairs */
340
KeyValuePair *
341
_PyPegen_key_value_pair(Parser *p, expr_ty key, expr_ty value)
342
35.1k
{
343
35.1k
    KeyValuePair *a = _PyArena_Malloc(p->arena, sizeof(KeyValuePair));
344
35.1k
    if (!a) {
345
0
        return NULL;
346
0
    }
347
35.1k
    a->key = key;
348
35.1k
    a->value = value;
349
35.1k
    return a;
350
35.1k
}
351
352
/* Extracts all keys from an asdl_seq* of KeyValuePair*'s */
353
asdl_expr_seq *
354
_PyPegen_get_keys(Parser *p, asdl_seq *seq)
355
6.29k
{
356
6.29k
    Py_ssize_t len = asdl_seq_LEN(seq);
357
6.29k
    asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
358
6.29k
    if (!new_seq) {
359
0
        return NULL;
360
0
    }
361
29.5k
    for (Py_ssize_t i = 0; i < len; i++) {
362
23.2k
        KeyValuePair *pair = asdl_seq_GET_UNTYPED(seq, i);
363
23.2k
        asdl_seq_SET(new_seq, i, pair->key);
364
23.2k
    }
365
6.29k
    return new_seq;
366
6.29k
}
367
368
/* Extracts all values from an asdl_seq* of KeyValuePair*'s */
369
asdl_expr_seq *
370
_PyPegen_get_values(Parser *p, asdl_seq *seq)
371
6.29k
{
372
6.29k
    Py_ssize_t len = asdl_seq_LEN(seq);
373
6.29k
    asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
374
6.29k
    if (!new_seq) {
375
0
        return NULL;
376
0
    }
377
29.5k
    for (Py_ssize_t i = 0; i < len; i++) {
378
23.2k
        KeyValuePair *pair = asdl_seq_GET_UNTYPED(seq, i);
379
23.2k
        asdl_seq_SET(new_seq, i, pair->value);
380
23.2k
    }
381
6.29k
    return new_seq;
382
6.29k
}
383
384
/* Constructs a KeyPatternPair that is used when parsing mapping & class patterns */
385
KeyPatternPair *
386
_PyPegen_key_pattern_pair(Parser *p, expr_ty key, pattern_ty pattern)
387
11.1k
{
388
11.1k
    KeyPatternPair *a = _PyArena_Malloc(p->arena, sizeof(KeyPatternPair));
389
11.1k
    if (!a) {
390
0
        return NULL;
391
0
    }
392
11.1k
    a->key = key;
393
11.1k
    a->pattern = pattern;
394
11.1k
    return a;
395
11.1k
}
396
397
/* Extracts all keys from an asdl_seq* of KeyPatternPair*'s */
398
asdl_expr_seq *
399
_PyPegen_get_pattern_keys(Parser *p, asdl_seq *seq)
400
1.37k
{
401
1.37k
    Py_ssize_t len = asdl_seq_LEN(seq);
402
1.37k
    asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
403
1.37k
    if (!new_seq) {
404
0
        return NULL;
405
0
    }
406
3.54k
    for (Py_ssize_t i = 0; i < len; i++) {
407
2.17k
        KeyPatternPair *pair = asdl_seq_GET_UNTYPED(seq, i);
408
2.17k
        asdl_seq_SET(new_seq, i, pair->key);
409
2.17k
    }
410
1.37k
    return new_seq;
411
1.37k
}
412
413
/* Extracts all patterns from an asdl_seq* of KeyPatternPair*'s */
414
asdl_pattern_seq *
415
_PyPegen_get_patterns(Parser *p, asdl_seq *seq)
416
1.37k
{
417
1.37k
    Py_ssize_t len = asdl_seq_LEN(seq);
418
1.37k
    asdl_pattern_seq *new_seq = _Py_asdl_pattern_seq_new(len, p->arena);
419
1.37k
    if (!new_seq) {
420
0
        return NULL;
421
0
    }
422
3.54k
    for (Py_ssize_t i = 0; i < len; i++) {
423
2.17k
        KeyPatternPair *pair = asdl_seq_GET_UNTYPED(seq, i);
424
2.17k
        asdl_seq_SET(new_seq, i, pair->pattern);
425
2.17k
    }
426
1.37k
    return new_seq;
427
1.37k
}
428
429
/* Constructs a NameDefaultPair */
430
NameDefaultPair *
431
_PyPegen_name_default_pair(Parser *p, arg_ty arg, expr_ty value, Token *tc)
432
546k
{
433
546k
    NameDefaultPair *a = _PyArena_Malloc(p->arena, sizeof(NameDefaultPair));
434
546k
    if (!a) {
435
0
        return NULL;
436
0
    }
437
546k
    a->arg = _PyPegen_add_type_comment_to_arg(p, arg, tc);
438
546k
    if (!a->arg) {
439
0
        return NULL;
440
0
    }
441
546k
    a->value = value;
442
546k
    return a;
443
546k
}
444
445
/* Constructs a SlashWithDefault */
446
SlashWithDefault *
447
_PyPegen_slash_with_default(Parser *p, asdl_arg_seq *plain_names, asdl_seq *names_with_defaults)
448
6.33k
{
449
6.33k
    SlashWithDefault *a = _PyArena_Malloc(p->arena, sizeof(SlashWithDefault));
450
6.33k
    if (!a) {
451
0
        return NULL;
452
0
    }
453
6.33k
    a->plain_names = plain_names;
454
6.33k
    a->names_with_defaults = names_with_defaults;
455
6.33k
    return a;
456
6.33k
}
457
458
/* Constructs a StarEtc */
459
StarEtc *
460
_PyPegen_star_etc(Parser *p, arg_ty vararg, asdl_seq *kwonlyargs, arg_ty kwarg)
461
4.58k
{
462
4.58k
    StarEtc *a = _PyArena_Malloc(p->arena, sizeof(StarEtc));
463
4.58k
    if (!a) {
464
0
        return NULL;
465
0
    }
466
4.58k
    a->vararg = vararg;
467
4.58k
    a->kwonlyargs = kwonlyargs;
468
4.58k
    a->kwarg = kwarg;
469
4.58k
    return a;
470
4.58k
}
471
472
asdl_seq *
473
_PyPegen_join_sequences(Parser *p, asdl_seq *a, asdl_seq *b)
474
327k
{
475
327k
    Py_ssize_t first_len = asdl_seq_LEN(a);
476
327k
    Py_ssize_t second_len = asdl_seq_LEN(b);
477
327k
    asdl_seq *new_seq = (asdl_seq*)_Py_asdl_generic_seq_new(first_len + second_len, p->arena);
478
327k
    if (!new_seq) {
479
0
        return NULL;
480
0
    }
481
482
327k
    int k = 0;
483
671k
    for (Py_ssize_t i = 0; i < first_len; i++) {
484
344k
        asdl_seq_SET_UNTYPED(new_seq, k++, asdl_seq_GET_UNTYPED(a, i));
485
344k
    }
486
336k
    for (Py_ssize_t i = 0; i < second_len; i++) {
487
9.12k
        asdl_seq_SET_UNTYPED(new_seq, k++, asdl_seq_GET_UNTYPED(b, i));
488
9.12k
    }
489
490
327k
    return new_seq;
491
327k
}
492
493
static asdl_arg_seq*
494
_get_names(Parser *p, asdl_seq *names_with_defaults)
495
331k
{
496
331k
    Py_ssize_t len = asdl_seq_LEN(names_with_defaults);
497
331k
    asdl_arg_seq *seq = _Py_asdl_arg_seq_new(len, p->arena);
498
331k
    if (!seq) {
499
0
        return NULL;
500
0
    }
501
346k
    for (Py_ssize_t i = 0; i < len; i++) {
502
14.8k
        NameDefaultPair *pair = asdl_seq_GET_UNTYPED(names_with_defaults, i);
503
14.8k
        asdl_seq_SET(seq, i, pair->arg);
504
14.8k
    }
505
331k
    return seq;
506
331k
}
507
508
static asdl_expr_seq *
509
_get_defaults(Parser *p, asdl_seq *names_with_defaults)
510
331k
{
511
331k
    Py_ssize_t len = asdl_seq_LEN(names_with_defaults);
512
331k
    asdl_expr_seq *seq = _Py_asdl_expr_seq_new(len, p->arena);
513
331k
    if (!seq) {
514
0
        return NULL;
515
0
    }
516
346k
    for (Py_ssize_t i = 0; i < len; i++) {
517
14.8k
        NameDefaultPair *pair = asdl_seq_GET_UNTYPED(names_with_defaults, i);
518
14.8k
        asdl_seq_SET(seq, i, pair->value);
519
14.8k
    }
520
331k
    return seq;
521
331k
}
522
523
static int
524
_make_posonlyargs(Parser *p,
525
                  asdl_arg_seq *slash_without_default,
526
                  SlashWithDefault *slash_with_default,
527
329k
                  asdl_arg_seq **posonlyargs) {
528
329k
    if (slash_without_default != NULL) {
529
1.29k
        *posonlyargs = slash_without_default;
530
1.29k
    }
531
328k
    else if (slash_with_default != NULL) {
532
2.04k
        asdl_arg_seq *slash_with_default_names =
533
2.04k
                _get_names(p, slash_with_default->names_with_defaults);
534
2.04k
        if (!slash_with_default_names) {
535
0
            return -1;
536
0
        }
537
2.04k
        *posonlyargs = (asdl_arg_seq*)_PyPegen_join_sequences(
538
2.04k
                p,
539
2.04k
                (asdl_seq*)slash_with_default->plain_names,
540
2.04k
                (asdl_seq*)slash_with_default_names);
541
2.04k
    }
542
326k
    else {
543
326k
        *posonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
544
326k
    }
545
329k
    return *posonlyargs == NULL ? -1 : 0;
546
329k
}
547
548
static int
549
_make_posargs(Parser *p,
550
              asdl_arg_seq *plain_names,
551
              asdl_seq *names_with_default,
552
329k
              asdl_arg_seq **posargs) {
553
554
329k
    if (names_with_default != NULL) {
555
326k
        if (plain_names != NULL) {
556
322k
            asdl_arg_seq *names_with_default_names = _get_names(p, names_with_default);
557
322k
            if (!names_with_default_names) {
558
0
                return -1;
559
0
            }
560
322k
            *posargs = (asdl_arg_seq*)_PyPegen_join_sequences(
561
322k
                    p,(asdl_seq*)plain_names, (asdl_seq*)names_with_default_names);
562
322k
        }
563
3.54k
        else {
564
3.54k
            *posargs = _get_names(p, names_with_default);
565
3.54k
        }
566
326k
    }
567
3.74k
    else {
568
3.74k
        if (plain_names != NULL) {
569
            // With the current grammar, we never get here.
570
            // If that has changed, remove the assert, and test thoroughly.
571
0
            assert(0);
572
0
            *posargs = plain_names;
573
0
        }
574
3.74k
        else {
575
3.74k
            *posargs = _Py_asdl_arg_seq_new(0, p->arena);
576
3.74k
        }
577
3.74k
    }
578
329k
    return *posargs == NULL ? -1 : 0;
579
329k
}
580
581
static int
582
_make_posdefaults(Parser *p,
583
                  SlashWithDefault *slash_with_default,
584
                  asdl_seq *names_with_default,
585
329k
                  asdl_expr_seq **posdefaults) {
586
329k
    if (slash_with_default != NULL && names_with_default != NULL) {
587
2.04k
        asdl_expr_seq *slash_with_default_values =
588
2.04k
                _get_defaults(p, slash_with_default->names_with_defaults);
589
2.04k
        if (!slash_with_default_values) {
590
0
            return -1;
591
0
        }
592
2.04k
        asdl_expr_seq *names_with_default_values = _get_defaults(p, names_with_default);
593
2.04k
        if (!names_with_default_values) {
594
0
            return -1;
595
0
        }
596
2.04k
        *posdefaults = (asdl_expr_seq*)_PyPegen_join_sequences(
597
2.04k
                p,
598
2.04k
                (asdl_seq*)slash_with_default_values,
599
2.04k
                (asdl_seq*)names_with_default_values);
600
2.04k
    }
601
327k
    else if (slash_with_default == NULL && names_with_default != NULL) {
602
324k
        *posdefaults = _get_defaults(p, names_with_default);
603
324k
    }
604
3.74k
    else if (slash_with_default != NULL && names_with_default == NULL) {
605
0
        *posdefaults = _get_defaults(p, slash_with_default->names_with_defaults);
606
0
    }
607
3.74k
    else {
608
3.74k
        *posdefaults = _Py_asdl_expr_seq_new(0, p->arena);
609
3.74k
    }
610
329k
    return *posdefaults == NULL ? -1 : 0;
611
329k
}
612
613
static int
614
_make_kwargs(Parser *p, StarEtc *star_etc,
615
             asdl_arg_seq **kwonlyargs,
616
329k
             asdl_expr_seq **kwdefaults) {
617
329k
    if (star_etc != NULL && star_etc->kwonlyargs != NULL) {
618
3.32k
        *kwonlyargs = _get_names(p, star_etc->kwonlyargs);
619
3.32k
    }
620
326k
    else {
621
326k
        *kwonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
622
326k
    }
623
624
329k
    if (*kwonlyargs == NULL) {
625
0
        return -1;
626
0
    }
627
628
329k
    if (star_etc != NULL && star_etc->kwonlyargs != NULL) {
629
3.32k
        *kwdefaults = _get_defaults(p, star_etc->kwonlyargs);
630
3.32k
    }
631
326k
    else {
632
326k
        *kwdefaults = _Py_asdl_expr_seq_new(0, p->arena);
633
326k
    }
634
635
329k
    if (*kwdefaults == NULL) {
636
0
        return -1;
637
0
    }
638
639
329k
    return 0;
640
329k
}
641
642
/* Constructs an arguments_ty object out of all the parsed constructs in the parameters rule */
643
arguments_ty
644
_PyPegen_make_arguments(Parser *p, asdl_arg_seq *slash_without_default,
645
                        SlashWithDefault *slash_with_default, asdl_arg_seq *plain_names,
646
                        asdl_seq *names_with_default, StarEtc *star_etc)
647
329k
{
648
329k
    asdl_arg_seq *posonlyargs;
649
329k
    if (_make_posonlyargs(p, slash_without_default, slash_with_default, &posonlyargs) == -1) {
650
0
        return NULL;
651
0
    }
652
653
329k
    asdl_arg_seq *posargs;
654
329k
    if (_make_posargs(p, plain_names, names_with_default, &posargs) == -1) {
655
0
        return NULL;
656
0
    }
657
658
329k
    asdl_expr_seq *posdefaults;
659
329k
    if (_make_posdefaults(p,slash_with_default, names_with_default, &posdefaults) == -1) {
660
0
        return NULL;
661
0
    }
662
663
329k
    arg_ty vararg = NULL;
664
329k
    if (star_etc != NULL && star_etc->vararg != NULL) {
665
2.18k
        vararg = star_etc->vararg;
666
2.18k
    }
667
668
329k
    asdl_arg_seq *kwonlyargs;
669
329k
    asdl_expr_seq *kwdefaults;
670
329k
    if (_make_kwargs(p, star_etc, &kwonlyargs, &kwdefaults) == -1) {
671
0
        return NULL;
672
0
    }
673
674
329k
    arg_ty kwarg = NULL;
675
329k
    if (star_etc != NULL && star_etc->kwarg != NULL) {
676
1.36k
        kwarg = star_etc->kwarg;
677
1.36k
    }
678
679
329k
    return _PyAST_arguments(posonlyargs, posargs, vararg, kwonlyargs,
680
329k
                            kwdefaults, kwarg, posdefaults, p->arena);
681
329k
}
682
683
684
/* Constructs an empty arguments_ty object, that gets used when a function accepts no
685
 * arguments. */
686
arguments_ty
687
_PyPegen_empty_arguments(Parser *p)
688
2.30k
{
689
2.30k
    asdl_arg_seq *posonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
690
2.30k
    if (!posonlyargs) {
691
0
        return NULL;
692
0
    }
693
2.30k
    asdl_arg_seq *posargs = _Py_asdl_arg_seq_new(0, p->arena);
694
2.30k
    if (!posargs) {
695
0
        return NULL;
696
0
    }
697
2.30k
    asdl_expr_seq *posdefaults = _Py_asdl_expr_seq_new(0, p->arena);
698
2.30k
    if (!posdefaults) {
699
0
        return NULL;
700
0
    }
701
2.30k
    asdl_arg_seq *kwonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
702
2.30k
    if (!kwonlyargs) {
703
0
        return NULL;
704
0
    }
705
2.30k
    asdl_expr_seq *kwdefaults = _Py_asdl_expr_seq_new(0, p->arena);
706
2.30k
    if (!kwdefaults) {
707
0
        return NULL;
708
0
    }
709
710
2.30k
    return _PyAST_arguments(posonlyargs, posargs, NULL, kwonlyargs,
711
2.30k
                            kwdefaults, NULL, posdefaults, p->arena);
712
2.30k
}
713
714
/* Encapsulates the value of an operator_ty into an AugOperator struct */
715
AugOperator *
716
_PyPegen_augoperator(Parser *p, operator_ty kind)
717
3.05k
{
718
3.05k
    AugOperator *a = _PyArena_Malloc(p->arena, sizeof(AugOperator));
719
3.05k
    if (!a) {
720
0
        return NULL;
721
0
    }
722
3.05k
    a->kind = kind;
723
3.05k
    return a;
724
3.05k
}
725
726
/* Construct a FunctionDef equivalent to function_def, but with decorators */
727
stmt_ty
728
_PyPegen_function_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty function_def)
729
434
{
730
434
    assert(function_def != NULL);
731
434
    if (function_def->kind == AsyncFunctionDef_kind) {
732
93
        return _PyAST_AsyncFunctionDef(
733
93
            function_def->v.AsyncFunctionDef.name,
734
93
            function_def->v.AsyncFunctionDef.args,
735
93
            function_def->v.AsyncFunctionDef.body, decorators,
736
93
            function_def->v.AsyncFunctionDef.returns,
737
93
            function_def->v.AsyncFunctionDef.type_comment,
738
93
            function_def->v.AsyncFunctionDef.type_params,
739
93
            function_def->lineno, function_def->col_offset,
740
93
            function_def->end_lineno, function_def->end_col_offset, p->arena);
741
93
    }
742
743
341
    return _PyAST_FunctionDef(
744
341
        function_def->v.FunctionDef.name,
745
341
        function_def->v.FunctionDef.args,
746
341
        function_def->v.FunctionDef.body, decorators,
747
341
        function_def->v.FunctionDef.returns,
748
341
        function_def->v.FunctionDef.type_comment,
749
341
        function_def->v.FunctionDef.type_params,
750
341
        function_def->lineno, function_def->col_offset,
751
341
        function_def->end_lineno, function_def->end_col_offset, p->arena);
752
434
}
753
754
/* Construct a ClassDef equivalent to class_def, but with decorators */
755
stmt_ty
756
_PyPegen_class_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty class_def)
757
281
{
758
281
    assert(class_def != NULL);
759
281
    return _PyAST_ClassDef(
760
281
        class_def->v.ClassDef.name,
761
281
        class_def->v.ClassDef.bases, class_def->v.ClassDef.keywords,
762
281
        class_def->v.ClassDef.body, decorators,
763
281
        class_def->v.ClassDef.type_params,
764
281
        class_def->lineno, class_def->col_offset, class_def->end_lineno,
765
281
        class_def->end_col_offset, p->arena);
766
281
}
767
768
/* Construct a KeywordOrStarred */
769
KeywordOrStarred *
770
_PyPegen_keyword_or_starred(Parser *p, void *element, int is_keyword)
771
35.7k
{
772
35.7k
    KeywordOrStarred *a = _PyArena_Malloc(p->arena, sizeof(KeywordOrStarred));
773
35.7k
    if (!a) {
774
0
        return NULL;
775
0
    }
776
35.7k
    a->element = element;
777
35.7k
    a->is_keyword = is_keyword;
778
35.7k
    return a;
779
35.7k
}
780
781
/* Get the number of starred expressions in an asdl_seq* of KeywordOrStarred*s */
782
static int
783
_seq_number_of_starred_exprs(asdl_seq *seq)
784
16.0k
{
785
16.0k
    int n = 0;
786
59.8k
    for (Py_ssize_t i = 0, l = asdl_seq_LEN(seq); i < l; i++) {
787
43.8k
        KeywordOrStarred *k = asdl_seq_GET_UNTYPED(seq, i);
788
43.8k
        if (!k->is_keyword) {
789
2.65k
            n++;
790
2.65k
        }
791
43.8k
    }
792
16.0k
    return n;
793
16.0k
}
794
795
/* Extract the starred expressions of an asdl_seq* of KeywordOrStarred*s */
796
asdl_expr_seq *
797
_PyPegen_seq_extract_starred_exprs(Parser *p, asdl_seq *kwargs)
798
8.00k
{
799
8.00k
    int new_len = _seq_number_of_starred_exprs(kwargs);
800
8.00k
    if (new_len == 0) {
801
7.52k
        return NULL;
802
7.52k
    }
803
477
    asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(new_len, p->arena);
804
477
    if (!new_seq) {
805
0
        return NULL;
806
0
    }
807
808
477
    int idx = 0;
809
2.90k
    for (Py_ssize_t i = 0, len = asdl_seq_LEN(kwargs); i < len; i++) {
810
2.42k
        KeywordOrStarred *k = asdl_seq_GET_UNTYPED(kwargs, i);
811
2.42k
        if (!k->is_keyword) {
812
1.32k
            asdl_seq_SET(new_seq, idx++, k->element);
813
1.32k
        }
814
2.42k
    }
815
477
    return new_seq;
816
477
}
817
818
/* Return a new asdl_seq* with only the keywords in kwargs */
819
asdl_keyword_seq*
820
_PyPegen_seq_delete_starred_exprs(Parser *p, asdl_seq *kwargs)
821
8.00k
{
822
8.00k
    Py_ssize_t len = asdl_seq_LEN(kwargs);
823
8.00k
    Py_ssize_t new_len = len - _seq_number_of_starred_exprs(kwargs);
824
8.00k
    if (new_len == 0) {
825
0
        return NULL;
826
0
    }
827
8.00k
    asdl_keyword_seq *new_seq = _Py_asdl_keyword_seq_new(new_len, p->arena);
828
8.00k
    if (!new_seq) {
829
0
        return NULL;
830
0
    }
831
832
8.00k
    int idx = 0;
833
29.9k
    for (Py_ssize_t i = 0; i < len; i++) {
834
21.9k
        KeywordOrStarred *k = asdl_seq_GET_UNTYPED(kwargs, i);
835
21.9k
        if (k->is_keyword) {
836
20.5k
            asdl_seq_SET(new_seq, idx++, k->element);
837
20.5k
        }
838
21.9k
    }
839
8.00k
    return new_seq;
840
8.00k
}
841
842
expr_ty
843
_PyPegen_ensure_imaginary(Parser *p, expr_ty exp)
844
328
{
845
328
    if (exp->kind != Constant_kind || !PyComplex_CheckExact(exp->v.Constant.value)) {
846
10
        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(exp, "imaginary number required in complex literal");
847
10
        return NULL;
848
10
    }
849
318
    return exp;
850
328
}
851
852
expr_ty
853
_PyPegen_ensure_real(Parser *p, expr_ty exp)
854
576
{
855
576
    if (exp->kind != Constant_kind || PyComplex_CheckExact(exp->v.Constant.value)) {
856
3
        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(exp, "real number required in complex literal");
857
3
        return NULL;
858
3
    }
859
573
    return exp;
860
576
}
861
862
mod_ty
863
7.03k
_PyPegen_make_module(Parser *p, asdl_stmt_seq *a) {
864
7.03k
    asdl_type_ignore_seq *type_ignores = NULL;
865
7.03k
    Py_ssize_t num = p->type_ignore_comments.num_items;
866
7.03k
    if (num > 0) {
867
        // Turn the raw (comment, lineno) pairs into TypeIgnore objects in the arena
868
0
        type_ignores = _Py_asdl_type_ignore_seq_new(num, p->arena);
869
0
        if (type_ignores == NULL) {
870
0
            return NULL;
871
0
        }
872
0
        for (Py_ssize_t i = 0; i < num; i++) {
873
0
            PyObject *tag = _PyPegen_new_type_comment(p, p->type_ignore_comments.items[i].comment);
874
0
            if (tag == NULL) {
875
0
                return NULL;
876
0
            }
877
0
            type_ignore_ty ti = _PyAST_TypeIgnore(p->type_ignore_comments.items[i].lineno,
878
0
                                                  tag, p->arena);
879
0
            if (ti == NULL) {
880
0
                return NULL;
881
0
            }
882
0
            asdl_seq_SET(type_ignores, i, ti);
883
0
        }
884
0
    }
885
7.03k
    return _PyAST_Module(a, type_ignores, p->arena);
886
7.03k
}
887
888
PyObject *
889
_PyPegen_new_type_comment(Parser *p, const char *s)
890
0
{
891
0
    PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
892
0
    if (res == NULL) {
893
0
        return NULL;
894
0
    }
895
0
    if (_PyArena_AddPyObject(p->arena, res) < 0) {
896
0
        Py_DECREF(res);
897
0
        return NULL;
898
0
    }
899
0
    return res;
900
0
}
901
902
arg_ty
903
_PyPegen_add_type_comment_to_arg(Parser *p, arg_ty a, Token *tc)
904
612k
{
905
612k
    if (tc == NULL) {
906
612k
        return a;
907
612k
    }
908
0
    const char *bytes = PyBytes_AsString(tc->bytes);
909
0
    if (bytes == NULL) {
910
0
        return NULL;
911
0
    }
912
0
    PyObject *tco = _PyPegen_new_type_comment(p, bytes);
913
0
    if (tco == NULL) {
914
0
        return NULL;
915
0
    }
916
0
    return _PyAST_arg(a->arg, a->annotation, tco,
917
0
                      a->lineno, a->col_offset, a->end_lineno, a->end_col_offset,
918
0
                      p->arena);
919
0
}
920
921
/* Checks if the NOTEQUAL token is valid given the current parser flags
922
0 indicates success and nonzero indicates failure (an exception may be set) */
923
int
924
1.08k
_PyPegen_check_barry_as_flufl(Parser *p, Token* t) {
925
1.08k
    assert(t->bytes != NULL);
926
1.08k
    assert(t->type == NOTEQUAL);
927
928
1.08k
    const char* tok_str = PyBytes_AS_STRING(t->bytes);
929
1.08k
    if (p->flags & PyPARSE_BARRY_AS_BDFL && strcmp(tok_str, "<>") != 0) {
930
1
        RAISE_SYNTAX_ERROR("with Barry as BDFL, use '<>' instead of '!='");
931
1
        return -1;
932
1
    }
933
1.08k
    if (!(p->flags & PyPARSE_BARRY_AS_BDFL)) {
934
1.05k
        return strcmp(tok_str, "!=");
935
1.05k
    }
936
34
    return 0;
937
1.08k
}
938
939
int
940
8.48k
_PyPegen_check_legacy_stmt(Parser *p, expr_ty name) {
941
8.48k
    if (name->kind != Name_kind) {
942
2.29k
        return 0;
943
2.29k
    }
944
6.18k
    const char* candidates[2] = {"print", "exec"};
945
18.4k
    for (int i=0; i<2; i++) {
946
12.3k
        if (PyUnicode_CompareWithASCIIString(name->v.Name.id, candidates[i]) == 0) {
947
74
            return 1;
948
74
        }
949
12.3k
    }
950
6.11k
    return 0;
951
6.18k
}
952
953
void *
954
_PyPegen_raise_error_for_missing_comma(Parser *p, expr_ty a, expr_ty b)
955
4.22k
{
956
    // Don't raise for legacy statements like "print x" or "exec x"
957
4.22k
    if (_PyPegen_check_legacy_stmt(p, a)) {
958
66
        return NULL;
959
66
    }
960
    // Only raise inside parentheses/brackets (level > 0)
961
4.15k
    if (p->tokens[p->mark - 1]->level == 0) {
962
3.98k
        return NULL;
963
3.98k
    }
964
    // For multi-line expressions (like string concatenations), point to the
965
    // last line instead of the first for a more helpful error message.
966
    // Use a->col_offset as the starting column since all strings in the
967
    // concatenation typically share the same indentation.
968
175
    if (a->end_lineno > a->lineno) {
969
5
        return RAISE_ERROR_KNOWN_LOCATION(
970
5
            p, PyExc_SyntaxError, a->end_lineno, a->col_offset,
971
5
            a->end_lineno, a->end_col_offset,
972
5
            "invalid syntax. Perhaps you forgot a comma?"
973
5
        );
974
5
    }
975
170
    return RAISE_ERROR_KNOWN_LOCATION(
976
170
        p, PyExc_SyntaxError, a->lineno, a->col_offset,
977
170
        b->end_lineno, b->end_col_offset,
978
170
        "invalid syntax. Perhaps you forgot a comma?"
979
170
    );
980
175
}
981
982
static ResultTokenWithMetadata *
983
result_token_with_metadata(Parser *p, void *result, PyObject *metadata)
984
9.53k
{
985
9.53k
    ResultTokenWithMetadata *res = _PyArena_Malloc(p->arena, sizeof(ResultTokenWithMetadata));
986
9.53k
    if (res == NULL) {
987
0
        return NULL;
988
0
    }
989
9.53k
    res->metadata = metadata;
990
9.53k
    res->result = result;
991
9.53k
    return res;
992
9.53k
}
993
994
ResultTokenWithMetadata *
995
_PyPegen_check_fstring_conversion(Parser *p, Token* conv_token, expr_ty conv)
996
2.92k
{
997
2.92k
    if (conv_token->lineno != conv->lineno || conv_token->end_col_offset != conv->col_offset) {
998
3
        return RAISE_SYNTAX_ERROR_KNOWN_RANGE(
999
3
            conv_token, conv,
1000
3
            "%c-string: conversion type must come right after the exclamation mark",
1001
3
            TOK_GET_STRING_PREFIX(p->tok)
1002
3
        );
1003
3
    }
1004
1005
2.91k
    Py_UCS4 first = PyUnicode_READ_CHAR(conv->v.Name.id, 0);
1006
2.91k
    if (PyUnicode_GET_LENGTH(conv->v.Name.id) > 1 ||
1007
2.91k
            !(first == 's' || first == 'r' || first == 'a')) {
1008
12
        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(conv,
1009
12
                                            "%c-string: invalid conversion character %R: expected 's', 'r', or 'a'",
1010
12
                                            TOK_GET_STRING_PREFIX(p->tok),
1011
12
                                            conv->v.Name.id);
1012
12
        return NULL;
1013
12
    }
1014
1015
2.90k
    return result_token_with_metadata(p, conv, conv_token->metadata);
1016
2.91k
}
1017
1018
ResultTokenWithMetadata *
1019
_PyPegen_setup_full_format_spec(Parser *p, Token *colon, asdl_expr_seq *spec, int lineno, int col_offset,
1020
                                int end_lineno, int end_col_offset, PyArena *arena)
1021
6.62k
{
1022
6.62k
    if (!spec) {
1023
0
        return NULL;
1024
0
    }
1025
1026
    // This is needed to keep compatibility with 3.11, where an empty format
1027
    // spec is parsed as an *empty* JoinedStr node, instead of having an empty
1028
    // constant in it.
1029
6.62k
    Py_ssize_t n_items = asdl_seq_LEN(spec);
1030
6.62k
    Py_ssize_t non_empty_count = 0;
1031
19.4k
    for (Py_ssize_t i = 0; i < n_items; i++) {
1032
12.8k
        expr_ty item = asdl_seq_GET(spec, i);
1033
12.8k
        non_empty_count += !(item->kind == Constant_kind &&
1034
12.8k
                             PyUnicode_CheckExact(item->v.Constant.value) &&
1035
9.70k
                             PyUnicode_GET_LENGTH(item->v.Constant.value) == 0);
1036
12.8k
    }
1037
6.62k
    if (non_empty_count != n_items) {
1038
2.14k
        asdl_expr_seq *resized_spec =
1039
2.14k
            _Py_asdl_expr_seq_new(non_empty_count, p->arena);
1040
2.14k
        if (resized_spec == NULL) {
1041
0
            return NULL;
1042
0
        }
1043
2.14k
        Py_ssize_t j = 0;
1044
5.33k
        for (Py_ssize_t i = 0; i < n_items; i++) {
1045
3.18k
            expr_ty item = asdl_seq_GET(spec, i);
1046
3.18k
            if (item->kind == Constant_kind &&
1047
3.18k
                PyUnicode_CheckExact(item->v.Constant.value) &&
1048
2.60k
                PyUnicode_GET_LENGTH(item->v.Constant.value) == 0) {
1049
2.14k
                continue;
1050
2.14k
            }
1051
1.04k
            asdl_seq_SET(resized_spec, j++, item);
1052
1.04k
        }
1053
2.14k
        assert(j == non_empty_count);
1054
2.14k
        spec = resized_spec;
1055
2.14k
    }
1056
6.62k
    expr_ty res;
1057
6.62k
    Py_ssize_t n = asdl_seq_LEN(spec);
1058
6.62k
    if (n == 0 || (n == 1 && asdl_seq_GET(spec, 0)->kind == Constant_kind)) {
1059
6.03k
        res = _PyAST_JoinedStr(spec, lineno, col_offset, end_lineno,
1060
6.03k
                                    end_col_offset, p->arena);
1061
6.03k
    } else {
1062
595
        res = _PyPegen_concatenate_strings(p, spec,
1063
595
                             lineno, col_offset, end_lineno,
1064
595
                             end_col_offset, arena);
1065
595
    }
1066
6.62k
    if (!res) {
1067
0
        return NULL;
1068
0
    }
1069
6.62k
    return result_token_with_metadata(p, res, colon->metadata);
1070
6.62k
}
1071
1072
const char *
1073
_PyPegen_get_expr_name(expr_ty e)
1074
200
{
1075
200
    assert(e != NULL);
1076
200
    switch (e->kind) {
1077
1
        case Attribute_kind:
1078
1
            return "attribute";
1079
1
        case Subscript_kind:
1080
1
            return "subscript";
1081
1
        case Starred_kind:
1082
1
            return "starred";
1083
7
        case Name_kind:
1084
7
            return "name";
1085
1
        case List_kind:
1086
1
            return "list";
1087
4
        case Tuple_kind:
1088
4
            return "tuple";
1089
2
        case Lambda_kind:
1090
2
            return "lambda";
1091
13
        case Call_kind:
1092
13
            return "function call";
1093
4
        case BoolOp_kind:
1094
32
        case BinOp_kind:
1095
44
        case UnaryOp_kind:
1096
44
            return "expression";
1097
1
        case GeneratorExp_kind:
1098
1
            return "generator expression";
1099
1
        case Yield_kind:
1100
2
        case YieldFrom_kind:
1101
2
            return "yield expression";
1102
1
        case Await_kind:
1103
1
            return "await expression";
1104
1
        case ListComp_kind:
1105
1
            return "list comprehension";
1106
2
        case SetComp_kind:
1107
2
            return "set comprehension";
1108
1
        case DictComp_kind:
1109
1
            return "dict comprehension";
1110
2
        case Dict_kind:
1111
2
            return "dict literal";
1112
1
        case Set_kind:
1113
1
            return "set display";
1114
6
        case JoinedStr_kind:
1115
6
        case FormattedValue_kind:
1116
6
            return "f-string expression";
1117
4
        case TemplateStr_kind:
1118
4
        case Interpolation_kind:
1119
4
            return "t-string expression";
1120
87
        case Constant_kind: {
1121
87
            PyObject *value = e->v.Constant.value;
1122
87
            if (value == Py_None) {
1123
1
                return "None";
1124
1
            }
1125
86
            if (value == Py_False) {
1126
1
                return "False";
1127
1
            }
1128
85
            if (value == Py_True) {
1129
3
                return "True";
1130
3
            }
1131
82
            if (value == Py_Ellipsis) {
1132
1
                return "ellipsis";
1133
1
            }
1134
81
            return "literal";
1135
82
        }
1136
16
        case Compare_kind:
1137
16
            return "comparison";
1138
1
        case IfExp_kind:
1139
1
            return "conditional expression";
1140
1
        case NamedExpr_kind:
1141
1
            return "named expression";
1142
0
        default:
1143
0
            PyErr_Format(PyExc_SystemError,
1144
0
                         "unexpected expression in assignment %d (line %d)",
1145
0
                         e->kind, e->lineno);
1146
0
            return NULL;
1147
200
    }
1148
200
}
1149
1150
expr_ty
1151
12
_PyPegen_get_last_comprehension_item(comprehension_ty comprehension) {
1152
12
    if (comprehension->ifs == NULL || asdl_seq_LEN(comprehension->ifs) == 0) {
1153
8
        return comprehension->iter;
1154
8
    }
1155
4
    return PyPegen_last_item(comprehension->ifs, expr_ty);
1156
12
}
1157
1158
expr_ty _PyPegen_collect_call_seqs(Parser *p, asdl_expr_seq *a, asdl_seq *b,
1159
                     int lineno, int col_offset, int end_lineno,
1160
24.2k
                     int end_col_offset, PyArena *arena) {
1161
24.2k
    Py_ssize_t args_len = asdl_seq_LEN(a);
1162
24.2k
    Py_ssize_t total_len = args_len;
1163
1164
24.2k
    if (b == NULL) {
1165
22.2k
        return _PyAST_Call(_PyPegen_dummy_name(p), a, NULL, lineno, col_offset,
1166
22.2k
                        end_lineno, end_col_offset, arena);
1167
1168
22.2k
    }
1169
1170
2.09k
    asdl_expr_seq *starreds = _PyPegen_seq_extract_starred_exprs(p, b);
1171
2.09k
    asdl_keyword_seq *keywords = _PyPegen_seq_delete_starred_exprs(p, b);
1172
1173
2.09k
    if (starreds) {
1174
217
        total_len += asdl_seq_LEN(starreds);
1175
217
    }
1176
1177
2.09k
    asdl_expr_seq *args = _Py_asdl_expr_seq_new(total_len, arena);
1178
2.09k
    if (args == NULL) {
1179
0
        return NULL;
1180
0
    }
1181
1182
2.09k
    Py_ssize_t i = 0;
1183
4.67k
    for (i = 0; i < args_len; i++) {
1184
2.58k
        asdl_seq_SET(args, i, asdl_seq_GET(a, i));
1185
2.58k
    }
1186
2.76k
    for (; i < total_len; i++) {
1187
670
        asdl_seq_SET(args, i, asdl_seq_GET(starreds, i - args_len));
1188
670
    }
1189
1190
2.09k
    return _PyAST_Call(_PyPegen_dummy_name(p), args, keywords, lineno,
1191
2.09k
                       col_offset, end_lineno, end_col_offset, arena);
1192
2.09k
}
1193
1194
// AST Error reporting helpers
1195
1196
expr_ty
1197
_PyPegen_get_invalid_target(expr_ty e, TARGETS_TYPE targets_type)
1198
7.02k
{
1199
7.02k
    if (e == NULL) {
1200
0
        return NULL;
1201
0
    }
1202
1203
7.02k
#define VISIT_CONTAINER(CONTAINER, TYPE) do { \
1204
1.70k
        Py_ssize_t len = asdl_seq_LEN((CONTAINER)->v.TYPE.elts);\
1205
5.79k
        for (Py_ssize_t i = 0; i < len; i++) {\
1206
4.23k
            expr_ty other = asdl_seq_GET((CONTAINER)->v.TYPE.elts, i);\
1207
4.23k
            expr_ty child = _PyPegen_get_invalid_target(other, targets_type);\
1208
4.23k
            if (child != NULL) {\
1209
151
                return child;\
1210
151
            }\
1211
4.23k
        }\
1212
1.70k
    } while (0)
1213
1214
    // We only need to visit List and Tuple nodes recursively as those
1215
    // are the only ones that can contain valid names in targets when
1216
    // they are parsed as expressions. Any other kind of expression
1217
    // that is a container (like Sets or Dicts) is directly invalid and
1218
    // we don't need to visit it recursively.
1219
1220
7.02k
    switch (e->kind) {
1221
727
        case List_kind:
1222
727
            VISIT_CONTAINER(e, List);
1223
690
            return NULL;
1224
978
        case Tuple_kind:
1225
978
            VISIT_CONTAINER(e, Tuple);
1226
864
            return NULL;
1227
1.16k
        case Starred_kind:
1228
1.16k
            if (targets_type == DEL_TARGETS) {
1229
1
                return e;
1230
1
            }
1231
1.16k
            return _PyPegen_get_invalid_target(e->v.Starred.value, targets_type);
1232
652
        case Compare_kind:
1233
            // This is needed, because the `a in b` in `for a in b` gets parsed
1234
            // as a comparison, and so we need to search the left side of the comparison
1235
            // for invalid targets.
1236
652
            if (targets_type == FOR_TARGETS) {
1237
642
                cmpop_ty cmpop = (cmpop_ty) asdl_seq_GET(e->v.Compare.ops, 0);
1238
642
                if (cmpop == In) {
1239
55
                    return _PyPegen_get_invalid_target(e->v.Compare.left, targets_type);
1240
55
                }
1241
587
                return NULL;
1242
642
            }
1243
10
            return e;
1244
2.67k
        case Name_kind:
1245
2.98k
        case Subscript_kind:
1246
3.38k
        case Attribute_kind:
1247
3.38k
            return NULL;
1248
128
        default:
1249
128
            return e;
1250
7.02k
    }
1251
7.02k
}
1252
1253
34
void *_PyPegen_arguments_parsing_error(Parser *p, expr_ty e) {
1254
34
    int kwarg_unpacking = 0;
1255
1.09k
    for (Py_ssize_t i = 0, l = asdl_seq_LEN(e->v.Call.keywords); i < l; i++) {
1256
1.05k
        keyword_ty keyword = asdl_seq_GET(e->v.Call.keywords, i);
1257
1.05k
        if (!keyword->arg) {
1258
641
            kwarg_unpacking = 1;
1259
641
        }
1260
1.05k
    }
1261
1262
34
    const char *msg = NULL;
1263
34
    if (kwarg_unpacking) {
1264
22
        msg = "positional argument follows keyword argument unpacking";
1265
22
    } else {
1266
12
        msg = "positional argument follows keyword argument";
1267
12
    }
1268
1269
34
    return RAISE_SYNTAX_ERROR(msg);
1270
34
}
1271
1272
void *
1273
_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args, asdl_comprehension_seq *comprehensions)
1274
219
{
1275
    /* The rule that calls this function is 'args for_if_clauses'.
1276
       For the input f(L, x for x in y), L and x are in args and
1277
       the for is parsed as a for_if_clause. We have to check if
1278
       len <= 1, so that input like dict((a, b) for a, b in x)
1279
       gets successfully parsed and then we pass the last
1280
       argument (x in the above example) as the location of the
1281
       error */
1282
219
    Py_ssize_t len = asdl_seq_LEN(args->v.Call.args);
1283
219
    if (len <= 1) {
1284
217
        return NULL;
1285
217
    }
1286
1287
2
    comprehension_ty last_comprehension = PyPegen_last_item(comprehensions, comprehension_ty);
1288
1289
2
    return RAISE_SYNTAX_ERROR_KNOWN_RANGE(
1290
219
        (expr_ty) asdl_seq_GET(args->v.Call.args, len - 1),
1291
219
        _PyPegen_get_last_comprehension_item(last_comprehension),
1292
219
        "Generator expression must be parenthesized"
1293
219
    );
1294
219
}
1295
1296
// Fstring stuff
1297
1298
static expr_ty
1299
27.6k
_PyPegen_decode_fstring_part(Parser* p, int is_raw, expr_ty constant, Token* token) {
1300
27.6k
    assert(PyUnicode_CheckExact(constant->v.Constant.value));
1301
1302
27.6k
    const char* bstr = PyUnicode_AsUTF8(constant->v.Constant.value);
1303
27.6k
    if (bstr == NULL) {
1304
0
        return NULL;
1305
0
    }
1306
1307
27.6k
    size_t len;
1308
27.6k
    if (strcmp(bstr, "{{") == 0 || strcmp(bstr, "}}") == 0) {
1309
0
        len = 1;
1310
27.6k
    } else {
1311
27.6k
        len = strlen(bstr);
1312
27.6k
    }
1313
1314
27.6k
    is_raw = is_raw || strchr(bstr, '\\') == NULL;
1315
27.6k
    PyObject *str = _PyPegen_decode_string(p, is_raw, bstr, len, token);
1316
27.6k
    if (str == NULL) {
1317
11
        _Pypegen_raise_decode_error(p);
1318
11
        return NULL;
1319
11
    }
1320
27.5k
    if (_PyArena_AddPyObject(p->arena, str) < 0) {
1321
0
        Py_DECREF(str);
1322
0
        return NULL;
1323
0
    }
1324
27.5k
    return _PyAST_Constant(str, NULL, constant->lineno, constant->col_offset,
1325
27.5k
                           constant->end_lineno, constant->end_col_offset,
1326
27.5k
                           p->arena);
1327
27.5k
}
1328
1329
static asdl_expr_seq *
1330
_get_resized_exprs(Parser *p, Token *a, asdl_expr_seq *raw_expressions, Token *b, enum string_kind_t string_kind)
1331
19.8k
{
1332
19.8k
    Py_ssize_t n_items = asdl_seq_LEN(raw_expressions);
1333
19.8k
    Py_ssize_t total_items = n_items;
1334
70.0k
    for (Py_ssize_t i = 0; i < n_items; i++) {
1335
50.1k
        expr_ty item = asdl_seq_GET(raw_expressions, i);
1336
50.1k
        if (item->kind == JoinedStr_kind) {
1337
6.34k
            total_items += asdl_seq_LEN(item->v.JoinedStr.values) - 1;
1338
6.34k
        }
1339
50.1k
    }
1340
1341
19.8k
    const char* quote_str = PyBytes_AsString(a->bytes);
1342
19.8k
    if (quote_str == NULL) {
1343
0
        return NULL;
1344
0
    }
1345
19.8k
    int is_raw = strpbrk(quote_str, "rR") != NULL;
1346
1347
19.8k
    asdl_expr_seq *seq = _Py_asdl_expr_seq_new(total_items, p->arena);
1348
19.8k
    if (seq == NULL) {
1349
0
        return NULL;
1350
0
    }
1351
1352
19.8k
    Py_ssize_t index = 0;
1353
69.9k
    for (Py_ssize_t i = 0; i < n_items; i++) {
1354
50.1k
        expr_ty item = asdl_seq_GET(raw_expressions, i);
1355
1356
        // This should correspond to a JoinedStr node of two elements
1357
        // created _PyPegen_formatted_value. This situation can only be the result of
1358
        // a (f|t)-string debug expression where the first element is a constant with the text and the second
1359
        // a formatted value with the expression.
1360
50.1k
        if (item->kind == JoinedStr_kind) {
1361
6.34k
            asdl_expr_seq *values = item->v.JoinedStr.values;
1362
6.34k
            if (asdl_seq_LEN(values) != 2) {
1363
0
                PyErr_Format(PyExc_SystemError,
1364
0
                             string_kind == TSTRING
1365
0
                             ? "unexpected TemplateStr node without debug data in t-string at line %d"
1366
0
                             : "unexpected JoinedStr node without debug data in f-string at line %d",
1367
0
                             item->lineno);
1368
0
                return NULL;
1369
0
            }
1370
1371
6.34k
            expr_ty first = asdl_seq_GET(values, 0);
1372
6.34k
            assert(first->kind == Constant_kind);
1373
6.34k
            asdl_seq_SET(seq, index++, first);
1374
1375
6.34k
            expr_ty second = asdl_seq_GET(values, 1);
1376
6.34k
            assert((string_kind == TSTRING && second->kind == Interpolation_kind) || second->kind == FormattedValue_kind);
1377
6.34k
            asdl_seq_SET(seq, index++, second);
1378
1379
6.34k
            continue;
1380
6.34k
        }
1381
1382
43.8k
        if (item->kind == Constant_kind) {
1383
27.6k
            item = _PyPegen_decode_fstring_part(p, is_raw, item, b);
1384
27.6k
            if (item == NULL) {
1385
11
                return NULL;
1386
11
            }
1387
1388
            /* Tokenizer emits string parts even when the underlying string
1389
            might become an empty value (e.g. FSTRING_MIDDLE with the value \\n)
1390
            so we need to check for them and simplify it here. */
1391
27.5k
            if (PyUnicode_CheckExact(item->v.Constant.value)
1392
27.5k
                && PyUnicode_GET_LENGTH(item->v.Constant.value) == 0) {
1393
309
                continue;
1394
309
            }
1395
27.5k
        }
1396
43.5k
        asdl_seq_SET(seq, index++, item);
1397
43.5k
    }
1398
1399
19.7k
    asdl_expr_seq *resized_exprs;
1400
19.7k
    if (index != total_items) {
1401
309
        resized_exprs = _Py_asdl_expr_seq_new(index, p->arena);
1402
309
        if (resized_exprs == NULL) {
1403
0
            return NULL;
1404
0
        }
1405
485
        for (Py_ssize_t i = 0; i < index; i++) {
1406
176
            asdl_seq_SET(resized_exprs, i, asdl_seq_GET(seq, i));
1407
176
        }
1408
309
    }
1409
19.4k
    else {
1410
19.4k
        resized_exprs = seq;
1411
19.4k
    }
1412
19.7k
    return resized_exprs;
1413
19.7k
}
1414
1415
expr_ty
1416
4.73k
_PyPegen_template_str(Parser *p, Token *a, asdl_expr_seq *raw_expressions, Token *b) {
1417
1418
4.73k
    asdl_expr_seq *resized_exprs = _get_resized_exprs(p, a, raw_expressions, b, TSTRING);
1419
4.73k
    return _PyAST_TemplateStr(resized_exprs, a->lineno, a->col_offset,
1420
4.73k
                              b->end_lineno, b->end_col_offset,
1421
4.73k
                              p->arena);
1422
4.73k
}
1423
1424
expr_ty
1425
15.0k
_PyPegen_joined_str(Parser *p, Token* a, asdl_expr_seq* raw_expressions, Token*b) {
1426
1427
15.0k
    asdl_expr_seq *resized_exprs = _get_resized_exprs(p, a, raw_expressions, b, FSTRING);
1428
15.0k
    return _PyAST_JoinedStr(resized_exprs, a->lineno, a->col_offset,
1429
15.0k
                            b->end_lineno, b->end_col_offset,
1430
15.0k
                            p->arena);
1431
15.0k
}
1432
1433
11.2k
expr_ty _PyPegen_decoded_constant_from_token(Parser* p, Token* tok) {
1434
11.2k
    Py_ssize_t bsize;
1435
11.2k
    char* bstr;
1436
11.2k
    if (PyBytes_AsStringAndSize(tok->bytes, &bstr, &bsize) == -1) {
1437
0
        return NULL;
1438
0
    }
1439
1440
    // Check if we're inside a raw f-string for format spec decoding
1441
11.2k
    int is_raw = 0;
1442
11.2k
    if (INSIDE_FSTRING(p->tok)) {
1443
7.26k
        tokenizer_mode *mode = TOK_GET_MODE(p->tok);
1444
7.26k
        is_raw = mode->raw;
1445
7.26k
    }
1446
1447
11.2k
    PyObject* str = _PyPegen_decode_string(p, is_raw, bstr, bsize, tok);
1448
11.2k
    if (str == NULL) {
1449
3
        return NULL;
1450
3
    }
1451
11.2k
    if (_PyArena_AddPyObject(p->arena, str) < 0) {
1452
0
        Py_DECREF(str);
1453
0
        return NULL;
1454
0
    }
1455
11.2k
    return _PyAST_Constant(str, NULL, tok->lineno, tok->col_offset,
1456
11.2k
                           tok->end_lineno, tok->end_col_offset,
1457
11.2k
                           p->arena);
1458
11.2k
}
1459
1460
31.8k
expr_ty _PyPegen_constant_from_token(Parser* p, Token* tok) {
1461
31.8k
    char* bstr = PyBytes_AsString(tok->bytes);
1462
31.8k
    if (bstr == NULL) {
1463
0
        return NULL;
1464
0
    }
1465
31.8k
    PyObject* str = PyUnicode_FromString(bstr);
1466
31.8k
    if (str == NULL) {
1467
0
        return NULL;
1468
0
    }
1469
31.8k
    if (_PyArena_AddPyObject(p->arena, str) < 0) {
1470
0
        Py_DECREF(str);
1471
0
        return NULL;
1472
0
    }
1473
31.8k
    return _PyAST_Constant(str, NULL, tok->lineno, tok->col_offset,
1474
31.8k
                           tok->end_lineno, tok->end_col_offset,
1475
31.8k
                           p->arena);
1476
31.8k
}
1477
1478
53.9k
expr_ty _PyPegen_constant_from_string(Parser* p, Token* tok) {
1479
53.9k
    char* the_str = PyBytes_AsString(tok->bytes);
1480
53.9k
    if (the_str == NULL) {
1481
0
        return NULL;
1482
0
    }
1483
53.9k
    PyObject *s = _PyPegen_parse_string(p, tok);
1484
53.9k
    if (s == NULL) {
1485
108
        _Pypegen_raise_decode_error(p);
1486
108
        return NULL;
1487
108
    }
1488
53.8k
    if (_PyArena_AddPyObject(p->arena, s) < 0) {
1489
0
        Py_DECREF(s);
1490
0
        return NULL;
1491
0
    }
1492
53.8k
    PyObject *kind = NULL;
1493
53.8k
    if (the_str && the_str[0] == 'u') {
1494
481
        kind = _PyPegen_new_identifier(p, "u");
1495
481
        if (kind == NULL) {
1496
0
            return NULL;
1497
0
        }
1498
481
    }
1499
53.8k
    return _PyAST_Constant(s, kind, tok->lineno, tok->col_offset, tok->end_lineno, tok->end_col_offset, p->arena);
1500
53.8k
}
1501
1502
static int
1503
_get_interpolation_conversion(Parser *p, Token *debug, ResultTokenWithMetadata *conversion,
1504
                              ResultTokenWithMetadata *format)
1505
28.5k
{
1506
28.5k
    if (conversion != NULL) {
1507
2.88k
        expr_ty conversion_expr = (expr_ty) conversion->result;
1508
2.88k
        assert(conversion_expr->kind == Name_kind);
1509
2.88k
        Py_UCS4 first = PyUnicode_READ_CHAR(conversion_expr->v.Name.id, 0);
1510
2.88k
        return Py_SAFE_DOWNCAST(first, Py_UCS4, int);
1511
2.88k
    }
1512
25.6k
    else if (debug && !format) {
1513
        /* If no conversion is specified, use !r for debug expressions */
1514
6.15k
        return (int)'r';
1515
6.15k
    }
1516
19.4k
    return -1;
1517
28.5k
}
1518
1519
static PyObject *
1520
_strip_interpolation_expr(PyObject *exprstr)
1521
6.06k
{
1522
6.06k
    Py_ssize_t len = PyUnicode_GET_LENGTH(exprstr);
1523
1524
14.6k
    for (Py_ssize_t i = len - 1; i >= 0; i--) {
1525
14.6k
        Py_UCS4 c = PyUnicode_READ_CHAR(exprstr, i);
1526
14.6k
        if (_PyUnicode_IsWhitespace(c) || c == '=') {
1527
8.58k
            len--;
1528
8.58k
        }
1529
6.06k
        else {
1530
6.06k
            break;
1531
6.06k
        }
1532
14.6k
    }
1533
1534
6.06k
    return PyUnicode_Substring(exprstr, 0, len);
1535
6.06k
}
1536
1537
expr_ty _PyPegen_interpolation(Parser *p, expr_ty expression, Token *debug, ResultTokenWithMetadata *conversion,
1538
                                 ResultTokenWithMetadata *format, Token *closing_brace, int lineno, int col_offset,
1539
6.06k
                                 int end_lineno, int end_col_offset, PyArena *arena) {
1540
1541
6.06k
    int conversion_val = _get_interpolation_conversion(p, debug, conversion, format);
1542
1543
    /* Find the non whitespace token after the "=" */
1544
6.06k
    int debug_end_line, debug_end_offset;
1545
6.06k
    PyObject *debug_metadata;
1546
6.06k
    constant exprstr;
1547
1548
6.06k
    if (conversion) {
1549
897
        debug_end_line = ((expr_ty) conversion->result)->lineno;
1550
897
        debug_end_offset = ((expr_ty) conversion->result)->col_offset;
1551
897
        debug_metadata = exprstr = conversion->metadata;
1552
897
    }
1553
5.16k
    else if (format) {
1554
969
        debug_end_line = ((expr_ty) format->result)->lineno;
1555
969
        debug_end_offset = ((expr_ty) format->result)->col_offset + 1;
1556
969
        debug_metadata = exprstr = format->metadata;
1557
969
    }
1558
4.19k
    else {
1559
4.19k
        debug_end_line = end_lineno;
1560
4.19k
        debug_end_offset = end_col_offset;
1561
4.19k
        debug_metadata = exprstr = closing_brace->metadata;
1562
4.19k
    }
1563
1564
6.06k
    assert(exprstr != NULL);
1565
6.06k
    PyObject *final_exprstr = _strip_interpolation_expr(exprstr);
1566
6.06k
    if (!final_exprstr || _PyArena_AddPyObject(arena, final_exprstr) < 0) {
1567
0
        Py_XDECREF(final_exprstr);
1568
0
        return NULL;
1569
0
    }
1570
1571
6.06k
    expr_ty interpolation = _PyAST_Interpolation(
1572
6.06k
        expression, final_exprstr, conversion_val, format ? (expr_ty) format->result : NULL,
1573
6.06k
        lineno, col_offset, end_lineno,
1574
6.06k
        end_col_offset, arena
1575
6.06k
    );
1576
1577
6.06k
    if (!debug) {
1578
5.91k
        return interpolation;
1579
5.91k
    }
1580
1581
148
    expr_ty debug_text = _PyAST_Constant(debug_metadata, NULL, lineno, col_offset + 1, debug_end_line,
1582
148
                                            debug_end_offset - 1, p->arena);
1583
148
    if (!debug_text) {
1584
0
        return NULL;
1585
0
    }
1586
1587
148
    asdl_expr_seq *values = _Py_asdl_expr_seq_new(2, arena);
1588
148
    asdl_seq_SET(values, 0, debug_text);
1589
148
    asdl_seq_SET(values, 1, interpolation);
1590
148
    return _PyAST_JoinedStr(values, lineno, col_offset, debug_end_line, debug_end_offset, p->arena);
1591
148
}
1592
1593
expr_ty _PyPegen_formatted_value(Parser *p, expr_ty expression, Token *debug, ResultTokenWithMetadata *conversion,
1594
                                 ResultTokenWithMetadata *format, Token *closing_brace, int lineno, int col_offset,
1595
22.4k
                                 int end_lineno, int end_col_offset, PyArena *arena) {
1596
22.4k
    int conversion_val = _get_interpolation_conversion(p, debug, conversion, format);
1597
1598
22.4k
    expr_ty formatted_value = _PyAST_FormattedValue(
1599
22.4k
        expression, conversion_val, format ? (expr_ty) format->result : NULL,
1600
22.4k
        lineno, col_offset, end_lineno,
1601
22.4k
        end_col_offset, arena
1602
22.4k
    );
1603
1604
22.4k
    if (!debug) {
1605
15.9k
        return formatted_value;
1606
15.9k
    }
1607
1608
    /* Find the non whitespace token after the "=" */
1609
6.47k
    int debug_end_line, debug_end_offset;
1610
6.47k
    PyObject *debug_metadata;
1611
1612
6.47k
    if (conversion) {
1613
196
        debug_end_line = ((expr_ty) conversion->result)->lineno;
1614
196
        debug_end_offset = ((expr_ty) conversion->result)->col_offset;
1615
196
        debug_metadata = conversion->metadata;
1616
196
    }
1617
6.28k
    else if (format) {
1618
227
        debug_end_line = ((expr_ty) format->result)->lineno;
1619
227
        debug_end_offset = ((expr_ty) format->result)->col_offset + 1;
1620
227
        debug_metadata = format->metadata;
1621
227
    }
1622
6.05k
    else {
1623
6.05k
        debug_end_line = end_lineno;
1624
6.05k
        debug_end_offset = end_col_offset;
1625
6.05k
        debug_metadata = closing_brace->metadata;
1626
6.05k
    }
1627
6.47k
    expr_ty debug_text = _PyAST_Constant(debug_metadata, NULL, lineno, col_offset + 1, debug_end_line,
1628
6.47k
                                            debug_end_offset - 1, p->arena);
1629
6.47k
    if (!debug_text) {
1630
2
        return NULL;
1631
2
    }
1632
1633
6.47k
    asdl_expr_seq *values = _Py_asdl_expr_seq_new(2, arena);
1634
6.47k
    asdl_seq_SET(values, 0, debug_text);
1635
6.47k
    asdl_seq_SET(values, 1, formatted_value);
1636
6.47k
    return _PyAST_JoinedStr(values, lineno, col_offset, debug_end_line, debug_end_offset, p->arena);
1637
6.47k
}
1638
1639
static expr_ty
1640
_build_concatenated_bytes(Parser *p, asdl_expr_seq *strings, int lineno,
1641
                        int col_offset, int end_lineno, int end_col_offset,
1642
                        PyArena *arena)
1643
640
{
1644
640
    Py_ssize_t len = asdl_seq_LEN(strings);
1645
640
    assert(len > 0);
1646
1647
    /* Bytes literals never get a kind, but just for consistency
1648
        since they are represented as Constant nodes, we'll mirror
1649
        the same behavior as unicode strings for determining the
1650
        kind. */
1651
640
    PyObject *kind = asdl_seq_GET(strings, 0)->v.Constant.kind;
1652
1653
640
    Py_ssize_t total = 0;
1654
3.25k
    for (Py_ssize_t i = 0; i < len; i++) {
1655
2.61k
        expr_ty elem = asdl_seq_GET(strings, i);
1656
2.61k
        PyObject *bytes = elem->v.Constant.value;
1657
2.61k
        Py_ssize_t part = PyBytes_GET_SIZE(bytes);
1658
2.61k
        if (part > PY_SSIZE_T_MAX - total) {
1659
0
            PyErr_NoMemory();
1660
0
            return NULL;
1661
0
        }
1662
2.61k
        total += part;
1663
2.61k
    }
1664
1665
640
    PyBytesWriter *writer = PyBytesWriter_Create(total);
1666
640
    if (writer == NULL) {
1667
0
        return NULL;
1668
0
    }
1669
640
    char *out = PyBytesWriter_GetData(writer);
1670
1671
3.25k
    for (Py_ssize_t i = 0; i < len; i++) {
1672
2.61k
        expr_ty elem = asdl_seq_GET(strings, i);
1673
2.61k
        PyObject *bytes = elem->v.Constant.value;
1674
2.61k
        Py_ssize_t part = PyBytes_GET_SIZE(bytes);
1675
2.61k
        if (part > 0) {
1676
1.05k
            memcpy(out, PyBytes_AS_STRING(bytes), part);
1677
1.05k
            out += part;
1678
1.05k
        }
1679
2.61k
    }
1680
1681
640
    PyObject *res = PyBytesWriter_Finish(writer);
1682
640
    if (res == NULL) {
1683
0
        return NULL;
1684
0
    }
1685
640
    if (_PyArena_AddPyObject(arena, res) < 0) {
1686
0
        Py_DECREF(res);
1687
0
        return NULL;
1688
0
    }
1689
640
    return _PyAST_Constant(res, kind, lineno, col_offset, end_lineno, end_col_offset, p->arena);
1690
640
}
1691
1692
static expr_ty
1693
_build_concatenated_unicode(Parser *p, asdl_expr_seq *strings, int lineno,
1694
                        int col_offset, int end_lineno, int end_col_offset,
1695
                        PyArena *arena)
1696
1.51k
{
1697
1.51k
    Py_ssize_t len = asdl_seq_LEN(strings);
1698
1.51k
    assert(len > 1);
1699
1700
1.51k
    expr_ty first = asdl_seq_GET(strings, 0);
1701
1702
    /* When a string is getting concatenated, the kind of the string
1703
        is determined by the first string in the concatenation
1704
        sequence.
1705
1706
        u"abc" "def" -> u"abcdef"
1707
        "abc" u"abc" ->  "abcabc" */
1708
1.51k
    PyObject *kind = first->v.Constant.kind;
1709
1710
1.51k
    PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
1711
1.51k
    if (writer == NULL) {
1712
0
        return NULL;
1713
0
    }
1714
1715
23.7k
    for (Py_ssize_t i = 0; i < len; i++) {
1716
22.2k
        expr_ty current_elem = asdl_seq_GET(strings, i);
1717
22.2k
        assert(current_elem->kind == Constant_kind);
1718
1719
22.2k
        if (PyUnicodeWriter_WriteStr(writer,
1720
22.2k
                                     current_elem->v.Constant.value)) {
1721
0
            PyUnicodeWriter_Discard(writer);
1722
0
            return NULL;
1723
0
        }
1724
22.2k
    }
1725
1726
1.51k
    PyObject *final = PyUnicodeWriter_Finish(writer);
1727
1.51k
    if (final == NULL) {
1728
0
        return NULL;
1729
0
    }
1730
1.51k
    if (_PyArena_AddPyObject(p->arena, final) < 0) {
1731
0
        Py_DECREF(final);
1732
0
        return NULL;
1733
0
    }
1734
1.51k
    return _PyAST_Constant(final, kind, lineno, col_offset,
1735
1.51k
                           end_lineno, end_col_offset, arena);
1736
1.51k
}
1737
1738
static asdl_expr_seq *
1739
_build_concatenated_str(Parser *p, asdl_expr_seq *strings,
1740
                               int lineno, int col_offset, int end_lineno,
1741
                               int end_col_offset, PyArena *arena)
1742
13.3k
{
1743
13.3k
    Py_ssize_t len = asdl_seq_LEN(strings);
1744
13.3k
    assert(len > 0);
1745
1746
13.3k
    Py_ssize_t n_flattened_elements = 0;
1747
41.7k
    for (Py_ssize_t i = 0; i < len; i++) {
1748
28.3k
        expr_ty elem = asdl_seq_GET(strings, i);
1749
28.3k
        switch(elem->kind) {
1750
13.1k
            case JoinedStr_kind:
1751
13.1k
                n_flattened_elements += asdl_seq_LEN(elem->v.JoinedStr.values);
1752
13.1k
                break;
1753
4.32k
            case TemplateStr_kind:
1754
4.32k
                n_flattened_elements += asdl_seq_LEN(elem->v.TemplateStr.values);
1755
4.32k
                break;
1756
10.8k
            default:
1757
10.8k
                n_flattened_elements++;
1758
10.8k
                break;
1759
28.3k
        }
1760
28.3k
    }
1761
1762
1763
13.3k
    asdl_expr_seq* flattened = _Py_asdl_expr_seq_new(n_flattened_elements, p->arena);
1764
13.3k
    if (flattened == NULL) {
1765
0
        return NULL;
1766
0
    }
1767
1768
    /* build flattened list */
1769
13.3k
    Py_ssize_t current_pos = 0;
1770
41.7k
    for (Py_ssize_t i = 0; i < len; i++) {
1771
28.3k
        expr_ty elem = asdl_seq_GET(strings, i);
1772
28.3k
        switch(elem->kind) {
1773
13.1k
            case JoinedStr_kind:
1774
57.3k
                for (Py_ssize_t j = 0; j < asdl_seq_LEN(elem->v.JoinedStr.values); j++) {
1775
44.2k
                    expr_ty subvalue = asdl_seq_GET(elem->v.JoinedStr.values, j);
1776
44.2k
                    if (subvalue == NULL) {
1777
0
                        return NULL;
1778
0
                    }
1779
44.2k
                    asdl_seq_SET(flattened, current_pos++, subvalue);
1780
44.2k
                }
1781
13.1k
                break;
1782
13.1k
            case TemplateStr_kind:
1783
12.1k
                for (Py_ssize_t j = 0; j < asdl_seq_LEN(elem->v.TemplateStr.values); j++) {
1784
7.82k
                    expr_ty subvalue = asdl_seq_GET(elem->v.TemplateStr.values, j);
1785
7.82k
                    if (subvalue == NULL) {
1786
0
                        return NULL;
1787
0
                    }
1788
7.82k
                    asdl_seq_SET(flattened, current_pos++, subvalue);
1789
7.82k
                }
1790
4.32k
                break;
1791
10.8k
            default:
1792
10.8k
                asdl_seq_SET(flattened, current_pos++, elem);
1793
10.8k
                break;
1794
28.3k
        }
1795
28.3k
    }
1796
1797
    /* calculate folded element count */
1798
13.3k
    Py_ssize_t n_elements = 0;
1799
13.3k
    int prev_is_constant = 0;
1800
76.3k
    for (Py_ssize_t i = 0; i < n_flattened_elements; i++) {
1801
62.9k
        expr_ty elem = asdl_seq_GET(flattened, i);
1802
1803
        /* The concatenation of a FormattedValue and an empty Constant should
1804
           lead to the FormattedValue itself. Thus, we will not take any empty
1805
           constants into account, just as in `_PyPegen_joined_str` */
1806
62.9k
        if (elem->kind == Constant_kind &&
1807
62.9k
            PyUnicode_CheckExact(elem->v.Constant.value) &&
1808
38.3k
            PyUnicode_GET_LENGTH(elem->v.Constant.value) == 0)
1809
981
            continue;
1810
1811
61.9k
        if (!prev_is_constant || elem->kind != Constant_kind) {
1812
49.8k
            n_elements++;
1813
49.8k
        }
1814
61.9k
        prev_is_constant = elem->kind == Constant_kind;
1815
61.9k
    }
1816
1817
13.3k
    asdl_expr_seq* values = _Py_asdl_expr_seq_new(n_elements, p->arena);
1818
13.3k
    if (values == NULL) {
1819
0
        return NULL;
1820
0
    }
1821
1822
    /* build folded list */
1823
13.3k
    current_pos = 0;
1824
63.5k
    for (Py_ssize_t i = 0; i < n_flattened_elements; i++) {
1825
50.1k
        expr_ty elem = asdl_seq_GET(flattened, i);
1826
1827
        /* if the current elem and the following are constants,
1828
           fold them and all consequent constants */
1829
50.1k
        if (elem->kind == Constant_kind) {
1830
25.6k
            if (i + 1 < n_flattened_elements &&
1831
20.6k
                asdl_seq_GET(flattened, i + 1)->kind == Constant_kind) {
1832
3.32k
                expr_ty first_elem = elem;
1833
1834
                /* When a string is getting concatenated, the kind of the string
1835
                   is determined by the first string in the concatenation
1836
                   sequence.
1837
1838
                   u"abc" "def" -> u"abcdef"
1839
                   "abc" u"abc" ->  "abcabc" */
1840
3.32k
                PyObject *kind = elem->v.Constant.kind;
1841
1842
3.32k
                PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
1843
3.32k
                if (writer == NULL) {
1844
0
                    return NULL;
1845
0
                }
1846
3.32k
                expr_ty last_elem = elem;
1847
3.32k
                Py_ssize_t j;
1848
19.4k
                for (j = i; j < n_flattened_elements; j++) {
1849
18.2k
                    expr_ty current_elem = asdl_seq_GET(flattened, j);
1850
18.2k
                    if (current_elem->kind == Constant_kind) {
1851
16.0k
                        if (PyUnicodeWriter_WriteStr(writer,
1852
16.0k
                                                     current_elem->v.Constant.value)) {
1853
0
                            PyUnicodeWriter_Discard(writer);
1854
0
                            return NULL;
1855
0
                        }
1856
16.0k
                        last_elem = current_elem;
1857
16.0k
                    } else {
1858
2.17k
                        break;
1859
2.17k
                    }
1860
18.2k
                }
1861
3.32k
                i = j - 1;
1862
1863
3.32k
                PyObject *concat_str = PyUnicodeWriter_Finish(writer);
1864
3.32k
                if (concat_str == NULL) {
1865
0
                    return NULL;
1866
0
                }
1867
3.32k
                if (_PyArena_AddPyObject(p->arena, concat_str) < 0) {
1868
0
                    Py_DECREF(concat_str);
1869
0
                    return NULL;
1870
0
                }
1871
3.32k
                elem = _PyAST_Constant(concat_str, kind, first_elem->lineno,
1872
3.32k
                                       first_elem->col_offset,
1873
3.32k
                                       last_elem->end_lineno,
1874
3.32k
                                       last_elem->end_col_offset, p->arena);
1875
3.32k
                if (elem == NULL) {
1876
0
                    return NULL;
1877
0
                }
1878
3.32k
            }
1879
1880
            /* Drop all empty contanst strings */
1881
25.6k
            if (PyUnicode_CheckExact(elem->v.Constant.value) &&
1882
25.6k
                PyUnicode_GET_LENGTH(elem->v.Constant.value) == 0) {
1883
322
                continue;
1884
322
            }
1885
25.6k
        }
1886
1887
49.8k
        asdl_seq_SET(values, current_pos++, elem);
1888
49.8k
    }
1889
1890
13.3k
    assert(current_pos == n_elements);
1891
13.3k
    return values;
1892
13.3k
}
1893
1894
static expr_ty
1895
_build_concatenated_joined_str(Parser *p, asdl_expr_seq *strings,
1896
                               int lineno, int col_offset, int end_lineno,
1897
                               int end_col_offset, PyArena *arena)
1898
10.0k
{
1899
10.0k
    asdl_expr_seq *values = _build_concatenated_str(p, strings, lineno,
1900
10.0k
        col_offset, end_lineno, end_col_offset, arena);
1901
10.0k
    return _PyAST_JoinedStr(values, lineno, col_offset, end_lineno, end_col_offset, p->arena);
1902
10.0k
}
1903
1904
expr_ty
1905
_PyPegen_concatenate_tstrings(Parser *p, asdl_expr_seq *strings,
1906
                               int lineno, int col_offset, int end_lineno,
1907
                               int end_col_offset, PyArena *arena)
1908
3.35k
{
1909
3.35k
    asdl_expr_seq *values = _build_concatenated_str(p, strings, lineno,
1910
3.35k
        col_offset, end_lineno, end_col_offset, arena);
1911
3.35k
    return _PyAST_TemplateStr(values, lineno, col_offset, end_lineno,
1912
3.35k
        end_col_offset, arena);
1913
3.35k
}
1914
1915
expr_ty
1916
_PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
1917
                             int lineno, int col_offset, int end_lineno,
1918
                             int end_col_offset, PyArena *arena)
1919
30.2k
{
1920
30.2k
    Py_ssize_t len = asdl_seq_LEN(strings);
1921
30.2k
    assert(len > 0);
1922
1923
30.2k
    int f_string_found = 0;
1924
30.2k
    int unicode_string_found = 0;
1925
30.2k
    int bytes_found = 0;
1926
1927
30.2k
    Py_ssize_t i = 0;
1928
97.2k
    for (i = 0; i < len; i++) {
1929
66.9k
        expr_ty elem = asdl_seq_GET(strings, i);
1930
66.9k
        switch(elem->kind) {
1931
50.8k
            case Constant_kind:
1932
50.8k
                if (PyBytes_CheckExact(elem->v.Constant.value)) {
1933
3.97k
                    bytes_found = 1;
1934
46.8k
                } else {
1935
46.8k
                    unicode_string_found = 1;
1936
46.8k
                }
1937
50.8k
                break;
1938
13.1k
            case JoinedStr_kind:
1939
13.1k
                f_string_found = 1;
1940
13.1k
                break;
1941
0
            case TemplateStr_kind:
1942
                // python.gram handles this; we should never get here
1943
0
                assert(0);
1944
0
                break;
1945
3.02k
            default:
1946
3.02k
                f_string_found = 1;
1947
3.02k
                break;
1948
66.9k
        }
1949
66.9k
    }
1950
1951
    // Cannot mix unicode and bytes
1952
30.2k
    if ((unicode_string_found || f_string_found) && bytes_found) {
1953
7
        RAISE_SYNTAX_ERROR("cannot mix bytes and nonbytes literals");
1954
7
        return NULL;
1955
7
    }
1956
1957
    // If it's only bytes or only unicode string, do a simple concat
1958
30.2k
    if (!f_string_found) {
1959
20.2k
        if (len == 1) {
1960
18.0k
            return asdl_seq_GET(strings, 0);
1961
18.0k
        }
1962
2.15k
        else if (bytes_found) {
1963
640
            return _build_concatenated_bytes(p, strings, lineno, col_offset,
1964
640
                end_lineno, end_col_offset, arena);
1965
640
        }
1966
1.51k
        else {
1967
1.51k
            return _build_concatenated_unicode(p, strings, lineno, col_offset,
1968
1.51k
                end_lineno, end_col_offset, arena);
1969
1.51k
        }
1970
20.2k
    }
1971
1972
10.0k
    return _build_concatenated_joined_str(p, strings, lineno,
1973
10.0k
        col_offset, end_lineno, end_col_offset, arena);
1974
30.2k
}
1975
1976
stmt_ty
1977
_PyPegen_checked_future_import(Parser *p, identifier module, asdl_alias_seq * names,
1978
                               int level, expr_ty lazy_token, int lineno,
1979
                               int col_offset, int end_lineno, int end_col_offset,
1980
1.81k
                               PyArena *arena) {
1981
1.81k
    if (level == 0 && PyUnicode_CompareWithASCIIString(module, "__future__") == 0) {
1982
731
        if (lazy_token) {
1983
1
            RAISE_SYNTAX_ERROR_KNOWN_LOCATION(lazy_token,
1984
1
                "lazy from __future__ import is not allowed");
1985
1
            return NULL;
1986
1
        }
1987
1.72k
        for (Py_ssize_t i = 0; i < asdl_seq_LEN(names); i++) {
1988
991
            alias_ty alias = asdl_seq_GET(names, i);
1989
991
            if (PyUnicode_CompareWithASCIIString(alias->name, "barry_as_FLUFL") == 0) {
1990
378
                p->flags |= PyPARSE_BARRY_AS_BDFL;
1991
378
            }
1992
991
        }
1993
730
    }
1994
1.81k
    return _PyAST_ImportFrom(module, names, level, lazy_token ? 1 : 0, lineno,
1995
1.81k
                             col_offset, end_lineno, end_col_offset, arena);
1996
1.81k
}
1997
1998
asdl_stmt_seq*
1999
25.0k
_PyPegen_register_stmts(Parser *p, asdl_stmt_seq* stmts) {
2000
25.0k
    if (!p->call_invalid_rules) {
2001
19.0k
        return stmts;
2002
19.0k
    }
2003
5.98k
    Py_ssize_t len = asdl_seq_LEN(stmts);
2004
5.98k
    if (len == 0) {
2005
0
        return stmts;
2006
0
    }
2007
5.98k
    stmt_ty last_stmt = asdl_seq_GET(stmts, len - 1);
2008
5.98k
    if (p->last_stmt_location.lineno > last_stmt->lineno) {
2009
127
        return stmts;
2010
127
    }
2011
5.86k
    p->last_stmt_location.lineno = last_stmt->lineno;
2012
5.86k
    p->last_stmt_location.col_offset = last_stmt->col_offset;
2013
5.86k
    p->last_stmt_location.end_lineno = last_stmt->end_lineno;
2014
5.86k
    p->last_stmt_location.end_col_offset = last_stmt->end_col_offset;
2015
5.86k
    return stmts;
2016
5.98k
}