8000 Add macros to get the current mode · python/cpython@349e8ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 349e8ff

Browse files
committed
Add macros to get the current mode
1 parent 367d7f6 commit 349e8ff

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

Parser/tokenizer.c

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@
4343
tok->lineno++; \
4444
tok->col_offset = 0;
4545

46+
#ifdef Py_DEBUG
47+
static inline tokenizer_mode* TOK_GET_MODE(struct tok_state* tok) {
48+
assert(tok->tok_mode_stack_index >= 0);
49+
assert(tok->tok_mode_stack_index < MAXLEVEL);
50+
return &(tok->tok_mode_stack[tok->tok_mode_stack_index]);
51+
}
52+
static inline tokenizer_mode* TOK_NEXT_MODE(struct tok_state* tok) {
53+
assert(tok->tok_mode_stack_index >= 0);
54+
assert(tok->tok_mode_stack_index < MAXLEVEL);
55+
return &(tok->tok_mode_stack[++tok->tok_mode_stack_index]);
56+
}
57+
static inline int *TOK_GET_BRACKET_MARK(tokenizer_mode* mode) {
58+
assert(mode->bracket_mark_index >= 0);
59+
assert(mode->bracket_mark_index < MAX_EXPR_NEXTING);
60+
return &(mode->bracket_mark[mode->bracket_mark_index]);
61+
}
62+
#else
63+
#define TOK_GET_MODE(tok) (&(tok->tok_mode_stack[tok->tok_mode_stack_index]))
64+
#define TOK_NEXT_MODE(tok) (&(tok->tok_mode_stack[++tok->tok_mode_stack_index]))
65+
#define TOK_GET_BRACKET_MARK(mode) (&(mode->bracket_mark[mode->bracket_mark_index]))
66+
#endif
67+
4668
/* Forward */
4769
static struct tok_state *tok_new(void);
4870
static int tok_nextc(struct tok_state *tok);
@@ -373,7 +395,7 @@ update_fstring_expr(struct tok_state *tok, char cur)
373395
assert(tok->cur != NULL);
374396

375397
Py_ssize_t size = strlen(tok->cur);
376-
tokenizer_mode *tok_mode = &(tok->tok_mode_stack[tok->tok_mode_stack_index]);
398+
tokenizer_mode *tok_mode = TOK_GET_MODE(tok);
377399

