@@ -649,9 +649,10 @@ type_param_seq[asdl_type_param_seq*]: a[asdl_type_param_seq*]=','.type_param+ ['
649
649
650
650
type_param[type_param_ty] (memo):
651
651
| a=NAME b=[type_param_bound] c=[type_param_default] { _PyAST_TypeVar(a->v.Name.id, b, c, EXTRA) }
652
- | invalid_type_param
652
+ | invalid_type_param_def
653
653
| '*' a=NAME b=[type_param_starred_default] { _PyAST_TypeVarTuple(a->v.Name.id, b, EXTRA) }
654
654
| '**' a=NAME b=[type_param_default] { _PyAST_ParamSpec(a->v.Name.id, b, EXTRA) }
655
+ | invalid_type_param_expr
655
656
656
657
type_param_bound[expr_ty]: ':' e=expression { e }
657
658
type_param_default[expr_ty]: '=' e=expression {
@@ -1165,7 +1166,15 @@ invalid_legacy_expression:
1165
1166
_PyPegen_check_legacy_stmt(p, a) ? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b,
1166
1167
"Missing parentheses in call to '%U'. Did you mean %U(...)?", a->v.Name.id, a->v.Name.id) : NULL}
1167
1168
1168
- invalid_type_param:
1169
+ # Invalid type parameters:
1170
+
1171
+ invalid_type_params:
1172
+ | '[' token=']' {
1173
+ RAISE_SYNTAX_ERROR_STARTING_FROM(
1174
+ token,
1175
+ "Type parameter list cannot be empty")}
1176
+
1177
+ invalid_type_param_def:
1169
1178
| '*' a=NAME colon=':' e=expression {
1170
1179
RAISE_SYNTAX_ERROR_STARTING_FROM(colon, e->kind == Tuple_kind
1171
1180
? "cannot use constraints with TypeVarTuple"
@@ -1177,6 +1186,23 @@ invalid_type_param:
1177
1186
: "cannot use bound with ParamSpec")
1178
1187
}
1179
1188
1189
+ invalid_type_param_expr:
1190
+ | a=expression [type_param_bound] [type_param_default] {
1191
+ RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
1192
+ a, "expected a type parameter definition, found %s",
1193
+ _PyPegen_get_expr_name(a))
1194
+ }
1195
+ | '*' a=expression [type_param_starred_default] {
1196
+ RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
1197
+ a, "expected a type parameter definition, found %s",
1198
+ _PyPegen_get_expr_name(a))
1199
+ }
1200
+ | '**' a=expression [type_param_default] {
1201
+ RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
1202
+ a, "expected a type parameter definition, found %s",
1203
+ _PyPegen_get_expr_name(a))
1204
+ }
1205
+
1180
1206
invalid_expression:
1181
1207
# !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf"
1182
1208
# Soft keywords need to also be ignored because they can be parsed as NAME NAME
@@ -1453,9 +1479,3 @@ invalid_arithmetic:
1453
1479
| sum ('+'|'-'|'*'|'/'|'%'|'//'|'@') a='not' b=inversion { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "'not' after an operator must be parenthesized") }
1454
1480
invalid_factor:
1455
1481
| ('+' | '-' | '~') a='not' b=factor { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "'not' after an operator must be parenthesized") }
1456
-
1457
- invalid_type_params:
1458
- | '[' token=']' {
1459
- RAISE_SYNTAX_ERROR_STARTING_FROM(
1460
- token,
1461
- "Type parameter list cannot be empty")}
0 commit comments