Coverage Report

Created: 2026-04-12 06:54

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
58.0k
{
13
58.0k
    return &_PyRuntime.parser.dummy_name;
14
58.0k
}
15
16
/* Creates a single-element asdl_seq* that contains a */
17
asdl_seq *
18
_PyPegen_singleton_seq(Parser *p, void *a)
19
116k
{
20
116k
    assert(a != NULL);
21
116k
    asdl_seq *seq = (asdl_seq*)_Py_asdl_generic_seq_new(1, p->arena);
22
116k
    if (!seq) {
23
0
        return NULL;
24
0
    }
25
116k
    asdl_seq_SET_UNTYPED(seq, 0, a);
26
116k
    return seq;
27
116k
}
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
272k
{
33
272k
    assert(a != NULL);
34
272k
    if (!seq) {
35
6.21k
        return _PyPegen_singleton_seq(p, a);
36
6.21k
    }
37
38
265k
    asdl_seq *new_seq = (asdl_seq*)_Py_asdl_generic_seq_new(asdl_seq_LEN(seq) + 1, p->arena);
39
265k
    if (!new_seq) {
40
0
        return NULL;
41
0
    }
42
43
265k
    asdl_seq_SET_UNTYPED(new_seq, 0, a);
44
532k
    for (Py_ssize_t i = 1, l = asdl_seq_LEN(new_seq); i < l; i++) {
45
266k
        asdl_seq_SET_UNTYPED(new_seq, i, asdl_seq_GET_UNTYPED(seq, i - 1));
46
266k
    }
47
265k
    return new_seq;
48
265k
}
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.0k
{
74
20.0k
    Py_ssize_t size = 0;
75
96.4k
    for (Py_ssize_t i = 0, l = asdl_seq_LEN(seqs); i < l; i++) {
76
76.4k
        asdl_seq *inner_seq = asdl_seq_GET_UNTYPED(seqs, i);
77
76.4k
        size += asdl_seq_LEN(inner_seq);
78
76.4k
    }
79
20.0k
    return size;
80
20.0k
}
81
82
/* Flattens an asdl_seq* of asdl_seq*s */
83
asdl_seq *
84
_PyPegen_seq_flatten(Parser *p, asdl_seq *seqs)
85
20.0k
{
86
20.0k
    Py_ssize_t flattened_seq_size = _get_flattened_seq_size(seqs);
87
20.0k
    assert(flattened_seq_size > 0);
88
89
20.0k
    asdl_seq *flattened_seq = (asdl_seq*)_Py_asdl_generic_seq_new(flattened_seq_size, p->arena);
90
20.0k
    if (!flattened_seq) {
91
0
        return NULL;
92
0
    }
93
94
20.0k
    int flattened_seq_idx = 0;
95
96.4k
    for (Py_ssize_t i = 0, l = asdl_seq_LEN(seqs); i < l; i++) {
96
76.4k
        asdl_seq *inner_seq = asdl_seq_GET_UNTYPED(seqs, i);
97
160k
        for (Py_ssize_t j = 0, li = asdl_seq_LEN(inner_seq); j < li; j++) {
98
83.9k
            asdl_seq_SET_UNTYPED(flattened_seq, flattened_seq_idx++, asdl_seq_GET_UNTYPED(inner_seq, j));
99
83.9k
        }
100
76.4k
    }
101
20.0k
    assert(flattened_seq_idx == flattened_seq_size);
102
103
20.0k
    return flattened_seq;
104
20.0k
}
105
106
void *
107
_PyPegen_seq_last_item(asdl_seq *seq)
108
81
{
109
81
    Py_ssize_t len = asdl_seq_LEN(seq);
110
81
    return asdl_seq_GET_UNTYPED(seq, len - 1);
111
81
}
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
4.04k
{
123
4.04k
    assert(first_name != NULL && second_name != NULL);
124
4.04k
    PyObject *uni = PyUnicode_FromFormat("%U.%U",
125
4.04k
            first_name->v.Name.id, second_name->v.Name.id);
126
4.04k
    if (!uni) {
127
0
        return NULL;
128
0
    }
129
4.04k
    PyInterpreterState *interp = _PyInterpreterState_GET();
130
4.04k
    _PyUnicode_InternImmortal(interp, &uni);
131
4.04k
    if (_PyArena_AddPyObject(p->arena, uni) < 0) {
132
0
        Py_DECREF(uni);
133
0
        return NULL;
134
0
    }
135
136
4.04k
    return _PyAST_Name(uni, Load, EXTRA_EXPR(first_name, second_name));
137
4.04k
}
138
139
/* Counts the total number of dots in seq's tokens */
140
int
141
_PyPegen_seq_count_dots(asdl_seq *seq)
142
2.59k
{
143
2.59k
    int number_of_dots = 0;
144
4.14k
    for (Py_ssize_t i = 0, l = asdl_seq_LEN(seq); i < l; i++) {
145
1.54k
        Token *current_expr = asdl_seq_GET_UNTYPED(seq, i);
146
1.54k
        switch (current_expr->type) {
147
324
            case ELLIPSIS:
148
324
                number_of_dots += 3;
149
324
                break;
150
1.22k
            case DOT:
151
1.22k
                number_of_dots += 1;
152
1.22k
                break;
153
0
            default:
154
0
                Py_UNREACHABLE();
155
1.54k
        }
156
1.54k
    }
157
158
2.59k
    return number_of_dots;
159
2.59k
}
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
182
                        int end_col_offset, PyArena *arena) {
165
182
    PyObject *str = PyUnicode_InternFromString("*");
166
182
    if (!str) {
167
0
        return NULL;
168
0
    }
169
182
    if (_PyArena_AddPyObject(p->arena, str) < 0) {
170
0
        Py_DECREF(str);
171
0
        return NULL;
172
0
    }
173
182
    return _PyAST_alias(str, NULL, lineno, col_offset, end_lineno, end_col_offset, arena);
174
182
}
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.51k
{
180
1.51k
    Py_ssize_t len = asdl_seq_LEN(seq);
181
1.51k
    assert(len > 0);
182
183
1.51k
    asdl_identifier_seq *new_seq = _Py_asdl_identifier_seq_new(len, p->arena);
184
1.51k
    if (!new_seq) {
185
0
        return NULL;
186
0
    }
187
4.60k
    for (Py_ssize_t i = 0; i < len; i++) {
188
3.08k
        expr_ty e = asdl_seq_GET(seq, i);
189
3.08k
        asdl_seq_SET(new_seq, i, e->v.Name.id);
190
3.08k
    }
191
1.51k
    return new_seq;
192
1.51k
}
193
194
/* Constructs a CmpopExprPair */
195
CmpopExprPair *
196
_PyPegen_cmpop_expr_pair(Parser *p, cmpop_ty cmpop, expr_ty expr)
197
14.2k
{
198
14.2k
    assert(expr != NULL);
199
14.2k
    CmpopExprPair *a = _PyArena_Malloc(p->arena, sizeof(CmpopExprPair));
200
14.2k
    if (!a) {
201
0
        return NULL;
202
0
    }
203
14.2k
    a->cmpop = cmpop;
204
14.2k
    a->expr = expr;
205
14.2k
    return a;
206
14.2k
}
207
208
asdl_int_seq *
209
_PyPegen_get_cmpops(Parser *p, asdl_seq *seq)
210
6.16k
{
211
6.16k
    Py_ssize_t len = asdl_seq_LEN(seq);
212
6.16k
    assert(len > 0);
213
214
6.16k
    asdl_int_seq *new_seq = _Py_asdl_int_seq_new(len, p->arena);
215
6.16k
    if (!new_seq) {
216
0
        return NULL;
217
0
    }
218
19.8k
    for (Py_ssize_t i = 0; i < len; i++) {
219
13.6k
        CmpopExprPair *pair = asdl_seq_GET_UNTYPED(seq, i);
220
13.6k
        asdl_seq_SET(new_seq, i, pair->cmpop);
221
13.6k
    }
222
6.16k
    return new_seq;
223
6.16k
}
224
225
asdl_expr_seq *
226
_PyPegen_get_exprs(Parser *p, asdl_seq *seq)
227
6.16k
{
228
6.16k
    Py_ssize_t len = asdl_seq_LEN(seq);
229
6.16k
    assert(len > 0);
230
231
6.16k
    asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
232
6.16k
    if (!new_seq) {
233
0
        return NULL;
234
0
    }
235
19.8k
    for (Py_ssize_t i = 0; i < len; i++) {
236
13.6k
        CmpopExprPair *pair = asdl_seq_GET_UNTYPED(seq, i);
237
13.6k
        asdl_seq_SET(new_seq, i, pair->expr);
238
13.6k
    }
239
6.16k
    return new_seq;
240
6.16k
}
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.34k
{
246
3.34k
    Py_ssize_t len = asdl_seq_LEN(seq);
247
3.34k
    if (len == 0) {
248
1.69k
        return NULL;
249
1.69k
    }
250
251
1.64k
    asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
252
1.64k
    if (!new_seq) {
253
0
        return NULL;
254
0
    }
255
5.02k
    for (Py_ssize_t i = 0; i < len; i++) {
256
3.37k
        expr_ty e = asdl_seq_GET(seq, i);
257
3.37k
        asdl_seq_SET(new_seq, i, _PyPegen_set_expr_context(p, e, ctx));
258
3.37k
    }
259
1.64k
    return new_seq;
260
1.64k
}
261
262
static expr_ty
263
_set_name_context(Parser *p, expr_ty e, expr_context_ty ctx)
264
160k
{
265
160k
    return _PyAST_Name(e->v.Name.id, ctx, EXTRA_EXPR(e, e));
266
160k
}
267
268
static expr_ty
269
_set_tuple_context(Parser *p, expr_ty e, expr_context_ty ctx)
270
2.69k
{
271
2.69k
    return _PyAST_Tuple(
272
2.69k
            _set_seq_context(p, e->v.Tuple.elts, ctx),
273
2.69k
            ctx,
274
2.69k
            EXTRA_EXPR(e, e));
275
2.69k
}
276
277
static expr_ty
278
_set_list_context(Parser *p, expr_ty e, expr_context_ty ctx)
279
653
{
280
653
    return _PyAST_List(
281
653
            _set_seq_context(p, e->v.List.elts, ctx),
282
653
            ctx,
283
653
            EXTRA_EXPR(e, e));
284
653
}
285
286
static expr_ty
287
_set_subscript_context(Parser *p, expr_ty e, expr_context_ty ctx)
288
84
{
289
84
    return _PyAST_Subscript(e->v.Subscript.value, e->v.Subscript.slice,
290
84
                            ctx, EXTRA_EXPR(e, e));
291
84
}
292
293
static expr_ty
294
_set_attribute_context(Parser *p, expr_ty e, expr_context_ty ctx)
295
60
{
296
60
    return _PyAST_Attribute(e->v.Attribute.value, e->v.Attribute.attr,
297
60
                            ctx, EXTRA_EXPR(e, e));
298
60
}
299
300
static expr_ty
301
_set_starred_context(Parser *p, expr_ty e, expr_context_ty ctx)
302
314
{
303
314
    return _PyAST_Starred(_PyPegen_set_expr_context(p, e->v.Starred.value, ctx),
304
314
                          ctx, EXTRA_EXPR(e, e));
305
314
}
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
163k
{
311
163k
    assert(expr != NULL);
312
313
163k
    expr_ty new = NULL;
314
163k
    switch (expr->kind) {
315
160k
        case Name_kind:
316
160k
            new = _set_name_context(p, expr, ctx);
317
160k
            break;
318
2.69k
        case Tuple_kind:
319
2.69k
            new = _set_tuple_context(p, expr, ctx);
320
2.69k
            break;
321
653
        case List_kind:
322
653
            new = _set_list_context(p, expr, ctx);
323
653
            break;
324
84
        case Subscript_kind:
325
84
            new = _set_subscript_context(p, expr, ctx);
326
84
            break;
327
60
        case Attribute_kind:
328
60
            new = _set_attribute_context(p, expr, ctx);
329
60
            break;
330
314
        case Starred_kind:
331
314
            new = _set_starred_context(p, expr, ctx);
332
314
            break;
333
0
        default:
334
0
            new = expr;
335
163k
    }
336
163k
    return new;
337
163k
}
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
36.3k
{
343
36.3k
    KeyValuePair *a = _PyArena_Malloc(p->arena, sizeof(KeyValuePair));
344
36.3k
    if (!a) {
345
0
        return NULL;
346
0
    }
347
36.3k
    a->key = key;
348
36.3k
    a->value = value;
349
36.3k
    return a;
350
36.3k
}
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.05k
{
356
6.05k
    Py_ssize_t len = asdl_seq_LEN(seq);
357
6.05k
    asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
358
6.05k
    if (!new_seq) {
359
0
        return NULL;
360
0
    }
361
30.1k
    for (Py_ssize_t i = 0; i < len; i++) {
362
24.1k
        KeyValuePair *pair = asdl_seq_GET_UNTYPED(seq, i);
363
24.1k
        asdl_seq_SET(new_seq, i, pair->key);
364
24.1k
    }
365
6.05k
    return new_seq;
366
6.05k
}
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.05k
{
372
6.05k
    Py_ssize_t len = asdl_seq_LEN(seq);
373
6.05k
    asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
374
6.05k
    if (!new_seq) {
375
0
        return NULL;
376
0
    }
377
30.1k
    for (Py_ssize_t i = 0; i < len; i++) {
378
24.1k
        KeyValuePair *pair = asdl_seq_GET_UNTYPED(seq, i);
379
24.1k
        asdl_seq_SET(new_seq, i, pair->value);
380
24.1k
    }
381
6.05k
    return new_seq;
382
6.05k
}
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
8.08k
{
388
8.08k
    KeyPatternPair *a = _PyArena_Malloc(p->arena, sizeof(KeyPatternPair));
389
8.08k
    if (!a) {
390
0
        return NULL;
391
0
    }
392
8.08k
    a->key = key;
393
8.08k
    a->pattern = pattern;
394
8.08k
    return a;
395
8.08k
}
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.18k
{
401
1.18k
    Py_ssize_t len = asdl_seq_LEN(seq);
402
1.18k
    asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
403
1.18k
    if (!new_seq) {
404
0
        return NULL;
405
0
    }
406
3.02k
    for (Py_ssize_t i = 0; i < len; i++) {
407
1.84k
        KeyPatternPair *pair = asdl_seq_GET_UNTYPED(seq, i);
408
1.84k
        asdl_seq_SET(new_seq, i, pair->key);
409
1.84k
    }
410
1.18k
    return new_seq;
411
1.18k
}
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.18k
{
417
1.18k
    Py_ssize_t len = asdl_seq_LEN(seq);
418
1.18k
    asdl_pattern_seq *new_seq = _Py_asdl_pattern_seq_new(len, p->arena);
419
1.18k
    if (!new_seq) {
420
0
        return NULL;
421
0
    }
422
3.02k
    for (Py_ssize_t i = 0; i < len; i++) {
423
1.84k
        KeyPatternPair *pair = asdl_seq_GET_UNTYPED(seq, i);
424
1.84k
        asdl_seq_SET(new_seq, i, pair->pattern);
425
1.84k
    }
426
1.18k
    return new_seq;
427
1.18k
}
428
429
/* Constructs a NameDefaultPair */
430
NameDefaultPair *
431
_PyPegen_name_default_pair(Parser *p, arg_ty arg, expr_ty value, Token *tc)
432
552k
{
433
552k
    NameDefaultPair *a = _PyArena_Malloc(p->arena, sizeof(NameDefaultPair));
434
552k
    if (!a) {
435
0
        return NULL;
436
0
    }
437
552k
    a->arg = _PyPegen_add_type_comment_to_arg(p, arg, tc);
438
552k
    if (!a->arg) {
439
0
        return NULL;
440
0
    }
441
552k
    a->value = value;
442
552k
    return a;
443
552k
}
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
5.06k
{
449
5.06k
    SlashWithDefault *a = _PyArena_Malloc(p->arena, sizeof(SlashWithDefault));
450
5.06k
    if (!a) {
451
0
        return NULL;
452
0
    }
453
5.06k
    a->plain_names = plain_names;
454
5.06k
    a->names_with_defaults = names_with_defaults;
455
5.06k
    return a;
456
5.06k
}
457
458
/* Constructs a StarEtc */
459
StarEtc *
460
_PyPegen_star_etc(Parser *p, arg_ty vararg, asdl_seq *kwonlyargs, arg_ty kwarg)
461
4.43k
{
462
4.43k
    StarEtc *a = _PyArena_Malloc(p->arena, sizeof(StarEtc));
463
4.43k
    if (!a) {
464
0
        return NULL;
465
0
    }
466
4.43k
    a->vararg = vararg;
467
4.43k
    a->kwonlyargs = kwonlyargs;
468
4.43k
    a->kwarg = kwarg;
469
4.43k
    return a;
470
4.43k
}
471
472
asdl_seq *
473
_PyPegen_join_sequences(Parser *p, asdl_seq *a, asdl_seq *b)
474
338k
{
475
338k
    Py_ssize_t first_len = asdl_seq_LEN(a);
476
338k
    Py_ssize_t second_len = asdl_seq_LEN(b);
477
338k
    asdl_seq *new_seq = (asdl_seq*)_Py_asdl_generic_seq_new(first_len + second_len, p->arena);
478
338k
    if (!new_seq) {
479
0
        return NULL;
480
0
    }
481
482
338k
    int k = 0;
483
688k
    for (Py_ssize_t i = 0; i < first_len; i++) {
484
349k
        asdl_seq_SET_UNTYPED(new_seq, k++, asdl_seq_GET_UNTYPED(a, i));
485
349k
    }
486
348k
    for (Py_ssize_t i = 0; i < second_len; i++) {
487
9.72k
        asdl_seq_SET_UNTYPED(new_seq, k++, asdl_seq_GET_UNTYPED(b, i));
488
9.72k
    }
489
490
338k
    return new_seq;
491
338k
}
492
493
static asdl_arg_seq*
494
_get_names(Parser *p, asdl_seq *names_with_defaults)
495
342k
{
496
342k
    Py_ssize_t len = asdl_seq_LEN(names_with_defaults);
497
342k
    asdl_arg_seq *seq = _Py_asdl_arg_seq_new(len, p->arena);
498
342k
    if (!seq) {
499
0
        return NULL;
500
0
    }
501
355k
    for (Py_ssize_t i = 0; i < len; i++) {
502
13.2k
        NameDefaultPair *pair = asdl_seq_GET_UNTYPED(names_with_defaults, i);
503
13.2k
        asdl_seq_SET(seq, i, pair->arg);
504
13.2k
    }
505
342k
    return seq;
506
342k
}
507
508
static asdl_expr_seq *
509
_get_defaults(Parser *p, asdl_seq *names_with_defaults)
510
342k
{
511
342k
    Py_ssize_t len = asdl_seq_LEN(names_with_defaults);
512
342k
    asdl_expr_seq *seq = _Py_asdl_expr_seq_new(len, p->arena);
513
342k
    if (!seq) {
514
0
        return NULL;
515
0
    }
516
355k
    for (Py_ssize_t i = 0; i < len; i++) {
517
13.2k
        NameDefaultPair *pair = asdl_seq_GET_UNTYPED(names_with_defaults, i);
518
13.2k
        asdl_seq_SET(seq, i, pair->value);
519
13.2k
    }
520
342k
    return seq;
521
342k
}
522
523
static int
524
_make_posonlyargs(Parser *p,
525
                  asdl_arg_seq *slash_without_default,
526
                  SlashWithDefault *slash_with_default,
527
341k
                  asdl_arg_seq **posonlyargs) {
528
341k
    if (slash_without_default != NULL) {
529
1.63k
        *posonlyargs = slash_without_default;
530
1.63k
    }
531
339k
    else if (slash_with_default != NULL) {
532
1.63k
        asdl_arg_seq *slash_with_default_names =
533
1.63k
                _get_names(p, slash_with_default->names_with_defaults);
534
1.63k
        if (!slash_with_default_names) {
535
0
            return -1;
536
0
        }
537
1.63k
        *posonlyargs = (asdl_arg_seq*)_PyPegen_join_sequences(
538
1.63k
                p,
539
1.63k
                (asdl_seq*)slash_with_default->plain_names,
540
1.63k
                (asdl_seq*)slash_with_default_names);
541
1.63k
    }
542
338k
    else {
543
338k
        *posonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
544
338k
    }
545
341k
    return *posonlyargs == NULL ? -1 : 0;
546
341k
}
547
548
static int
549
_make_posargs(Parser *p,
550
              asdl_arg_seq *plain_names,
551
              asdl_seq *names_with_default,
552
341k
              asdl_arg_seq **posargs) {
553
554
341k
    if (names_with_default != NULL) {
555
337k
        if (plain_names != NULL) {
556
334k
            asdl_arg_seq *names_with_default_names = _get_names(p, names_with_default);
557
334k
            if (!names_with_default_names) {
558
0
                return -1;
559
0
            }
560
334k
            *posargs = (asdl_arg_seq*)_PyPegen_join_sequences(
561
334k
                    p,(asdl_seq*)plain_names, (asdl_seq*)names_with_default_names);
562
334k
        }
563
3.05k
        else {
564
3.05k
            *posargs = _get_names(p, names_with_default);
565
3.05k
        }
566
337k
    }
567
3.71k
    else {
568
3.71k
        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.71k
        else {
575
3.71k
            *posargs = _Py_asdl_arg_seq_new(0, p->arena);
576
3.71k
        }
577
3.71k
    }
578
341k
    return *posargs == NULL ? -1 : 0;
579
341k
}
580
581
static int
582
_make_posdefaults(Parser *p,
583
                  SlashWithDefault *slash_with_default,
584
                  asdl_seq *names_with_default,
585
341k
                  asdl_expr_seq **posdefaults) {
586
341k
    if (slash_with_default != NULL && names_with_default != NULL) {
587
1.63k
        asdl_expr_seq *slash_with_default_values =
588
1.63k
                _get_defaults(p, slash_with_default->names_with_defaults);
589
1.63k
        if (!slash_with_default_values) {
590
0
            return -1;
591
0
        }
592
1.63k
        asdl_expr_seq *names_with_default_values = _get_defaults(p, names_with_default);
593
1.63k
        if (!names_with_default_values) {
594
0
            return -1;
595
0
        }
596
1.63k
        *posdefaults = (asdl_expr_seq*)_PyPegen_join_sequences(
597
1.63k
                p,
598
1.63k
                (asdl_seq*)slash_with_default_values,
599
1.63k
                (asdl_seq*)names_with_default_values);
600
1.63k
    }
601
339k
    else if (slash_with_default == NULL && names_with_default != NULL) {
602
336k
        *posdefaults = _get_defaults(p, names_with_default);
603
336k
    }
604
3.71k
    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.71k
    else {
608
3.71k
        *posdefaults = _Py_asdl_expr_seq_new(0, p->arena);
609
3.71k
    }
610
341k
    return *posdefaults == NULL ? -1 : 0;
611
341k
}
612
613
static int
614
_make_kwargs(Parser *p, StarEtc *star_etc,
615
             asdl_arg_seq **kwonlyargs,
616
341k
             asdl_expr_seq **kwdefaults) {
617
341k
    if (star_etc != NULL && star_etc->kwonlyargs != NULL) {
618
3.26k
        *kwonlyargs = _get_names(p, star_etc->kwonlyargs);
619
3.26k
    }
620
338k
    else {
621
338k
        *kwonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
622
338k
    }
623
624
341k
    if (*kwonlyargs == NULL) {
625
0
        return -1;
626
0
    }
627
628
341k
    if (star_etc != NULL && star_etc->kwonlyargs != NULL) {
629
3.26k
        *kwdefaults = _get_defaults(p, star_etc->kwonlyargs);
630
3.26k
    }
631
338k
    else {
632
338k
        *kwdefaults = _Py_asdl_expr_seq_new(0, p->arena);
633
338k
    }
634
635
341k
    if (*kwdefaults == NULL) {
636
0
        return -1;
637
0
    }
638
639
341k
    return 0;
640
341k
}
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
341k
{
648
341k
    asdl_arg_seq *posonlyargs;
649
341k
    if (_make_posonlyargs(p, slash_without_default, slash_with_default, &posonlyargs) == -1) {
650
0
        return NULL;
651
0
    }
652
653
341k
    asdl_arg_seq *posargs;
654
341k
    if (_make_posargs(p, plain_names, names_with_default, &posargs) == -1) {
655
0
        return NULL;
656
0
    }
657
658
341k
    asdl_expr_seq *posdefaults;
659
341k
    if (_make_posdefaults(p,slash_with_default, names_with_default, &posdefaults) == -1) {
660
0
        return NULL;
661
0
    }
662
663
341k
    arg_ty vararg = NULL;
664
341k
    if (star_etc != NULL && star_etc->vararg != NULL) {
665
2.11k
        vararg = star_etc->vararg;
666
2.11k
    }
667
668
341k
    asdl_arg_seq *kwonlyargs;
669
341k
    asdl_expr_seq *kwdefaults;
670
341k
    if (_make_kwargs(p, star_etc, &kwonlyargs, &kwdefaults) == -1) {
671
0
        return NULL;
672
0
    }
673
674
341k
    arg_ty kwarg = NULL;
675
341k
    if (star_etc != NULL && star_etc->kwarg != NULL) {
676
1.26k
        kwarg = star_etc->kwarg;
677
1.26k
    }
678
679
341k
    return _PyAST_arguments(posonlyargs, posargs, vararg, kwonlyargs,
680
341k
                            kwdefaults, kwarg, posdefaults, p->arena);
681
341k
}
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.19k
{
689
2.19k
    asdl_arg_seq *posonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
690
2.19k
    if (!posonlyargs) {
691
0
        return NULL;
692
0
    }
693
2.19k
    asdl_arg_seq *posargs = _Py_asdl_arg_seq_new(0, p->arena);
694
2.19k
    if (!posargs) {
695
0
        return NULL;
696
0
    }
697
2.19k
    asdl_expr_seq *posdefaults = _Py_asdl_expr_seq_new(0, p->arena);
698
2.19k
    if (!posdefaults) {
699
0
        return NULL;
700
0
    }
701
2.19k
    asdl_arg_seq *kwonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
702
2.19k
    if (!kwonlyargs) {
703
0
        return NULL;
704
0
    }
705
2.19k
    asdl_expr_seq *kwdefaults = _Py_asdl_expr_seq_new(0, p->arena);
706
2.19k
    if (!kwdefaults) {
707
0
        return NULL;
708
0
    }
709
710
2.19k
    return _PyAST_arguments(posonlyargs, posargs, NULL, kwonlyargs,
711
2.19k
                            kwdefaults, NULL, posdefaults, p->arena);
712
2.19k
}
713
714
/* Encapsulates the value of an operator_ty into an AugOperator struct */
715
AugOperator *
716
_PyPegen_augoperator(Parser *p, operator_ty kind)
717
2.59k
{
718
2.59k
    AugOperator *a = _PyArena_Malloc(p->arena, sizeof(AugOperator));
719
2.59k
    if (!a) {
720
0
        return NULL;
721
0
    }
722
2.59k
    a->kind = kind;
723
2.59k
    return a;
724
2.59k
}
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
423
{
730
423
    assert(function_def != NULL);
731
423
    if (function_def->kind == AsyncFunctionDef_kind) {
732
78
        return _PyAST_AsyncFunctionDef(
733
78
            function_def->v.AsyncFunctionDef.name,
734
78
            function_def->v.AsyncFunctionDef.args,
735
78
            function_def->v.AsyncFunctionDef.body, decorators,
736
78
            function_def->v.AsyncFunctionDef.returns,
737
78
            function_def->v.AsyncFunctionDef.type_comment,
738
78
            function_def->v.AsyncFunctionDef.type_params,
739
78
            function_def->lineno, function_def->col_offset,
740
78
            function_def->end_lineno, function_def->end_col_offset, p->arena);
741
78
    }
742
743
345
    return _PyAST_FunctionDef(
744
345
        function_def->v.FunctionDef.name,
745
345
        function_def->v.FunctionDef.args,
746
345
        function_def->v.FunctionDef.body, decorators,
747
345
        function_def->v.FunctionDef.returns,
748
345
        function_def->v.FunctionDef.type_comment,
749
345
        function_def->v.FunctionDef.type_params,
750
345
        function_def->lineno, function_def->col_offset,
751
345
        function_def->end_lineno, function_def->end_col_offset, p->arena);
752
423
}
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
284
{
758
284
    assert(class_def != NULL);
759
284
    return _PyAST_ClassDef(
760
284
        class_def->v.ClassDef.name,
761
284
        class_def->v.ClassDef.bases, class_def->v.ClassDef.keywords,
762
284
        class_def->v.ClassDef.body, decorators,
763
284
        class_def->v.ClassDef.type_params,
764
284
        class_def->lineno, class_def->col_offset, class_def->end_lineno,
765
284
        class_def->end_col_offset, p->arena);
766
284
}
767
768
/* Construct a KeywordOrStarred */
769
KeywordOrStarred *
770
_PyPegen_keyword_or_starred(Parser *p, void *element, int is_keyword)
771
36.7k
{
772
36.7k
    KeywordOrStarred *a = _PyArena_Malloc(p->arena, sizeof(KeywordOrStarred));
773
36.7k
    if (!a) {
774
0
        return NULL;
775
0
    }
776
36.7k
    a->element = element;
777
36.7k
    a->is_keyword = is_keyword;
778
36.7k
    return a;
779
36.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
17.1k
{
785
17.1k
    int n = 0;
786
62.2k
    for (Py_ssize_t i = 0, l = asdl_seq_LEN(seq); i < l; i++) {
787
45.1k
        KeywordOrStarred *k = asdl_seq_GET_UNTYPED(seq, i);
788
45.1k
        if (!k->is_keyword) {
789
2.85k
            n++;
790
2.85k
        }
791
45.1k
    }
792
17.1k
    return n;
793
17.1k
}
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.55k
{
799
8.55k
    int new_len = _seq_number_of_starred_exprs(kwargs);
800
8.55k
    if (new_len == 0) {
801
8.01k
        return NULL;
802
8.01k
    }
803
544
    asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(new_len, p->arena);
804
544
    if (!new_seq) {
805
0
        return NULL;
806
0
    }
807
808
544
    int idx = 0;
809
3.23k
    for (Py_ssize_t i = 0, len = asdl_seq_LEN(kwargs); i < len; i++) {
810
2.69k
        KeywordOrStarred *k = asdl_seq_GET_UNTYPED(kwargs, i);
811
2.69k
        if (!k->is_keyword) {
812
1.42k
            asdl_seq_SET(new_seq, idx++, k->element);
813
1.42k
        }
814
2.69k
    }
815
544
    return new_seq;
816
544
}
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.55k
{
822
8.55k
    Py_ssize_t len = asdl_seq_LEN(kwargs);
823
8.55k
    Py_ssize_t new_len = len - _seq_number_of_starred_exprs(kwargs);
824
8.55k
    if (new_len == 0) {
825
0
        return NULL;
826
0
    }
827
8.55k
    asdl_keyword_seq *new_seq = _Py_asdl_keyword_seq_new(new_len, p->arena);
828
8.55k
    if (!new_seq) {
829
0
        return NULL;
830
0
    }
831
832
8.55k
    int idx = 0;
833
31.1k
    for (Py_ssize_t i = 0; i < len; i++) {
834
22.5k
        KeywordOrStarred *k = asdl_seq_GET_UNTYPED(kwargs, i);
835
22.5k
        if (k->is_keyword) {
836
21.1k
            asdl_seq_SET(new_seq, idx++, k->element);
837
21.1k
        }
838
22.5k
    }
839
8.55k
    return new_seq;
840
8.55k
}
841
842
expr_ty
843
_PyPegen_ensure_imaginary(Parser *p, expr_ty exp)
844
345
{
845
345
    if (exp->kind != Constant_kind || !PyComplex_CheckExact(exp->v.Constant.value)) {
846
9
        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(exp, "imaginary number required in complex literal");
847
9
        return NULL;
848
9
    }
849
336
    return exp;
850
345
}
851
852
expr_ty
853
_PyPegen_ensure_real(Parser *p, expr_ty exp)
854
592
{
855
592
    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
589
    return exp;
860
592
}
861
862
mod_ty
863
6.73k
_PyPegen_make_module(Parser *p, asdl_stmt_seq *a) {
864
6.73k
    asdl_type_ignore_seq *type_ignores = NULL;
865
6.73k
    Py_ssize_t num = p->type_ignore_comments.num_items;
866
6.73k
    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
6.73k
    return _PyAST_Module(a, type_ignores, p->arena);
886
6.73k
}
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
616k
{
905
616k
    if (tc == NULL) {
906
616k
        return a;
907
616k
    }
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.05k
_PyPegen_check_barry_as_flufl(Parser *p, Token* t) {
925
1.05k
    assert(t->bytes != NULL);
926
1.05k
    assert(t->type == NOTEQUAL);
927
928
1.05k
    const char* tok_str = PyBytes_AS_STRING(t->bytes);
929
1.05k
    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.05k
    if (!(p->flags & PyPARSE_BARRY_AS_BDFL)) {
934
1.01k
        return strcmp(tok_str, "!=");
935
1.01k
    }
936
36
    return 0;
937
1.05k
}
938
939
int
940
8.20k
_PyPegen_check_legacy_stmt(Parser *p, expr_ty name) {
941
8.20k
    if (name->kind != Name_kind) {
942
2.32k
        return 0;
943
2.32k
    }
944
5.87k
    const char* candidates[2] = {"print", "exec"};
945
17.5k
    for (int i=0; i<2; i++) {
946
11.7k
        if (PyUnicode_CompareWithASCIIString(name->v.Name.id, candidates[i]) == 0) {
947
41
            return 1;
948
41
        }
949
11.7k
    }
950
5.83k
    return 0;
951
5.87k
}
952
953
void *
954
_PyPegen_raise_error_for_missing_comma(Parser *p, expr_ty a, expr_ty b)
955
4.18k
{
956
    // Don't raise for legacy statements like "print x" or "exec x"
957
4.18k
    if (_PyPegen_check_legacy_stmt(p, a)) {
958
34
        return NULL;
959
34
    }
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
173
    if (a->end_lineno > a->lineno) {
969
7
        return RAISE_ERROR_KNOWN_LOCATION(
970
7
            p, PyExc_SyntaxError, a->end_lineno, a->col_offset,
971
7
            a->end_lineno, a->end_col_offset,
972
7
            "invalid syntax. Perhaps you forgot a comma?"
973
7
        );
974
7
    }
975
166
    return RAISE_ERROR_KNOWN_LOCATION(
976
166
        p, PyExc_SyntaxError, a->lineno, a->col_offset,
977
166
        b->end_lineno, b->end_col_offset,
978
166
        "invalid syntax. Perhaps you forgot a comma?"
979
166
    );
980
173
}
981
982
static ResultTokenWithMetadata *
983
result_token_with_metadata(Parser *p, void *result, PyObject *metadata)
984
8.96k
{
985
8.96k
    ResultTokenWithMetadata *res = _PyArena_Malloc(p->arena, sizeof(ResultTokenWithMetadata));
986
8.96k
    if (res == NULL) {
987
0
        return NULL;
988
0
    }
989
8.96k
    res->metadata = metadata;
990
8.96k
    res->result = result;
991
8.96k
    return res;
992
8.96k
}
993
994
ResultTokenWithMetadata *
995
_PyPegen_check_fstring_conversion(Parser *p, Token* conv_token, expr_ty conv)
996
2.34k
{
997
2.34k
    if (conv_token->lineno != conv->lineno || conv_token->end_col_offset != conv->col_offset) {
998
2
        return RAISE_SYNTAX_ERROR_KNOWN_RANGE(
999
2
            conv_token, conv,
1000
2
            "%c-string: conversion type must come right after the exclamation mark",
1001
2
            TOK_GET_STRING_PREFIX(p->tok)
1002
2
        );
1003
2
    }
1004
1005
2.34k
    Py_UCS4 first = PyUnicode_READ_CHAR(conv->v.Name.id, 0);
1006
2.34k
    if (PyUnicode_GET_LENGTH(conv->v.Name.id) > 1 ||
1007
2.34k
            !(first == 's' || first == 'r' || first == 'a')) {
1008
17
        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(conv,
1009
17
                                            "%c-string: invalid conversion character %R: expected 's', 'r', or 'a'",
1010
17
                                            TOK_GET_STRING_PREFIX(p->tok),
1011
17
                                            conv->v.Name.id);
1012
17
        return NULL;
1013
17
    }
1014
1015
2.32k
    return result_token_with_metadata(p, conv, conv_token->metadata);
1016
2.34k
}
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.64k
{
1022
6.64k
    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.64k
    Py_ssize_t n_items = asdl_seq_LEN(spec);
1030
6.64k
    Py_ssize_t non_empty_count = 0;
1031
19.6k
    for (Py_ssize_t i = 0; i < n_items; i++) {
1032
13.0k
        expr_ty item = asdl_seq_GET(spec, i);
1033
13.0k
        non_empty_count += !(item->kind == Constant_kind &&
1034
13.0k
                             PyUnicode_CheckExact(item->v.Constant.value) &&
1035
9.67k
                             PyUnicode_GET_LENGTH(item->v.Constant.value) == 0);
1036
13.0k
    }
1037
6.64k
    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.38k
        for (Py_ssize_t i = 0; i < n_items; i++) {
1045
3.23k
            expr_ty item = asdl_seq_GET(spec, i);
1046
3.23k
            if (item->kind == Constant_kind &&
1047
3.23k
                PyUnicode_CheckExact(item->v.Constant.value) &&
1048
2.59k
                PyUnicode_GET_LENGTH(item->v.Constant.value) == 0) {
1049
2.14k
                continue;
1050
2.14k
            }
1051
1.09k
            asdl_seq_SET(resized_spec, j++, item);
1052
1.09k
        }
1053
2.14k
        assert(j == non_empty_count);
1054
2.14k
        spec = resized_spec;
1055
2.14k
    }
1056
6.64k
    expr_ty res;
1057
6.64k
    Py_ssize_t n = asdl_seq_LEN(spec);
1058
6.64k
    if (n == 0 || (n == 1 && asdl_seq_GET(spec, 0)->kind == Constant_kind)) {
1059
5.99k
        res = _PyAST_JoinedStr(spec, lineno, col_offset, end_lineno,
1060
5.99k
                                    end_col_offset, p->arena);
1061
5.99k
    } else {
1062
649
        res = _PyPegen_concatenate_strings(p, spec,
1063
649
                             lineno, col_offset, end_lineno,
1064
649
                             end_col_offset, arena);
1065
649
    }
1066
6.64k
    if (!res) {
1067
0
        return NULL;
1068
0
    }
1069
6.64k
    return result_token_with_metadata(p, res, colon->metadata);
1070
6.64k
}
1071
1072
const char *
1073
_PyPegen_get_expr_name(expr_ty e)
1074
186
{
1075
186
    assert(e != NULL);
1076
186
    switch (e->kind) {
1077
1
        case Attribute_kind:
1078
1
            return "attribute";
1079
2
        case Subscript_kind:
1080
2
            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
28
        case BinOp_kind:
1095
41
        case UnaryOp_kind:
1096
41
            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
4
        case JoinedStr_kind:
1115
4
        case FormattedValue_kind:
1116
4
            return "f-string expression";
1117
5
        case TemplateStr_kind:
1118
5
        case Interpolation_kind:
1119
5
            return "t-string expression";
1120
76
        case Constant_kind: {
1121
76
            PyObject *value = e->v.Constant.value;
1122
76
            if (value == Py_None) {
1123
1
                return "None";
1124
1
            }
1125
75
            if (value == Py_False) {
1126
1
                return "False";
1127
1
            }
1128
74
            if (value == Py_True) {
1129
3
                return "True";
1130
3
            }
1131
71
            if (value == Py_Ellipsis) {
1132
1
                return "ellipsis";
1133
1
            }
1134
70
            return "literal";
1135
71
        }
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
186
    }
1148
186
}
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
23.9k
                     int end_col_offset, PyArena *arena) {
1161
23.9k
    Py_ssize_t args_len = asdl_seq_LEN(a);
1162
23.9k
    Py_ssize_t total_len = args_len;
1163
1164
23.9k
    if (b == NULL) {
1165
21.7k
        return _PyAST_Call(_PyPegen_dummy_name(p), a, NULL, lineno, col_offset,
1166
21.7k
                        end_lineno, end_col_offset, arena);
1167
1168
21.7k
    }
1169
1170
2.17k
    asdl_expr_seq *starreds = _PyPegen_seq_extract_starred_exprs(p, b);
1171
2.17k
    asdl_keyword_seq *keywords = _PyPegen_seq_delete_starred_exprs(p, b);
1172
1173
2.17k
    if (starreds) {
1174
270
        total_len += asdl_seq_LEN(starreds);
1175
270
    }
1176
1177
2.17k
    asdl_expr_seq *args = _Py_asdl_expr_seq_new(total_len, arena);
1178
2.17k
    if (args == NULL) {
1179
0
        return NULL;
1180
0
    }
1181
1182
2.17k
    Py_ssize_t i = 0;
1183
4.86k
    for (i = 0; i < args_len; i++) {
1184
2.68k
        asdl_seq_SET(args, i, asdl_seq_GET(a, i));
1185
2.68k
    }
1186
2.89k
    for (; i < total_len; i++) {
1187
723
        asdl_seq_SET(args, i, asdl_seq_GET(starreds, i - args_len));
1188
723
    }
1189
1190
2.17k
    return _PyAST_Call(_PyPegen_dummy_name(p), args, keywords, lineno,
1191
2.17k
                       col_offset, end_lineno, end_col_offset, arena);
1192
2.17k
}
1193
1194
// AST Error reporting helpers
1195
1196
expr_ty
1197
_PyPegen_get_invalid_target(expr_ty e, TARGETS_TYPE targets_type)
1198
6.78k
{
1199
6.78k
    if (e == NULL) {
1200
0
        return NULL;
1201
0
    }
1202
1203
6.78k
#define VISIT_CONTAINER(CONTAINER, TYPE) do { \
1204
1.61k
        Py_ssize_t len = asdl_seq_LEN((CONTAINER)->v.TYPE.elts);\
1205
5.56k
        for (Py_ssize_t i = 0; i < len; i++) {\
1206
4.07k
            expr_ty other = asdl_seq_GET((CONTAINER)->v.TYPE.elts, i);\
1207
4.07k
            expr_ty child = _PyPegen_get_invalid_target(other, targets_type);\
1208
4.07k
            if (child != NULL) {\
1209
122
                return child;\
1210
122
            }\
1211
4.07k
        }\
1212
1.61k
    } 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
