Coverage Report

Created: 2026-12-01 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Parser/lexer/string.c
Line
Count
Source
1
#include "Python.h"
2
#include "pycore_token.h"
3
#include "errcode.h"
4
5
#include "lexer_internal.h"
6
#include "../tokenizer/helpers.h"
7
8
108k
#define MAKE_TOKEN(token_type) _PyLexer_token_setup(tok, token, token_type, p_start, p_end)
9
10
static int
11
string_error_token(struct tok_state *tok, struct token *token,
12
                   _PyTok_Off start, _PyTok_Loc location)
13
687
{
14
687
    tok->diagnostic = (_PyTokenizer_Diagnostic){
15
687
        .location = {location.lineno, location.byte_col + 1},
16
687
        .text_span = {start - location.byte_col, tok->inp},
17
687
    };
18
687
    int type = _PyLexer_token_setup(tok, token, ERRORTOKEN, -1, -1);
19
687
    token->start_loc = location;
20
687
    token->end_loc = (_PyTok_Loc){location.lineno, -1};
21
687
    return type;
22
687
}
23
24
static int
25
incomplete_string_token(struct tok_state *tok, struct token *token,
26
                        int type, _PyTok_Off start, _PyTok_Loc location)
27
0
{
28
0
    string_error_token(tok, token, start, location);
29
0
    tok->done = E_INCOMPLETE;
30
0
    token->is_incomplete = 1;
31
0
    return type;
32
0
}
33
34
int
35
_PyLexer_record_ftstring_comment(struct tok_state *tok, ftstring_state *state,
36
                                 _PyTok_Off start, _PyTok_Off end)
37
2.41k
{
38
2.41k
    assert(state == _PyLexer_CurrentFTString(tok) && state->mode == FTSTRING_MODE_EXPRESSION);
39
2.41k
    if (state->expr_span.end >= 0) {
40
211
        return 0;
41
211
    }
42
2.41k
    assert(state->expr_span.start >= 0);
43
2.20k
    tokenizer_comments *comments = state->comments;
44
2.20k
    if (comments == NULL || comments->count == comments->capacity) {
45
658
        Py_ssize_t max_capacity = (PY_SSIZE_T_MAX -
46
658
            (Py_ssize_t)sizeof(*comments)) /
47
658
            (Py_ssize_t)sizeof(*comments->spans);
48
658
        if (comments != NULL && comments->capacity > max_capacity / 2) {
49
0
            PyErr_NoMemory();
50
0
            return -1;
51
0
        }
52
658
        Py_ssize_t capacity = comments == NULL ? 4 : comments->capacity * 2;
53
658
        size_t size = sizeof(*comments) +
54
658
            (size_t)capacity * sizeof(*comments->spans);
55
658
        tokenizer_comments *resized = PyMem_Realloc(comments, size);
56
658
        if (resized == NULL) {
57
0
            PyErr_NoMemory();
58
0
            return -1;
59
0
        }
60
658
        comments = resized;
61
658
        if (state->comments == NULL) {
62
416
            comments->count = 0;
63
416
        }
64
658
        comments->capacity = capacity;
65
658
        state->comments = comments;
66
658
    }
67
2.20k
    comments->spans[comments->count++] =
68
2.20k
        (_PyTok_Span){start, end};
69
2.20k
    return 0;
70
2.20k
}
71
72
static int
73
finish_ftstring_expr(struct tok_state *tok, ftstring_state *state,
74
                     struct token *token)
