gh-153569: share string prefix and quote scanning · python/cpython@5aae423 · GitHub
Skip to content

Commit 5aae423

Browse files
committed
gh-153569: share string prefix and quote scanning
The lexer tracks each string prefix with a separate local and scans opening quotes twice, once for ordinary strings and once for formatted strings. Use prefix flags and one quote scanner for ordinary strings, f-strings, and t-strings. Prefix validation and raw-string selection now use the same parsed value.
1 parent 1874ce4 commit 5aae423

3 files changed

Lines changed: 78 additions & 105 deletions

File tree

Parser/lexer/lexer.c

Lines changed: 14 additions & 32 deletions

Parser/lexer/lexer_internal.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
|| c == '_'\
1818
|| (c >= 128))
1919

20+
enum string_prefix_flags {
21+
STRING_PREFIX_B = 1 << 0,
22+
STRING_PREFIX_R = 1 << 1,
23+
STRING_PREFIX_U = 1 << 2,
24+
STRING_PREFIX_F = 1 << 3,
25+
STRING_PREFIX_T = 1 << 4,
26+
};
27+
2028
#ifdef Py_DEBUG
2129
static inline tokenizer_mode *
2230
TOK_GET_MODE(struct tok_state *tok)
@@ -58,9 +66,9 @@ void _PyLexer_update_ftstring_expr(struct tok_state *, char);
5866
int _PyLexer_record_ftstring_comment(
5967
struct tok_state *, const char *, const char *);
6068
int _PyLexer_set_ftstring_expr_metadata(struct tok_state *, struct token *);
61-
int _PyLexer_check_string_prefixes(struct tok_state *, int, int, int, int, int);
6269
int _PyLexer_scan_number(struct tok_state *, struct token *, int, int);
63-
int _PyLexer_scan_fstring_start(struct tok_state *, struct token *, int);
70+
int _PyLexer_scan_prefixed_string(
71+
struct tok_state *, struct token *, int, unsigned int);
6472
int _PyLexer_scan_string(struct tok_state *, struct token *, int);
6573
int _PyLexer_get_normal_mode(struct tok_state *, tokenizer_mode *, struct token *);
6674
int _PyLexer_get_fstring_mode(struct tok_state *, tokenizer_mode *, struct token *);

Parser/lexer/string.c

Lines changed: 54 additions & 71 deletions

0 commit comments

Comments
 (0)