Skip to content

Commit 3ce81ac

Browse files
committed
gh-153569: remove duplicated tokenizer state
1 parent eae3e33 commit 3ce81ac

24 files changed

Lines changed: 172 additions & 284 deletions

Makefile.pre.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ PEGEN_OBJS= \
394394
Parser/peg_api.o
395395

396396
TOKENIZER_OBJS= \
397-
Parser/lexer/buffer.o \
398397
Parser/lexer/lexer.o \
399398
Parser/lexer/number.o \
400399
Parser/lexer/state.o \
@@ -410,7 +409,6 @@ PEGEN_HEADERS= \
410409
$(srcdir)/Parser/string_parser.h
411410

412411
TOKENIZER_HEADERS= \
413-
Parser/lexer/buffer.h \
414412
Parser/lexer/lexer.h \
415413
Parser/lexer/lexer_internal.h \
416414
Parser/lexer/state.h \

PCbuild/_freeze_module.vcxproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@
181181
<ClCompile Include="..\Parser\action_helpers.c" />
182182
<ClCompile Include="..\Parser\string_parser.c" />
183183
<ClCompile Include="..\Parser\token.c" />
184-
<ClCompile Include="..\Parser\lexer\buffer.c" />
185184
<ClCompile Include="..\Parser\lexer\state.c" />
186185
<ClCompile Include="..\Parser\lexer\lexer.c" />
187186
<ClCompile Include="..\Parser\lexer\number.c" />

PCbuild/_freeze_module.vcxproj.filters

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,6 @@
469469
<ClCompile Include="..\Parser\lexer\string.c">
470470
<Filter>Source Files</Filter>
471471
</ClCompile>
472-
<ClCompile Include="..\Parser\lexer\buffer.c">
473-
<Filter>Source Files</Filter>
474-
</ClCompile>
475472
<ClCompile Include="..\Parser\lexer\state.c">
476473
<Filter>Source Files</Filter>
477474
</ClCompile>

PCbuild/pythoncore.vcxproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@
422422
<ClInclude Include="..\Parser\lexer\state.h" />
423423
<ClInclude Include="..\Parser\lexer\lexer.h" />
424424
<ClInclude Include="..\Parser\lexer\lexer_internal.h" />
425-
<ClInclude Include="..\Parser\lexer\buffer.h" />
426425
<ClInclude Include="..\Parser\tokenizer\reader.h" />
427426
<ClInclude Include="..\Parser\tokenizer\reader_internal.h" />
428427
<ClInclude Include="..\Parser\tokenizer\source.h" />
@@ -591,7 +590,6 @@
591590
<ClCompile Include="..\Parser\lexer\lexer.c" />
592591
<ClCompile Include="..\Parser\lexer\number.c" />
593592
<ClCompile Include="..\Parser\lexer\string.c" />
594-
<ClCompile Include="..\Parser\lexer\buffer.c" />
595593
<ClCompile Include="..\Parser\tokenizer\source.c" />
596594
<ClCompile Include="..\Parser\tokenizer\decoder.c" />
597595
<ClCompile Include="..\Parser\tokenizer\reader.c" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,6 @@
330330
<ClInclude Include="..\Parser\lexer\lexer_internal.h">
331331
<Filter>Parser</Filter>
332332
</ClInclude>
333-
<ClInclude Include="..\Parser\lexer\buffer.h">
334-
<Filter>Parser</Filter>
335-
</ClInclude>
336333
<ClInclude Include="..\Parser\tokenizer\reader.h">
337334
<Filter>Parser</Filter>
338335
</ClInclude>
@@ -1355,9 +1352,6 @@
13551352
<ClCompile Include="..\Parser\lexer\state.c">
13561353
<Filter>Parser</Filter>
13571354
</ClCompile>
1358-
<ClCompile Include="..\Parser\lexer\buffer.c">
1359-
<Filter>Parser</Filter>
1360-
</ClCompile>
13611355
<ClCompile Include="..\Parser\tokenizer\source.c">
13621356
<Filter>Parser</Filter>
13631357
</ClCompile>

Parser/lexer/buffer.c

Lines changed: 0 additions & 33 deletions
This file was deleted.

Parser/lexer/buffer.h

Lines changed: 0 additions & 22 deletions
This file was deleted.

Parser/lexer/lexer.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "../tokenizer/helpers.h"
88
#include "../tokenizer/reader.h"
99

10-
/* Alternate tab spacing */
10+
#define TABSIZE 8
1111
#define ALTTABSIZE 1
1212

1313