75
34.1k
{
76
34.1k
    assert(token != NULL && state == _PyLexer_CurrentFTString(tok));
77
34.1k
    assert(state->mode == FTSTRING_MODE_EXPRESSION && tok->start >= 0);
78
79
34.1k
    if (state->expr_span.end >= 0) {
80
6.07k
        return 0;
81
6.07k
    }
82
34.1k
    assert(state->expr_span.start >= 0);
83
28.0k
    state->expr_span.end = tok->start;
84
28.0k
    int tstring_interpolation = _PyLexer_IsTString(state->kind) &&
85
7.25k
        state->replacement_depth == 1;
86
28.0k
    if (!(state->debug_expr || tstring_interpolation) || token->metadata) {
87
13.9k
        return 0;
88
13.9k
    }
89
14.0k
    tokenizer_comments *comments = state->comments;
90
14.0k
    PyObject *res;
91
14.0k
    if (comments != NULL && comments->count > 0) {
92
223
        Py_ssize_t stripped_size =
93
223
            state->expr_span.end - state->expr_span.start;
94
223
        Py_ssize_t comment_count = 0;
95
720
        for (Py_ssize_t i = 0; i < comments->count; i++) {
96
497
            _PyTok_Span comment = comments->spans[i];
97
497
            assert(_PyTok_SpanIsValid(comment));
98
497
            assert(comment.start >= (i == 0 ? state->expr_span.start
99
497
                                           : comments->spans[i - 1].end));
100
497
            if (comment.start >= state->expr_span.end) {
101
0
                break;
102
0
            }
103
497
            assert(comment.end <= state->expr_span.end);
104
497
            stripped_size -= comment.end - comment.start;
105
497
            comment_count++;
106
497
        }
107
223
        char *stripped = PyMem_Malloc((size_t)stripped_size);
108
223
        if (stripped == NULL) {
109
0
            PyErr_NoMemory();
110
0
            return -1;
111
0
        }
112
223
        _PyTok_Span kept = {state->expr_span.start, state->expr_span.start};
113
223
        Py_ssize_t stripped_len = 0;
114
720
        for (Py_ssize_t i = 0; i < comment_count; i++) {
115
497
            kept.end = comments->spans[i].start;
116
497
            Py_ssize_t length;
117
497
            const char *text = _PyTok_SourceSpanView(
118
497
                &tok->source, kept, &length);
119
497
            memcpy(stripped + stripped_len, text, (size_t)length);
120
497
            stripped_len += length;
121
497
            kept.start = comments->spans[i].end;
122
497
        }
123
223
        kept.end = state->expr_span.end;
124
223
        Py_ssize_t length;
125
223
        const char *text = _PyTok_SourceSpanView(&tok->source, kept, &length);
126
223
        memcpy(stripped + stripped_len, text, (size_t)length);
127
223
        res = PyUnicode_DecodeUTF8(stripped, stripped_size, NULL);
128
223
        PyMem_Free(stripped);
129
223
    }
130
13.8k
    else {
131
13.8k
        Py_ssize_t expr_len;
132
13.8k
        const char *expr = _PyTok_SourceSpanView(
133
13.8k
            &tok->source, state->expr_span, &expr_len);
134
13.8k
        res = PyUnicode_DecodeUTF8(expr, expr_len, NULL);
135
13.8k
    }
136
137
14.0k
    if (!res) {
138
0
        return -1;
139
0
    }
140
14.0k
    token->metadata = res;
141
14.0k
    return 0;
142
14.0k
}
143
144
static int
145
begin_ftstring_expr(struct tok_state *tok, ftstring_state *state,
146
                    _PyTok_Off expr_start)
147
34.5k
{
148
34.5k
    assert(state->mode != FTSTRING_MODE_EXPRESSION);
149
34.5k
    state->expr_span = (_PyTok_Span){expr_start, -1};
150
34.5k
    if (state->comments != NULL) {
151
701
        state->comments->count = 0;
152
701
    }
153
34.5k
    if (state->replacement_depth >= MAX_EXPR_NESTING) {
154
5
        _PyTokenizer_syntaxerror(
155
5
            tok, "%c-string: expressions nested too deeply",
156
5
            _PyLexer_StringPrefix(state->kind));
157
5
        return -1;
158
5
    }
159
34.5k
    state->replacement_depth++;
160
34.5k
    state->mode = FTSTRING_MODE_EXPRESSION;
161
34.5k
    state->debug_expr = 0;
162
34.5k
    return 0;
163
34.5k
}
164
165
int
166
_PyLexer_ftstring_punctuation(struct tok_state *tok, ftstring_state *state,
167
                              struct token *token, int c)