6.78k
    switch (e->kind) {
1221
693
        case List_kind:
1222
693
            VISIT_CONTAINER(e, List);
1223
656
            return NULL;
1224
922
        case Tuple_kind:
1225
922
            VISIT_CONTAINER(e, Tuple);
1226
837
            return NULL;
1227
1.15k
        case Starred_kind:
1228
1.15k
            if (targets_type == DEL_TARGETS) {
1229
1
                return e;
1230
1
            }
1231
1.15k
            return _PyPegen_get_invalid_target(e->v.Starred.value, targets_type);
1232
582
        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
582
            if (targets_type == FOR_TARGETS) {
1237
572
                cmpop_ty cmpop = (cmpop_ty) asdl_seq_GET(e->v.Compare.ops, 0);
1238
572
                if (cmpop == In) {
1239
41
                    return _PyPegen_get_invalid_target(e->v.Compare.left, targets_type);
1240
41
                }
1241
531
                return NULL;
1242
572
            }
1243
10
            return e;
1244
2.58k
        case Name_kind:
1245
2.88k
        case Subscript_kind:
1246
3.31k
        case Attribute_kind:
1247
3.31k
            return NULL;
1248
120
        default:
1249
120
            return e;
1250
6.78k
    }
1251
6.78k
}
1252
1253
32
void *_PyPegen_arguments_parsing_error(Parser *p, expr_ty e) {
1254
32
    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.06k
        keyword_ty keyword = asdl_seq_GET(e->v.Call.keywords, i);
1257
1.06k
        if (!keyword->arg) {
1258
647
            kwarg_unpacking = 1;
1259
647
        }
1260
1.06k
    }
