Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1 | # Simplified grammar for Python |
| 2 | |
| 3 | @bytecode True |
| 4 | @trailer ''' |
| 5 | void * |
| 6 | _PyPegen_parse(Parser *p) |
| 7 | { |
| 8 | // Initialize keywords |
| 9 | p->keywords = reserved_keywords; |
| 10 | p->n_keyword_lists = n_keyword_lists; |
| 11 | |
| 12 | // Run parser |
| 13 | void *result = NULL; |
| 14 | if (p->start_rule == Py_file_input) { |
| 15 | result = file_rule(p); |
| 16 | } else if (p->start_rule == Py_single_input) { |
| 17 | result = interactive_rule(p); |
| 18 | } else if (p->start_rule == Py_eval_input) { |
| 19 | result = eval_rule(p); |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 20 | } else if (p->start_rule == Py_func_type_input) { |
| 21 | result = func_type_rule(p); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 22 | } else if (p->start_rule == Py_fstring_input) { |
| 23 | result = fstring_rule(p); |
| 24 | } |
| 25 | |
| 26 | return result; |
| 27 | } |
| 28 | |
| 29 | // The end |
| 30 | ''' |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 31 | file[mod_ty]: a=[statements] ENDMARKER { _PyPegen_make_module(p, a) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 32 | interactive[mod_ty]: a=statement_newline { Interactive(a, p->arena) } |
| 33 | eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { Expression(a, p->arena) } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 34 | func_type[mod_ty]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMARKER { FunctionType(a, b, p->arena) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 35 | fstring[expr_ty]: star_expressions |
| 36 | |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 37 | # type_expressions allow */** but ignore them |
| 38 | type_expressions[asdl_seq*]: |
| 39 | | a=','.expression+ ',' '*' b=expression ',' '**' c=expression { |
| 40 | _PyPegen_seq_append_to_end(p, CHECK(_PyPegen_seq_append_to_end(p, a, b)), c) } |
| 41 | | a=','.expression+ ',' '*' b=expression { _PyPegen_seq_append_to_end(p, a, b) } |
| 42 | | a=','.expression+ ',' '**' b=expression { _PyPegen_seq_append_to_end(p, a, b) } |
Shantanu | 603d354 | 2020-05-03 22:08:14 -0700 | [diff] [blame] | 43 | | '*' a=expression ',' '**' b=expression { |
| 44 | _PyPegen_seq_append_to_end(p, CHECK(_PyPegen_singleton_seq(p, a)), b) } |
| 45 | | '*' a=expression { _PyPegen_singleton_seq(p, a) } |
| 46 | | '**' a=expression { _PyPegen_singleton_seq(p, a) } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 47 | | ','.expression+ |
| 48 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 49 | statements[asdl_seq*]: a=statement+ { _PyPegen_seq_flatten(p, a) } |
| 50 | statement[asdl_seq*]: a=compound_stmt { _PyPegen_singleton_seq(p, a) } | simple_stmt |
| 51 | statement_newline[asdl_seq*]: |
| 52 | | a=compound_stmt NEWLINE { _PyPegen_singleton_seq(p, a) } |
| 53 | | simple_stmt |
| 54 | | NEWLINE { _PyPegen_singleton_seq(p, CHECK(_Py_Pass(EXTRA))) } |
| 55 | | ENDMARKER { _PyPegen_interactive_exit(p) } |
| 56 | simple_stmt[asdl_seq*]: |
| 57 | | a=small_stmt !';' NEWLINE { _PyPegen_singleton_seq(p, a) } # Not needed, there for speedup |
| 58 | | a=';'.small_stmt+ [';'] NEWLINE { a } |
| 59 | # NOTE: assignment MUST precede expression, else parsing a simple assignment |
| 60 | # will throw a SyntaxError. |
| 61 | small_stmt[stmt_ty] (memo): |
| 62 | | assignment |
| 63 | | e=star_expressions { _Py_Expr(e, EXTRA) } |
| 64 | | &'return' return_stmt |
| 65 | | &('import' | 'from') import_stmt |
| 66 | | &'raise' raise_stmt |
| 67 | | 'pass' { _Py_Pass(EXTRA) } |
| 68 | | &'del' del_stmt |
| 69 | | &'yield' yield_stmt |
| 70 | | &'assert' assert_stmt |
| 71 | | 'break' { _Py_Break(EXTRA) } |
| 72 | | 'continue' { _Py_Continue(EXTRA) } |
| 73 | | &'global' global_stmt |
| 74 | | &'nonlocal' nonlocal_stmt |
| 75 | compound_stmt[stmt_ty]: |
| 76 | | &('def' | '@' | ASYNC) function_def |
| 77 | | &'if' if_stmt |
| 78 | | &('class' | '@') class_def |
| 79 | | &('with' | ASYNC) with_stmt |
| 80 | | &('for' | ASYNC) for_stmt |
| 81 | | &'try' try_stmt |
| 82 | | &'while' while_stmt |
| 83 | |
| 84 | # NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield' |
Lysandros Nikolaou | 999ec9a | 2020-05-06 21:11:04 +0300 | [diff] [blame] | 85 | assignment[stmt_ty]: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 86 | | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] { |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 87 | CHECK_VERSION( |
| 88 | 6, |
| 89 | "Variable annotation syntax is", |
| 90 | _Py_AnnAssign(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA) |
| 91 | ) } |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 92 | | a=('(' b=single_target ')' { b } |
| 93 | | single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] { |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 94 | CHECK_VERSION(6, "Variable annotations syntax is", _Py_AnnAssign(a, b, c, 0, EXTRA)) } |
Pablo Galindo | 9f49590 | 2020-06-08 02:57:00 +0100 | [diff] [blame^] | 95 | | a=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] { |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 96 | _Py_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) } |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 97 | | a=single_target b=augassign c=(yield_expr | star_expressions) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 98 | _Py_AugAssign(a, b->kind, c, EXTRA) } |
| 99 | | invalid_assignment |
| 100 | |
| 101 | augassign[AugOperator*]: |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 102 | | '+=' { _PyPegen_augoperator(p, Add) } |
| 103 | | '-=' { _PyPegen_augoperator(p, Sub) } |
| 104 | | '*=' { _PyPegen_augoperator(p, Mult) } |
| 105 | | '@=' { CHECK_VERSION(5, "The '@' operator is", _PyPegen_augoperator(p, MatMult)) } |
| 106 | | '/=' { _PyPegen_augoperator(p, Div) } |
| 107 | | '%=' { _PyPegen_augoperator(p, Mod) } |
| 108 | | '&=' { _PyPegen_augoperator(p, BitAnd) } |
| 109 | | '|=' { _PyPegen_augoperator(p, BitOr) } |
| 110 | | '^=' { _PyPegen_augoperator(p, BitXor) } |
| 111 | | '<<=' { _PyPegen_augoperator(p, LShift) } |
| 112 | | '>>=' { _PyPegen_augoperator(p, RShift) } |
| 113 | | '**=' { _PyPegen_augoperator(p, Pow) } |
| 114 | | '//=' { _PyPegen_augoperator(p, FloorDiv) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 115 | |
| 116 | global_stmt[stmt_ty]: 'global' a=','.NAME+ { |
| 117 | _Py_Global(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) } |
| 118 | nonlocal_stmt[stmt_ty]: 'nonlocal' a=','.NAME+ { |
| 119 | _Py_Nonlocal(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) } |
| 120 | |
| 121 | yield_stmt[stmt_ty]: y=yield_expr { _Py_Expr(y, EXTRA) } |
| 122 | |
| 123 | assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _Py_Assert(a, b, EXTRA) } |
| 124 | |
| 125 | del_stmt[stmt_ty]: 'del' a=del_targets { _Py_Delete(a, EXTRA) } |
| 126 | |
| 127 | import_stmt[stmt_ty]: import_name | import_from |
| 128 | import_name[stmt_ty]: 'import' a=dotted_as_names { _Py_Import(a, EXTRA) } |
| 129 | # note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS |
| 130 | import_from[stmt_ty]: |
| 131 | | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets { |
| 132 | _Py_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) } |
| 133 | | 'from' a=('.' | '...')+ 'import' b=import_from_targets { |
| 134 | _Py_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) } |
| 135 | import_from_targets[asdl_seq*]: |
| 136 | | '(' a=import_from_as_names [','] ')' { a } |
Batuhan Taskaya | 72e0aa2 | 2020-05-21 23:41:58 +0300 | [diff] [blame] | 137 | | import_from_as_names !',' |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 138 | | '*' { _PyPegen_singleton_seq(p, CHECK(_PyPegen_alias_for_star(p))) } |
Batuhan Taskaya | 72e0aa2 | 2020-05-21 23:41:58 +0300 | [diff] [blame] | 139 | | invalid_import_from_targets |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 140 | import_from_as_names[asdl_seq*]: |
| 141 | | a=','.import_from_as_name+ { a } |
| 142 | import_from_as_name[alias_ty]: |
| 143 | | a=NAME b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id, |
| 144 | (b) ? ((expr_ty) b)->v.Name.id : NULL, |
| 145 | p->arena) } |
| 146 | dotted_as_names[asdl_seq*]: |
| 147 | | a=','.dotted_as_name+ { a } |
| 148 | dotted_as_name[alias_ty]: |
| 149 | | a=dotted_name b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id, |
| 150 | (b) ? ((expr_ty) b)->v.Name.id : NULL, |
| 151 | p->arena) } |
| 152 | dotted_name[expr_ty]: |
| 153 | | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) } |
| 154 | | NAME |
| 155 | |
| 156 | if_stmt[stmt_ty]: |
| 157 | | 'if' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK(_PyPegen_singleton_seq(p, c)), EXTRA) } |
| 158 | | 'if' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) } |
| 159 | elif_stmt[stmt_ty]: |
| 160 | | 'elif' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK(_PyPegen_singleton_seq(p, c)), EXTRA) } |
| 161 | | 'elif' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) } |
| 162 | else_block[asdl_seq*]: 'else' ':' b=block { b } |
| 163 | |
| 164 | while_stmt[stmt_ty]: |
| 165 | | 'while' a=named_expression ':' b=block c=[else_block] { _Py_While(a, b, c, EXTRA) } |
| 166 | |
| 167 | for_stmt[stmt_ty]: |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 168 | | 'for' t=star_targets 'in' ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] { |
| 169 | _Py_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) } |
| 170 | | ASYNC 'for' t=star_targets 'in' ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] { |
| 171 | CHECK_VERSION(5, "Async for loops are", _Py_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 172 | |
| 173 | with_stmt[stmt_ty]: |
Pablo Galindo | 99db2a1 | 2020-05-06 22:54:34 +0100 | [diff] [blame] | 174 | | 'with' '(' a=','.with_item+ ','? ')' ':' b=block { |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 175 | _Py_With(a, b, NULL, EXTRA) } |
| 176 | | 'with' a=','.with_item+ ':' tc=[TYPE_COMMENT] b=block { |
| 177 | _Py_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) } |
Pablo Galindo | 99db2a1 | 2020-05-06 22:54:34 +0100 | [diff] [blame] | 178 | | ASYNC 'with' '(' a=','.with_item+ ','? ')' ':' b=block { |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 179 | CHECK_VERSION(5, "Async with statements are", _Py_AsyncWith(a, b, NULL, EXTRA)) } |
| 180 | | ASYNC 'with' a=','.with_item+ ':' tc=[TYPE_COMMENT] b=block { |
| 181 | CHECK_VERSION(5, "Async with statements are", _Py_AsyncWith(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 182 | with_item[withitem_ty]: |
| 183 | | e=expression o=['as' t=target { t }] { _Py_withitem(e, o, p->arena) } |
| 184 | |
| 185 | try_stmt[stmt_ty]: |
| 186 | | 'try' ':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) } |
| 187 | | 'try' ':' b=block ex=except_block+ el=[else_block] f=[finally_block] { _Py_Try(b, ex, el, f, EXTRA) } |
| 188 | except_block[excepthandler_ty]: |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 189 | | 'except' e=expression t=['as' z=NAME { z }] ':' b=block { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 190 | _Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) } |
| 191 | | 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) } |
| 192 | finally_block[asdl_seq*]: 'finally' ':' a=block { a } |
| 193 | |
| 194 | return_stmt[stmt_ty]: |
| 195 | | 'return' a=[star_expressions] { _Py_Return(a, EXTRA) } |
| 196 | |
| 197 | raise_stmt[stmt_ty]: |
| 198 | | 'raise' a=expression b=['from' z=expression { z }] { _Py_Raise(a, b, EXTRA) } |
| 199 | | 'raise' { _Py_Raise(NULL, NULL, EXTRA) } |
| 200 | |
| 201 | function_def[stmt_ty]: |
| 202 | | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) } |
| 203 | | function_def_raw |
| 204 | |
| 205 | function_def_raw[stmt_ty]: |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 206 | | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block { |
| 207 | _Py_FunctionDef(n->v.Name.id, |
| 208 | (params) ? params : CHECK(_PyPegen_empty_arguments(p)), |
| 209 | b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) } |
| 210 | | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block { |
| 211 | CHECK_VERSION( |
| 212 | 5, |
| 213 | "Async functions are", |
| 214 | _Py_AsyncFunctionDef(n->v.Name.id, |
| 215 | (params) ? params : CHECK(_PyPegen_empty_arguments(p)), |
| 216 | b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) |
| 217 | ) } |
Pablo Galindo | d955241 | 2020-05-01 16:32:09 +0100 | [diff] [blame] | 218 | func_type_comment[Token*]: |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 219 | | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block |
| 220 | | invalid_double_type_comments |
| 221 | | TYPE_COMMENT |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 222 | |
| 223 | params[arguments_ty]: |
| 224 | | invalid_parameters |
| 225 | | parameters |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 226 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 227 | parameters[arguments_ty]: |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 228 | | a=slash_no_default b=param_no_default* c=param_with_default* d=[star_etc] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 229 | _PyPegen_make_arguments(p, a, NULL, b, c, d) } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 230 | | a=slash_with_default b=param_with_default* c=[star_etc] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 231 | _PyPegen_make_arguments(p, NULL, a, NULL, b, c) } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 232 | | a=param_no_default+ b=param_with_default* c=[star_etc] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 233 | _PyPegen_make_arguments(p, NULL, NULL, a, b, c) } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 234 | | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)} |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 235 | | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 236 | |
| 237 | # Some duplication here because we can't write (',' | &')'), |
| 238 | # which is because we don't support empty alternatives (yet). |
| 239 | # |
| 240 | slash_no_default[asdl_seq*]: |
| 241 | | a=param_no_default+ '/' ',' { a } |
| 242 | | a=param_no_default+ '/' &')' { a } |
| 243 | slash_with_default[SlashWithDefault*]: |
| 244 | | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, a, b) } |
| 245 | | a=param_no_default* b=param_with_default+ '/' &')' { _PyPegen_slash_with_default(p, a, b) } |
| 246 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 247 | star_etc[StarEtc*]: |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 248 | | '*' a=param_no_default b=param_maybe_default* c=[kwds] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 249 | _PyPegen_star_etc(p, a, b, c) } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 250 | | '*' ',' b=param_maybe_default+ c=[kwds] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 251 | _PyPegen_star_etc(p, NULL, b, c) } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 252 | | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) } |
Lysandros Nikolaou | e10e7c7 | 2020-05-04 13:58:31 +0300 | [diff] [blame] | 253 | | invalid_star_etc |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 254 | |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 255 | kwds[arg_ty]: '**' a=param_no_default { a } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 256 | |
| 257 | # One parameter. This *includes* a following comma and type comment. |
| 258 | # |
| 259 | # There are three styles: |
| 260 | # - No default |
| 261 | # - With default |
| 262 | # - Maybe with default |
| 263 | # |
| 264 | # There are two alternative forms of each, to deal with type comments: |
| 265 | # - Ends in a comma followed by an optional type comment |
| 266 | # - No comma, optional type comment, must be followed by close paren |
| 267 | # The latter form is for a final parameter without trailing comma. |
| 268 | # |
| 269 | param_no_default[arg_ty]: |
| 270 | | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) } |
| 271 | | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) } |
| 272 | param_with_default[NameDefaultPair*]: |
| 273 | | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) } |
| 274 | | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) } |
| 275 | param_maybe_default[NameDefaultPair*]: |
| 276 | | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) } |
| 277 | | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) } |
| 278 | param[arg_ty]: a=NAME b=annotation? { _Py_arg(a->v.Name.id, b, NULL, EXTRA) } |
| 279 | |
| 280 | annotation[expr_ty]: ':' a=expression { a } |
| 281 | default[expr_ty]: '=' a=expression { a } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 282 | |
| 283 | decorators[asdl_seq*]: a=('@' f=named_expression NEWLINE { f })+ { a } |
| 284 | |
| 285 | class_def[stmt_ty]: |
| 286 | | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) } |
| 287 | | class_def_raw |
| 288 | class_def_raw[stmt_ty]: |
| 289 | | 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block { |
| 290 | _Py_ClassDef(a->v.Name.id, |
| 291 | (b) ? ((expr_ty) b)->v.Call.args : NULL, |
| 292 | (b) ? ((expr_ty) b)->v.Call.keywords : NULL, |
| 293 | c, NULL, EXTRA) } |
| 294 | |
| 295 | block[asdl_seq*] (memo): |
| 296 | | NEWLINE INDENT a=statements DEDENT { a } |
| 297 | | simple_stmt |
| 298 | | invalid_block |
| 299 | |
| 300 | expressions_list[asdl_seq*]: a=','.star_expression+ [','] { a } |
| 301 | star_expressions[expr_ty]: |
| 302 | | a=star_expression b=(',' c=star_expression { c })+ [','] { |
| 303 | _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) } |
| 304 | | a=star_expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) } |
| 305 | | star_expression |
| 306 | star_expression[expr_ty] (memo): |
| 307 | | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) } |
| 308 | | expression |
| 309 | |
| 310 | star_named_expressions[asdl_seq*]: a=','.star_named_expression+ [','] { a } |
| 311 | star_named_expression[expr_ty]: |
| 312 | | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) } |
| 313 | | named_expression |
| 314 | named_expression[expr_ty]: |
| 315 | | a=NAME ':=' b=expression { _Py_NamedExpr(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, EXTRA) } |
| 316 | | expression !':=' |
| 317 | | invalid_named_expression |
| 318 | |
| 319 | annotated_rhs[expr_ty]: yield_expr | star_expressions |
| 320 | |
| 321 | expressions[expr_ty]: |
| 322 | | a=expression b=(',' c=expression { c })+ [','] { |
| 323 | _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) } |
| 324 | | a=expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) } |
| 325 | | expression |
| 326 | expression[expr_ty] (memo): |
| 327 | | a=disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) } |
| 328 | | disjunction |
| 329 | | lambdef |
| 330 | |
| 331 | lambdef[expr_ty]: |
| 332 | | 'lambda' a=[lambda_parameters] ':' b=expression { _Py_Lambda((a) ? a : CHECK(_PyPegen_empty_arguments(p)), b, EXTRA) } |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 333 | |
| 334 | # lambda_parameters etc. duplicates parameters but without annotations |
| 335 | # or type comments, and if there's no comma after a parameter, we expect |
| 336 | # a colon, not a close parenthesis. (For more, see parameters above.) |
| 337 | # |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 338 | lambda_parameters[arguments_ty]: |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 339 | | a=lambda_slash_no_default b=lambda_param_no_default* c=lambda_param_with_default* d=[lambda_star_etc] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 340 | _PyPegen_make_arguments(p, a, NULL, b, c, d) } |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 341 | | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 342 | _PyPegen_make_arguments(p, NULL, a, NULL, b, c) } |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 343 | | a=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 344 | _PyPegen_make_arguments(p, NULL, NULL, a, b, c) } |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 345 | | a=lambda_param_with_default+ b=[lambda_star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)} |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 346 | | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) } |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 347 | |
| 348 | lambda_slash_no_default[asdl_seq*]: |
| 349 | | a=lambda_param_no_default+ '/' ',' { a } |
| 350 | | a=lambda_param_no_default+ '/' &':' { a } |
| 351 | lambda_slash_with_default[SlashWithDefault*]: |
| 352 | | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, a, b) } |
| 353 | | a=lambda_param_no_default* b=lambda_param_with_default+ '/' &':' { _PyPegen_slash_with_default(p, a, b) } |
| 354 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 355 | lambda_star_etc[StarEtc*]: |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 356 | | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 357 | _PyPegen_star_etc(p, a, b, c) } |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 358 | | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 359 | _PyPegen_star_etc(p, NULL, b, c) } |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 360 | | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) } |
Lysandros Nikolaou | e10e7c7 | 2020-05-04 13:58:31 +0300 | [diff] [blame] | 361 | | invalid_lambda_star_etc |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 362 | |
| 363 | lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a } |
| 364 | |
| 365 | lambda_param_no_default[arg_ty]: |
| 366 | | a=lambda_param ',' { a } |
| 367 | | a=lambda_param &':' { a } |
| 368 | lambda_param_with_default[NameDefaultPair*]: |
| 369 | | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) } |
| 370 | | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) } |
| 371 | lambda_param_maybe_default[NameDefaultPair*]: |
| 372 | | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) } |
| 373 | | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) } |
| 374 | lambda_param[arg_ty]: a=NAME { _Py_arg(a->v.Name.id, NULL, NULL, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 375 | |
| 376 | disjunction[expr_ty] (memo): |
| 377 | | a=conjunction b=('or' c=conjunction { c })+ { _Py_BoolOp( |
| 378 | Or, |
| 379 | CHECK(_PyPegen_seq_insert_in_front(p, a, b)), |
| 380 | EXTRA) } |
| 381 | | conjunction |
| 382 | conjunction[expr_ty] (memo): |
| 383 | | a=inversion b=('and' c=inversion { c })+ { _Py_BoolOp( |
| 384 | And, |
| 385 | CHECK(_PyPegen_seq_insert_in_front(p, a, b)), |
| 386 | EXTRA) } |
| 387 | | inversion |
| 388 | inversion[expr_ty] (memo): |
| 389 | | 'not' a=inversion { _Py_UnaryOp(Not, a, EXTRA) } |
| 390 | | comparison |
| 391 | comparison[expr_ty]: |
| 392 | | a=bitwise_or b=compare_op_bitwise_or_pair+ { |
| 393 | _Py_Compare(a, CHECK(_PyPegen_get_cmpops(p, b)), CHECK(_PyPegen_get_exprs(p, b)), EXTRA) } |
| 394 | | bitwise_or |
| 395 | compare_op_bitwise_or_pair[CmpopExprPair*]: |
| 396 | | eq_bitwise_or |
| 397 | | noteq_bitwise_or |
| 398 | | lte_bitwise_or |
| 399 | | lt_bitwise_or |
| 400 | | gte_bitwise_or |
| 401 | | gt_bitwise_or |
| 402 | | notin_bitwise_or |
| 403 | | in_bitwise_or |
| 404 | | isnot_bitwise_or |
| 405 | | is_bitwise_or |
| 406 | eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) } |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 407 | noteq_bitwise_or[CmpopExprPair*]: |
| 408 | | (tok='!=' {_PyPegen_check_barry_as_flufl(p) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 409 | lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) } |
| 410 | lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) } |
| 411 | gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) } |
| 412 | gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) } |
| 413 | notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) } |
| 414 | in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) } |
| 415 | isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) } |
| 416 | is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) } |
| 417 | |
| 418 | bitwise_or[expr_ty]: |
| 419 | | a=bitwise_or '|' b=bitwise_xor { _Py_BinOp(a, BitOr, b, EXTRA) } |
| 420 | | bitwise_xor |
| 421 | bitwise_xor[expr_ty]: |
| 422 | | a=bitwise_xor '^' b=bitwise_and { _Py_BinOp(a, BitXor, b, EXTRA) } |
| 423 | | bitwise_and |
| 424 | bitwise_and[expr_ty]: |
| 425 | | a=bitwise_and '&' b=shift_expr { _Py_BinOp(a, BitAnd, b, EXTRA) } |
| 426 | | shift_expr |
| 427 | shift_expr[expr_ty]: |
| 428 | | a=shift_expr '<<' b=sum { _Py_BinOp(a, LShift, b, EXTRA) } |
| 429 | | a=shift_expr '>>' b=sum { _Py_BinOp(a, RShift, b, EXTRA) } |
| 430 | | sum |
| 431 | |
| 432 | sum[expr_ty]: |
| 433 | | a=sum '+' b=term { _Py_BinOp(a, Add, b, EXTRA) } |
| 434 | | a=sum '-' b=term { _Py_BinOp(a, Sub, b, EXTRA) } |
| 435 | | term |
| 436 | term[expr_ty]: |
| 437 | | a=term '*' b=factor { _Py_BinOp(a, Mult, b, EXTRA) } |
| 438 | | a=term '/' b=factor { _Py_BinOp(a, Div, b, EXTRA) } |
| 439 | | a=term '//' b=factor { _Py_BinOp(a, FloorDiv, b, EXTRA) } |
| 440 | | a=term '%' b=factor { _Py_BinOp(a, Mod, b, EXTRA) } |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 441 | | a=term '@' b=factor { CHECK_VERSION(5, "The '@' operator is", _Py_BinOp(a, MatMult, b, EXTRA)) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 442 | | factor |
| 443 | factor[expr_ty] (memo): |
| 444 | | '+' a=factor { _Py_UnaryOp(UAdd, a, EXTRA) } |
| 445 | | '-' a=factor { _Py_UnaryOp(USub, a, EXTRA) } |
| 446 | | '~' a=factor { _Py_UnaryOp(Invert, a, EXTRA) } |
| 447 | | power |
| 448 | power[expr_ty]: |
| 449 | | a=await_primary '**' b=factor { _Py_BinOp(a, Pow, b, EXTRA) } |
| 450 | | await_primary |
| 451 | await_primary[expr_ty] (memo): |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 452 | | AWAIT a=primary { CHECK_VERSION(5, "Await expressions are", _Py_Await(a, EXTRA)) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 453 | | primary |
| 454 | primary[expr_ty]: |
| 455 | | a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) } |
| 456 | | a=primary b=genexp { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) } |
| 457 | | a=primary '(' b=[arguments] ')' { |
| 458 | _Py_Call(a, |
| 459 | (b) ? ((expr_ty) b)->v.Call.args : NULL, |
| 460 | (b) ? ((expr_ty) b)->v.Call.keywords : NULL, |
| 461 | EXTRA) } |
| 462 | | a=primary '[' b=slices ']' { _Py_Subscript(a, b, Load, EXTRA) } |
| 463 | | atom |
| 464 | |
| 465 | slices[expr_ty]: |
| 466 | | a=slice !',' { a } |
| 467 | | a=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) } |
| 468 | slice[expr_ty]: |
| 469 | | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) } |
| 470 | | a=expression { a } |
| 471 | atom[expr_ty]: |
| 472 | | NAME |
| 473 | | 'True' { _Py_Constant(Py_True, NULL, EXTRA) } |
| 474 | | 'False' { _Py_Constant(Py_False, NULL, EXTRA) } |
| 475 | | 'None' { _Py_Constant(Py_None, NULL, EXTRA) } |
| 476 | | '__new_parser__' { RAISE_SYNTAX_ERROR("You found it!") } |
| 477 | | &STRING strings |
| 478 | | NUMBER |
| 479 | | &'(' (tuple | group | genexp) |
| 480 | | &'[' (list | listcomp) |
| 481 | | &'{' (dict | set | dictcomp | setcomp) |
| 482 | | '...' { _Py_Constant(Py_Ellipsis, NULL, EXTRA) } |
| 483 | |
| 484 | strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) } |
| 485 | list[expr_ty]: |
| 486 | | '[' a=[star_named_expressions] ']' { _Py_List(a, Load, EXTRA) } |
| 487 | listcomp[expr_ty]: |
| 488 | | '[' a=named_expression b=for_if_clauses ']' { _Py_ListComp(a, b, EXTRA) } |
| 489 | | invalid_comprehension |
| 490 | tuple[expr_ty]: |
| 491 | | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' { |
| 492 | _Py_Tuple(a, Load, EXTRA) } |
| 493 | group[expr_ty]: '(' a=(yield_expr | named_expression) ')' { a } |
| 494 | genexp[expr_ty]: |
| 495 | | '(' a=expression b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) } |
| 496 | | invalid_comprehension |
| 497 | set[expr_ty]: '{' a=expressions_list '}' { _Py_Set(a, EXTRA) } |
| 498 | setcomp[expr_ty]: |
| 499 | | '{' a=expression b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) } |
| 500 | | invalid_comprehension |
| 501 | dict[expr_ty]: |
Batuhan Taskaya | b8a65ec | 2020-05-22 01:39:56 +0300 | [diff] [blame] | 502 | | '{' a=[double_starred_kvpairs] '}' { |
| 503 | _Py_Dict(CHECK(_PyPegen_get_keys(p, a)), CHECK(_PyPegen_get_values(p, a)), EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 504 | dictcomp[expr_ty]: |
| 505 | | '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) } |
Batuhan Taskaya | b8a65ec | 2020-05-22 01:39:56 +0300 | [diff] [blame] | 506 | | invalid_dict_comprehension |
| 507 | double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a } |
| 508 | double_starred_kvpair[KeyValuePair*]: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 509 | | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) } |
Batuhan Taskaya | b8a65ec | 2020-05-22 01:39:56 +0300 | [diff] [blame] | 510 | | kvpair |
| 511 | kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 512 | for_if_clauses[asdl_seq*]: |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 513 | | for_if_clause+ |
| 514 | for_if_clause[comprehension_ty]: |
| 515 | | ASYNC 'for' a=star_targets 'in' b=disjunction c=('if' z=disjunction { z })* { |
| 516 | CHECK_VERSION(6, "Async comprehensions are", _Py_comprehension(a, b, c, 1, p->arena)) } |
| 517 | | 'for' a=star_targets 'in' b=disjunction c=('if' z=disjunction { z })* { |
| 518 | _Py_comprehension(a, b, c, 0, p->arena) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 519 | |
| 520 | yield_expr[expr_ty]: |
| 521 | | 'yield' 'from' a=expression { _Py_YieldFrom(a, EXTRA) } |
| 522 | | 'yield' a=[star_expressions] { _Py_Yield(a, EXTRA) } |
| 523 | |
| 524 | arguments[expr_ty] (memo): |
| 525 | | a=args [','] &')' { a } |
| 526 | | incorrect_arguments |
| 527 | args[expr_ty]: |
| 528 | | a=starred_expression b=[',' c=args { c }] { |
| 529 | _Py_Call(_PyPegen_dummy_name(p), |
| 530 | (b) ? CHECK(_PyPegen_seq_insert_in_front(p, a, ((expr_ty) b)->v.Call.args)) |
| 531 | : CHECK(_PyPegen_singleton_seq(p, a)), |
| 532 | (b) ? ((expr_ty) b)->v.Call.keywords : NULL, |
| 533 | EXTRA) } |
| 534 | | a=kwargs { _Py_Call(_PyPegen_dummy_name(p), |
| 535 | CHECK_NULL_ALLOWED(_PyPegen_seq_extract_starred_exprs(p, a)), |
| 536 | CHECK_NULL_ALLOWED(_PyPegen_seq_delete_starred_exprs(p, a)), |
| 537 | EXTRA) } |
| 538 | | a=named_expression b=[',' c=args { c }] { |
| 539 | _Py_Call(_PyPegen_dummy_name(p), |
| 540 | (b) ? CHECK(_PyPegen_seq_insert_in_front(p, a, ((expr_ty) b)->v.Call.args)) |
| 541 | : CHECK(_PyPegen_singleton_seq(p, a)), |
| 542 | (b) ? ((expr_ty) b)->v.Call.keywords : NULL, |
| 543 | EXTRA) } |
| 544 | kwargs[asdl_seq*]: |
| 545 | | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) } |
| 546 | | ','.kwarg_or_starred+ |
| 547 | | ','.kwarg_or_double_starred+ |
| 548 | starred_expression[expr_ty]: |
| 549 | | '*' a=expression { _Py_Starred(a, Load, EXTRA) } |
| 550 | kwarg_or_starred[KeywordOrStarred*]: |
| 551 | | a=NAME '=' b=expression { |
| 552 | _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) } |
| 553 | | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) } |
Lysandros Nikolaou | 4638c64 | 2020-05-07 13:44:06 +0300 | [diff] [blame] | 554 | | invalid_kwarg |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 555 | kwarg_or_double_starred[KeywordOrStarred*]: |
| 556 | | a=NAME '=' b=expression { |
| 557 | _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) } |
| 558 | | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(NULL, a, EXTRA)), 1) } |
Lysandros Nikolaou | 4638c64 | 2020-05-07 13:44:06 +0300 | [diff] [blame] | 559 | | invalid_kwarg |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 560 | |
| 561 | # NOTE: star_targets may contain *bitwise_or, targets may not. |
| 562 | star_targets[expr_ty]: |
| 563 | | a=star_target !',' { a } |
| 564 | | a=star_target b=(',' c=star_target { c })* [','] { |
| 565 | _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) } |
| 566 | star_targets_seq[asdl_seq*]: a=','.star_target+ [','] { a } |
| 567 | star_target[expr_ty] (memo): |
| 568 | | '*' a=(!'*' star_target) { |
| 569 | _Py_Starred(CHECK(_PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) } |
| 570 | | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) } |
| 571 | | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) } |
| 572 | | star_atom |
| 573 | star_atom[expr_ty]: |
| 574 | | a=NAME { _PyPegen_set_expr_context(p, a, Store) } |
| 575 | | '(' a=star_target ')' { _PyPegen_set_expr_context(p, a, Store) } |
| 576 | | '(' a=[star_targets_seq] ')' { _Py_Tuple(a, Store, EXTRA) } |
| 577 | | '[' a=[star_targets_seq] ']' { _Py_List(a, Store, EXTRA) } |
| 578 | |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 579 | single_target[expr_ty]: |
| 580 | | single_subscript_attribute_target |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 581 | | a=NAME { _PyPegen_set_expr_context(p, a, Store) } |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 582 | | '(' a=single_target ')' { a } |
| 583 | single_subscript_attribute_target[expr_ty]: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 584 | | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) } |
| 585 | | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) } |
| 586 | |
| 587 | del_targets[asdl_seq*]: a=','.del_target+ [','] { a } |
Shantanu | 27c0d9b | 2020-05-11 14:53:58 -0700 | [diff] [blame] | 588 | # The lookaheads to del_target_end ensure that we don't match expressions where a prefix of the |
| 589 | # expression matches our rule, thereby letting these cases fall through to invalid_del_target. |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 590 | del_target[expr_ty] (memo): |
Shantanu | 27c0d9b | 2020-05-11 14:53:58 -0700 | [diff] [blame] | 591 | | a=t_primary '.' b=NAME &del_target_end { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) } |
| 592 | | a=t_primary '[' b=slices ']' &del_target_end { _Py_Subscript(a, b, Del, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 593 | | del_t_atom |
| 594 | del_t_atom[expr_ty]: |
Shantanu | 27c0d9b | 2020-05-11 14:53:58 -0700 | [diff] [blame] | 595 | | a=NAME &del_target_end { _PyPegen_set_expr_context(p, a, Del) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 596 | | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) } |
| 597 | | '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) } |
| 598 | | '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) } |
Shantanu | 27c0d9b | 2020-05-11 14:53:58 -0700 | [diff] [blame] | 599 | | invalid_del_target |
| 600 | del_target_end: ')' | ']' | ',' | ';' | NEWLINE |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 601 | |
| 602 | targets[asdl_seq*]: a=','.target+ [','] { a } |
| 603 | target[expr_ty] (memo): |
| 604 | | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) } |
| 605 | | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) } |
| 606 | | t_atom |
| 607 | t_primary[expr_ty]: |
| 608 | | a=t_primary '.' b=NAME &t_lookahead { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) } |
| 609 | | a=t_primary '[' b=slices ']' &t_lookahead { _Py_Subscript(a, b, Load, EXTRA) } |
| 610 | | a=t_primary b=genexp &t_lookahead { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) } |
| 611 | | a=t_primary '(' b=[arguments] ')' &t_lookahead { |
| 612 | _Py_Call(a, |
| 613 | (b) ? ((expr_ty) b)->v.Call.args : NULL, |
| 614 | (b) ? ((expr_ty) b)->v.Call.keywords : NULL, |
| 615 | EXTRA) } |
| 616 | | a=atom &t_lookahead { a } |
| 617 | t_lookahead: '(' | '[' | '.' |
| 618 | t_atom[expr_ty]: |
| 619 | | a=NAME { _PyPegen_set_expr_context(p, a, Store) } |
| 620 | | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) } |
| 621 | | '(' b=[targets] ')' { _Py_Tuple(b, Store, EXTRA) } |
| 622 | | '[' b=[targets] ']' { _Py_List(b, Store, EXTRA) } |
| 623 | |
| 624 | |
| 625 | # From here on, there are rules for invalid syntax with specialised error messages |
| 626 | incorrect_arguments: |
| 627 | | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") } |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 628 | | a=expression for_if_clauses ',' [args | expression for_if_clauses] { |
| 629 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") } |
Lysandros Nikolaou | ae14583 | 2020-05-22 03:56:52 +0300 | [diff] [blame] | 630 | | a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) } |
| 631 | | args ',' a=expression for_if_clauses { |
| 632 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 633 | | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) } |
Lysandros Nikolaou | 4638c64 | 2020-05-07 13:44:06 +0300 | [diff] [blame] | 634 | invalid_kwarg: |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 635 | | a=expression '=' { |
| 636 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION( |
| 637 | a, "expression cannot contain assignment, perhaps you meant \"==\"?") } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 638 | invalid_named_expression: |
| 639 | | a=expression ':=' expression { |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 640 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION( |
| 641 | a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 642 | invalid_assignment: |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 643 | | a=list ':' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not list) can be annotated") } |
| 644 | | a=tuple ':' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") } |
| 645 | | a=star_named_expression ',' star_named_expressions* ':' { |
| 646 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") } |
| 647 | | a=expression ':' expression ['=' annotated_rhs] { |
| 648 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") } |
Pablo Galindo | 9f49590 | 2020-06-08 02:57:00 +0100 | [diff] [blame^] | 649 | | (star_targets '=')* a=star_expressions '=' { |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 650 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION( |
| 651 | _PyPegen_get_invalid_target(a), |
| 652 | "cannot assign to %s", _PyPegen_get_expr_name(_PyPegen_get_invalid_target(a))) } |
Pablo Galindo | 9f49590 | 2020-06-08 02:57:00 +0100 | [diff] [blame^] | 653 | | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") } |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 654 | | a=star_expressions augassign (yield_expr | star_expressions) { |
| 655 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION( |
| 656 | a, |
| 657 | "'%s' is an illegal expression for augmented assignment", |
| 658 | _PyPegen_get_expr_name(a) |
| 659 | )} |
| 660 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 661 | invalid_block: |
| 662 | | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") } |
| 663 | invalid_comprehension: |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 664 | | ('[' | '(' | '{') a=starred_expression for_if_clauses { |
| 665 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") } |
Batuhan Taskaya | b8a65ec | 2020-05-22 01:39:56 +0300 | [diff] [blame] | 666 | invalid_dict_comprehension: |
| 667 | | '{' a='**' bitwise_or for_if_clauses '}' { |
| 668 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 669 | invalid_parameters: |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 670 | | param_no_default* (slash_with_default | param_with_default+) param_no_default { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 671 | RAISE_SYNTAX_ERROR("non-default argument follows default argument") } |
Lysandros Nikolaou | e10e7c7 | 2020-05-04 13:58:31 +0300 | [diff] [blame] | 672 | invalid_star_etc: |
| 673 | | '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") } |
Lysandros Nikolaou | 75b863a | 2020-05-18 22:14:47 +0300 | [diff] [blame] | 674 | | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") } |
Lysandros Nikolaou | e10e7c7 | 2020-05-04 13:58:31 +0300 | [diff] [blame] | 675 | invalid_lambda_star_etc: |
| 676 | | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 677 | invalid_double_type_comments: |
| 678 | | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT { |
| 679 | RAISE_SYNTAX_ERROR("Cannot have two type comments on def") } |
Shantanu | 27c0d9b | 2020-05-11 14:53:58 -0700 | [diff] [blame] | 680 | invalid_del_target: |
| 681 | | a=star_expression &del_target_end { |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 682 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot delete %s", _PyPegen_get_expr_name(a)) } |
Batuhan Taskaya | 72e0aa2 | 2020-05-21 23:41:58 +0300 | [diff] [blame] | 683 | invalid_import_from_targets: |
| 684 | | import_from_as_names ',' { |
| 685 | RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") } |