168
35.3k
{
169
35.3k
    assert(state->mode == FTSTRING_MODE_EXPRESSION);
170
35.3k
    assert(c == ':' || c == '}' || c == '!');
171
35.3k
    if (_PyLexer_FTStringBracketDepth(tok, state) != state->replacement_depth) {
172
1.00k
        return 0;
173
1.00k
    }
174
34.3k
    if (c == '!') {
175
2.41k
        int next = tok_nextc(tok);
176
2.41k
        tok_backup(tok, next);
177
2.41k
        if (next == '=') {
178
197
            return 0;
179
197
        }
180
2.41k
    }
181
34.1k
    if (finish_ftstring_expr(tok, state, token) < 0) {
182
0
        return -1;
183
0
    }
184
34.1k
    if (c == ':') {
185
5.39k
        state->mode = FTSTRING_MODE_FORMAT_SPEC;
186
5.39k
        return COLON;
187
5.39k
    }
188
28.7k
    return 0;
189
34.1k
}
190
191
int
192
_PyLexer_close_ftstring_expr(struct tok_state *tok, ftstring_state *state,
193
                             int c)
194
27.6k
{
195
27.6k
    assert(state->mode == FTSTRING_MODE_EXPRESSION);
196
27.6k
    assert(c == ')' || c == ']' || c == '}');
197
27.6k
    int depth = _PyLexer_FTStringBracketDepth(tok, state);
198
27.6k
    if (depth < 0) {
199
0
        _PyTokenizer_syntaxerror(tok, "%c-string: unmatched '%c'",
200
0
                                _PyLexer_StringPrefix(state->kind), c);
201
0
        return -1;
202
0
    }
203
27.6k
    if (c == '}' && depth == state->replacement_depth - 1) {
204
26.5k
        state->replacement_depth--;
205
26.5k
        state->mode = FTSTRING_MODE_MIDDLE;
206
26.5k
        state->debug_expr = 0;
207
26.5k
    }
208
27.6k
    return 0;
209
27.6k
}
210
211
void
212
_PyLexer_mark_ftstring_debug(struct tok_state *tok, ftstring_state *state)
213
9.58k
{
214
9.58k
    assert(state->mode == FTSTRING_MODE_EXPRESSION);
215
9.58k
    if (_PyLexer_FTStringBracketDepth(tok, state) == state->replacement_depth) {
216
9.27k
        state->debug_expr = 1;
217
9.27k
    }
218
9.58k
}
219
220
int
221
_PyLexer_check_string_prefixes(struct tok_state *tok,
222
                                             int saw_b, int saw_r, int saw_u,
223
28.0k
                                             int saw_f, int saw_t) {
224
    // Supported: rb, rf, rt (in any order)
225
    // Unsupported: ub, ur, uf, ut, bf, bt, ft (in any order)
226
227
28.0k
#define RETURN_SYNTAX_ERROR(PREFIX1, PREFIX2)                             \
228
28.0k
    do {                                                                  \
229
8
        (void)_PyTokenizer_syntaxerror_known_range(                       \
230
8
            tok, (int)(tok->start + 1 - tok->line_start),                 \
231
8
            (int)(tok->cur - tok->line_start),                            \
232
8
            "'" PREFIX1 "' and '" PREFIX2 "' prefixes are incompatible"); \
233
8
        return -1;                                                        \
234
8
    } while (0)
235
236
28.0k
    if (saw_u && saw_b) {
237
1
        RETURN_SYNTAX_ERROR("u", "b");
238
1
    }
239
28.0k
    if (saw_u && saw_r) {
240
1
        RETURN_SYNTAX_ERROR("u", "r");
241
1
    }
242
28.0k
    if (saw_u && saw_f) {
243
1
        RETURN_SYNTAX_ERROR("u", "f");
244
1
    }
245
28.0k
    if (saw_u && saw_t) {
246
1
        RETURN_SYNTAX_ERROR("u", "t");
247
1
    }
248
249
28.0k
    if (saw_b && saw_f) {
250
1
        RETURN_SYNTAX_ERROR("b", "f");
251
1
    }
252
28.0k
    if (saw_b && saw_t) {
253
1
        RETURN_SYNTAX_ERROR("b", "t");
254
1
    }
255
256
28.0k
    if (saw_f && saw_t) {
257
2
        RETURN_SYNTAX_ERROR("f", "t");
258
2
    }
259
260
28.0k
#undef RETURN_SYNTAX_ERROR
261
262
28.0k
    return 0;
263
28.0k
}
264
265
int
266
_PyLexer_scan_fstring_start(struct tok_state *tok, struct token *token,
267
                            int c, ftstring_kind kind)