1261
1262
32
    const char *msg = NULL;
1263
32
    if (kwarg_unpacking) {
1264
22
        msg = "positional argument follows keyword argument unpacking";
1265
22
    } else {
1266
10
        msg = "positional argument follows keyword argument";
1267
10
    }
1268
1269
32
    return RAISE_SYNTAX_ERROR(msg);
1270
32
}
1271
1272
void *
1273
_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args, asdl_comprehension_seq *comprehensions)
1274
221
{
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
221
    Py_ssize_t len = asdl_seq_LEN(args->v.Call.args);
1283
221
    if (len <= 1) {
1284
218
        return NULL;
1285
218
    }
1286
1287
3
    comprehension_ty last_comprehension = PyPegen_last_item(comprehensions, comprehension_ty);
1288
1289
3
    return RAISE_SYNTAX_ERROR_KNOWN_RANGE(
1290
221
        (expr_ty) asdl_seq_GET(args->v.Call.args, len - 1),
1291
221
        _PyPegen_get_last_comprehension_item(last_comprehension),
1292
221
        "Generator expression must be parenthesized"
1293
221
    );
1294
221
}
1295
1296
// Fstring stuff
1297
1298
static expr_ty
1299
26.3k
_PyPegen_decode_fstring_part(Parser* p, int is_raw, expr_ty constant, Token* token) {
1300
26.3k
    assert(PyUnicode_CheckExact(constant->v.Constant.value));
1301
1302
26.3k
    const char* bstr = PyUnicode_AsUTF8(constant->v.Constant.value);
1303
26.3k
    if (bstr == NULL) {
1304
0
        return NULL;
1305
0
    }
1306
1307
26.3k
    size_t len;
1308
26.3k
    if (strcmp(bstr, "{{") == 0 || strcmp(bstr, "}}") == 0) {
1309
0
        len = 1;
1310
26.3k
    } else {
1311
26.3k
        len = strlen(bstr);
1312
26.3k
    }
1313
1314
26.3k
    is_raw = is_raw || strchr(bstr, '\\') == NULL;
1315
26.3k
    PyObject *str = _PyPegen_decode_string(p, is_raw, bstr, len, token);
1316
26.3k
    if (str == NULL) {
1317
11
        _Pypegen_raise_decode_error(p);
1318
11
        return NULL;
1319
11
    }
1320
26.3k
    if (_PyArena_AddPyObject(p->arena, str) < 0) {
1321
0
        Py_DECREF(str);
1322
0
        return NULL;
1323
0
    }
1324
26.3k
    return _PyAST_Constant(str, NULL, constant->lineno, constant->col_offset,
1325
26.3k
                           constant->end_lineno, constant->end_col_offset,
1326
26.3k
                           p->arena);
1327
26.3k
}
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.0k
{
1332
19.0k
    Py_ssize_t n_items = asdl_seq_LEN(raw_expressions);
1333
19.0k
    Py_ssize_t total_items = n_items;
1334
68.1k
    for (Py_ssize_t i = 0; i < n_items; i++) {
1335
49.0k
        expr_ty item = asdl_seq_GET(raw_expressions, i);
1336
49.0k
        if (item->kind == JoinedStr_kind) {
1337
6.01k
            total_items += asdl_seq_LEN(item->v.JoinedStr.values) - 1;
1338
6.01k
        }
1339
49.0k
    }
1340
1341
19.0k
    const char* quote_str = PyBytes_AsString(a->bytes);
1342
19.0k
    if (quote_str == NULL) {
1343
0
        return NULL;
1344
0
    }
1345
19.0k
    int is_raw = strpbrk(quote_str, "rR") != NULL;
1346
1347
19.0k
    asdl_expr_seq *seq = _Py_asdl_expr_seq_new(total_items, p->arena);
1348
19.0k
    if (seq == NULL) {
1349
0
        return NULL;
1350
0
    }
1351
1352
19.0k
    Py_ssize_t index = 0;
1353
67.9k
    for (Py_ssize_t i = 0; i < n_items; i++) {
1354
48.9k
        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
48.9k
        if (item->kind == JoinedStr_kind) {
1361
6.01k
            asdl_expr_seq *values = item->v.JoinedStr.values;
1362
6.01k
            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.01k
            expr_ty first = asdl_seq_GET(values, 0);
1372
6.01k
            assert(first->kind == Constant_kind);
1373
6.01k
            asdl_seq_SET(seq, index++, first);
1374
1375
6.01k
            expr_ty second = asdl_seq_GET(values, 1);
1376
6.01k
            assert((string_kind == TSTRING && second->kind == Interpolation_kind) || second->kind == FormattedValue_kind);
1377
6.01k
            asdl_seq_SET(seq, index++, second);
1378
1379
6.01k
            continue;
1380
6.01k
        }
1381
1382
42.8k
        if (item->kind == Constant_kind) {
1383
26.3k
            item = _PyPegen_decode_fstring_part(p, is_raw, item, b);
1384
26.3k
            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
26.3k
            if (PyUnicode_CheckExact(item->v.Constant.value)
1392
26.3k
                && PyUnicode_GET_LENGTH(item->v.Constant.value) == 0) {
1393
202
                continue;
1394
202
            }
1395
26.3k
        }
1396
42.6k
        asdl_seq_SET(seq, index++, item);
1397
42.6k
    }
1398
1399
19.0k
    asdl_expr_seq *resized_exprs;
1400
19.0k
    if (index != total_items) {
1401
202
        resized_exprs = _Py_asdl_expr_seq_new(index, p->arena);
1402
202
        if (resized_exprs == NULL) {
1403
0
            return NULL;
1404
0
        }
1405
552
        for (Py_ssize_t i = 0; i < index; i++) {
1406
350
            asdl_seq_SET(resized_exprs, i, asdl_seq_GET(seq, i));
1407
350
        }
1408
202
    }
1409
18.8k
    else {
1410
18.8k
        resized_exprs = seq;
1411
18.8k
    }
1412
19.0k
    return resized_exprs;
1413
19.0k
}
1414
1415
expr_ty
1416
4.82k
_PyPegen_template_str(Parser *p, Token *a, asdl_expr_seq *raw_expressions, Token *b) {
1417
1418
4.82k
    asdl_expr_seq *resized_exprs = _get_resized_exprs(p, a, raw_expressions, b, TSTRING);
1419
4.82k
    return _PyAST_TemplateStr(resized_exprs, a->lineno, a->col_offset,
1420
4.82k
                              b->end_lineno, b->end_col_offset,
1421
4.82k
                              p->arena);
1422
4.82k
}
1423
1424
expr_ty
1425
14.2k
_PyPegen_joined_str(Parser *p, Token* a, asdl_expr_seq* raw_expressions, Token*b) {
1426
1427
14.2k
    asdl_expr_seq *resized_exprs = _get_resized_exprs(p, a, raw_expressions, b, FSTRING);
1428
14.2k
    return _PyAST_JoinedStr(resized_exprs, a->lineno, a->col_offset,
1429
14.2k
                            b->end_lineno, b->end_col_offset,
1430
14.2k
                            p->arena);
1431
14.2k
}
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.20k
        tokenizer_mode *mode = TOK_GET_MODE(p->tok);
