77
88#define MAKE_TOKEN (token_type ) _PyLexer_token_setup(tok, token, token_type, p_start, p_end)
99
10+ static void
11+ rewind_to_string_start (struct tok_state * tok , const char * start ,
12+ _PyTok_Loc location )
13+ {
14+ tok -> cur = (char * )start + 1 ;
15+ tok -> line_start = start - location .byte_col ;
16+ tok -> lineno = location .lineno ;
17+ }
18+
1019int
1120_PyLexer_record_ftstring_comment (struct tok_state * tok , const char * start ,
1221 const char * end )
@@ -194,13 +203,6 @@ _PyLexer_scan_fstring_start(struct tok_state *tok, struct token *token, int c)
194203 int quote = c ;
195204 int quote_size = 1 ; /* 1 or 3 */
196205
197- /* Nodes of type STRING, especially multi line strings
198- must be handled differently in order to get both
199- the starting line number and the column offset right.
200- (cf. issue 16806) */
201- tok -> first_lineno = tok -> lineno ;
202- tok -> multi_line_start = tok -> line_start ;
203-
204206 /* Find the quote size and start of string */
205207 int after_quote = tok_nextc (tok );
206208 if (after_quote == quote ) {
@@ -276,13 +278,6 @@ _PyLexer_scan_string(struct tok_state *tok, struct token *token, int c)
276278 int end_quote_size = 0 ;
277279 int has_escaped_quote = 0 ;
278280
279- /* Nodes of type STRING, especially multi line strings
280- must be handled differently in order to get both
281- the starting line number and the column offset right.
282- (cf. issue 16806) */
283- tok -> first_lineno = tok -> lineno ;
284- tok -> multi_line_start = tok -> line_start ;
285-
286281 /* Find the quote size and start of string */
287282 c = tok_nextc (tok );
288283 if (c == quote ) {
@@ -308,15 +303,8 @@ _PyLexer_scan_string(struct tok_state *tok, struct token *token, int c)
308303 break ;
309304 }
310305 if (c == EOF || (quote_size == 1 && c == '\n' )) {
311- assert (tok -> multi_line_start != NULL );
312- // shift the tok_state's location into
313- // the start of string, and report the error
314- // from the initial quote character
315- tok -> cur = (char * )tok -> start ;
316- tok -> cur ++ ;
317- tok -> line_start = tok -> multi_line_start ;
318- int start = tok -> lineno ;
319- tok -> lineno = tok -> first_lineno ;
306+ int end_lineno = tok -> lineno ;
307+ rewind_to_string_start (tok , tok -> start , tok -> start_loc );
320308
321309 if (INSIDE_FSTRING (tok )) {
322310 /* When we are in an f-string, before raising the
@@ -334,7 +322,7 @@ _PyLexer_scan_string(struct tok_state *tok, struct token *token, int c)
334322
335323 if (quote_size == 3 ) {
336324 _PyTokenizer_syntaxerror (tok , "unterminated triple-quoted string literal"
337- " (detected at line %d)" , start );
325+ " (detected at line %d)" , end_lineno );
338326 if (c != '\n' ) {
339327 tok -> done = E_EOFS ;
340328 }
@@ -346,11 +334,11 @@ _PyLexer_scan_string(struct tok_state *tok, struct token *token, int c)
346334 tok ,
347335 "unterminated string literal (detected at line %d); "
348336 "perhaps you escaped the end quote?" ,
349- start
337+ end_lineno
350338 );
351339 } else {
352340 _PyTokenizer_syntaxerror (
353- tok , "unterminated string literal (detected at line %d)" , start
341+ tok , "unterminated string literal (detected at line %d)" , end_lineno
354342 );
355343 }
356344 if (c != '\n' ) {
@@ -390,8 +378,7 @@ _PyLexer_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, st
390378 int unicode_escape = 0 ;
391379
392380 tok -> start = tok -> cur ;
393- tok -> first_lineno = tok -> lineno ;
394- tok -> starting_col_offset = tok -> col_offset ;
381+ tok -> start_loc = (_PyTok_Loc ){tok -> lineno , _PyLexer_ByteColumn (tok )};
395382
396383 // If we start with a bracket, we defer to the normal mode as there is nothing for us to tokenize
397384 // before it.
@@ -432,9 +419,6 @@ _PyLexer_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, st
432419
433420f_string_middle :
434421
435- // TODO: This is a bit of a hack, but it works for now. We need to find a better way to handle
436- // this.
437- tok -> multi_line_start = tok -> line_start ;
438422 while (end_quote_size != current_tok -> quote_size ) {
439423 int c = tok_nextc (tok );
440424 if (tok -> done == E_ERROR || tok -> done == E_DECODE ) {
@@ -447,7 +431,7 @@ _PyLexer_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, st
447431 );
448432
449433 if (c == EOF || (current_tok -> quote_size == 1 && c == '\n' )) {
450- if (tok -> input_error ) {
434+ if (tok_failed ( tok ) ) {
451435 return MAKE_TOKEN (ERRORTOKEN );
452436 }
453437
@@ -472,7 +456,6 @@ _PyLexer_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, st
472456 return MAKE_TOKEN (FTSTRING_MIDDLE (current_tok ));
473457 }
474458
475- assert (tok -> multi_line_start != NULL );
476459 // shift the tok_state's location into
477460 // the start of string, and report the error
478461 // from the initial quote character
0 commit comments