378400
switch (cur) {
379401
case '{':
@@ -2186,7 +2208,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
21862208

21872209
p_start = tok->start;
21882210
p_end = tok->cur;
2189-
tokenizer_mode *current_tok = &(tok->tok_mode_stack[++tok->tok_mode_stack_index]);
2211+
tokenizer_mode *current_tok = TOK_NEXT_MODE(tok);
21902212
current_tok->kind = TOK_FSTRING_MODE;
21912213
current_tok->f_string_quote = quote;
21922214
current_tok->f_string_quote_size = quote_size;
@@ -2266,7 +2288,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
22662288
* does the initial quote matches with f-strings quotes
22672289
* and if it is, then this must be a missing '}' token
22682290
* so raise the proper error */
2269-
tokenizer_mode *current_tok = &(tok->tok_mode_stack[tok->tok_mode_stack_index]);
2291+
tokenizer_mode *current_tok = TOK_GET_MODE(tok);
22702292
if (current_tok->f_string_quote == quote &&
22712293
current_tok->f_string_quote_size == quote_size) {
22722294
return MAKE_TOKEN(syntaxerror(tok, "f-string: expecting '}'", start));
@@ -2318,7 +2340,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
23182340
/* Punctuation character */
23192341
int is_punctuation = (c == ':' || c == '}' || c == '!' || c == '{');
23202342
if (is_punctuation && tok->tok_mode_stack_index > 0 && current_tok->bracket_mark_index >= 0) {
2321-
int mark = current_tok->bracket_mark[current_tok->bracket_mark_index];
2343+
int mark = *TOK_GET_BRACKET_MARK(current_tok);
23222344
/* This code block gets executed before the bracket_stack is incremented
23232345
* by the `{` case, so for ensuring that we are on the 0th level, we need
23242346
* to adjust it manually */
@@ -2372,7 +2394,6 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
23722394
if (tok->tok_mode_stack_index > 0) {
23732395
current_tok->bracket_stack++;
23742396
}
2375-
23762397
break;
23772398
case ')':
23782399
case ']':
@@ -2397,7 +2418,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
23972418
if (tok->tok_mode_stack_index > 0 && opening == '{') {
23982419
assert(current_tok->bracket_stack >= 0);
23992420
int previous_bracket = current_tok->bracket_stack - 1;
2400-
if (previous_bracket == current_tok->bracket_mark[current_tok->bracket_mark_index]) {
2421+
if (previous_bracket == *TOK_GET_BRACKET_MARK(current_tok)) {
24012422
return MAKE_TOKEN(syntaxerror(tok, "f-string: unmatched '%c'", c));
24022423
}
24032424
}
@@ -2417,7 +2438,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
24172438

24182439
if (tok->tok_mode_stack_index > 0) {
24192440
current_tok->bracket_stack--;
2420-
if (c == '}' && current_tok->bracket_stack == current_tok->bracket_mark[current_tok->bracket_mark_index]) {
2441+
if (c == '}' && current_tok->bracket_stack == *TOK_GET_BRACKET_MARK(current_tok)) {
24212442
current_tok->bracket_mark_index--;
24222443
current_tok->kind = TOK_FSTRING_MODE;
24232444
}
@@ -2461,10 +2482,10 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
24612482
if (current_tok->bracket_mark_index >= MAX_EXPR_NEXTING) {
24622483
return MAKE_TOKEN(syntaxerror(tok, "f-string: expressions nested too deeply"));
24632484
}
2464-
2465-
current_tok->bracket_mark[++current_tok->bracket_mark_index] = current_tok->bracket_stack;
2485+
current_tok->bracket_mark_index++;
2486+
*TOK_GET_BRACKET_MARK(current_tok) = current_tok->bracket_stack;
24662487
}
2467-
tok->tok_mode_stack[tok->tok_mode_stack_index].kind = TOK_REGULAR_MODE;
2488+
TOK_GET_MODE(tok)->kind = TOK_REGULAR_MODE;
24682489
return tok_get_normal_mode(tok, current_tok, token);
24692490
}
24702491

@@ -2529,8 +2550,9 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
25292550
if (peek != '{' || in_format_spec) {
25302551
tok_backup(tok, peek);
25312552
tok_backup(tok, c);
2532-
current_tok->bracket_mark[++current_tok->bracket_mark_index] = current_tok->bracket_stack;
2533-
tok->tok_mode_stack[tok->tok_mode_stack_index].kind = TOK_REGULAR_MODE;
2553+
current_tok->bracket_mark_index++;
2554+
*TOK_GET_BRACKET_MARK(current_tok) = current_tok->bracket_stack;
2555+
TOK_GET_MODE(tok)->kind = TOK_REGULAR_MODE;
25342556
p_start = tok->start;
25352557
p_end = tok->cur;
25362558
} else {
@@ -2556,7 +2578,7 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
25562578
} else {
25572579
tok_backup(tok, peek);
25582580
tok_backup(tok, c);
2559-
tok->tok_mode_stack[tok->tok_mode_stack_index].kind = TOK_REGULAR_MODE;
2581+
TOK_GET_MODE(tok)->kind = TOK_REGULAR_MODE;
25602582
p_start = tok->start;
25612583
p_end = tok->cur;
25622584
}
@@ -2606,7 +2628,7 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
26062628
static int
26072629
tok_get(struct tok_state *tok, struct token *token)
26082630
{
2609-
tokenizer_mode *current_tok = &(tok->tok_mode_stack[tok->tok_mode_stack_index]);
2631+
tokenizer_mode *current_tok = TOK_GET_MODE(tok);
26102632
if (current_tok->kind == TOK_REGULAR_MODE) {
26112633
return tok_get_normal_mode(tok, current_tok, token);
26122634
} else {

0 commit comments

Comments
 (0)
0