1444
7.20k
        is_raw = mode->raw;
1445
7.20k
    }
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
30.2k
expr_ty _PyPegen_constant_from_token(Parser* p, Token* tok) {
1461
30.2k
    char* bstr = PyBytes_AsString(tok->bytes);
1462
30.2k
    if (bstr == NULL) {
1463
0
        return NULL;
1464
0
    }
1465
30.2k
    PyObject* str = PyUnicode_FromString(bstr);
1466
30.2k
    if (str == NULL) {
1467
0
        return NULL;
1468
0
    }
1469
30.2k
    if (_PyArena_AddPyObject(p->arena, str) < 0) {
1470
0
        Py_DECREF(str);
1471
0
        return NULL;
1472
0
    }
1473
30.2k
    return _PyAST_Constant(str, NULL, tok->lineno, tok->col_offset,
1474
30.2k
                           tok->end_lineno, tok->end_col_offset,
1475
30.2k
                           p->arena);
1476
30.2k
}
1477
1478
51.3k
expr_ty _PyPegen_constant_from_string(Parser* p, Token* tok) {
1479
51.3k
    char* the_str = PyBytes_AsString(tok->bytes);
1480
51.3k
    if (the_str == NULL) {
1481
0
        return NULL;
1482
0
    }
1483
51.3k
    PyObject *s = _PyPegen_parse_string(p, tok);
1484
51.3k
    if (s == NULL) {
1485
112
        _Pypegen_raise_decode_error(p);
1486
112
        return NULL;
1487
112
    }
1488
51.2k
    if (_PyArena_AddPyObject(p->arena, s) < 0) {
1489
0
        Py_DECREF(s);
1490
0
        return NULL;
1491
0
    }
1492
51.2k
    PyObject *kind = NULL;
1493
51.2k
    if (the_str && the_str[0] == 'u') {
1494
467
        kind = _PyPegen_new_identifier(p, "u");
1495
467
        if (kind == NULL) {
1496
0
            return NULL;
1497
0
        }
1498
467
    }
1499
51.2k
    return _PyAST_Constant(s, kind, tok->lineno, tok->col_offset, tok->end_lineno, tok->end_col_offset, p->arena);
1500
51.2k
}
1501
1502
static int
1503
_get_interpolation_conversion(Parser *p, Token *debug, ResultTokenWithMetadata *conversion,
1504
                              ResultTokenWithMetadata *format)