268
22.9k
{
269
22.9k
    _PyTok_Off p_start = -1;
270
22.9k
    _PyTok_Off p_end = -1;
271
272
22.9k
    int quote = c;
273
22.9k
    int quote_size = 1;             /* 1 or 3 */
274
275
    /* Find the quote size and start of string */
276
22.9k
    int after_quote = tok_nextc(tok);
277
22.9k
    if (after_quote == quote) {
278
3.20k
        int after_after_quote = tok_nextc(tok);
279
3.20k
        if (after_after_quote == quote) {
280
871
            quote_size = 3;
281
871
        }
282
2.33k
        else {
283
            // TODO: Check this
284
2.33k
            tok_backup(tok, after_after_quote);
285
2.33k
            tok_backup(tok, after_quote);
286
2.33k
        }
287
3.20k
    }
288
22.9k
    if (after_quote != quote) {
289
19.7k
        tok_backup(tok, after_quote);
290
19.7k
    }
291
292
22.9k
    p_start = tok->start;
293
22.9k
    p_end = tok->cur;
294
22.9k
    ftstring_state *state = _PyLexer_PushFTString(tok);
295
22.9k
    if (state == NULL) {
296
1
        return MAKE_TOKEN(ERRORTOKEN);
297
1
    }
298
22.9k
    state->mode = FTSTRING_MODE_MIDDLE;
299
22.9k
    state->quote = quote;
300
22.9k
    state->quote_size = quote_size;
301
22.9k
    state->paren_level = tok->level;
302
22.9k
    state->start = tok->start;
303
22.9k
    state->start_loc = tok->start_loc;
304
22.9k
    state->expr_span = (_PyTok_Span){-1, -1};
305
306
22.9k
    state->kind = kind;
307
22.9k
    return _PyLexer_IsTString(kind)
308
22.9k
        ? MAKE_TOKEN(TSTRING_START) : MAKE_TOKEN(FSTRING_START);
309
22.9k
}
310
311
int
312
_PyLexer_scan_string(struct tok_state *tok, struct token *token, int c)
313
38.7k
{
314
38.7k
    _PyTok_Off p_start = -1;
315
38.7k
    _PyTok_Off p_end = -1;
316
317
38.7k
    int quote = c;
318
38.7k
    int quote_size = 1;             /* 1 or 3 */
319
38.7k
    int end_quote_size = 0;
320
38.7k
    int has_escaped_quote = 0;
321
322
    /* Find the quote size and start of string */
323
38.7k
    c = tok_nextc(tok);
324
38.7k
    if (c == quote) {
325
7.71k
        c = tok_nextc(tok);
326
7.71k
        if (c == quote) {
327
755
            quote_size = 3;
328
755
        }
329
6.95k
        else {
330
6.95k
            end_quote_size = 1;     /* empty string found */
331
6.95k
        }
332
7.71k
    }
333
38.7k
    if (c != quote) {
334
37.9k
        tok_backup(tok, c);
335
37.9k
    }
336
337
    /* Get rest of string */
338
383k
    while (end_quote_size != quote_size) {
339
345k
        c = tok_nextc(tok);
340
345k
        if (tok->done == E_ERROR) {
341
1
            return MAKE_TOKEN(ERRORTOKEN);
342
1
        }
343
345k
        if (tok->done == E_DECODE) {
344
0
            break;
345
0
        }
346
345k
        if (c == EOF || (quote_size == 1 && c == '\n')) {
347
390
            if (tok_failed(tok)) {
348
0
                return MAKE_TOKEN(ERRORTOKEN);
349
0
            }
350
390
            if (c == EOF && tok->allow_incomplete_input) {
351
0
                return incomplete_string_token(tok, token, STRING,
352
0
                                               tok->start, tok->start_loc);
353
0
            }
354
390
            int end_lineno = tok->lineno;
355
390
            _PyTok_Loc location = tok->start_loc;
356
390
            const char *line = _PyTok_SourcePointer(
357
390
                &tok->source, tok->start - location.byte_col);
358
390
            Py_ssize_t cursor_offset = (Py_ssize_t)location.byte_col + 1;
359
360
390
            const ftstring_state *state = _PyLexer_CurrentFTString(tok);
361
390
            if (state != NULL) {
362
                /* A matching quote may have been intended to close the
363
                 * surrounding formatted string instead of opening a string
364
                 * inside a replacement field. */
365
41
                if (state->quote == quote && state->quote_size == quote_size) {
366
29
                    int level = state->paren_level + state->replacement_depth - 1;
367
29
                    assert(level >= 0 && level < tok->level);
368
29
                    assert(tok->parenstack[level] == '{');
369
29
                    int lineno = tok->parenlinenostack[level];
370
29
                    if (lineno != location.lineno) {
371
4
                        _PyTokenizer_syntaxerror_at(
372
4
                            tok, line, cursor_offset, location.lineno, -1, -1,
373
4
                            "%c-string: expecting '}' to close '{' on line %d",
374
4
                            _PyLexer_StringPrefix(state->kind), lineno);
375
4
                    }
376
25
                    else {
377
25
                        _PyTokenizer_syntaxerror_at(
378
25
                            tok, line, cursor_offset, location.lineno, -1, -1,
379
25
                            "%c-string: expecting '}'",
380
25
                            _PyLexer_StringPrefix(state->kind));
381
25
                    }
382
29
                    return string_error_token(tok, token, tok->start, location);
383
29
                }
384
41
            }
385
386
361
            if (quote_size == 3) {
387
26
                _PyTokenizer_syntaxerror_at(
388
26
                    tok, line, cursor_offset, location.lineno, -1, -1,
389
26
                    "unterminated triple-quoted string literal"
390
26
                    " (detected at line %d)", end_lineno);
391
26
                if (c != '\n') {
392
26
                    tok->done = E_EOFS;
393
26
                }
394
26
                return string_error_token(tok, token, tok->start, location);
395
26
            }
396
335
            else {
397
335
                if (has_escaped_quote) {
398
9
                    _PyTokenizer_syntaxerror_at(
399
9
                        tok, line, cursor_offset, location.lineno, -1, -1,
400
9
                        "unterminated string literal (detected at line %d); "
401
9
                        "perhaps you escaped the end quote?",
402
9
                        end_lineno
403
9
                    );
404
326
                } else {
405
326
                    _PyTokenizer_syntaxerror_at(
406
326
                        tok, line, cursor_offset, location.lineno, -1, -1,
407
326
                        "unterminated string literal (detected at line %d)", end_lineno
408
326
                    );
409
326
                }
410
335
                if (c != '\n') {
411
9
                    tok->done = E_EOLS;
412
9
                }
413
335
                return string_error_token(tok, token, tok->start, location);
414
335
            }
415
361
        }
416
345k
        if (c == quote) {
417
33.2k
            end_quote_size += 1;
418
33.2k
        }
419
311k
        else {
420
311k
            end_quote_size = 0;
421
311k
            if (c == '\\') {
422
32.6k
                c = tok_nextc(tok);  /* skip escaped char */
423
32.6k
                if (c == quote) {  /* but record whether the escaped char was a quote */
424
1.21k
                    has_escaped_quote = 1;
425
1.21k
                }
426
32.6k
                if (c == '\r') {
427
0
                    c = tok_nextc(tok);
428
0
                }
429
32.6k
            }
430
311k
        }
431
345k
    }
432
433
38.3k
    p_start = tok->start;
434
38.3k
    p_end = tok->cur;
435
38.3k
    return MAKE_TOKEN(STRING);
436
38.7k
}
437
438
int
439
_PyLexer_get_ftstring(struct tok_state *tok, ftstring_state *current, struct token *token)
440
65.7k
{
441
65.7k
    assert(current == _PyLexer_CurrentFTString(tok) && current->mode != FTSTRING_MODE_EXPRESSION);
442
65.7k
    assert((current->quote_size == 1 || current->quote_size == 3) &&
443
65.7k
           current->replacement_depth <= MAX_EXPR_NESTING);
444
65.7k
    _PyTok_Off p_start = -1;
445
65.7k
    _PyTok_Off p_end = -1;
446
65.7k
    int token_type;
447
65.7k
    int end_quote_size = 0;
448
65.7k
    int unicode_escape = 0;
449
65.7k
    int quote = current->quote;
450
65.7k
    int quote_size = current->quote_size;
451
65.7k
    int in_format_spec = current->mode == FTSTRING_MODE_FORMAT_SPEC;
452
65.7k
    int raw = _PyLexer_IsRawString(current->kind);
453
454
65.7k
    tok->start = tok->cur;
455
65.7k
    tok->start_loc = (_PyTok_Loc){tok->lineno, _PyLexer_ByteColumn(tok)};
456
457
266k
    while (end_quote_size != quote_size) {
458
244k
        int c = tok_nextc(tok);
459
244k
        if (tok->done == E_ERROR || tok->done == E_DECODE) {
460
0
            return MAKE_TOKEN(ERRORTOKEN);
461
0
        }
462
463
244k
        if (c == EOF || (quote_size == 1 && c == '\n')) {
464
346
            if (tok_failed(tok)) {
465
0
                return MAKE_TOKEN(ERRORTOKEN);
466
0
            }
467
346
            if (c == EOF && tok->allow_incomplete_input) {
468
0
                int type = incomplete_string_token(tok, token,
469
0
                    FTSTRING_MIDDLE(current), current->start, current->start_loc);
470
                // Preserve the actual prefix for incremental escape decoding.
471
0
                _PyTok_Off end = tok->cur;
472
0
                if (end > tok->start && tok->implicit_newline) {
473
0
                    end--;
474
0
                }
475
0
                token->span = (_PyTok_Span){tok->start, end};
476
0
                token->start_loc = tok->start_loc;
477
0
                token->end_loc = (_PyTok_Loc){tok->lineno, (int)(end - tok->line_start)};
478
0
                token->is_raw = raw;
479
0
                return type;
480
0
            }
481
482
346
            if (in_format_spec && c == '\n') {
483
49
                return MAKE_TOKEN(_PyTokenizer_syntaxerror(
484
49
                    tok,
485
49
                    "%c-string: newlines are not allowed in format specifiers for single quoted %c-strings",
486
49
                    _PyLexer_StringPrefix(current->kind),
487
49
                    _PyLexer_StringPrefix(current->kind)));
488
49
            }
489
490
297
            int end_lineno = tok->lineno;
491
297
            _PyTok_Loc location = current->start_loc;
492
297
            const char *line = _PyTok_SourcePointer(
493
297
                &tok->source, current->start - location.byte_col);
494
297
            Py_ssize_t cursor_offset = (Py_ssize_t)location.byte_col + 1;
495
496
297
            if (quote_size == 3) {
497
20
                _PyTokenizer_syntaxerror_at(
498
20
                    tok, line, cursor_offset, location.lineno, -1, -1,
499
20
                    "unterminated triple-quoted %c-string literal"
500
20
                    " (detected at line %d)",
501
20
                    _PyLexer_StringPrefix(current->kind), end_lineno);
502
20
                if (c != '\n') {
503
20
                    tok->done = E_EOFS;
504
20
                }
505
20
                return string_error_token(tok, token,
506
20
                    current->start, location);
507
20
            }
508
277
            else {
509
277
                _PyTokenizer_syntaxerror_at(
510
277
                    tok, line, cursor_offset, location.lineno, -1, -1,
511
277
                    "unterminated %c-string literal (detected at line %d)",
512
277
                    _PyLexer_StringPrefix(current->kind), end_lineno);
513
277
                return string_error_token(tok, token,
514
277
                    current->start, location);
515
277
            }
516
297
        }
517
518
244k
        if (c == quote) {
519
30.1k
            end_quote_size += 1;
520
30.1k
            continue;
521
214k
        } else {
522
214k
            end_quote_size = 0;
523
214k
        }
524
525
214k
        if (c == '{') {
526
38.4k
            int peek = tok_nextc(tok);
527
38.4k
            if (peek != '{' || in_format_spec) {
528
34.5k
                tok_backup(tok, peek);
529
34.5k
                _PyTok_Off expr_start = tok->cur;
530
34.5k
                tok_backup(tok, c);
531
34.5k
                if (begin_ftstring_expr(tok, current, expr_start) < 0) {
532
5
                    return MAKE_TOKEN(ERRORTOKEN);
533
5
                }
534
34.5k
                p_start = tok->start;
535
34.5k
                p_end = tok->cur;
536
34.5k
                if (p_start == p_end) {
537
18.5k
                    return _PyLexer_get_normal(tok, current, token);
538
18.5k
                }
539
34.5k
            } else {
540
3.89k
                p_start = tok->start;
541
3.89k
                p_end = tok->cur - 1;
542
3.89k
            }
543
19.8k
            goto emit_middle;
544
175k
        } else if (c == '}') {
545
5.20k
            if (unicode_escape) {
546
249
                p_start = tok->start;
547
249
                p_end = tok->cur;
548
249
                goto emit_middle;
549
249
            }
550
4.95k
            int peek = tok_nextc(tok);
551
552
4.95k
            int bracket_depth = _PyLexer_FTStringBracketDepth(tok, current);
553
4.95k
            if (peek == '}' && !in_format_spec && bracket_depth == 0) {
554
919
                p_start = tok->start;
555
919
                p_end = tok->cur - 1;
556
919
            }
557
4.03k
            else {
558
4.03k
                tok_backup(tok, peek);
559
4.03k
                if (!in_format_spec && bracket_depth == 0) {
560
58
                    if (tok->start == tok->cur - 1) {
561
38
                        return MAKE_TOKEN(_PyTokenizer_syntaxerror(
562
38
                            tok, "%c-string: single '}' is not allowed",
563
38
                            _PyLexer_StringPrefix(current->kind)));
564
38
                    }
565
20
                    tok_backup(tok, c);
566
20
                    p_start = tok->start;
567
20
                    p_end = tok->cur;
568
20
                    goto emit_middle;
569
58
                }
570
3.97k
                tok_backup(tok, c);
571
3.97k
                current->mode = FTSTRING_MODE_EXPRESSION;
572
3.97k
                p_start = tok->start;
573
3.97k
                p_end = tok->cur;
574
3.97k
            }
575
4.89k
            goto emit_middle;
576
170k
        } else if (c == '\\') {
577
5.88k
            int peek = tok_nextc(tok);
578
5.88k
            if (peek == '\r') {
579
0
                peek = tok_nextc(tok);
580
0
            }
581
5.88k
            if (peek == '{' || peek == '}') {
582
1.70k
                if (!raw) {
583
1.45k
                    if (_PyTokenizer_warn_invalid_escape_sequence(tok, peek)) {
584
0
                        return MAKE_TOKEN(ERRORTOKEN);
585
0
                    }
586
1.45k
                }
587
1.70k
                tok_backup(tok, peek);
588
1.70k
                continue;
589
1.70k
            }
590
591
4.17k
            if (!raw) {
592
3.94k
                if (peek == 'N') {
593
                    /* Handle named unicode escapes (\N{BULLET}) */
594
496
                    peek = tok_nextc(tok);
595
496
                    if (peek == '{') {
596
292
                        unicode_escape = 1;
597
292
                    } else {
598
204
                        tok_backup(tok, peek);
599
204
                    }
600
496
                }
601
3.94k
            }
602
4.17k
        }
603
214k
    }
604
605
21.7k
    p_start = tok->start;
606
21.7k
    p_end = tok->cur;
607
21.7k
    if (p_end - quote_size == p_start) {
608
15.9k
        int end_token = FTSTRING_END(current);
609
15.9k
        _PyLexer_PopFTString(tok);
610
15.9k
        return MAKE_TOKEN(end_token);
611
15.9k
    }
612
12.8k
    for (int i = 0; i < quote_size; i++) {
613
7.00k
        tok_backup(tok, quote);
614
7.00k
    }
615
5.85k
    p_end = tok->cur;
616
30.8k
emit_middle:
617
30.8k
    token_type = MAKE_TOKEN(FTSTRING_MIDDLE(current));
618
30.8k
    token->is_raw = raw;
619
30.8k
    return token_type;
620
5.85k
}