Skip to content

Commit d03c162

Browse files
committed
gh-153569: use tokenizer views and remove obsolete API remnants
1 parent 029775f commit d03c162

15 files changed

Lines changed: 27 additions & 100 deletions

Makefile.pre.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,6 @@ PEGEN_HEADERS= \
411411
$(srcdir)/Parser/string_parser.h
412412

413413
TOKENIZER_HEADERS= \
414-
Parser/lexer/lexer.h \
415414
Parser/lexer/lexer_internal.h \
416415
Parser/lexer/state.h \
417416
Parser/tokenizer/reader.h \

PCbuild/pythoncore.vcxproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,6 @@
421421
<ClInclude Include="..\Objects\stringlib\split.h" />
422422
<ClInclude Include="..\Objects\unicodetype_db.h" />
423423
<ClInclude Include="..\Parser\lexer\state.h" />
424-
<ClInclude Include="..\Parser\lexer\lexer.h" />
425424
<ClInclude Include="..\Parser\lexer\lexer_internal.h" />
426425
<ClInclude Include="..\Parser\tokenizer\reader.h" />
427426
<ClInclude Include="..\Parser\tokenizer\reader_internal.h" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,6 @@
321321
<ClInclude Include="..\Objects\unicodetype_db.h">
322322
<Filter>Objects</Filter>
323323
</ClInclude>
324-
<ClInclude Include="..\Parser\lexer\lexer.h">
325-
<Filter>Parser</Filter>
326-
</ClInclude>
327324
<ClInclude Include="..\Parser\lexer\state.h">
328325
<Filter>Parser</Filter>
329326
</ClInclude>