@@ -45,11 +45,10 @@ _PyLexer_nextc(struct tok_state *tok)
4545
int rc;
4646
for (;;) {
4747
if (tok->cur != tok->inp) {
48-
if ((unsigned int) tok->col_offset >= (unsigned int) INT_MAX) {
48+
if (tok->cur - tok->line_start >= INT_MAX) {
4949
tok->done = E_COLUMNOVERFLOW;
5050
return EOF;
5151
}
52-
tok->col_offset++;
5352
return Py_CHARMASK(*tok->cur++); /* Fast path */
5453
}
5554
if (tok->done != E_OK) {
@@ -89,7 +88,6 @@ _PyLexer_backup(struct tok_state *tok, int c)
8988
if ((int)(unsigned char)*tok->cur != Py_CHARMASK(c)) {
9089
Py_FatalError("tok_backup: wrong character");
9190
}
92-
tok->col_offset--;
9391
}
9492
}
9593

@@ -103,7 +101,7 @@ verify_identifier(struct tok_state *tok)
103101
return 1;
104102
}
105103
PyObject *s;
106-
if (tok->input_error)
104+
if (tok_failed(tok))
107105
return 0;
108106
s = PyUnicode_DecodeUTF8(tok->start, tok->cur - tok->start, NULL);
109107
if (s == NULL) {
@@ -180,7 +178,7 @@ _PyLexer_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, str
180178
const char *p_end = NULL;
181179
nextline:
182180
tok->start = NULL;
183-
tok->starting_col_offset = -1;
181+
tok->start_loc = (_PyTok_Loc){tok->lineno, -1};
184182
blankline = 0;
185183

186184

@@ -196,7 +194,7 @@ _PyLexer_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, str
196194
col++, altcol++;
197195
}
198196
else if (c == '\t') {
199-
col = (col / tok->tabsize + 1) * tok->tabsize;
197+
col = (col / TABSIZE + 1) * TABSIZE;
200198
altcol = (altcol / ALTTABSIZE + 1) * ALTTABSIZE;
201199
}
202200
else if (c == '\014') {/* Control-L (formfeed) */
@@ -221,15 +219,16 @@ _PyLexer_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, str
221219
}
222220
tok_backup(tok, c);
223221
if (c == '#' || c == '\n' || c == '\r') {
222+
int interactive = _PyTok_ReaderIsInteractive(tok);
224223
/* Lines with only whitespace and/or comments
225224
shouldn't affect the indentation and are
226225
not passed to the parser as NEWLINE tokens,
227226
except *totally* empty lines in interactive
228227
mode, which signal the end of a command group. */
229-
if (col == 0 && c == '\n' && tok->prompt != NULL) {
228+
if (col == 0 && c == '\n' && interactive) {
230229
blankline = 0; /* Let it through */
231230
}
232-
else if (tok->prompt != NULL && tok->lineno == 1) {
231+
else if (interactive && tok->lineno == 1) {
233232
/* In interactive mode, if the first line contains
234233
only spaces and/or a comment, let it through. */
235234
blankline = 0;
@@ -284,7 +283,8 @@ _PyLexer_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, str
284283
}
285284

286285
tok->start = tok->cur;
287-
tok->starting_col_offset = tok->col_offset;
286+
tok->start_loc = (_PyTok_Loc){
287+
tok->lineno, tok->cur != NULL ? _PyLexer_ByteColumn(tok) : -1};
288288

289289
/* Return pending indents/dedents */
290290
if (tok->pendin != 0) {
@@ -319,7 +319,8 @@ _PyLexer_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, str
319319

320320
/* Set start of current token */
321321
tok->start = tok->cur == NULL ? NULL : tok->cur - 1;
322-
tok->starting_col_offset = tok->col_offset - 1;
322+
tok->start_loc = (_PyTok_Loc){
323+
tok->lineno, tok->cur != NULL ? _PyLexer_ByteColumn(tok) - 1 : -1};
323324

324325
/* Skip comment, unless it's a type comment */
325326
if (c == '#') {
@@ -350,7 +351,7 @@ _PyLexer_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, str
350351

351352
if (tok->type_comments) {
352353
p = tok->start;
353-
current_starting_col_offset = tok->starting_col_offset;
354+
current_starting_col_offset = tok->start_loc.byte_col;
354355
prefix = type_comment_prefix;
355356
while (*prefix && p < tok->cur) {
356357
if (*prefix == ' ') {
@@ -394,11 +395,15 @@ _PyLexer_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, str
394395
tok_nextc(tok);
395396
tok->atbol = 1;
396397
}
397-
return MAKE_TYPE_COMMENT_TOKEN(TYPE_IGNORE, ignore_end_col_offset, tok->col_offset);
398+
return MAKE_TYPE_COMMENT_TOKEN(
399+
TYPE_IGNORE, ignore_end_col_offset,
400+
_PyLexer_ByteColumn(tok));
398401
} else {
399402
p_start = type_start;
400403
p_end = tok->cur;
401-
return MAKE_TYPE_COMMENT_TOKEN(TYPE_COMMENT, current_starting_col_offset, tok->col_offset);
404+
return MAKE_TYPE_COMMENT_TOKEN(
405+
TYPE_COMMENT, current_starting_col_offset,
406+
_PyLexer_ByteColumn(tok));
402407
}
403408
}
404409
}
@@ -719,7 +724,7 @@ int
719724
_PyTokenizer_Get(struct tok_state *tok, struct token *token)
720725
{
721726
int result = tok_get(tok, token);
722-
if (tok->input_error) {
727+
if (tok_failed(tok)) {
723728
result = ERRORTOKEN;
724729
}
725730
return result;

Parser/lexer/lexer_internal.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef _PY_LEXER_INTERNAL_H_
22
#define _PY_LEXER_INTERNAL_H_
33

4+
#include "errcode.h"
45
#include "lexer.h"
56

67
#define is_potential_identifier_start(c) (\
@@ -44,6 +45,13 @@ TOK_NEXT_MODE(struct tok_state *tok)
4445
#define tok_nextc _PyLexer_nextc
4546
#define tok_backup _PyLexer_backup
4647

48+
static inline int
49+
tok_failed(const struct tok_state *tok)
50+
{
51+
return tok->done != E_OK && tok->done != E_EOF &&
52+
tok->done != E_INTERACT_STOP;
53+
}
54+
4755
int _PyLexer_nextc(struct tok_state *);
4856
void _PyLexer_backup(struct tok_state *, int);
4957
void _PyLexer_update_ftstring_expr(struct tok_state *, char);

Parser/lexer/state.c

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
#include "state.h"
77
#include "../tokenizer/reader.h"
88

9-
/* Never change this */
10-
#define TABSIZE 8
11-
129
/* Create and initialize a new tok_state structure */
1310
struct tok_state *
1411
_PyTokenizer_tok_new(void)
@@ -22,31 +19,21 @@ _PyTokenizer_tok_new(void)
2219
}
2320

2421
tok->buf = tok->cur = tok->inp = NULL;
25-
tok->fp_interactive = 0;
26-
tok->interactive_src_start = NULL;
27-
tok->interactive_src_end = NULL;
2822
tok->start = NULL;
2923
tok->done = E_OK;
3024
tok->fp = NULL;
31-
tok->tabsize = TABSIZE;
3225
tok->indent = 0;
3326
tok->indstack[0] = 0;
3427
tok->atbol = 1;
3528
tok->pendin = 0;
36-
tok->prompt = NULL;
3729
tok->lineno = 0;
38-
tok->starting_col_offset = -1;
39-
tok->col_offset = -1;
30+
tok->start_loc = (_PyTok_Loc){-1, -1};
4031
tok->level = 0;
4132
tok->altindstack[0] = 0;
42-
tok->input_error = 0;
4333
tok->encoding = NULL;
4434
tok->filename = NULL;
4535
tok->module = NULL;
4636
tok->type_comments = 0;
47-
tok->interactive_underflow = IUNDERFLOW_NORMAL;
48-
tok->str = NULL;
49-
tok->report_warnings = 1;
5037
tok->tok_extra_tokens = 0;
5138
tok->comment_newline = 0;
5239
tok->implicit_newline = 0;
@@ -100,13 +87,12 @@ _PyLexer_token_setup(struct tok_state *tok, struct token *token, int type, const
10087
{
10188
token->level = tok->level;
10289
token->span = _PyLexer_BufferSpan(tok, start, end);
103-
int lineno = ISSTRINGLIT(type) ? tok->first_lineno : tok->lineno;
104-
token->start_loc = (_PyTok_Loc){lineno, -1};
105-
token->end_loc = (_PyTok_Loc){tok->lineno, -1};
106-
10790
if (start != NULL && end != NULL) {
108-
token->start_loc.byte_col = tok->starting_col_offset;
109-
token->end_loc.byte_col = tok->col_offset;
91+
token->start_loc = tok->start_loc;
92+
token->end_loc = (_PyTok_Loc){tok->lineno, _PyLexer_ByteColumn(tok)};
93+
}
94+
else {
95+
token->start_loc = token->end_loc = (_PyTok_Loc){tok->lineno, -1};
11096
}
11197
return type;
11298
}

0 commit comments

Comments
 (0)