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 }
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 ;
0 commit comments