1505
28.1k
{
1506
28.1k
    if (conversion != NULL) {
1507
2.30k
        expr_ty conversion_expr = (expr_ty) conversion->result;
1508
2.30k
        assert(conversion_expr->kind == Name_kind);
1509
2.30k
        Py_UCS4 first = PyUnicode_READ_CHAR(conversion_expr->v.Name.id, 0);
1510
2.30k
        return Py_SAFE_DOWNCAST(first, Py_UCS4, int);
1511
2.30k
    }
1512
25.8k
    else if (debug && !format) {
1513
        /* If no conversion is specified, use !r for debug expressions */
1514
5.83k
        return (int)'r';
1515
5.83k
    }
1516
19.9k
    return -1;
1517
28.1k
}
1518
1519
static PyObject *
1520
_strip_interpolation_expr(PyObject *exprstr)
1521
4.97k
{
1522
4.97k
    Py_ssize_t len = PyUnicode_GET_LENGTH(exprstr);
1523
1524
13.0k
    for (Py_ssize_t i = len - 1; i >= 0; i--) {
1525
13.0k
        Py_UCS4 c = PyUnicode_READ_CHAR(exprstr, i);
1526
13.0k
        if (_PyUnicode_IsWhitespace(c) || c == '=') {
1527
8.02k
            len--;
1528
8.02k
        }
1529
4.97k
        else {
1530
4.97k
            break;
1531
4.97k
        }
1532
13.0k
    }
1533
1534
4.97k
    return PyUnicode_Substring(exprstr, 0, len);
1535
4.97k
}
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
4.97k
                                 int end_lineno, int end_col_offset, PyArena *arena) {
1540
1541
4.97k
    int conversion_val = _get_interpolation_conversion(p, debug, conversion, format);
1542
1543
    /* Find the non whitespace token after the "=" */
1544
4.97k
    int debug_end_line, debug_end_offset;
1545
4.97k
    PyObject *debug_metadata;
1546
4.97k
    constant exprstr;
1547
1548
4.97k
    if (conversion) {
1549
327
        debug_end_line = ((expr_ty) conversion->result)->lineno;
1550
327
        debug_end_offset = ((expr_ty) conversion->result)->col_offset;
1551
327
        debug_metadata = exprstr = conversion->metadata;
1552
327
    }
1553
4.64k
    else if (format) {
1554
906
        debug_end_line = ((expr_ty) format->result)->lineno;
1555
906
        debug_end_offset = ((expr_ty) format->result)->col_offset + 1;
1556
906
        debug_metadata = exprstr = format->metadata;
1557
906
    }
1558
3.74k
    else {
1559
3.74k
        debug_end_line = end_lineno;
1560
3.74k
        debug_end_offset = end_col_offset;
1561
3.74k
        debug_metadata = exprstr = closing_brace->metadata;
1562
3.74k
    }
1563
1564
4.97k
    assert(exprstr != NULL);
1565
4.97k
    PyObject *final_exprstr = _strip_interpolation_expr(exprstr);
1566
4.97k
    if (!final_exprstr || _PyArena_AddPyObject(arena, final_exprstr) < 0) {
1567
0
        Py_XDECREF(final_exprstr);
1568
0
        return NULL;
1569
0
    }
1570
1571
4.97k
    expr_ty interpolation = _PyAST_Interpolation(
1572
4.97k
        expression, final_exprstr, conversion_val, format ? (expr_ty) format->result : NULL,
1573
4.97k
        lineno, col_offset, end_lineno,
1574
4.97k
        end_col_offset, arena
1575
4.97k
    );
1576
1577
4.97k
    if (!debug) {
1578
4.83k
        return interpolation;
1579
4.83k
    }
1580
1581
142
    expr_ty debug_text = _PyAST_Constant(debug_metadata, NULL, lineno, col_offset + 1, debug_end_line,
1582
142
                                            debug_end_offset - 1, p->arena);
1583
142
    if (!debug_text) {
1584
0
        return NULL;
1585
0
    }
1586
1587
142
    asdl_expr_seq *values = _Py_asdl_expr_seq_new(2, arena);
1588
142
    asdl_seq_SET(values, 0, debug_text);
1589
142
    asdl_seq_SET(values, 1, interpolation);
1590
142
    return _PyAST_JoinedStr(values, lineno, col_offset, debug_end_line, debug_end_offset, p->arena);
1591
142
}
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
23.1k
                                 int end_lineno, int end_col_offset, PyArena *arena) {
1596
23.1k
    int conversion_val = _get_interpolation_conversion(p, debug, conversion, format);
1597
1598
23.1k
    expr_ty formatted_value = _PyAST_FormattedValue(
1599
23.1k
        expression, conversion_val, format ? (expr_ty) format->result : NULL,
1600
23.1k
        lineno, col_offset, end_lineno,
1601
23.1k
        end_col_offset, arena
1602
23.1k
    );
1603
1604
23.1k
    if (!debug) {
1605
16.9k
        return formatted_value;
1606
16.9k
    }
1607
1608
    /* Find the non whitespace token after the "=" */
1609
6.16k
    int debug_end_line, debug_end_offset;
1610
6.16k
    PyObject *debug_metadata;
1611
1612
6.16k
    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
5.96k
    else if (format) {
1618
223
        debug_end_line = ((expr_ty) format->result)->lineno;
1619
223
        debug_end_offset = ((expr_ty) format->result)->col_offset + 1;
1620
223
        debug_metadata = format->metadata;
1621
223
    }
1622
5.74k
    else {
1623
5.74k
        debug_end_line = end_lineno;
1624
5.74k
        debug_end_offset = end_col_offset;
1625
5.74k
        debug_metadata = closing_brace->metadata;
1626
5.74k
    }
1627
6.16k
    expr_ty debug_text = _PyAST_Constant(debug_metadata, NULL, lineno, col_offset + 1, debug_end_line,
1628
6.16k
                                            debug_end_offset - 1, p->arena);
1629
6.16k
    if (!debug_text) {
1630
2
        return NULL;
1631
2
    }
1632
1633
6.15k
    asdl_expr_seq *values = _Py_asdl_expr_seq_new(2, arena);
1634
6.15k
    asdl_seq_SET(values, 0, debug_text);
1635
6.15k
    asdl_seq_SET(values, 1, formatted_value);
1636
6.15k
    return _PyAST_JoinedStr(values, lineno, col_offset, debug_end_line, debug_end_offset, p->arena);
1637
6.16k
}
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
635
{
1644
635
    Py_ssize_t len = asdl_seq_LEN(strings);
1645
635
    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
635
    PyObject *kind = asdl_seq_GET(strings, 0)->v.Constant.kind;
1652
1653
635
    Py_ssize_t total = 0;
1654
2.98k
    for (Py_ssize_t i = 0; i < len; i++) {
1655
2.35k
        expr_ty elem = asdl_seq_GET(strings, i);
1656
2.35k
        PyObject *bytes = elem->v.Constant.value;
1657
2.35k
        Py_ssize_t part = PyBytes_GET_SIZE(bytes);
1658
2.35k
        if (part > PY_SSIZE_T_MAX - total) {
1659
0
            PyErr_NoMemory();
1660
0
            return NULL;
1661
0
        }
1662
2.35k
        total += part;
1663
2.35k
    }
1664
1665
635
    PyBytesWriter *writer = PyBytesWriter_Create(total);
1666
635
    if (writer == NULL) {
1667
0
        return NULL;
1668
0
    }
1669
635
    char *out = PyBytesWriter_GetData(writer);
1670
1671
2.98k
    for (Py_ssize_t i = 0; i < len; i++) {
1672
2.35k
        expr_ty elem = asdl_seq_GET(strings, i);
1673
2.35k
        PyObject *bytes = elem->v.Constant.value;
1674
2.35k
        Py_ssize_t part = PyBytes_GET_SIZE(bytes);
1675
2.35k
        if (part > 0) {
1676
869
            memcpy(out, PyBytes_AS_STRING(bytes), part);
1677
869
            out += part;
1678
869
        }
1679
2.35k
    }
1680
1681
635
    PyObject *res = PyBytesWriter_Finish(writer);
1682
635
    if (res == NULL) {
1683
0
        return NULL;
1684
0
    }
1685
635
    if (_PyArena_AddPyObject(arena, res) < 0) {
1686
0
        Py_DECREF(res);
1687
0
        return NULL;
1688
0
    }
1689
635
    return _PyAST_Constant(res, kind, lineno, col_offset, end_lineno, end_col_offset, p->arena);
1690
635
}
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.10k
{
1697
1.10k
    Py_ssize_t len = asdl_seq_LEN(strings);
1698
1.10k
    assert(len > 1);
1699
1700
1.10k
    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.10k
    PyObject *kind = first->v.Constant.kind;
1709
1710
1.10k
    PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
1711
1.10k
    if (writer == NULL) {
1712
0
        return NULL;
1713
0
    }
1714
1715
22.4k
    for (Py_ssize_t i = 0; i < len; i++) {
1716
21.3k
        expr_ty current_elem = asdl_seq_GET(strings, i);
1717
21.3k
        assert(current_elem->kind == Constant_kind);
1718
1719
21.3k
        if (PyUnicodeWriter_WriteStr(writer,
1720
21.3k
                                     current_elem->v.Constant.value)) {
1721
0
            PyUnicodeWriter_Discard(writer);
1722
0
            return NULL;
1723
0
        }
1724
21.3k
    }
1725
1726
1.10k
    PyObject *final = PyUnicodeWriter_Finish(writer);
1727
1.10k
    if (final == NULL) {
1728
0
        return NULL;
1729
0
    }
1730
1.10k
    if (_PyArena_AddPyObject(p->arena, final) < 0) {
1731
0
        Py_DECREF(final);
1732
0
        return NULL;
1733
0
    }
1734
1.10k
    return _PyAST_Constant(final, kind, lineno, col_offset,
1735
1.10k
                           end_lineno, end_col_offset, arena);
1736
1.10k
}
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.0k
{
1743
13.0k
    Py_ssize_t len = asdl_seq_LEN(strings);
1744
13.0k
    assert(len > 0);
1745
1746
13.0k
    Py_ssize_t n_flattened_elements = 0;
1747
40.9k
    for (Py_ssize_t i = 0; i < len; i++) {
1748
27.9k
        expr_ty elem = asdl_seq_GET(strings, i);
1749
27.9k
        switch(elem->kind) {
1750
12.5k
            case JoinedStr_kind:
1751
12.5k
                n_flattened_elements += asdl_seq_LEN(elem->v.JoinedStr.values);
1752
12.5k
                break;
1753
4.40k
            case TemplateStr_kind:
1754
4.40k
                n_flattened_elements += asdl_seq_LEN(elem->v.TemplateStr.values);
1755
4.40k
                break;
1756
11.0k
            default:
1757
11.0k
                n_flattened_elements++;
1758
11.0k
                break;
1759
27.9k
        }
1760
27.9k
    }
1761
1762
1763
13.0k
    asdl_expr_seq* flattened = _Py_asdl_expr_seq_new(n_flattened_elements, p->arena);
1764
13.0k
    if (flattened == NULL) {
1765
0
        return NULL;
1766
0
    }
1767
1768
    /* build flattened list */
1769
13.0k
    Py_ssize_t current_pos = 0;
1770
40.9k
    for (Py_ssize_t i = 0; i < len; i++) {
1771
27.9k
        expr_ty elem = asdl_seq_GET(strings, i);
1772
27.9k
        switch(elem->kind) {
1773
12.5k
            case JoinedStr_kind:
1774
56.1k
                for (Py_ssize_t j = 0; j < asdl_seq_LEN(elem->v.JoinedStr.values); j++) {
1775
43.6k
                    expr_ty subvalue = asdl_seq_GET(elem->v.JoinedStr.values, j);
1776
43.6k
                    if (subvalue == NULL) {
1777
0
                        return NULL;
1778
0
                    }
1779
43.6k
                    asdl_seq_SET(flattened, current_pos++, subvalue);
1780
43.6k
                }
1781
12.5k
                break;
1782
12.5k
            case TemplateStr_kind:
1783
11.8k
                for (Py_ssize_t j = 0; j < asdl_seq_LEN(elem->v.TemplateStr.values); j++) {
1784
7.45k
                    expr_ty subvalue = asdl_seq_GET(elem->v.TemplateStr.values, j);
1785
7.45k
                    if (subvalue == NULL) {
1786
0
                        return NULL;
1787
0
                    }
1788
7.45k
                    asdl_seq_SET(flattened, current_pos++, subvalue);
1789
7.45k
                }
1790
4.40k
                break;
1791
11.0k
            default:
1792
11.0k
                asdl_seq_SET(flattened, current_pos++, elem);
1793
11.0k
                break;
1794
27.9k
        }
1795
27.9k
    }
1796
1797
    /* calculate folded element count */
1798
13.0k
    Py_ssize_t n_elements = 0;
1799
13.0k
    int prev_is_constant = 0;
1800
75.1k
    for (Py_ssize_t i = 0; i < n_flattened_elements; i++) {
1801
62.1k
        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.1k
        if (elem->kind == Constant_kind &&
1807
62.1k
            PyUnicode_CheckExact(elem->v.Constant.value) &&
1808
37.1k
            PyUnicode_GET_LENGTH(elem->v.Constant.value) == 0)
1809
968
            continue;
1810
1811
61.1k
        if (!prev_is_constant || elem->kind != Constant_kind) {
1812
50.2k
            n_elements++;
1813
50.2k
        }
1814
61.1k
        prev_is_constant = elem->kind == Constant_kind;
1815
61.1k
    }
1816
1817
13.0k
    asdl_expr_seq* values = _Py_asdl_expr_seq_new(n_elements, p->arena);
1818
13.0k
    if (values == NULL) {
1819
0
        return NULL;
1820
0
    }
1821
1822
    /* build folded list */
1823
13.0k
    current_pos = 0;
1824
63.5k
    for (Py_ssize_t i = 0; i < n_flattened_elements; i++) {
1825
50.5k
        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.5k
        if (elem->kind == Constant_kind) {
1830
25.5k
            if (i + 1 < n_flattened_elements &&
1831
20.7k
                asdl_seq_GET(flattened, i + 1)->kind == Constant_kind) {
1832
3.31k
                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.31k
                PyObject *kind = elem->v.Constant.kind;
1841
1842
3.31k
                PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
1843
3.31k
                if (writer == NULL) {
1844
0
                    return NULL;
1845
0
                }
1846
3.31k
                expr_ty last_elem = elem;
1847
3.31k
                Py_ssize_t j;
1848
18.1k
                for (j = i; j < n_flattened_elements; j++) {
1849
17.0k
                    expr_ty current_elem = asdl_seq_GET(flattened, j);
1850
17.0k
                    if (current_elem->kind == Constant_kind) {
1851
14.8k
                        if (PyUnicodeWriter_WriteStr(writer,
1852
14.8k
                                                     current_elem->v.Constant.value)) {
1853
0
                            PyUnicodeWriter_Discard(writer);
1854
0
                            return NULL;
1855
0
                        }
1856
14.8k
                        last_elem = current_elem;
1857
14.8k
                    } else {
1858
2.20k
                        break;
1859
2.20k
                    }
1860
17.0k
                }
1861
3.31k
                i = j - 1;
1862
1863
3.31k
                PyObject *concat_str = PyUnicodeWriter_Finish(writer);
1864
3.31k
                if (concat_str == NULL) {
1865
0
                    return NULL;
1866
0
                }
1867
3.31k
                if (_PyArena_AddPyObject(p->arena, concat_str) < 0) {
1868
0
                    Py_DECREF(concat_str);
1869
0
                    return NULL;
1870
0
                }
1871
3.31k
                elem = _PyAST_Constant(concat_str, kind, first_elem->lineno,
1872
3.31k
                                       first_elem->col_offset,
1873
3.31k
                                       last_elem->end_lineno,
1874
3.31k
                                       last_elem->end_col_offset, p->arena);
1875
3.31k
                if (elem == NULL) {
1876
0
                    return NULL;
1877
0
                }
1878
3.31k
            }
1879
1880
            /* Drop all empty contanst strings */
1881
25.5k
            if (PyUnicode_CheckExact(elem->v.Constant.value) &&
1882
25.5k
                PyUnicode_GET_LENGTH(elem->v.Constant.value) == 0) {
1883
317
                continue;
1884
317
            }
1885
25.5k
        }
1886
1887
50.2k
        asdl_seq_SET(values, current_pos++, elem);
1888
50.2k
    }
1889
1890
13.0k
    assert(current_pos == n_elements);
1891
13.0k
    return values;
1892
13.0k
}
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
9.55k
{
1899
9.55k
    asdl_expr_seq *values = _build_concatenated_str(p, strings, lineno,
1900
9.55k
        col_offset, end_lineno, end_col_offset, arena);
1901
9.55k
    return _PyAST_JoinedStr(values, lineno, col_offset, end_lineno, end_col_offset, p->arena);
1902
9.55k
}
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.45k
{
1909
3.45k
    asdl_expr_seq *values = _build_concatenated_str(p, strings, lineno,
1910
3.45k
        col_offset, end_lineno, end_col_offset, arena);
1911
3.45k
    return _PyAST_TemplateStr(values, lineno, col_offset, end_lineno,
1912
3.45k
        end_col_offset, arena);
1913
3.45k
}
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
29.2k
{
1920
29.2k
    Py_ssize_t len = asdl_seq_LEN(strings);
1921
29.2k
    assert(len > 0);
1922
1923
29.2k
    int f_string_found = 0;
1924
29.2k
    int unicode_string_found = 0;
1925
29.2k
    int bytes_found = 0;
1926
1927
29.2k
    Py_ssize_t i = 0;
1928
94.5k
    for (i = 0; i < len; i++) {
1929
65.2k
        expr_ty elem = asdl_seq_GET(strings, i);
1930
65.2k
        switch(elem->kind) {
1931
49.4k
            case Constant_kind:
1932
49.4k
                if (PyBytes_CheckExact(elem->v.Constant.value)) {
1933
3.72k
                    bytes_found = 1;
1934
45.7k
                } else {
1935
45.7k
                    unicode_string_found = 1;
1936
45.7k
                }
1937
49.4k
                break;
1938
12.5k
            case JoinedStr_kind:
1939
12.5k
                f_string_found = 1;
1940
12.5k
                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.27k
            default:
1946
3.27k
                f_string_found = 1;
1947
3.27k
                break;
1948
65.2k
        }
1949
65.2k
    }
1950
1951
    // Cannot mix unicode and bytes
1952
29.2k
    if ((unicode_string_found || f_string_found) && bytes_found) {
1953
9
        RAISE_SYNTAX_ERROR("cannot mix bytes and nonbytes literals");
1954
9
        return NULL;
1955
9
    }
1956
1957
    // If it's only bytes or only unicode string, do a simple concat
1958
29.2k
    if (!f_string_found) {
1959
19.7k
        if (len == 1) {
1960
17.9k
            return asdl_seq_GET(strings, 0);
1961
17.9k
        }
1962
1.74k
        else if (bytes_found) {
1963
635
            return _build_concatenated_bytes(p, strings, lineno, col_offset,
1964
635
                end_lineno, end_col_offset, arena);
1965
635
        }
1966
1.10k
        else {
1967
1.10k
            return _build_concatenated_unicode(p, strings, lineno, col_offset,
1968
1.10k
                end_lineno, end_col_offset, arena);
1969
1.10k
        }
1970
19.7k
    }
1971
1972
9.55k
    return _build_concatenated_joined_str(p, strings, lineno,
1973
9.55k
        col_offset, end_lineno, end_col_offset, arena);
1974
29.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.90k
                               PyArena *arena) {
1981
1.90k
    if (level == 0 && PyUnicode_CompareWithASCIIString(module, "__future__") == 0) {
1982
748
        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.58k
        for (Py_ssize_t i = 0; i < asdl_seq_LEN(names); i++) {
1988
834
            alias_ty alias = asdl_seq_GET(names, i);
1989
834
            if (PyUnicode_CompareWithASCIIString(alias->name, "barry_as_FLUFL") == 0) {
1990
374
                p->flags |= PyPARSE_BARRY_AS_BDFL;
1991
374
            }
1992
834
        }
1993
747
    }
1994
1.90k
    return _PyAST_ImportFrom(module, names, level, lazy_token ? 1 : 0, lineno,
1995
1.90k
                             col_offset, end_lineno, end_col_offset, arena);
1996
1.90k
}
1997
1998
asdl_stmt_seq*
1999
24.8k
_PyPegen_register_stmts(Parser *p, asdl_stmt_seq* stmts) {
2000
24.8k
    if (!p->call_invalid_rules) {
2001
19.0k
        return stmts;
2002
19.0k
    }
2003
5.82k
    Py_ssize_t len = asdl_seq_LEN(stmts);
2004
5.82k
    if (len == 0) {
2005
0
        return stmts;
2006
0
    }
2007
5.82k
    stmt_ty last_stmt = asdl_seq_GET(stmts, len - 1);
2008
5.82k
    if (p->last_stmt_location.lineno > last_stmt->lineno) {
2009
122
        return stmts;
2010
122
    }
2011
5.70k
    p->last_stmt_location.lineno = last_stmt->lineno;
2012
5.70k
    p->last_stmt_location.col_offset = last_stmt->col_offset;
2013
5.70k
    p->last_stmt_location.end_lineno = last_stmt->end_lineno;
2014
5.70k
    p->last_stmt_location.end_col_offset = last_stmt->end_col_offset;
2015
5.70k
    return stmts;
2016
5.82k
}