@@ -38,6 +38,7 @@ static inline tokenizer_mode* TOK_NEXT_MODE(struct tok_state* tok) {
3838#define TOK_NEXT_MODE (tok ) (&(tok->tok_mode_stack[++tok->tok_mode_stack_index]))
3939#endif
4040
41+ #define TOK_GET_STRING_PREFIX (tok ) (TOK_GET_MODE(tok)->string_kind == TSTRING ? 't' : 'f')
4142#define MAKE_TOKEN (token_type ) _PyLexer_token_setup(tok, token, token_type, p_start, p_end)
4243#define MAKE_TYPE_COMMENT_TOKEN (token_type , col_offset , end_col_offset ) (\
4344 _PyLexer_type_comment_token_setup(tok, token, token_type, col_offset, end_col_offset, p_start, p_end))
@@ -113,7 +114,7 @@ set_ftstring_expr(struct tok_state* tok, struct token *token, char c) {
113114 assert (c == '}' || c == ':' || c == '!' );
114115 tokenizer_mode * tok_mode = TOK_GET_MODE (tok );
115116
116- if (!(tok_mode -> f_string_debug || tok_mode -> tstring ) || token -> metadata ) {
117+ if (!(tok_mode -> f_string_debug || tok_mode -> string_kind == TSTRING ) || token -> metadata ) {
117118 return 0 ;
118119 }
119120 PyObject * res = NULL ;
@@ -993,12 +994,12 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
993994 the_current_tok -> in_format_spec = 0 ;
994995 the_current_tok -> f_string_debug = 0 ;
995996
996- int tstring = 0 ;
997+ enum string_kind_t string_kind = FSTRING ;
997998 switch (* tok -> start ) {
998999 case 'T' :
9991000 case 't' :
10001001 the_current_tok -> f_string_raw = Py_TOLOWER (* (tok -> start + 1 )) == 'r' ;
1001- tstring = 1 ;
1002+ string_kind = TSTRING ;
10021003 break ;
10031004 case 'F' :
10041005 case 'f' :
@@ -1007,16 +1008,18 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
10071008 case 'R' :
10081009 case 'r' :
10091010 the_current_tok -> f_string_raw = 1 ;
1010- tstring = Py_TOLOWER (* (tok -> start + 1 )) == 't' ;
1011+ if (Py_TOLOWER (* (tok -> start + 1 )) == 't' ) {
1012+ string_kind = TSTRING ;
1013+ }
10111014 break ;
10121015 default :
10131016 Py_UNREACHABLE ();
10141017 }
10151018
1016- the_current_tok -> tstring = tstring ;
1019+ the_current_tok -> string_kind = TSTRING ;
10171020 the_current_tok -> curly_bracket_depth = 0 ;
10181021 the_current_tok -> curly_bracket_expr_start_depth = -1 ;
1019- return tstring ? MAKE_TOKEN (TSTRING_START ) : MAKE_TOKEN (FSTRING_START );
1022+ return string_kind == TSTRING ? MAKE_TOKEN (TSTRING_START ) : MAKE_TOKEN (FSTRING_START );
10201023 }
10211024
10221025 letter_quote :
@@ -1079,7 +1082,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
10791082 if (the_current_tok -> f_string_quote == quote &&
10801083 the_current_tok -> f_string_quote_size == quote_size ) {
10811084 return MAKE_TOKEN (_PyTokenizer_syntaxerror (tok ,
1082- "%c-string: expecting '}'" , TOK_GET_MODE (tok )-> tstring ? 't' : 'f' ));
1085+ "%c-string: expecting '}'" , TOK_GET_STRING_PREFIX (tok )));
10831086 }
10841087 }
10851088
@@ -1209,7 +1212,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
12091212 case '}' :
12101213 if (INSIDE_FSTRING (tok ) && !current_tok -> curly_bracket_depth && c == '}' ) {
12111214 return MAKE_TOKEN (_PyTokenizer_syntaxerror (tok ,
1212- "%c-string: single '}' is not allowed" , TOK_GET_MODE (tok )-> tstring ? 't' : 'f' ));
1215+ "%c-string: single '}' is not allowed" , TOK_GET_STRING_PREFIX (tok )));
12131216 }
12141217 if (!tok -> tok_extra_tokens && !tok -> level ) {
12151218 return MAKE_TOKEN (_PyTokenizer_syntaxerror (tok , "unmatched '%c'" , c ));
@@ -1230,7 +1233,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
12301233 int previous_bracket = current_tok -> curly_bracket_depth - 1 ;
12311234 if (previous_bracket == current_tok -> curly_bracket_expr_start_depth ) {
12321235 return MAKE_TOKEN (_PyTokenizer_syntaxerror (tok ,
1233- "%c-string: unmatched '%c'" , TOK_GET_MODE (tok )-> tstring ? 't' : 'f' , c ));
1236+ "%c-string: unmatched '%c'" , TOK_GET_STRING_PREFIX (tok ), c ));
12341237 }
12351238 }
12361239 if (tok -> parenlinenostack [tok -> level ] != tok -> lineno ) {
@@ -1252,7 +1255,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
12521255 current_tok -> curly_bracket_depth -- ;
12531256 if (current_tok -> curly_bracket_depth < 0 ) {
12541257 return MAKE_TOKEN (_PyTokenizer_syntaxerror (tok , "%c-string: unmatched '%c'" ,
1255- TOK_GET_MODE (tok )-> tstring ? 't' : 'f' , c ));
1258+ TOK_GET_STRING_PREFIX (tok ), c ));
12561259 }
12571260 if (c == '}' && current_tok -> curly_bracket_depth == current_tok -> curly_bracket_expr_start_depth ) {
12581261 current_tok -> curly_bracket_expr_start_depth -- ;
@@ -1303,7 +1306,7 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
13031306 current_tok -> curly_bracket_expr_start_depth ++ ;
13041307 if (current_tok -> curly_bracket_expr_start_depth >= MAX_EXPR_NESTING ) {
13051308 return MAKE_TOKEN (_PyTokenizer_syntaxerror (tok ,
1306- "%c-string: expressions nested too deeply" , TOK_GET_MODE (tok )-> tstring ? 't' : 'f' ));
1309+ "%c-string: expressions nested too deeply" , TOK_GET_STRING_PREFIX (tok )));
13071310 }
13081311 TOK_GET_MODE (tok )-> kind = TOK_REGULAR_MODE ;
13091312 return tok_get_normal_mode (tok , current_tok , token );
@@ -1383,7 +1386,7 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
13831386 _PyTokenizer_syntaxerror (tok ,
13841387 "unterminated triple-quoted %c-string literal"
13851388 " (detected at line %d)" ,
1386- TOK_GET_MODE (tok )-> tstring ? 't' : 'f' , start );
1389+ TOK_GET_STRING_PREFIX (tok ), start );
13871390 if (c != '\n' ) {
13881391 tok -> done = E_EOFS ;
13891392 }
@@ -1392,7 +1395,7 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
13921395 else {
13931396 return MAKE_TOKEN (_PyTokenizer_syntaxerror (tok ,
13941397 "unterminated %c-string literal (detected at"
1395- " line %d)" , TOK_GET_MODE (tok )-> tstring ? 't' : 'f' , start ));
1398+ " line %d)" , TOK_GET_STRING_PREFIX (tok ), start ));
13961399 }
13971400 }
13981401
@@ -1414,7 +1417,7 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
14141417 current_tok -> curly_bracket_expr_start_depth ++ ;
14151418 if (current_tok -> curly_bracket_expr_start_depth >= MAX_EXPR_NESTING ) {
14161419 return MAKE_TOKEN (_PyTokenizer_syntaxerror (tok ,
1417- "%c-string: expressions nested too deeply" , TOK_GET_MODE (tok )-> tstring ? 't' : 'f' ));
1420+ "%c-string: expressions nested too deeply" , TOK_GET_STRING_PREFIX (tok )));
14181421 }
14191422 TOK_GET_MODE (tok )-> kind = TOK_REGULAR_MODE ;
14201423 current_tok -> in_format_spec = 0 ;
0 commit comments