Parser/lexer/lexer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ verify_identifier(struct tok_state *tok)
8989
assert(PyUnicode_GET_LENGTH(s) > 0);
9090
if (invalid < PyUnicode_GET_LENGTH(s)) {
9191
Py_UCS4 ch = PyUnicode_READ_CHAR(s, invalid);
92-
const char *error_cursor = tok->cur;
92+
_PyTok_Off error_cursor = tok->cur;
9393
if (invalid + 1 < PyUnicode_GET_LENGTH(s)) {
9494
/* Determine the offset in UTF-8 encoded input */
9595
Py_SETREF(s, PyUnicode_Substring(s, 0, invalid + 1));
@@ -105,13 +105,13 @@ verify_identifier(struct tok_state *tok)
105105
Py_DECREF(s);
106106
if (Py_UNICODE_ISPRINTABLE(ch)) {
107107
_PyTokenizer_syntaxerror_at(
108-
tok, tok->line_start,
108+
tok, _PyLexer_BufferPointer(tok, tok->line_start),
109109
error_cursor - tok->line_start, tok->lineno, -1, -1,
110110
"invalid character '%c' (U+%04X)", ch, ch);
111111
}
112112
else {
113113
_PyTokenizer_syntaxerror_at(
114-
tok, tok->line_start,
114+
tok, _PyLexer_BufferPointer(tok, tok->line_start),
115115
error_cursor - tok->line_start, tok->lineno, -1, -1,
116116
"invalid non-printable character U+%04X", ch);
117117
}

Parser/lexer/lexer.h

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

Parser/lexer/lexer_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define _PY_LEXER_INTERNAL_H_
33

44
#include "errcode.h"
5-
#include "lexer.h"
5+
#include "state.h"
66

77
#define is_potential_identifier_start(c) (\
88
(c >= 'a' && c <= 'z')\

Parser/lexer/state.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#define MAXFTSTRINGLEVEL 150
1111
#define FTSTRING_STACK_INLINE_CAPACITY 1
1212

13-
14-
1513
typedef enum {
1614
FTSTRING_MODE_MIDDLE,
1715
FTSTRING_MODE_EXPRESSION,
@@ -72,14 +70,6 @@ typedef struct {
7270
indentation_level stack[MAXINDENT];
7371
} lexer_layout_state;
7472

75-
/* Supplemental source context for a terminal error. location is the reporting
76-
cursor, independent of the scanner cursor; lineno == 0 means absent.
77-
The text span may cover multiple physical lines. */
78-
typedef struct {
79-
_PyTok_Loc location;
80-
_PyTok_Span text_span;
81-
} _PyTokenizer_Diagnostic;
82-
8373
/* Tokenizer state */
8474
struct tok_state {
8575
_PyTok_Off buf_offset;

Parser/lexer/string.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ _PyLexer_scan_string(struct tok_state *tok, struct token *token, int c)
357357
if (c == EOF || (quote_size == 1 && c == '\n')) {
358358
int end_lineno = tok->lineno;
359359
_PyTok_Loc location = tok->start_loc;
360-
const char *line = tok->start - location.byte_col;
360+
const char *line = _PyLexer_BufferPointer(tok, tok->start) - location.byte_col;
361361
Py_ssize_t cursor_offset = (Py_ssize_t)location.byte_col + 1;
362362

363363
const ftstring_state *state = _PyLexer_CurrentFTString(tok);
@@ -478,15 +478,15 @@ _PyLexer_get_ftstring(struct tok_state *tok, ftstring_state *current, struct tok
478478
tok->done = E_EOFS;
479479
}
480480
return string_error_token(tok, token,
481-
_PyLexer_BufferPointer(tok, current->start), location);
481+
current->start, location);
482482
}
483483
else {
484484
_PyTokenizer_syntaxerror_at(
485485
tok, line, cursor_offset, location.lineno, -1, -1,
486486
"unterminated %c-string literal (detected at line %d)",
487487
_PyLexer_StringPrefix(current->kind), end_lineno);
488488
return string_error_token(tok, token,
489-
_PyLexer_BufferPointer(tok, current->start), location);
489+
current->start, location);
490490
}
491491
}
492492

Parser/pegen_errors.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ _PyPegen_raise_error(Parser *p, PyObject *errtype, int use_mark, const char *err
206206
Py_ssize_t end_col_offset = -1;
207207
if (t->col_offset == -1) {
208208
_PyTokenizer_Info info = _PyTokenizer_GetInfo(p->tok);
209-
if (info.cursor == info.input_span.start) {
209+
if (info.diagnostic.location.lineno != 0) {
210+
col_offset = info.diagnostic.location.byte_col;
211+
} else if (info.cursor == info.input_span.start) {
210212
col_offset = 0;
211213
} else {
212214
col_offset = Py_SAFE_DOWNCAST(
@@ -256,8 +258,10 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
256258
PyObject *tmp = NULL;
257259
p->error_indicator = 1;
258260
_PyTokenizer_Info info = _PyTokenizer_GetInfo(p->tok);
259-
_PyTok_Loc location = info.location;
260-
_PyTok_Span text_span = info.line_span;
261+
_PyTok_Loc location = info.diagnostic.location.lineno != 0
262+
? info.diagnostic.location : info.location;
263+
_PyTok_Span text_span = info.diagnostic.location.lineno != 0
264+
? info.diagnostic.text_span : info.line_span;
261265

262266
if (end_lineno == CURRENT_POS) {
263267
end_lineno = location.lineno;

Parser/tokenizer/api.c

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
#include "tokenizer.h"
66
#include "reader.h"
7-
#include "reader_internal.h"
87
#include "../lexer/state.h"
98

109
_PyTokenizer_Info
1110
_PyTokenizer_GetInfo(const struct tok_state *tok)
1211
{
1312
_PyTokenizer_Info info = {
1413
.status = tok->done,
14+
.diagnostic = tok->diagnostic,
1515
.location = {tok->lineno, tok->line_start < 0
1616
? -1 : (int)(tok->cur - tok->line_start)},
1717
.cursor = tok->cur,
@@ -20,7 +20,7 @@ _PyTokenizer_GetInfo(const struct tok_state *tok)
2020
.level = tok->level,
2121
.delimiter_loc = {-1, -1},
2222
.in_formatted_string = tok->ftstring_depth != 0,
23-
.is_interactive = tok->reader->kind == _PYTOK_READER_INTERACTIVE,
23+
.is_interactive = _PyTok_ReaderIsInteractive(tok),
2424
.is_file = tok->fp != NULL && tok->fp != stdin,
2525
.filename = tok->filename,
2626
.module = tok->module,
@@ -81,32 +81,7 @@ const char *
8181
_PyTokenizer_LineView(const struct tok_state *tok, Py_ssize_t lineno,
8282
Py_ssize_t *length)
8383
{
84-
const char *line = _PyTokenizer_RetainedSource(tok);
85-
if (line == NULL) {
86-
line = _PyLexer_BufferPointer(tok, tok->buf_offset);
87-
}
88-
for (Py_ssize_t i = 1; i < lineno; i++) {
89-
const char *next = strchr(line, '\n');
90-
if (next == NULL) {
91-
break;
92-
}
93-
line = next + 1;
94-
}
95-
const char *end = strchr(line, '\n');
96-
*length = end != NULL ? end - line : (Py_ssize_t)strlen(line);
97-
return line;
98-
}
99-
100-
const char *
101-
_PyTokenizer_RetainedSource(const struct tok_state *tok)
102-
{
103-
if (tok->reader->kind == _PYTOK_READER_PREPARED) {
104-
return _PyTok_SourceData(&tok->source);
105-
}
106-
if (tok->reader->kind == _PYTOK_READER_INTERACTIVE) {
107-
return tok->source.bytes;
108-
}
109-
return NULL;
84+
return _PyTok_SourceLineView(&tok->source, lineno, length);
11085
}
11186

11287
void

0 commit comments

Comments
 (0)