@@ -2496,41 +2496,42 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
2496
2496
case ')' :
2497
2497
case ']' :
2498
2498
case '}' :
2499
- if (! tok -> level ) {
2500
- if ( INSIDE_FSTRING (tok ) && ! current_tok -> curly_bracket_depth && c == '}' ) {
2501
- return MAKE_TOKEN ( syntaxerror ( tok , "f-string: single '}' is not allowed" ));
2502
- }
2499
+ if (INSIDE_FSTRING ( tok ) && ! current_tok -> curly_bracket_depth && c == '}' ) {
2500
+ return MAKE_TOKEN ( syntaxerror (tok , "f-string: single '}' is not allowed" ));
2501
+ }
2502
+ if (! tok -> tok_extra_tokens && ! tok -> level ) {
2503
2503
return MAKE_TOKEN (syntaxerror (tok , "unmatched '%c'" , c ));
2504
2504
}
2505
- tok -> level -- ;
2506
- int opening = tok -> parenstack [tok -> level ];
2507
- if (!((opening == '(' && c == ')' ) ||
2508
- (opening == '[' && c == ']' ) ||
2509
- (opening == '{' && c == '}' )))
2510
- {
2511
- /* If the opening bracket belongs to an f-string's expression
2512
- part (e.g. f"{)}") and the closing bracket is an arbitrary
2513
- nested expression, then instead of matching a different
2514
- syntactical construct with it; we'll throw an unmatched
2515
- parentheses error. */
2516
- if (INSIDE_FSTRING (tok ) && opening == '{' ) {
2517
- assert (current_tok -> curly_bracket_depth >= 0 );
2518
- int previous_bracket = current_tok -> curly_bracket_depth - 1 ;
2519
- if (previous_bracket == current_tok -> curly_bracket_expr_start_depth ) {
2520
- return MAKE_TOKEN (syntaxerror (tok , "f-string: unmatched '%c'" , c ));
2505
+ if (tok -> level > 0 ) {
2506
+ tok -> level -- ;
2507
+ int opening = tok -> parenstack [tok -> level ];
2508
+ if (!tok -> tok_extra_tokens && !((opening == '(' && c == ')' ) ||
2509
+ (opening == '[' && c == ']' ) ||
2510
+ (opening == '{' && c == '}' ))) {
2511
+ /* If the opening bracket belongs to an f-string's expression
2512
+ part (e.g. f"{)}") and the closing bracket is an arbitrary
2513
+ nested expression, then instead of matching a different
2514
+ syntactical construct with it; we'll throw an unmatched
2515
+ parentheses error. */
2516
+ if (INSIDE_FSTRING (tok ) && opening == '{' ) {
2517
+ assert (current_tok -> curly_bracket_depth >= 0 );
2518
+ int previous_bracket = current_tok -> curly_bracket_depth - 1 ;
2519
+ if (previous_bracket == current_tok -> curly_bracket_expr_start_depth ) {
2520
+ return MAKE_TOKEN (syntaxerror (tok , "f-string: unmatched '%c'" , c ));
2521
+ }
2522
+ }
2523
+ if (tok -> parenlinenostack [tok -> level ] != tok -> lineno ) {
2524
+ return MAKE_TOKEN (syntaxerror (tok ,
2525
+ "closing parenthesis '%c' does not match "
2526
+ "opening parenthesis '%c' on line %d" ,
2527
+ c , opening , tok -> parenlinenostack [tok -> level ]));
2528
+ }
2529
+ else {
2530
+ return MAKE_TOKEN (syntaxerror (tok ,
2531
+ "closing parenthesis '%c' does not match "
2532
+ "opening parenthesis '%c'" ,
2533
+ c , opening ));
2521
2534
}
2522
- }
2523
- if (tok -> parenlinenostack [tok -> level ] != tok -> lineno ) {
2524
- return MAKE_TOKEN (syntaxerror (tok ,
2525
- "closing parenthesis '%c' does not match "
2526
- "opening parenthesis '%c' on line %d" ,
2527
- c , opening , tok -> parenlinenostack [tok -> level ]));
2528
- }
2529
- else {
2530
- return MAKE_TOKEN (syntaxerror (tok ,
2531
- "closing parenthesis '%c' does not match "
2532
- "opening parenthesis '%c'" ,
2533
- c , opening ));
2534
2535
}
2535
2536
}
2536
2537
0 commit comments