@@ -1001,14 +1001,21 @@ result_token_with_metadata(Parser *p, void *result, PyObject *metadata)
10011001 return res ;
10021002}
10031003
1004+ static char
1005+ formatted_string_prefix (const Parser * p )
1006+ {
1007+ const ftstring_state * state = _PyLexer_CurrentFTString (p -> tok );
1008+ return state == NULL ? 'f' : _PyLexer_StringPrefix (state -> kind );
1009+ }
1010+
10041011ResultTokenWithMetadata *
10051012_PyPegen_check_fstring_conversion (Parser * p , Token * conv_token , expr_ty conv )
10061013{
10071014 if (conv_token -> lineno != conv -> lineno || conv_token -> end_col_offset != conv -> col_offset ) {
10081015 return RAISE_SYNTAX_ERROR_KNOWN_RANGE (
10091016 conv_token , conv ,
10101017 "%c-string: conversion type must come right after the exclamation mark" ,
1011- TOK_GET_STRING_PREFIX ( p -> tok )
1018+ formatted_string_prefix ( p )
10121019 );
10131020 }
10141021
@@ -1017,7 +1024,7 @@ _PyPegen_check_fstring_conversion(Parser *p, Token* conv_token, expr_ty conv)
10171024 !(first == 's' || first == 'r' || first == 'a' )) {
10181025 RAISE_SYNTAX_ERROR_KNOWN_LOCATION (conv ,
10191026 "%c-string: invalid conversion character %R: expected 's', 'r', or 'a'" ,
1020- TOK_GET_STRING_PREFIX ( p -> tok ),
1027+ formatted_string_prefix ( p ),
10211028 conv -> v .Name .id );
10221029 return NULL ;
10231030 }
@@ -1344,7 +1351,8 @@ _PyPegen_decode_fstring_part(Parser* p, int is_raw, expr_ty constant, Token* tok
13441351}
13451352
13461353static asdl_expr_seq *
1347- _get_resized_exprs (Parser * p , Token * a , asdl_expr_seq * raw_expressions , Token * b , enum string_kind_t string_kind )
1354+ _get_resized_exprs (Parser * p , Token * a , asdl_expr_seq * raw_expressions ,
1355+ Token * b , ftstring_kind string_kind )
13481356{
13491357 Py_ssize_t n_items = asdl_seq_LEN (raw_expressions );
13501358 Py_ssize_t total_items = n_items ;
@@ -1370,15 +1378,13 @@ _get_resized_exprs(Parser *p, Token *a, asdl_expr_seq *raw_expressions, Token *b
13701378 for (Py_ssize_t i = 0 ; i < n_items ; i ++ ) {
13711379 expr_ty item = asdl_seq_GET (raw_expressions , i );
13721380
1373- // This should correspond to a JoinedStr node of two elements
1374- // created _PyPegen_formatted_value. This situation can only be the result of
1375- // a (f|t)-string debug expression where the first element is a constant with the text and the second
1376- // a formatted value with the expression.
1381+ /* Debug expressions arrive as JoinedStr(text, value); flatten them
1382+ into the surrounding string. */
13771383 if (item -> kind == JoinedStr_kind ) {
13781384 asdl_expr_seq * values = item -> v .JoinedStr .values ;
13791385 if (asdl_seq_LEN (values ) != 2 ) {
13801386 PyErr_Format (PyExc_SystemError ,
1381- string_kind == TSTRING
1387+ _PyLexer_IsTString ( string_kind )
13821388 ? "unexpected TemplateStr node without debug data in t-string at line %d"
13831389 : "unexpected JoinedStr node without debug data in f-string at line %d" ,
13841390 item -> lineno );
@@ -1390,7 +1396,9 @@ _get_resized_exprs(Parser *p, Token *a, asdl_expr_seq *raw_expressions, Token *b
13901396 asdl_seq_SET (seq , index ++ , first );
13911397
13921398 expr_ty second = asdl_seq_GET (values , 1 );
1393- assert ((string_kind == TSTRING && second -> kind == Interpolation_kind ) || second -> kind == FormattedValue_kind );
1399+ assert ((_PyLexer_IsTString (string_kind ) &&
1400+ second -> kind == Interpolation_kind ) ||
1401+ second -> kind == FormattedValue_kind );
13941402 asdl_seq_SET (seq , index ++ , second );
13951403
13961404 continue ;
@@ -1460,12 +1468,8 @@ expr_ty _PyPegen_decoded_constant_from_token(Parser* p, Token* tok) {
14601468 return NULL ;
14611469 }
14621470
1463- // Check if we're inside a raw f-string for format spec decoding
1464- int is_raw = 0 ;
1465- if (INSIDE_FSTRING (p -> tok )) {
1466- tokenizer_mode * mode = TOK_GET_MODE (p -> tok );
1467- is_raw = mode -> raw ;
1468- }
1471+ const ftstring_state * state = _PyLexer_CurrentFTString (p -> tok );
1472+ int is_raw = state != NULL && _PyLexer_IsRawString (state -> kind );
14691473
14701474 PyObject * str = _PyPegen_decode_string (p , is_raw , bstr , bsize , tok );
14711475 if (str == NULL ) {
0 commit comments