Skip to content

Commit 99a1a09

Browse files
authored
Merge pull request #49 from isidentical/simplify-layers
Infer format specificer enclosing automatically
2 parents 0fc633c + 739c16f commit 99a1a09

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

Parser/tokenizer.c

+7-13
Original file line numberDiff line numberDiff line change
@@ -2190,7 +2190,6 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
21902190
current_tok->last_expr_buffer = NULL;
21912191
current_tok->last_expr_size = 0;
21922192
current_tok->last_expr_end = -1;
2193-
current_tok->format_spec = 0;
21942193

21952194
switch (*tok->start) {
21962195
case 'F':
@@ -2326,7 +2325,6 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
23262325

23272326
if (c == ':' && cursor == mark) {
23282327
current_tok->kind = TOK_FSTRING_MODE;
2329-
current_tok->format_spec = 1;
23302328
p_start = tok->start;
23312329
p_end = tok->cur;
23322330
return MAKE_TOKEN(_PyToken_OneChar(c));
@@ -2400,12 +2398,6 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
24002398
if (tok->tok_mode_stack_index > 0) {
24012399
current_tok->bracket_stack--;
24022400
if (c == '}' && current_tok->bracket_stack == current_tok->bracket_mark[current_tok->bracket_mark_index]) {
2403-
// When the expression is complete, we can exit the format
2404-
// spec mode (no matter if we were in it or not).
2405-
if (current_tok->bracket_mark_index <= 0) {
2406-
current_tok->format_spec = 0;
2407-
}
2408-
24092401
current_tok->bracket_mark_index--;
24102402
current_tok->kind = TOK_FSTRING_MODE;
24112403
}
@@ -2504,11 +2496,13 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
25042496
return MAKE_TOKEN(FSTRING_MIDDLE);
25052497
}
25062498
char peek = tok_nextc(tok);
2507-
if (peek == '}' && current_tok->bracket_mark_index <= 0
2508-
// We can not have }} inside the format spec, so we are going to assume
2509-
// this that the first closing brace belongs to the f-string expression
2510-
// and the second one needs to deal with later (e.g. f"{1:<3}}}").
2511-
&& !current_tok->format_spec) {
2499+
2500+
// The tokenizer can only be in the format spec if we have already completed the expression
2501+
// scanning (indicated by the end of the expression being set) and we are not at the top level
2502+
// of the bracket stack (-1 is the top level). Since format specifiers can't legally use double
2503+
// brackets, we can bypass it here.
2504+
int in_format_spec = current_tok->last_expr_end != -1 && current_tok->bracket_mark_index >= 0;
2505+
if (peek == '}' && !in_format_spec) {
25122506
p_start = tok->start;
25132507
p_end = tok->cur - 1;
25142508
} else {

Parser/tokenizer.h

-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ enum tokenizer_mode_kind_t {
4343
typedef struct _tokenizer_mode {
4444
enum tokenizer_mode_kind_t kind;
4545

46-
// TODO: we probably can infer this without storing it
47-
// from the other information available here.
48-
int format_spec;
49-
5046
int bracket_stack;
5147
int bracket_mark[MAX_EXPR_NEXTING];
5248
int bracket_mark_index;

0 commit comments

Comments
 (0)