@@ -327,6 +327,28 @@ _resize_tokens_array(Parser *p) {
327327 return 0 ;
328328}
329329
330+ int
331+ _PyPegen_append_token (Parser * p , struct token * token )
332+ {
333+ int type = token -> type ;
334+ // Single input inserts a final newline and may still need dedents. Apply
335+ // the same normalization when the error pass fills the replay cache.
336+ if (p -> start_rule == Py_single_input && type == ENDMARKER && p -> parsing_started ) {
337+ type = NEWLINE ;
338+ p -> parsing_started = 0 ;
339+ if (!(p -> flags & PyPARSE_DONT_IMPLY_DEDENT )) {
340+ _PyTokenizer_ImplyDedents (p -> tok );
341+ }
342+ }
343+ else {
344+ p -> parsing_started = 1 ;
345+ }
346+ if (p -> fill == p -> size && _resize_tokens_array (p ) < 0 ) {
347+ return -1 ;
348+ }
349+ return initialize_token (p , p -> tokens [p -> fill ], token , type );
350+ }
351+
330352int
331353_PyPegen_fill_token (Parser * p )
332354{
@@ -363,26 +385,7 @@ _PyPegen_fill_token(Parser *p)
363385 p -> input_exhausted = 1 ;
364386 }
365387
366- // If we have reached the end and we are in single input mode we need to insert a newline and reset the parsing
367- if (p -> start_rule == Py_single_input && type == ENDMARKER && p -> parsing_started ) {
368- type = NEWLINE ; /* Add an extra newline */
369- p -> parsing_started = 0 ;
370-
371- if (!(p -> flags & PyPARSE_DONT_IMPLY_DEDENT )) {
372- _PyTokenizer_ImplyDedents (p -> tok );
373- }
374- }
375- else {
376- p -> parsing_started = 1 ;
377- }
378-
379- // Check if we are at the limit of the token array capacity and resize if needed
380- if ((p -> fill == p -> size ) && (_resize_tokens_array (p ) != 0 )) {
381- goto error ;
382- }
383-
384- Token * t = p -> tokens [p -> fill ];
385- int result = initialize_token (p , t , & new_token , type );
388+ int result = _PyPegen_append_token (p , & new_token );
386389 _PyToken_Free (& new_token );
387390 return result ;
388391error :
0 commit comments