diff --git a/Grammar/python.gram b/Grammar/python.gram index a225664ebb8..7247962d3b9 100644 --- a/Grammar/python.gram +++ b/Grammar/python.gram @@ -211,6 +211,7 @@ finally_block[asdl_stmt_seq*]: 'finally' ':' a=block { a } match_stmt[stmt_ty]: | "match" subject=subject_expr ':' NEWLINE INDENT cases[asdl_match_case_seq*]=case_block+ DEDENT { CHECK_VERSION(stmt_ty, 10, "Pattern matching is", _Py_Match(subject, cases, EXTRA)) } + | invalid_match_stmt subject_expr[expr_ty]: | value=star_named_expression ',' values=star_named_expressions? { _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, value, values)), Load, EXTRA) } @@ -218,6 +219,7 @@ subject_expr[expr_ty]: case_block[match_case_ty]: | "case" pattern=patterns guard=guard? ':' body=block { _Py_match_case(pattern, guard, body, p->arena) } + | invalid_case_block guard[expr_ty]: 'if' guard=named_expression { guard } patterns[expr_ty]: @@ -853,3 +855,9 @@ invalid_except_block: RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "exception group must be parenthesized") } | 'except' expression ['as' NAME ] &&':' | 'except' &&':' + +invalid_match_stmt: + | "match" subject_expr !':' { CHECK_VERSION(void*, 10, "Pattern matching is", RAISE_SYNTAX_ERROR("expected ':'") ) } + +invalid_case_block: + | "case" patterns guard? !':' { RAISE_SYNTAX_ERROR("expected ':'") } diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index 47df0579f1e..24233b29171 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -825,6 +825,24 @@ leading to spurious errors. Traceback (most recent call last): SyntaxError: expected ':' + >>> match x + ... case list(): + ... pass + Traceback (most recent call last): + SyntaxError: expected ':' + + >>> match x: + ... case list() + ... pass + Traceback (most recent call last): + SyntaxError: expected ':' + + >>> match x: + ... case [y] if y > 0 + ... pass + Traceback (most recent call last): + SyntaxError: expected ':' + Make sure that the old "raise X, Y[, Z]" form is gone: >>> raise X, Y Traceback (most recent call last): @@ -1159,6 +1177,24 @@ def func2(): for paren in ")]}": self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'") + def test_match_call_does_not_raise_syntax_error(self): + code = """ +def match(x): + return 1+1 + +match(34) +""" + compile(code, "", "exec") + + def test_case_call_does_not_raise_syntax_error(self): + code = """ +def case(x): + return 1+1 + +case(34) +""" + compile(code, "", "exec") + def test_main(): support.run_unittest(SyntaxTestCase) diff --git a/Parser/parser.c b/Parser/parser.c index 9432746921a..acfaf80094c 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -263,188 +263,190 @@ static KeywordToken *reserved_keywords[] = { #define invalid_import_from_targets_type 1194 #define invalid_with_stmt_type 1195 #define invalid_except_block_type 1196 -#define _loop0_1_type 1197 -#define _loop0_2_type 1198 -#define _loop0_4_type 1199 -#define _gather_3_type 1200 -#define _loop0_6_type 1201 -#define _gather_5_type 1202 -#define _loop0_8_type 1203 -#define _gather_7_type 1204 -#define _loop0_10_type 1205 -#define _gather_9_type 1206 -#define _loop1_11_type 1207 -#define _loop0_13_type 1208 -#define _gather_12_type 1209 -#define _tmp_14_type 1210 -#define _tmp_15_type 1211 -#define _tmp_16_type 1212 -#define _tmp_17_type 1213 -#define _tmp_18_type 1214 -#define _tmp_19_type 1215 -#define _tmp_20_type 1216 -#define _tmp_21_type 1217 -#define _loop1_22_type 1218 -#define _tmp_23_type 1219 -#define _tmp_24_type 1220 -#define _loop0_26_type 1221 -#define _gather_25_type 1222 -#define _loop0_28_type 1223 -#define _gather_27_type 1224 -#define _tmp_29_type 1225 -#define _tmp_30_type 1226 -#define _loop0_31_type 1227 -#define _loop1_32_type 1228 -#define _loop0_34_type 1229 -#define _gather_33_type 1230 -#define _tmp_35_type 1231 -#define _loop0_37_type 1232 -#define _gather_36_type 1233 -#define _tmp_38_type 1234 -#define _loop0_40_type 1235 -#define _gather_39_type 1236 -#define _loop0_42_type 1237 -#define _gather_41_type 1238 -#define _loop0_44_type 1239 -#define _gather_43_type 1240 -#define _loop0_46_type 1241 -#define _gather_45_type 1242 -#define _tmp_47_type 1243 -#define _loop1_48_type 1244 -#define _tmp_49_type 1245 -#define _loop1_50_type 1246 -#define _loop0_52_type 1247 -#define _gather_51_type 1248 -#define _tmp_53_type 1249 -#define _tmp_54_type 1250 -#define _tmp_55_type 1251 -#define _loop0_57_type 1252 -#define _gather_56_type 1253 -#define _tmp_58_type 1254 -#define _loop0_60_type 1255 -#define _gather_59_type 1256 -#define _tmp_61_type 1257 -#define _loop0_63_type 1258 -#define _gather_62_type 1259 -#define _loop0_65_type 1260 -#define _gather_64_type 1261 -#define _tmp_66_type 1262 -#define _tmp_67_type 1263 -#define _tmp_68_type 1264 -#define _tmp_69_type 1265 -#define _loop0_70_type 1266 -#define _loop0_71_type 1267 -#define _loop0_72_type 1268 -#define _loop1_73_type 1269 -#define _loop0_74_type 1270 -#define _loop1_75_type 1271 -#define _loop1_76_type 1272 -#define _loop1_77_type 1273 -#define _loop0_78_type 1274 -#define _loop1_79_type 1275 -#define _loop0_80_type 1276 -#define _loop1_81_type 1277 -#define _loop0_82_type 1278 -#define _loop1_83_type 1279 -#define _loop1_84_type 1280 -#define _tmp_85_type 1281 -#define _loop1_86_type 1282 -#define _loop0_88_type 1283 -#define _gather_87_type 1284 -#define _loop1_89_type 1285 -#define _loop0_90_type 1286 -#define _loop0_91_type 1287 -#define _loop0_92_type 1288 -#define _loop1_93_type 1289 -#define _loop0_94_type 1290 -#define _loop1_95_type 1291 -#define _loop1_96_type 1292 -#define _loop1_97_type 1293 -#define _loop0_98_type 1294 -#define _loop1_99_type 1295 -#define _loop0_100_type 1296 -#define _loop1_101_type 1297 -#define _loop0_102_type 1298 -#define _loop1_103_type 1299 -#define _loop1_104_type 1300 -#define _loop1_105_type 1301 -#define _loop1_106_type 1302 -#define _tmp_107_type 1303 -#define _loop0_109_type 1304 -#define _gather_108_type 1305 -#define _tmp_110_type 1306 -#define _tmp_111_type 1307 -#define _tmp_112_type 1308 -#define _tmp_113_type 1309 -#define _loop1_114_type 1310 -#define _tmp_115_type 1311 -#define _tmp_116_type 1312 -#define _loop0_118_type 1313 -#define _gather_117_type 1314 -#define _loop1_119_type 1315 -#define _loop0_120_type 1316 -#define _loop0_121_type 1317 -#define _loop0_123_type 1318 -#define _gather_122_type 1319 -#define _tmp_124_type 1320 -#define _loop0_126_type 1321 -#define _gather_125_type 1322 -#define _loop0_128_type 1323 -#define _gather_127_type 1324 -#define _loop0_130_type 1325 -#define _gather_129_type 1326 -#define _loop0_132_type 1327 -#define _gather_131_type 1328 -#define _loop0_133_type 1329 -#define _loop0_135_type 1330 -#define _gather_134_type 1331 -#define _loop1_136_type 1332 -#define _tmp_137_type 1333 -#define _loop0_139_type 1334 -#define _gather_138_type 1335 -#define _loop0_141_type 1336 -#define _gather_140_type 1337 -#define _tmp_142_type 1338 -#define _loop0_143_type 1339 -#define _loop0_144_type 1340 -#define _loop0_145_type 1341 -#define _tmp_146_type 1342 -#define _tmp_147_type 1343 -#define _tmp_148_type 1344 -#define _loop0_149_type 1345 -#define _loop1_150_type 1346 -#define _loop0_151_type 1347 -#define _loop1_152_type 1348 -#define _tmp_153_type 1349 -#define _tmp_154_type 1350 -#define _tmp_155_type 1351 -#define _loop0_157_type 1352 -#define _gather_156_type 1353 -#define _loop0_159_type 1354 -#define _gather_158_type 1355 -#define _tmp_160_type 1356 -#define _tmp_161_type 1357 -#define _tmp_162_type 1358 -#define _tmp_163_type 1359 -#define _tmp_164_type 1360 -#define _tmp_165_type 1361 -#define _tmp_166_type 1362 -#define _tmp_167_type 1363 -#define _tmp_168_type 1364 -#define _tmp_169_type 1365 -#define _tmp_170_type 1366 -#define _tmp_171_type 1367 -#define _tmp_172_type 1368 -#define _tmp_173_type 1369 -#define _tmp_174_type 1370 -#define _tmp_175_type 1371 -#define _tmp_176_type 1372 -#define _tmp_177_type 1373 -#define _tmp_178_type 1374 -#define _tmp_179_type 1375 -#define _tmp_180_type 1376 -#define _tmp_181_type 1377 -#define _tmp_182_type 1378 +#define invalid_match_stmt_type 1197 +#define invalid_case_block_type 1198 +#define _loop0_1_type 1199 +#define _loop0_2_type 1200 +#define _loop0_4_type 1201 +#define _gather_3_type 1202 +#define _loop0_6_type 1203 +#define _gather_5_type 1204 +#define _loop0_8_type 1205 +#define _gather_7_type 1206 +#define _loop0_10_type 1207 +#define _gather_9_type 1208 +#define _loop1_11_type 1209 +#define _loop0_13_type 1210 +#define _gather_12_type 1211 +#define _tmp_14_type 1212 +#define _tmp_15_type 1213 +#define _tmp_16_type 1214 +#define _tmp_17_type 1215 +#define _tmp_18_type 1216 +#define _tmp_19_type 1217 +#define _tmp_20_type 1218 +#define _tmp_21_type 1219 +#define _loop1_22_type 1220 +#define _tmp_23_type 1221 +#define _tmp_24_type 1222 +#define _loop0_26_type 1223 +#define _gather_25_type 1224 +#define _loop0_28_type 1225 +#define _gather_27_type 1226 +#define _tmp_29_type 1227 +#define _tmp_30_type 1228 +#define _loop0_31_type 1229 +#define _loop1_32_type 1230 +#define _loop0_34_type 1231 +#define _gather_33_type 1232 +#define _tmp_35_type 1233 +#define _loop0_37_type 1234 +#define _gather_36_type 1235 +#define _tmp_38_type 1236 +#define _loop0_40_type 1237 +#define _gather_39_type 1238 +#define _loop0_42_type 1239 +#define _gather_41_type 1240 +#define _loop0_44_type 1241 +#define _gather_43_type 1242 +#define _loop0_46_type 1243 +#define _gather_45_type 1244 +#define _tmp_47_type 1245 +#define _loop1_48_type 1246 +#define _tmp_49_type 1247 +#define _loop1_50_type 1248 +#define _loop0_52_type 1249 +#define _gather_51_type 1250 +#define _tmp_53_type 1251 +#define _tmp_54_type 1252 +#define _tmp_55_type 1253 +#define _loop0_57_type 1254 +#define _gather_56_type 1255 +#define _tmp_58_type 1256 +#define _loop0_60_type 1257 +#define _gather_59_type 1258 +#define _tmp_61_type 1259 +#define _loop0_63_type 1260 +#define _gather_62_type 1261 +#define _loop0_65_type 1262 +#define _gather_64_type 1263 +#define _tmp_66_type 1264 +#define _tmp_67_type 1265 +#define _tmp_68_type 1266 +#define _tmp_69_type 1267 +#define _loop0_70_type 1268 +#define _loop0_71_type 1269 +#define _loop0_72_type 1270 +#define _loop1_73_type 1271 +#define _loop0_74_type 1272 +#define _loop1_75_type 1273 +#define _loop1_76_type 1274 +#define _loop1_77_type 1275 +#define _loop0_78_type 1276 +#define _loop1_79_type 1277 +#define _loop0_80_type 1278 +#define _loop1_81_type 1279 +#define _loop0_82_type 1280 +#define _loop1_83_type 1281 +#define _loop1_84_type 1282 +#define _tmp_85_type 1283 +#define _loop1_86_type 1284 +#define _loop0_88_type 1285 +#define _gather_87_type 1286 +#define _loop1_89_type 1287 +#define _loop0_90_type 1288 +#define _loop0_91_type 1289 +#define _loop0_92_type 1290 +#define _loop1_93_type 1291 +#define _loop0_94_type 1292 +#define _loop1_95_type 1293 +#define _loop1_96_type 1294 +#define _loop1_97_type 1295 +#define _loop0_98_type 1296 +#define _loop1_99_type 1297 +#define _loop0_100_type 1298 +#define _loop1_101_type 1299 +#define _loop0_102_type 1300 +#define _loop1_103_type 1301 +#define _loop1_104_type 1302 +#define _loop1_105_type 1303 +#define _loop1_106_type 1304 +#define _tmp_107_type 1305 +#define _loop0_109_type 1306 +#define _gather_108_type 1307 +#define _tmp_110_type 1308 +#define _tmp_111_type 1309 +#define _tmp_112_type 1310 +#define _tmp_113_type 1311 +#define _loop1_114_type 1312 +#define _tmp_115_type 1313 +#define _tmp_116_type 1314 +#define _loop0_118_type 1315 +#define _gather_117_type 1316 +#define _loop1_119_type 1317 +#define _loop0_120_type 1318 +#define _loop0_121_type 1319 +#define _loop0_123_type 1320 +#define _gather_122_type 1321 +#define _tmp_124_type 1322 +#define _loop0_126_type 1323 +#define _gather_125_type 1324 +#define _loop0_128_type 1325 +#define _gather_127_type 1326 +#define _loop0_130_type 1327 +#define _gather_129_type 1328 +#define _loop0_132_type 1329 +#define _gather_131_type 1330 +#define _loop0_133_type 1331 +#define _loop0_135_type 1332 +#define _gather_134_type 1333 +#define _loop1_136_type 1334 +#define _tmp_137_type 1335 +#define _loop0_139_type 1336 +#define _gather_138_type 1337 +#define _loop0_141_type 1338 +#define _gather_140_type 1339 +#define _tmp_142_type 1340 +#define _loop0_143_type 1341 +#define _loop0_144_type 1342 +#define _loop0_145_type 1343 +#define _tmp_146_type 1344 +#define _tmp_147_type 1345 +#define _tmp_148_type 1346 +#define _loop0_149_type 1347 +#define _loop1_150_type 1348 +#define _loop0_151_type 1349 +#define _loop1_152_type 1350 +#define _tmp_153_type 1351 +#define _tmp_154_type 1352 +#define _tmp_155_type 1353 +#define _loop0_157_type 1354 +#define _gather_156_type 1355 +#define _loop0_159_type 1356 +#define _gather_158_type 1357 +#define _tmp_160_type 1358 +#define _tmp_161_type 1359 +#define _tmp_162_type 1360 +#define _tmp_163_type 1361 +#define _tmp_164_type 1362 +#define _tmp_165_type 1363 +#define _tmp_166_type 1364 +#define _tmp_167_type 1365 +#define _tmp_168_type 1366 +#define _tmp_169_type 1367 +#define _tmp_170_type 1368 +#define _tmp_171_type 1369 +#define _tmp_172_type 1370 +#define _tmp_173_type 1371 +#define _tmp_174_type 1372 +#define _tmp_175_type 1373 +#define _tmp_176_type 1374 +#define _tmp_177_type 1375 +#define _tmp_178_type 1376 +#define _tmp_179_type 1377 +#define _tmp_180_type 1378 +#define _tmp_181_type 1379 +#define _tmp_182_type 1380 static mod_ty file_rule(Parser *p); static mod_ty interactive_rule(Parser *p); @@ -643,6 +645,8 @@ static void *invalid_group_rule(Parser *p); static void *invalid_import_from_targets_rule(Parser *p); static void *invalid_with_stmt_rule(Parser *p); static void *invalid_except_block_rule(Parser *p); +static void *invalid_match_stmt_rule(Parser *p); +static void *invalid_case_block_rule(Parser *p); static asdl_seq *_loop0_1_rule(Parser *p); static asdl_seq *_loop0_2_rule(Parser *p); static asdl_seq *_loop0_4_rule(Parser *p); @@ -4848,7 +4852,9 @@ finally_block_rule(Parser *p) return _res; } -// match_stmt: "match" subject_expr ':' NEWLINE INDENT case_block+ DEDENT +// match_stmt: +// | "match" subject_expr ':' NEWLINE INDENT case_block+ DEDENT +// | invalid_match_stmt static stmt_ty match_stmt_rule(Parser *p) { @@ -4919,6 +4925,25 @@ match_stmt_rule(Parser *p) D(fprintf(stderr, "%*c%s match_stmt[%d-%d]: %s failed!\n", p->level, ' ', p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "\"match\" subject_expr ':' NEWLINE INDENT case_block+ DEDENT")); } + if (p->call_invalid_rules) { // invalid_match_stmt + if (p->error_indicator) { + D(p->level--); + return NULL; + } + D(fprintf(stderr, "%*c> match_stmt[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "invalid_match_stmt")); + void *invalid_match_stmt_var; + if ( + (invalid_match_stmt_var = invalid_match_stmt_rule(p)) // invalid_match_stmt + ) + { + D(fprintf(stderr, "%*c+ match_stmt[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "invalid_match_stmt")); + _res = invalid_match_stmt_var; + goto done; + } + p->mark = _mark; + D(fprintf(stderr, "%*c%s match_stmt[%d-%d]: %s failed!\n", p->level, ' ', + p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "invalid_match_stmt")); + } _res = NULL; done: D(p->level--); @@ -5009,7 +5034,7 @@ subject_expr_rule(Parser *p) return _res; } -// case_block: "case" patterns guard? ':' block +// case_block: "case" patterns guard? ':' block | invalid_case_block static match_case_ty case_block_rule(Parser *p) { @@ -5056,6 +5081,25 @@ case_block_rule(Parser *p) D(fprintf(stderr, "%*c%s case_block[%d-%d]: %s failed!\n", p->level, ' ', p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "\"case\" patterns guard? ':' block")); } + if (p->call_invalid_rules) { // invalid_case_block + if (p->error_indicator) { + D(p->level--); + return NULL; + } + D(fprintf(stderr, "%*c> case_block[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "invalid_case_block")); + void *invalid_case_block_var; + if ( + (invalid_case_block_var = invalid_case_block_rule(p)) // invalid_case_block + ) + { + D(fprintf(stderr, "%*c+ case_block[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "invalid_case_block")); + _res = invalid_case_block_var; + goto done; + } + p->mark = _mark; + D(fprintf(stderr, "%*c%s case_block[%d-%d]: %s failed!\n", p->level, ' ', + p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "invalid_case_block")); + } _res = NULL; done: D(p->level--); @@ -18531,6 +18575,102 @@ invalid_except_block_rule(Parser *p) return _res; } +// invalid_match_stmt: "match" subject_expr !':' +static void * +invalid_match_stmt_rule(Parser *p) +{ + D(p->level++); + if (p->error_indicator) { + D(p->level--); + return NULL; + } + void * _res = NULL; + int _mark = p->mark; + { // "match" subject_expr !':' + if (p->error_indicator) { + D(p->level--); + return NULL; + } + D(fprintf(stderr, "%*c> invalid_match_stmt[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "\"match\" subject_expr !':'")); + expr_ty _keyword; + expr_ty subject_expr_var; + if ( + (_keyword = _PyPegen_expect_soft_keyword(p, "match")) // soft_keyword='"match"' + && + (subject_expr_var = subject_expr_rule(p)) // subject_expr + && + _PyPegen_lookahead_with_int(0, _PyPegen_expect_token, p, 11) // token=':' + ) + { + D(fprintf(stderr, "%*c+ invalid_match_stmt[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "\"match\" subject_expr !':'")); + _res = CHECK_VERSION ( void * , 10 , "Pattern matching is" , RAISE_SYNTAX_ERROR ( "expected ':'" ) ); + if (_res == NULL && PyErr_Occurred()) { + p->error_indicator = 1; + D(p->level--); + return NULL; + } + goto done; + } + p->mark = _mark; + D(fprintf(stderr, "%*c%s invalid_match_stmt[%d-%d]: %s failed!\n", p->level, ' ', + p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "\"match\" subject_expr !':'")); + } + _res = NULL; + done: + D(p->level--); + return _res; +} + +// invalid_case_block: "case" patterns guard? !':' +static void * +invalid_case_block_rule(Parser *p) +{ + D(p->level++); + if (p->error_indicator) { + D(p->level--); + return NULL; + } + void * _res = NULL; + int _mark = p->mark; + { // "case" patterns guard? !':' + if (p->error_indicator) { + D(p->level--); + return NULL; + } + D(fprintf(stderr, "%*c> invalid_case_block[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "\"case\" patterns guard? !':'")); + expr_ty _keyword; + void *_opt_var; + UNUSED(_opt_var); // Silence compiler warnings + expr_ty patterns_var; + if ( + (_keyword = _PyPegen_expect_soft_keyword(p, "case")) // soft_keyword='"case"' + && + (patterns_var = patterns_rule(p)) // patterns + && + (_opt_var = guard_rule(p), 1) // guard? + && + _PyPegen_lookahead_with_int(0, _PyPegen_expect_token, p, 11) // token=':' + ) + { + D(fprintf(stderr, "%*c+ invalid_case_block[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "\"case\" patterns guard? !':'")); + _res = RAISE_SYNTAX_ERROR ( "expected ':'" ); + if (_res == NULL && PyErr_Occurred()) { + p->error_indicator = 1; + D(p->level--); + return NULL; + } + goto done; + } + p->mark = _mark; + D(fprintf(stderr, "%*c%s invalid_case_block[%d-%d]: %s failed!\n", p->level, ' ', + p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "\"case\" patterns guard? !':'")); + } + _res = NULL; + done: + D(p->level--); + return _res; +} + // _loop0_1: NEWLINE static asdl_seq * _loop0_1_rule(Parser *p)