LCOV - code coverage report
Current view: top level - Parser - action_helpers.c (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 583 675 86.4 %
Date: 2022-07-07 18:19:46 Functions: 62 63 98.4 %

          Line data    Source code
       1             : #include <Python.h>
       2             : 
       3             : #include "pegen.h"
       4             : #include "string_parser.h"
       5             : 
       6             : static PyObject *
       7        2787 : _create_dummy_identifier(Parser *p)
       8             : {
       9        2787 :     return _PyPegen_new_identifier(p, "");
      10             : }
      11             : 
      12             : void *
      13     1389090 : _PyPegen_dummy_name(Parser *p, ...)
      14             : {
      15             :     static void *cache = NULL;
      16             : 
      17     1389090 :     if (cache != NULL) {
      18     1386300 :         return cache;
      19             :     }
      20             : 
      21        2787 :     PyObject *id = _create_dummy_identifier(p);
      22        2787 :     if (!id) {
      23           0 :         return NULL;
      24             :     }
      25        2787 :     cache = _PyAST_Name(id, Load, 1, 0, 1, 0, p->arena);
      26        2787 :     return cache;
      27             : }
      28             : 
      29             : /* Creates a single-element asdl_seq* that contains a */
      30             : asdl_seq *
      31     3368550 : _PyPegen_singleton_seq(Parser *p, void *a)
      32             : {
      33     3368550 :     assert(a != NULL);
      34     3368550 :     asdl_seq *seq = (asdl_seq*)_Py_asdl_generic_seq_new(1, p->arena);
      35     3368550 :     if (!seq) {
      36           0 :         return NULL;
      37             :     }
      38     3368550 :     asdl_seq_SET_UNTYPED(seq, 0, a);
      39     3368550 :     return seq;
      40             : }
      41             : 
      42             : /* Creates a copy of seq and prepends a to it */
      43             : asdl_seq *
      44     3166870 : _PyPegen_seq_insert_in_front(Parser *p, void *a, asdl_seq *seq)
      45             : {
      46     3166870 :     assert(a != NULL);
      47     3166870 :     if (!seq) {
      48       98676 :         return _PyPegen_singleton_seq(p, a);
      49             :     }
      50             : 
      51     3068200 :     asdl_seq *new_seq = (asdl_seq*)_Py_asdl_generic_seq_new(asdl_seq_LEN(seq) + 1, p->arena);
      52     3068200 :     if (!new_seq) {
      53           0 :         return NULL;
      54             :     }
      55             : 
      56     3068200 :     asdl_seq_SET_UNTYPED(new_seq, 0, a);
      57    11205000 :     for (Py_ssize_t i = 1, l = asdl_seq_LEN(new_seq); i < l; i++) {
      58     8136840 :         asdl_seq_SET_UNTYPED(new_seq, i, asdl_seq_GET_UNTYPED(seq, i - 1));
      59             :     }
      60     3068200 :     return new_seq;
      61             : }
      62             : 
      63             : /* Creates a copy of seq and appends a to it */
      64             : asdl_seq *
      65           7 : _PyPegen_seq_append_to_end(Parser *p, asdl_seq *seq, void *a)
      66             : {
      67           7 :     assert(a != NULL);
      68           7 :     if (!seq) {
      69           0 :         return _PyPegen_singleton_seq(p, a);
      70             :     }
      71             : 
      72           7 :     asdl_seq *new_seq = (asdl_seq*)_Py_asdl_generic_seq_new(asdl_seq_LEN(seq) + 1, p->arena);
      73           7 :     if (!new_seq) {
      74           0 :         return NULL;
      75             :     }
      76             : 
      77          15 :     for (Py_ssize_t i = 0, l = asdl_seq_LEN(new_seq); i + 1 < l; i++) {
      78           8 :         asdl_seq_SET_UNTYPED(new_seq, i, asdl_seq_GET_UNTYPED(seq, i));
      79             :     }
      80           7 :     asdl_seq_SET_UNTYPED(new_seq, asdl_seq_LEN(new_seq) - 1, a);
      81           7 :     return new_seq;
      82             : }
      83             : 
      84             : static Py_ssize_t
      85     1043400 : _get_flattened_seq_size(asdl_seq *seqs)
      86             : {
      87     1043400 :     Py_ssize_t size = 0;
      88     4055360 :     for (Py_ssize_t i = 0, l = asdl_seq_LEN(seqs); i < l; i++) {
      89     3011960 :         asdl_seq *inner_seq = asdl_seq_GET_UNTYPED(seqs, i);
      90     3011960 :         size += asdl_seq_LEN(inner_seq);
      91             :     }
      92     1043400 :     return size;
      93             : }
      94             : 
      95             : /* Flattens an asdl_seq* of asdl_seq*s */
      96             : asdl_seq *
      97     1043400 : _PyPegen_seq_flatten(Parser *p, asdl_seq *seqs)
      98             : {
      99     1043400 :     Py_ssize_t flattened_seq_size = _get_flattened_seq_size(seqs);
     100     1043400 :     assert(flattened_seq_size > 0);
     101             : 
     102     1043400 :     asdl_seq *flattened_seq = (asdl_seq*)_Py_asdl_generic_seq_new(flattened_seq_size, p->arena);
     103     1043400 :     if (!flattened_seq) {
     104           0 :         return NULL;
     105             :     }
     106             : 
     107     1043400 :     int flattened_seq_idx = 0;
     108     4055360 :     for (Py_ssize_t i = 0, l = asdl_seq_LEN(seqs); i < l; i++) {
     109     3011960 :         asdl_seq *inner_seq = asdl_seq_GET_UNTYPED(seqs, i);
     110     6031390 :         for (Py_ssize_t j = 0, li = asdl_seq_LEN(inner_seq); j < li; j++) {
     111     3019430 :             asdl_seq_SET_UNTYPED(flattened_seq, flattened_seq_idx++, asdl_seq_GET_UNTYPED(inner_seq, j));
     112             :         }
     113             :     }
     114     1043400 :     assert(flattened_seq_idx == flattened_seq_size);
     115             : 
     116     1043400 :     return flattened_seq;
     117             : }
     118             : 
     119             : void *
     120          49 : _PyPegen_seq_last_item(asdl_seq *seq)
     121             : {
     122          49 :     Py_ssize_t len = asdl_seq_LEN(seq);
     123          49 :     return asdl_seq_GET_UNTYPED(seq, len - 1);
     124             : }
     125             : 
     126             : void *
     127           8 : _PyPegen_seq_first_item(asdl_seq *seq)
     128             : {
     129           8 :     return asdl_seq_GET_UNTYPED(seq, 0);
     130             : }
     131             : 
     132             : /* Creates a new name of the form <first_name>.<second_name> */
     133             : expr_ty
     134       40058 : _PyPegen_join_names_with_dot(Parser *p, expr_ty first_name, expr_ty second_name)
     135             : {
     136       40058 :     assert(first_name != NULL && second_name != NULL);
     137       40058 :     PyObject *first_identifier = first_name->v.Name.id;
     138       40058 :     PyObject *second_identifier = second_name->v.Name.id;
     139             : 
     140       40058 :     if (PyUnicode_READY(first_identifier) == -1) {
     141           0 :         return NULL;
     142             :     }
     143       40058 :     if (PyUnicode_READY(second_identifier) == -1) {
     144           0 :         return NULL;
     145             :     }
     146       40058 :     const char *first_str = PyUnicode_AsUTF8(first_identifier);
     147       40058 :     if (!first_str) {
     148           0 :         return NULL;
     149             :     }
     150       40058 :     const char *second_str = PyUnicode_AsUTF8(second_identifier);
     151       40058 :     if (!second_str) {
     152           0 :         return NULL;
     153             :     }
     154       40058 :     Py_ssize_t len = strlen(first_str) + strlen(second_str) + 1;  // +1 for the dot
     155             : 
     156       40058 :     PyObject *str = PyBytes_FromStringAndSize(NULL, len);
     157       40058 :     if (!str) {
     158           0 :         return NULL;
     159             :     }
     160             : 
     161       40058 :     char *s = PyBytes_AS_STRING(str);
     162       40058 :     if (!s) {
     163           0 :         return NULL;
     164             :     }
     165             : 
     166       40058 :     strcpy(s, first_str);
     167       40058 :     s += strlen(first_str);
     168       40058 :     *s++ = '.';
     169       40058 :     strcpy(s, second_str);
     170       40058 :     s += strlen(second_str);
     171       40058 :     *s = '\0';
     172             : 
     173       40058 :     PyObject *uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), PyBytes_GET_SIZE(str), NULL);
     174       40058 :     Py_DECREF(str);
     175       40058 :     if (!uni) {
     176           0 :         return NULL;
     177             :     }
     178       40058 :     PyUnicode_InternInPlace(&uni);
     179       40058 :     if (_PyArena_AddPyObject(p->arena, uni) < 0) {
     180           0 :         Py_DECREF(uni);
     181           0 :         return NULL;
     182             :     }
     183             : 
     184       40058 :     return _PyAST_Name(uni, Load, EXTRA_EXPR(first_name, second_name));
     185             : }
     186             : 
     187             : /* Counts the total number of dots in seq's tokens */
     188             : int
     189       46133 : _PyPegen_seq_count_dots(asdl_seq *seq)
     190             : {
     191       46133 :     int number_of_dots = 0;
     192       57935 :     for (Py_ssize_t i = 0, l = asdl_seq_LEN(seq); i < l; i++) {
     193       11802 :         Token *current_expr = asdl_seq_GET_UNTYPED(seq, i);
     194       11802 :         switch (current_expr->type) {
     195           6 :             case ELLIPSIS:
     196           6 :                 number_of_dots += 3;
     197           6 :                 break;
     198       11796 :             case DOT:
     199       11796 :                 number_of_dots += 1;
     200       11796 :                 break;
     201           0 :             default:
     202           0 :                 Py_UNREACHABLE();
     203             :         }
     204             :     }
     205             : 
     206       46133 :     return number_of_dots;
     207             : }
     208             : 
     209             : /* Creates an alias with '*' as the identifier name */
     210             : alias_ty
     211        1130 : _PyPegen_alias_for_star(Parser *p, int lineno, int col_offset, int end_lineno,
     212             :                         int end_col_offset, PyArena *arena) {
     213        1130 :     PyObject *str = PyUnicode_InternFromString("*");
     214        1130 :     if (!str) {
     215           0 :         return NULL;
     216             :     }
     217        1130 :     if (_PyArena_AddPyObject(p->arena, str) < 0) {
     218           0 :         Py_DECREF(str);
     219           0 :         return NULL;
     220             :     }
     221        1130 :     return _PyAST_alias(str, NULL, lineno, col_offset, end_lineno, end_col_offset, arena);
     222             : }
     223             : 
     224             : /* Creates a new asdl_seq* with the identifiers of all the names in seq */
     225             : asdl_identifier_seq *
     226        2829 : _PyPegen_map_names_to_ids(Parser *p, asdl_expr_seq *seq)
     227             : {
     228        2829 :     Py_ssize_t len = asdl_seq_LEN(seq);
     229        2829 :     assert(len > 0);
     230             : 
     231        2829 :     asdl_identifier_seq *new_seq = _Py_asdl_identifier_seq_new(len, p->arena);
     232        2829 :     if (!new_seq) {
     233           0 :         return NULL;
     234             :     }
     235        6439 :     for (Py_ssize_t i = 0; i < len; i++) {
     236        3610 :         expr_ty e = asdl_seq_GET(seq, i);
     237        3610 :         asdl_seq_SET(new_seq, i, e->v.Name.id);
     238             :     }
     239        2829 :     return new_seq;
     240             : }
     241             : 
     242             : /* Constructs a CmpopExprPair */
     243             : CmpopExprPair *
     244      246101 : _PyPegen_cmpop_expr_pair(Parser *p, cmpop_ty cmpop, expr_ty expr)
     245             : {
     246      246101 :     assert(expr != NULL);
     247      246101 :     CmpopExprPair *a = _PyArena_Malloc(p->arena, sizeof(CmpopExprPair));
     248      246101 :     if (!a) {
     249           0 :         return NULL;
     250             :     }
     251      246101 :     a->cmpop = cmpop;
     252      246101 :     a->expr = expr;
     253      246101 :     return a;
     254             : }
     255             : 
     256             : asdl_int_seq *
     257      242788 : _PyPegen_get_cmpops(Parser *p, asdl_seq *seq)
     258             : {
     259      242788 :     Py_ssize_t len = asdl_seq_LEN(seq);
     260      242788 :     assert(len > 0);
     261             : 
     262      242788 :     asdl_int_seq *new_seq = _Py_asdl_int_seq_new(len, p->arena);
     263      242788 :     if (!new_seq) {
     264           0 :         return NULL;
     265             :     }
     266      488889 :     for (Py_ssize_t i = 0; i < len; i++) {
     267      246101 :         CmpopExprPair *pair = asdl_seq_GET_UNTYPED(seq, i);
     268      246101 :         asdl_seq_SET(new_seq, i, pair->cmpop);
     269             :     }
     270      242788 :     return new_seq;
     271             : }
     272             : 
     273             : asdl_expr_seq *
     274      242788 : _PyPegen_get_exprs(Parser *p, asdl_seq *seq)
     275             : {
     276      242788 :     Py_ssize_t len = asdl_seq_LEN(seq);
     277      242788 :     assert(len > 0);
     278             : 
     279      242788 :     asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
     280      242788 :     if (!new_seq) {
     281           0 :         return NULL;
     282             :     }
     283      488889 :     for (Py_ssize_t i = 0; i < len; i++) {
     284      246101 :         CmpopExprPair *pair = asdl_seq_GET_UNTYPED(seq, i);
     285      246101 :         asdl_seq_SET(new_seq, i, pair->expr);
     286             :     }
     287      242788 :     return new_seq;
     288             : }
     289             : 
     290             : /* Creates an asdl_seq* where all the elements have been changed to have ctx as context */
     291             : static asdl_expr_seq *
     292           7 : _set_seq_context(Parser *p, asdl_expr_seq *seq, expr_context_ty ctx)
     293             : {
     294           7 :     Py_ssize_t len = asdl_seq_LEN(seq);
     295           7 :     if (len == 0) {
     296           5 :         return NULL;
     297             :     }
     298             : 
     299           2 :     asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
     300           2 :     if (!new_seq) {
     301           0 :         return NULL;
     302             :     }
     303           6 :     for (Py_ssize_t i = 0; i < len; i++) {
     304           4 :         expr_ty e = asdl_seq_GET(seq, i);
     305           4 :         asdl_seq_SET(new_seq, i, _PyPegen_set_expr_context(p, e, ctx));
     306             :     }
     307           2 :     return new_seq;
     308             : }
     309             : 
     310             : static expr_ty
     311     2845640 : _set_name_context(Parser *p, expr_ty e, expr_context_ty ctx)
     312             : {
     313     2845640 :     return _PyAST_Name(e->v.Name.id, ctx, EXTRA_EXPR(e, e));
     314             : }
     315             : 
     316             : static expr_ty
     317           4 : _set_tuple_context(Parser *p, expr_ty e, expr_context_ty ctx)
     318             : {
     319           4 :     return _PyAST_Tuple(
     320             :             _set_seq_context(p, e->v.Tuple.elts, ctx),
     321             :             ctx,
     322             :             EXTRA_EXPR(e, e));
     323             : }
     324             : 
     325             : static expr_ty
     326           3 : _set_list_context(Parser *p, expr_ty e, expr_context_ty ctx)
     327             : {
     328           3 :     return _PyAST_List(
     329             :             _set_seq_context(p, e->v.List.elts, ctx),
     330             :             ctx,
     331             :             EXTRA_EXPR(e, e));
     332             : }
     333             : 
     334             : static expr_ty
     335          64 : _set_subscript_context(Parser *p, expr_ty e, expr_context_ty ctx)
     336             : {
     337          64 :     return _PyAST_Subscript(e->v.Subscript.value, e->v.Subscript.slice,
     338             :                             ctx, EXTRA_EXPR(e, e));
     339             : }
     340             : 
     341             : static expr_ty
     342         120 : _set_attribute_context(Parser *p, expr_ty e, expr_context_ty ctx)
     343             : {
     344         120 :     return _PyAST_Attribute(e->v.Attribute.value, e->v.Attribute.attr,
     345             :                             ctx, EXTRA_EXPR(e, e));
     346             : }
     347             : 
     348             : static expr_ty
     349           0 : _set_starred_context(Parser *p, expr_ty e, expr_context_ty ctx)
     350             : {
     351           0 :     return _PyAST_Starred(_PyPegen_set_expr_context(p, e->v.Starred.value, ctx),
     352             :                           ctx, EXTRA_EXPR(e, e));
     353             : }
     354             : 
     355             : /* Creates an `expr_ty` equivalent to `expr` but with `ctx` as context */
     356             : expr_ty
     357     2845830 : _PyPegen_set_expr_context(Parser *p, expr_ty expr, expr_context_ty ctx)
     358             : {
     359     2845830 :     assert(expr != NULL);
     360             : 
     361     2845830 :     expr_ty new = NULL;
     362     2845830 :     switch (expr->kind) {
     363     2845640 :         case Name_kind:
     364     2845640 :             new = _set_name_context(p, expr, ctx);
     365     2845640 :             break;
     366           4 :         case Tuple_kind:
     367           4 :             new = _set_tuple_context(p, expr, ctx);
     368           4 :             break;
     369           3 :         case List_kind:
     370           3 :             new = _set_list_context(p, expr, ctx);
     371           3 :             break;
     372          64 :         case Subscript_kind:
     373          64 :             new = _set_subscript_context(p, expr, ctx);
     374          64 :             break;
     375         120 :         case Attribute_kind:
     376         120 :             new = _set_attribute_context(p, expr, ctx);
     377         120 :             break;
     378           0 :         case Starred_kind:
     379           0 :             new = _set_starred_context(p, expr, ctx);
     380           0 :             break;
     381           0 :         default:
     382           0 :             new = expr;
     383             :     }
     384     2845830 :     return new;
     385             : }
     386             : 
     387             : /* Constructs a KeyValuePair that is used when parsing a dict's key value pairs */
     388             : KeyValuePair *
     389     1531900 : _PyPegen_key_value_pair(Parser *p, expr_ty key, expr_ty value)
     390             : {
     391     1531900 :     KeyValuePair *a = _PyArena_Malloc(p->arena, sizeof(KeyValuePair));
     392     1531900 :     if (!a) {
     393           0 :         return NULL;
     394             :     }
     395     1531900 :     a->key = key;
     396     1531900 :     a->value = value;
     397     1531900 :     return a;
     398             : }
     399             : 
     400             : /* Extracts all keys from an asdl_seq* of KeyValuePair*'s */
     401             : asdl_expr_seq *
     402       99192 : _PyPegen_get_keys(Parser *p, asdl_seq *seq)
     403             : {
     404       99192 :     Py_ssize_t len = asdl_seq_LEN(seq);
     405       99192 :     asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
     406       99192 :     if (!new_seq) {
     407           0 :         return NULL;
     408             :     }
     409     1619670 :     for (Py_ssize_t i = 0; i < len; i++) {
     410     1520480 :         KeyValuePair *pair = asdl_seq_GET_UNTYPED(seq, i);
     411     1520480 :         asdl_seq_SET(new_seq, i, pair->key);
     412             :     }
     413       99192 :     return new_seq;
     414             : }
     415             : 
     416             : /* Extracts all values from an asdl_seq* of KeyValuePair*'s */
     417             : asdl_expr_seq *
     418       99192 : _PyPegen_get_values(Parser *p, asdl_seq *seq)
     419             : {
     420       99192 :     Py_ssize_t len = asdl_seq_LEN(seq);
     421       99192 :     asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
     422       99192 :     if (!new_seq) {
     423           0 :         return NULL;
     424             :     }
     425     1619670 :     for (Py_ssize_t i = 0; i < len; i++) {
     426     1520480 :         KeyValuePair *pair = asdl_seq_GET_UNTYPED(seq, i);
     427     1520480 :         asdl_seq_SET(new_seq, i, pair->value);
     428             :     }
     429       99192 :     return new_seq;
     430             : }
     431             : 
     432             : /* Constructs a KeyPatternPair that is used when parsing mapping & class patterns */
     433             : KeyPatternPair *
     434         881 : _PyPegen_key_pattern_pair(Parser *p, expr_ty key, pattern_ty pattern)
     435             : {
     436         881 :     KeyPatternPair *a = _PyArena_Malloc(p->arena, sizeof(KeyPatternPair));
     437         881 :     if (!a) {
     438           0 :         return NULL;
     439             :     }
     440         881 :     a->key = key;
     441         881 :     a->pattern = pattern;
     442         881 :     return a;
     443             : }
     444             : 
     445             : /* Extracts all keys from an asdl_seq* of KeyPatternPair*'s */
     446             : asdl_expr_seq *
     447         382 : _PyPegen_get_pattern_keys(Parser *p, asdl_seq *seq)
     448             : {
     449         382 :     Py_ssize_t len = asdl_seq_LEN(seq);
     450         382 :     asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
     451         382 :     if (!new_seq) {
     452           0 :         return NULL;
     453             :     }
     454         876 :     for (Py_ssize_t i = 0; i < len; i++) {
     455         494 :         KeyPatternPair *pair = asdl_seq_GET_UNTYPED(seq, i);
     456         494 :         asdl_seq_SET(new_seq, i, pair->key);
     457             :     }
     458         382 :     return new_seq;
     459             : }
     460             : 
     461             : /* Extracts all patterns from an asdl_seq* of KeyPatternPair*'s */
     462             : asdl_pattern_seq *
     463         382 : _PyPegen_get_patterns(Parser *p, asdl_seq *seq)
     464             : {
     465         382 :     Py_ssize_t len = asdl_seq_LEN(seq);
     466         382 :     asdl_pattern_seq *new_seq = _Py_asdl_pattern_seq_new(len, p->arena);
     467         382 :     if (!new_seq) {
     468           0 :         return NULL;
     469             :     }
     470         876 :     for (Py_ssize_t i = 0; i < len; i++) {
     471         494 :         KeyPatternPair *pair = asdl_seq_GET_UNTYPED(seq, i);
     472         494 :         asdl_seq_SET(new_seq, i, pair->pattern);
     473             :     }
     474         382 :     return new_seq;
     475             : }
     476             : 
     477             : /* Constructs a NameDefaultPair */
     478             : NameDefaultPair *
     479      241525 : _PyPegen_name_default_pair(Parser *p, arg_ty arg, expr_ty value, Token *tc)
     480             : {
     481      241525 :     NameDefaultPair *a = _PyArena_Malloc(p->arena, sizeof(NameDefaultPair));
     482      241525 :     if (!a) {
     483           0 :         return NULL;
     484             :     }
     485      241525 :     a->arg = _PyPegen_add_type_comment_to_arg(p, arg, tc);
     486      241525 :     a->value = value;
     487      241525 :     return a;
     488             : }
     489             : 
     490             : /* Constructs a SlashWithDefault */
     491             : SlashWithDefault *
     492         448 : _PyPegen_slash_with_default(Parser *p, asdl_arg_seq *plain_names, asdl_seq *names_with_defaults)
     493             : {
     494         448 :     SlashWithDefault *a = _PyArena_Malloc(p->arena, sizeof(SlashWithDefault));
     495         448 :     if (!a) {
     496           0 :         return NULL;
     497             :     }
     498         448 :     a->plain_names = plain_names;
     499         448 :     a->names_with_defaults = names_with_defaults;
     500         448 :     return a;
     501             : }
     502             : 
     503             : /* Constructs a StarEtc */
     504             : StarEtc *
     505       37006 : _PyPegen_star_etc(Parser *p, arg_ty vararg, asdl_seq *kwonlyargs, arg_ty kwarg)
     506             : {
     507       37006 :     StarEtc *a = _PyArena_Malloc(p->arena, sizeof(StarEtc));
     508       37006 :     if (!a) {
     509           0 :         return NULL;
     510             :     }
     511       37006 :     a->vararg = vararg;
     512       37006 :     a->kwonlyargs = kwonlyargs;
     513       37006 :     a->kwarg = kwarg;
     514       37006 :     return a;
     515             : }
     516             : 
     517             : asdl_seq *
     518      353954 : _PyPegen_join_sequences(Parser *p, asdl_seq *a, asdl_seq *b)
     519             : {
     520      353954 :     Py_ssize_t first_len = asdl_seq_LEN(a);
     521      353954 :     Py_ssize_t second_len = asdl_seq_LEN(b);
     522      353954 :     asdl_seq *new_seq = (asdl_seq*)_Py_asdl_generic_seq_new(first_len + second_len, p->arena);
     523      353954 :     if (!new_seq) {
     524           0 :         return NULL;
     525             :     }
     526             : 
     527      353954 :     int k = 0;
     528      993600 :     for (Py_ssize_t i = 0; i < first_len; i++) {
     529      639646 :         asdl_seq_SET_UNTYPED(new_seq, k++, asdl_seq_GET_UNTYPED(a, i));
     530             :     }
     531      422916 :     for (Py_ssize_t i = 0; i < second_len; i++) {
     532       68962 :         asdl_seq_SET_UNTYPED(new_seq, k++, asdl_seq_GET_UNTYPED(b, i));
     533             :     }
     534             : 
     535      353954 :     return new_seq;
     536             : }
     537             : 
     538             : static asdl_arg_seq*
     539      389629 : _get_names(Parser *p, asdl_seq *names_with_defaults)
     540             : {
     541      389629 :     Py_ssize_t len = asdl_seq_LEN(names_with_defaults);
     542      389629 :     asdl_arg_seq *seq = _Py_asdl_arg_seq_new(len, p->arena);
     543      389629 :     if (!seq) {
     544           0 :         return NULL;
     545             :     }
     546      482982 :     for (Py_ssize_t i = 0; i < len; i++) {
     547       93353 :         NameDefaultPair *pair = asdl_seq_GET_UNTYPED(names_with_defaults, i);
     548       93353 :         asdl_seq_SET(seq, i, pair->arg);
     549             :     }
     550      389629 :     return seq;
     551             : }
     552             : 
     553             : static asdl_expr_seq *
     554      389629 : _get_defaults(Parser *p, asdl_seq *names_with_defaults)
     555             : {
     556      389629 :     Py_ssize_t len = asdl_seq_LEN(names_with_defaults);
     557      389629 :     asdl_expr_seq *seq = _Py_asdl_expr_seq_new(len, p->arena);
     558      389629 :     if (!seq) {
     559           0 :         return NULL;
     560             :     }
     561      482982 :     for (Py_ssize_t i = 0; i < len; i++) {
     562       93353 :         NameDefaultPair *pair = asdl_seq_GET_UNTYPED(names_with_defaults, i);
     563       93353 :         asdl_seq_SET(seq, i, pair->value);
     564             :     }
     565      389629 :     return seq;
     566             : }
     567             : 
     568             : static int
     569      359321 : _make_posonlyargs(Parser *p,
     570             :                   asdl_arg_seq *slash_without_default,
     571             :                   SlashWithDefault *slash_with_default,
     572             :                   asdl_arg_seq **posonlyargs) {
     573      359321 :     if (slash_without_default != NULL) {
     574       17490 :         *posonlyargs = slash_without_default;
     575             :     }
     576      341831 :     else if (slash_with_default != NULL) {
     577             :         asdl_arg_seq *slash_with_default_names =
     578         431 :                 _get_names(p, slash_with_default->names_with_defaults);
     579         431 :         if (!slash_with_default_names) {
     580           0 :             return -1;
     581             :         }
     582         431 :         *posonlyargs = (asdl_arg_seq*)_PyPegen_join_sequences(
     583             :                 p,
     584         431 :                 (asdl_seq*)slash_with_default->plain_names,
     585             :                 (asdl_seq*)slash_with_default_names);
     586             :     }
     587             :     else {
     588      341400 :         *posonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
     589             :     }
     590      359321 :     return *posonlyargs == NULL ? -1 : 0;
     591             : }
     592             : 
     593             : static int
     594      359321 : _make_posargs(Parser *p,
     595             :               asdl_arg_seq *plain_names,
     596             :               asdl_seq *names_with_default,
     597             :               asdl_arg_seq **posargs) {
     598      359321 :     if (plain_names != NULL && names_with_default != NULL) {
     599      352053 :         asdl_arg_seq *names_with_default_names = _get_names(p, names_with_default);
     600      352053 :         if (!names_with_default_names) {
     601           0 :             return -1;
     602             :         }
     603      352053 :         *posargs = (asdl_arg_seq*)_PyPegen_join_sequences(
     604             :                 p,(asdl_seq*)plain_names, (asdl_seq*)names_with_default_names);
     605             :     }
     606        7268 :     else if (plain_names == NULL && names_with_default != NULL) {
     607        3682 :         *posargs = _get_names(p, names_with_default);
     608             :     }
     609        3586 :     else if (plain_names != NULL && names_with_default == NULL) {
     610           0 :         *posargs = plain_names;
     611             :     }
     612             :     else {
     613        3586 :         *posargs = _Py_asdl_arg_seq_new(0, p->arena);
     614             :     }
     615      359321 :     return *posargs == NULL ? -1 : 0;
     616             : }
     617             : 
     618             : static int
     619      359321 : _make_posdefaults(Parser *p,
     620             :                   SlashWithDefault *slash_with_default,
     621             :                   asdl_seq *names_with_default,
     622             :                   asdl_expr_seq **posdefaults) {
     623      359321 :     if (slash_with_default != NULL && names_with_default != NULL) {
     624             :         asdl_expr_seq *slash_with_default_values =
     625         431 :                 _get_defaults(p, slash_with_default->names_with_defaults);
     626         431 :         if (!slash_with_default_values) {
     627           0 :             return -1;
     628             :         }
     629         431 :         asdl_expr_seq *names_with_default_values = _get_defaults(p, names_with_default);
     630         431 :         if (!names_with_default_values) {
     631           0 :             return -1;
     632             :         }
     633         431 :         *posdefaults = (asdl_expr_seq*)_PyPegen_join_sequences(
     634             :                 p,
     635             :                 (asdl_seq*)slash_with_default_values,
     636             :                 (asdl_seq*)names_with_default_values);
     637             :     }
     638      358890 :     else if (slash_with_default == NULL && names_with_default != NULL) {
     639      355304 :         *posdefaults = _get_defaults(p, names_with_default);
     640             :     }
     641        3586 :     else if (slash_with_default != NULL && names_with_default == NULL) {
     642           0 :         *posdefaults = _get_defaults(p, slash_with_default->names_with_defaults);
     643             :     }
     644             :     else {
     645        3586 :         *posdefaults = _Py_asdl_expr_seq_new(0, p->arena);
     646             :     }
     647      359321 :     return *posdefaults == NULL ? -1 : 0;
     648             : }
     649             : 
     650             : static int
     651      359321 : _make_kwargs(Parser *p, StarEtc *star_etc,
     652             :              asdl_arg_seq **kwonlyargs,
     653             :              asdl_expr_seq **kwdefaults) {
     654      359321 :     if (star_etc != NULL && star_etc->kwonlyargs != NULL) {
     655       33463 :         *kwonlyargs = _get_names(p, star_etc->kwonlyargs);
     656             :     }
     657             :     else {
     658      325858 :         *kwonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
     659             :     }
     660             : 
     661      359321 :     if (*kwonlyargs == NULL) {
     662           0 :         return -1;
     663             :     }
     664             : 
     665      359321 :     if (star_etc != NULL && star_etc->kwonlyargs != NULL) {
     666       33463 :         *kwdefaults = _get_defaults(p, star_etc->kwonlyargs);
     667             :     }
     668             :     else {
     669      325858 :         *kwdefaults = _Py_asdl_expr_seq_new(0, p->arena);
     670             :     }
     671             : 
     672      359321 :     if (*kwdefaults == NULL) {
     673           0 :         return -1;
     674             :     }
     675             : 
     676      359321 :     return 0;
     677             : }
     678             : 
     679             : /* Constructs an arguments_ty object out of all the parsed constructs in the parameters rule */
     680             : arguments_ty
     681      359321 : _PyPegen_make_arguments(Parser *p, asdl_arg_seq *slash_without_default,
     682             :                         SlashWithDefault *slash_with_default, asdl_arg_seq *plain_names,
     683             :                         asdl_seq *names_with_default, StarEtc *star_etc)
     684             : {
     685             :     asdl_arg_seq *posonlyargs;
     686      359321 :     if (_make_posonlyargs(p, slash_without_default, slash_with_default, &posonlyargs) == -1) {
     687           0 :         return NULL;
     688             :     }
     689             : 
     690             :     asdl_arg_seq *posargs;
     691      359321 :     if (_make_posargs(p, plain_names, names_with_default, &posargs) == -1) {
     692           0 :         return NULL;
     693             :     }
     694             : 
     695             :     asdl_expr_seq *posdefaults;
     696      359321 :     if (_make_posdefaults(p,slash_with_default, names_with_default, &posdefaults) == -1) {
     697           0 :         return NULL;
     698             :     }
     699             : 
     700      359321 :     arg_ty vararg = NULL;
     701      359321 :     if (star_etc != NULL && star_etc->vararg != NULL) {
     702       27452 :         vararg = star_etc->vararg;
     703             :     }
     704             : 
     705             :     asdl_arg_seq *kwonlyargs;
     706             :     asdl_expr_seq *kwdefaults;
     707      359321 :     if (_make_kwargs(p, star_etc, &kwonlyargs, &kwdefaults) == -1) {
     708           0 :         return NULL;
     709             :     }
     710             : 
     711      359321 :     arg_ty kwarg = NULL;
     712      359321 :     if (star_etc != NULL && star_etc->kwarg != NULL) {
     713       25559 :         kwarg = star_etc->kwarg;
     714             :     }
     715             : 
     716      359321 :     return _PyAST_arguments(posonlyargs, posargs, vararg, kwonlyargs,
     717             :                             kwdefaults, kwarg, posdefaults, p->arena);
     718             : }
     719             : 
     720             : 
     721             : /* Constructs an empty arguments_ty object, that gets used when a function accepts no
     722             :  * arguments. */
     723             : arguments_ty
     724       20695 : _PyPegen_empty_arguments(Parser *p)
     725             : {
     726       20695 :     asdl_arg_seq *posonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
     727       20695 :     if (!posonlyargs) {
     728           0 :         return NULL;
     729             :     }
     730       20695 :     asdl_arg_seq *posargs = _Py_asdl_arg_seq_new(0, p->arena);
     731       20695 :     if (!posargs) {
     732           0 :         return NULL;
     733             :     }
     734       20695 :     asdl_expr_seq *posdefaults = _Py_asdl_expr_seq_new(0, p->arena);
     735       20695 :     if (!posdefaults) {
     736           0 :         return NULL;
     737             :     }
     738       20695 :     asdl_arg_seq *kwonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
     739       20695 :     if (!kwonlyargs) {
     740           0 :         return NULL;
     741             :     }
     742       20695 :     asdl_expr_seq *kwdefaults = _Py_asdl_expr_seq_new(0, p->arena);
     743       20695 :     if (!kwdefaults) {
     744           0 :         return NULL;
     745             :     }
     746             : 
     747       20695 :     return _PyAST_arguments(posonlyargs, posargs, NULL, kwonlyargs,
     748             :                             kwdefaults, NULL, posdefaults, p->arena);
     749             : }
     750             : 
     751             : /* Encapsulates the value of an operator_ty into an AugOperator struct */
     752             : AugOperator *
     753       25709 : _PyPegen_augoperator(Parser *p, operator_ty kind)
     754             : {
     755       25709 :     AugOperator *a = _PyArena_Malloc(p->arena, sizeof(AugOperator));
     756       25709 :     if (!a) {
     757           0 :         return NULL;
     758             :     }
     759       25709 :     a->kind = kind;
     760       25709 :     return a;
     761             : }
     762             : 
     763             : /* Construct a FunctionDef equivalent to function_def, but with decorators */
     764             : stmt_ty
     765       31329 : _PyPegen_function_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty function_def)
     766             : {
     767       31329 :     assert(function_def != NULL);
     768       31329 :     if (function_def->kind == AsyncFunctionDef_kind) {
     769         390 :         return _PyAST_AsyncFunctionDef(
     770             :             function_def->v.FunctionDef.name, function_def->v.FunctionDef.args,
     771             :             function_def->v.FunctionDef.body, decorators, function_def->v.FunctionDef.returns,
     772             :             function_def->v.FunctionDef.type_comment, function_def->lineno,
     773             :             function_def->col_offset, function_def->end_lineno, function_def->end_col_offset,
     774             :             p->arena);
     775             :     }
     776             : 
     777       30939 :     return _PyAST_FunctionDef(
     778             :         function_def->v.FunctionDef.name, function_def->v.FunctionDef.args,
     779             :         function_def->v.FunctionDef.body, decorators,
     780             :         function_def->v.FunctionDef.returns,
     781             :         function_def->v.FunctionDef.type_comment, function_def->lineno,
     782             :         function_def->col_offset, function_def->end_lineno,
     783             :         function_def->end_col_offset, p->arena);
     784             : }
     785             : 
     786             : /* Construct a ClassDef equivalent to class_def, but with decorators */
     787             : stmt_ty
     788        3176 : _PyPegen_class_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty class_def)
     789             : {
     790        3176 :     assert(class_def != NULL);
     791        3176 :     return _PyAST_ClassDef(
     792             :         class_def->v.ClassDef.name, class_def->v.ClassDef.bases,
     793             :         class_def->v.ClassDef.keywords, class_def->v.ClassDef.body, decorators,
     794             :         class_def->lineno, class_def->col_offset, class_def->end_lineno,
     795             :         class_def->end_col_offset, p->arena);
     796             : }
     797             : 
     798             : /* Construct a KeywordOrStarred */
     799             : KeywordOrStarred *
     800      357292 : _PyPegen_keyword_or_starred(Parser *p, void *element, int is_keyword)
     801             : {
     802      357292 :     KeywordOrStarred *a = _PyArena_Malloc(p->arena, sizeof(KeywordOrStarred));
     803      357292 :     if (!a) {
     804           0 :         return NULL;
     805             :     }
     806      357292 :     a->element = element;
     807      357292 :     a->is_keyword = is_keyword;
     808      357292 :     return a;
     809             : }
     810             : 
     811             : /* Get the number of starred expressions in an asdl_seq* of KeywordOrStarred*s */
     812             : static int
     813      212912 : _seq_number_of_starred_exprs(asdl_seq *seq)
     814             : {
     815      212912 :     int n = 0;
     816      581292 :     for (Py_ssize_t i = 0, l = asdl_seq_LEN(seq); i < l; i++) {
     817      368380 :         KeywordOrStarred *k = asdl_seq_GET_UNTYPED(seq, i);
     818      368380 :         if (!k->is_keyword) {
     819         250 :             n++;
     820             :         }
     821             :     }
     822      212912 :     return n;
     823             : }
     824             : 
     825             : /* Extract the starred expressions of an asdl_seq* of KeywordOrStarred*s */
     826             : asdl_expr_seq *
     827      106456 : _PyPegen_seq_extract_starred_exprs(Parser *p, asdl_seq *kwargs)
     828             : {
     829      106456 :     int new_len = _seq_number_of_starred_exprs(kwargs);
     830      106456 :     if (new_len == 0) {
     831      106331 :         return NULL;
     832             :     }
     833         125 :     asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(new_len, p->arena);
     834         125 :     if (!new_seq) {
     835           0 :         return NULL;
     836             :     }
     837             : 
     838         125 :     int idx = 0;
     839         496 :     for (Py_ssize_t i = 0, len = asdl_seq_LEN(kwargs); i < len; i++) {
     840         371 :         KeywordOrStarred *k = asdl_seq_GET_UNTYPED(kwargs, i);
     841         371 :         if (!k->is_keyword) {
     842         125 :             asdl_seq_SET(new_seq, idx++, k->element);
     843             :         }
     844             :     }
     845         125 :     return new_seq;
     846             : }
     847             : 
     848             : /* Return a new asdl_seq* with only the keywords in kwargs */
     849             : asdl_keyword_seq*
     850      106456 : _PyPegen_seq_delete_starred_exprs(Parser *p, asdl_seq *kwargs)
     851             : {
     852      106456 :     Py_ssize_t len = asdl_seq_LEN(kwargs);
     853      106456 :     Py_ssize_t new_len = len - _seq_number_of_starred_exprs(kwargs);
     854      106456 :     if (new_len == 0) {
     855           0 :         return NULL;
     856             :     }
     857      106456 :     asdl_keyword_seq *new_seq = _Py_asdl_keyword_seq_new(new_len, p->arena);
     858      106456 :     if (!new_seq) {
     859           0 :         return NULL;
     860             :     }
     861             : 
     862      106456 :     int idx = 0;
     863      290646 :     for (Py_ssize_t i = 0; i < len; i++) {
     864      184190 :         KeywordOrStarred *k = asdl_seq_GET_UNTYPED(kwargs, i);
     865      184190 :         if (k->is_keyword) {
     866      184065 :             asdl_seq_SET(new_seq, idx++, k->element);
     867             :         }
     868             :     }
     869      106456 :     return new_seq;
     870             : }
     871             : 
     872             : expr_ty
     873     1607680 : _PyPegen_concatenate_strings(Parser *p, asdl_seq *strings)
     874             : {
     875     1607680 :     Py_ssize_t len = asdl_seq_LEN(strings);
     876     1607680 :     assert(len > 0);
     877             : 
     878     1607680 :     Token *first = asdl_seq_GET_UNTYPED(strings, 0);
     879     1607680 :     Token *last = asdl_seq_GET_UNTYPED(strings, len - 1);
     880             : 
     881     1607680 :     int bytesmode = 0;
     882     1607680 :     PyObject *bytes_str = NULL;
     883             : 
     884             :     FstringParser state;
     885     1607680 :     _PyPegen_FstringParser_Init(&state);
     886             : 
     887     3298340 :     for (Py_ssize_t i = 0; i < len; i++) {
     888     1691440 :         Token *t = asdl_seq_GET_UNTYPED(strings, i);
     889             : 
     890             :         int this_bytesmode;
     891             :         int this_rawmode;
     892             :         PyObject *s;
     893             :         const char *fstr;
     894     1691440 :         Py_ssize_t fstrlen = -1;
     895             : 
     896     1691440 :         if (_PyPegen_parsestr(p, &this_bytesmode, &this_rawmode, &s, &fstr, &fstrlen, t) != 0) {
     897         767 :             goto error;
     898             :         }
     899             : 
     900             :         /* Check that we are not mixing bytes with unicode. */
     901     1690800 :         if (i != 0 && bytesmode != this_bytesmode) {
     902           2 :             RAISE_SYNTAX_ERROR("cannot mix bytes and nonbytes literals");
     903           2 :             Py_XDECREF(s);
     904           2 :             goto error;
     905             :         }
     906     1690790 :         bytesmode = this_bytesmode;
     907             : 
     908     1690790 :         if (fstr != NULL) {
     909       22296 :             assert(s == NULL && !bytesmode);
     910             : 
     911       22296 :             int result = _PyPegen_FstringParser_ConcatFstring(p, &state, &fstr, fstr + fstrlen,
     912             :                                                      this_rawmode, 0, first, t, last);
     913       22296 :             if (result < 0) {
     914         124 :                 goto error;
     915             :             }
     916             :         }
     917             :         else {
     918             :             /* String or byte string. */
     919     1668500 :             assert(s != NULL && fstr == NULL);
     920     1668500 :             assert(bytesmode ? PyBytes_CheckExact(s) : PyUnicode_CheckExact(s));
     921             : 
     922     1668500 :             if (bytesmode) {
     923       52307 :                 if (i == 0) {
     924       48743 :                     bytes_str = s;
     925             :                 }
     926             :                 else {
     927        3564 :                     PyBytes_ConcatAndDel(&bytes_str, s);
     928        3564 :                     if (!bytes_str) {
     929           0 :                         goto error;
     930             :                     }
     931             :                 }
     932             :             }
     933             :             else {
     934             :                 /* This is a regular string. Concatenate it. */
     935     1616190 :                 if (_PyPegen_FstringParser_ConcatAndDel(&state, s) < 0) {
     936           0 :                     goto error;
     937             :                 }
     938             :             }
     939             :         }
     940             :     }
     941             : 
     942     1606910 :     if (bytesmode) {
     943       48742 :         if (_PyArena_AddPyObject(p->arena, bytes_str) < 0) {
     944           0 :             goto error;
     945             :         }
     946       48742 :         return _PyAST_Constant(bytes_str, NULL, first->lineno,
     947             :                                first->col_offset, last->end_lineno,
     948             :                                last->end_col_offset, p->arena);
     949             :     }
     950             : 
     951     1558170 :     return _PyPegen_FstringParser_Finish(p, &state, first, last);
     952             : 
     953         767 : error:
     954         767 :     Py_XDECREF(bytes_str);
     955         767 :     _PyPegen_FstringParser_Dealloc(&state);
     956         767 :     if (PyErr_Occurred()) {
     957         767 :         _Pypegen_raise_decode_error(p);
     958             :     }
     959         767 :     return NULL;
     960             : }
     961             : 
     962             : expr_ty
     963          50 : _PyPegen_ensure_imaginary(Parser *p, expr_ty exp)
     964             : {
     965          50 :     if (exp->kind != Constant_kind || !PyComplex_CheckExact(exp->v.Constant.value)) {
     966           2 :         RAISE_SYNTAX_ERROR_KNOWN_LOCATION(exp, "imaginary number required in complex literal");
     967           2 :         return NULL;
     968             :     }
     969          48 :     return exp;
     970             : }
     971             : 
     972             : expr_ty
     973          82 : _PyPegen_ensure_real(Parser *p, expr_ty exp)
     974             : {
     975          82 :     if (exp->kind != Constant_kind || PyComplex_CheckExact(exp->v.Constant.value)) {
     976           4 :         RAISE_SYNTAX_ERROR_KNOWN_LOCATION(exp, "real number required in complex literal");
     977           4 :         return NULL;
     978             :     }
     979          78 :     return exp;
     980             : }
     981             : 
     982             : mod_ty
     983       48204 : _PyPegen_make_module(Parser *p, asdl_stmt_seq *a) {
     984       48204 :     asdl_type_ignore_seq *type_ignores = NULL;
     985       48204 :     Py_ssize_t num = p->type_ignore_comments.num_items;
     986       48204 :     if (num > 0) {
     987             :         // Turn the raw (comment, lineno) pairs into TypeIgnore objects in the arena
     988          29 :         type_ignores = _Py_asdl_type_ignore_seq_new(num, p->arena);
     989          29 :         if (type_ignores == NULL) {
     990           0 :             return NULL;
     991             :         }
     992         103 :         for (int i = 0; i < num; i++) {
     993          74 :             PyObject *tag = _PyPegen_new_type_comment(p, p->type_ignore_comments.items[i].comment);
     994          74 :             if (tag == NULL) {
     995           0 :                 return NULL;
     996             :             }
     997          74 :             type_ignore_ty ti = _PyAST_TypeIgnore(p->type_ignore_comments.items[i].lineno,
     998             :                                                   tag, p->arena);
     999          74 :             if (ti == NULL) {
    1000           0 :                 return NULL;
    1001             :             }
    1002          74 :             asdl_seq_SET(type_ignores, i, ti);
    1003             :         }
    1004             :     }
    1005       48204 :     return _PyAST_Module(a, type_ignores, p->arena);
    1006             : }
    1007             : 
    1008             : PyObject *
    1009         913 : _PyPegen_new_type_comment(Parser *p, const char *s)
    1010             : {
    1011         913 :     PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
    1012         913 :     if (res == NULL) {
    1013           0 :         return NULL;
    1014             :     }
    1015         913 :     if (_PyArena_AddPyObject(p->arena, res) < 0) {
    1016           0 :         Py_DECREF(res);
    1017           0 :         return NULL;
    1018             :     }
    1019         913 :     return res;
    1020             : }
    1021             : 
    1022             : arg_ty
    1023     2650760 : _PyPegen_add_type_comment_to_arg(Parser *p, arg_ty a, Token *tc)
    1024             : {
    1025     2650760 :     if (tc == NULL) {
    1026     2650020 :         return a;
    1027             :     }
    1028         747 :     const char *bytes = PyBytes_AsString(tc->bytes);
    1029         747 :     if (bytes == NULL) {
    1030           0 :         return NULL;
    1031             :     }
    1032         747 :     PyObject *tco = _PyPegen_new_type_comment(p, bytes);
    1033         747 :     if (tco == NULL) {
    1034           0 :         return NULL;
    1035             :     }
    1036         747 :     return _PyAST_arg(a->arg, a->annotation, tco,
    1037             :                       a->lineno, a->col_offset, a->end_lineno, a->end_col_offset,
    1038             :                       p->arena);
    1039             : }
    1040             : 
    1041             : /* Checks if the NOTEQUAL token is valid given the current parser flags
    1042             : 0 indicates success and nonzero indicates failure (an exception may be set) */
    1043             : int
    1044       17661 : _PyPegen_check_barry_as_flufl(Parser *p, Token* t) {
    1045       17661 :     assert(t->bytes != NULL);
    1046       17661 :     assert(t->type == NOTEQUAL);
    1047             : 
    1048       17661 :     const char* tok_str = PyBytes_AS_STRING(t->bytes);
    1049       17661 :     if (p->flags & PyPARSE_BARRY_AS_BDFL && strcmp(tok_str, "<>") != 0) {
    1050           1 :         RAISE_SYNTAX_ERROR("with Barry as BDFL, use '<>' instead of '!='");
    1051           1 :         return -1;
    1052             :     }
    1053       17660 :     if (!(p->flags & PyPARSE_BARRY_AS_BDFL)) {
    1054       17659 :         return strcmp(tok_str, "!=");
    1055             :     }
    1056           1 :     return 0;
    1057             : }
    1058             : 
    1059             : int
    1060         179 : _PyPegen_check_legacy_stmt(Parser *p, expr_ty name) {
    1061         179 :     if (name->kind != Name_kind) {
    1062          17 :         return 0;
    1063             :     }
    1064         162 :     const char* candidates[2] = {"print", "exec"};
    1065         411 :     for (int i=0; i<2; i++) {
    1066         294 :         if (PyUnicode_CompareWithASCIIString(name->v.Name.id, candidates[i]) == 0) {
    1067          45 :             return 1;
    1068             :         }
    1069             :     }
    1070         117 :     return 0;
    1071             : }
    1072             : 
    1073             : const char *
    1074         130 : _PyPegen_get_expr_name(expr_ty e)
    1075             : {
    1076         130 :     assert(e != NULL);
    1077         130 :     switch (e->kind) {
    1078           2 :         case Attribute_kind:
    1079           2 :             return "attribute";
    1080           0 :         case Subscript_kind:
    1081           0 :             return "subscript";
    1082           3 :         case Starred_kind:
    1083           3 :             return "starred";
    1084           0 :         case Name_kind:
    1085           0 :             return "name";
    1086           4 :         case List_kind:
    1087           4 :             return "list";
    1088           8 :         case Tuple_kind:
    1089           8 :             return "tuple";
    1090           3 :         case Lambda_kind:
    1091           3 :             return "lambda";
    1092          29 :         case Call_kind:
    1093          29 :             return "function call";
    1094          21 :         case BoolOp_kind:
    1095             :         case BinOp_kind:
    1096             :         case UnaryOp_kind:
    1097          21 :             return "expression";
    1098           4 :         case GeneratorExp_kind:
    1099           4 :             return "generator expression";
    1100           3 :         case Yield_kind:
    1101             :         case YieldFrom_kind:
    1102           3 :             return "yield expression";
    1103           0 :         case Await_kind:
    1104           0 :             return "await expression";
    1105           4 :         case ListComp_kind:
    1106           4 :             return "list comprehension";
    1107           2 :         case SetComp_kind:
    1108           2 :             return "set comprehension";
    1109           2 :         case DictComp_kind:
    1110           2 :             return "dict comprehension";
    1111           1 :         case Dict_kind:
    1112           1 :             return "dict literal";
    1113           1 :         case Set_kind:
    1114           1 :             return "set display";
    1115           2 :         case JoinedStr_kind:
    1116             :         case FormattedValue_kind:
    1117           2 :             return "f-string expression";
    1118          38 :         case Constant_kind: {
    1119          38 :             PyObject *value = e->v.Constant.value;
    1120          38 :             if (value == Py_None) {
    1121          17 :                 return "None";
    1122             :             }
    1123          21 :             if (value == Py_False) {
    1124           1 :                 return "False";
    1125             :             }
    1126          20 :             if (value == Py_True) {
    1127           7 :                 return "True";
    1128             :             }
    1129          13 :             if (value == Py_Ellipsis) {
    1130           1 :                 return "ellipsis";
    1131             :             }
    1132          12 :             return "literal";
    1133             :         }
    1134           0 :         case Compare_kind:
    1135           0 :             return "comparison";
    1136           2 :         case IfExp_kind:
    1137           2 :             return "conditional expression";
    1138           1 :         case NamedExpr_kind:
    1139           1 :             return "named expression";
    1140           0 :         default:
    1141           0 :             PyErr_Format(PyExc_SystemError,
    1142             :                          "unexpected expression in assignment %d (line %d)",
    1143           0 :                          e->kind, e->lineno);
    1144           0 :             return NULL;
    1145             :     }
    1146             : }
    1147             : 
    1148             : expr_ty
    1149          38 : _PyPegen_get_last_comprehension_item(comprehension_ty comprehension) {
    1150          38 :     if (comprehension->ifs == NULL || asdl_seq_LEN(comprehension->ifs) == 0) {
    1151          34 :         return comprehension->iter;
    1152             :     }
    1153           4 :     return PyPegen_last_item(comprehension->ifs, expr_ty);
    1154             : }
    1155             : 
    1156     1349880 : expr_ty _PyPegen_collect_call_seqs(Parser *p, asdl_expr_seq *a, asdl_seq *b,
    1157             :                      int lineno, int col_offset, int end_lineno,
    1158             :                      int end_col_offset, PyArena *arena) {
    1159     1349880 :     Py_ssize_t args_len = asdl_seq_LEN(a);
    1160     1349880 :     Py_ssize_t total_len = args_len;
    1161             : 
    1162     1349880 :     if (b == NULL) {
    1163     1282250 :         return _PyAST_Call(_PyPegen_dummy_name(p), a, NULL, lineno, col_offset,
    1164             :                         end_lineno, end_col_offset, arena);
    1165             : 
    1166             :     }
    1167             : 
    1168       67638 :     asdl_expr_seq *starreds = _PyPegen_seq_extract_starred_exprs(p, b);
    1169       67638 :     asdl_keyword_seq *keywords = _PyPegen_seq_delete_starred_exprs(p, b);
    1170             : 
    1171       67638 :     if (starreds) {
    1172          68 :         total_len += asdl_seq_LEN(starreds);
    1173             :     }
    1174             : 
    1175       67638 :     asdl_expr_seq *args = _Py_asdl_expr_seq_new(total_len, arena);
    1176             : 
    1177       67638 :     Py_ssize_t i = 0;
    1178      171689 :     for (i = 0; i < args_len; i++) {
    1179      104051 :         asdl_seq_SET(args, i, asdl_seq_GET(a, i));
    1180             :     }
    1181       67706 :     for (; i < total_len; i++) {
    1182          68 :         asdl_seq_SET(args, i, asdl_seq_GET(starreds, i - args_len));
    1183             :     }
    1184             : 
    1185       67638 :     return _PyAST_Call(_PyPegen_dummy_name(p), args, keywords, lineno,
    1186             :                        col_offset, end_lineno, end_col_offset, arena);
    1187             : }
    1188             : 
    1189             : // AST Error reporting helpers
    1190             : 
    1191             : expr_ty
    1192         183 : _PyPegen_get_invalid_target(expr_ty e, TARGETS_TYPE targets_type)
    1193             : {
    1194         183 :     if (e == NULL) {
    1195           0 :         return NULL;
    1196             :     }
    1197             : 
    1198             : #define VISIT_CONTAINER(CONTAINER, TYPE) do { \
    1199             :         Py_ssize_t len = asdl_seq_LEN((CONTAINER)->v.TYPE.elts);\
    1200             :         for (Py_ssize_t i = 0; i < len; i++) {\
    1201             :             expr_ty other = asdl_seq_GET((CONTAINER)->v.TYPE.elts, i);\
    1202             :             expr_ty child = _PyPegen_get_invalid_target(other, targets_type);\
    1203             :             if (child != NULL) {\
    1204             :                 return child;\
    1205             :             }\
    1206             :         }\
    1207             :     } while (0)
    1208             : 
    1209             :     // We only need to visit List and Tuple nodes recursively as those
    1210             :     // are the only ones that can contain valid names in targets when
    1211             :     // they are parsed as expressions. Any other kind of expression
    1212             :     // that is a container (like Sets or Dicts) is directly invalid and
    1213             :     // we don't need to visit it recursively.
    1214             : 
    1215         183 :     switch (e->kind) {
    1216           9 :         case List_kind:
    1217          21 :             VISIT_CONTAINER(e, List);
    1218           0 :             return NULL;
    1219          32 :         case Tuple_kind:
    1220          63 :             VISIT_CONTAINER(e, Tuple);
    1221           2 :             return NULL;
    1222           8 :         case Starred_kind:
    1223           8 :             if (targets_type == DEL_TARGETS) {
    1224           3 :                 return e;
    1225             :             }
    1226           5 :             return _PyPegen_get_invalid_target(e->v.Starred.value, targets_type);
    1227          13 :         case Compare_kind:
    1228             :             // This is needed, because the `a in b` in `for a in b` gets parsed
    1229             :             // as a comparison, and so we need to search the left side of the comparison
    1230             :             // for invalid targets.
    1231          13 :             if (targets_type == FOR_TARGETS) {
    1232          13 :                 cmpop_ty cmpop = (cmpop_ty) asdl_seq_GET(e->v.Compare.ops, 0);
    1233          13 :                 if (cmpop == In) {
    1234          12 :                     return _PyPegen_get_invalid_target(e->v.Compare.left, targets_type);
    1235             :                 }
    1236           1 :                 return NULL;
    1237             :             }
    1238           0 :             return e;
    1239          49 :         case Name_kind:
    1240             :         case Subscript_kind:
    1241             :         case Attribute_kind:
    1242          49 :             return NULL;
    1243          72 :         default:
    1244          72 :             return e;
    1245             :     }
    1246             : }
    1247             : 
    1248          11 : void *_PyPegen_arguments_parsing_error(Parser *p, expr_ty e) {
    1249          11 :     int kwarg_unpacking = 0;
    1250          23 :     for (Py_ssize_t i = 0, l = asdl_seq_LEN(e->v.Call.keywords); i < l; i++) {
    1251          12 :         keyword_ty keyword = asdl_seq_GET(e->v.Call.keywords, i);
    1252          12 :         if (!keyword->arg) {
    1253           1 :             kwarg_unpacking = 1;
    1254             :         }
    1255             :     }
    1256             : 
    1257          11 :     const char *msg = NULL;
    1258          11 :     if (kwarg_unpacking) {
    1259           1 :         msg = "positional argument follows keyword argument unpacking";
    1260             :     } else {
    1261          10 :         msg = "positional argument follows keyword argument";
    1262             :     }
    1263             : 
    1264          11 :     return RAISE_SYNTAX_ERROR(msg);
    1265             : }
    1266             : 
    1267             : void *
    1268          11 : _PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args, asdl_comprehension_seq *comprehensions)
    1269             : {
    1270             :     /* The rule that calls this function is 'args for_if_clauses'.
    1271             :        For the input f(L, x for x in y), L and x are in args and
    1272             :        the for is parsed as a for_if_clause. We have to check if
    1273             :        len <= 1, so that input like dict((a, b) for a, b in x)
    1274             :        gets successfully parsed and then we pass the last
    1275             :        argument (x in the above example) as the location of the
    1276             :        error */
    1277          11 :     Py_ssize_t len = asdl_seq_LEN(args->v.Call.args);
    1278          11 :     if (len <= 1) {
    1279           2 :         return NULL;
    1280             :     }
    1281             : 
    1282           9 :     comprehension_ty last_comprehension = PyPegen_last_item(comprehensions, comprehension_ty);
    1283             : 
    1284           9 :     return RAISE_SYNTAX_ERROR_KNOWN_RANGE(
    1285             :         (expr_ty) asdl_seq_GET(args->v.Call.args, len - 1),
    1286             :         _PyPegen_get_last_comprehension_item(last_comprehension),
    1287             :         "Generator expression must be parenthesized"
    1288             :     );
    1289             : }

Generated by: LCOV version 1.14