@@ -21,15 +21,17 @@ static inline int
2121_PyTok_LexerAdvance (struct _PyTokenizer * tok )
2222{
2323 for (;;) {
24- if (tok -> cursor .pos < tok -> cursor .line_end &&
25- tok -> cursor .pos - tok -> cursor .line_start >= INT_MAX ) {
26- _PyTok_RecordCurrentError (
27- tok , _PYTOK_ERR_COLUMN_OVERFLOW , NULL );
28- return EOF ;
29- }
30- int c = _PyTok_CursorAdvance (& tok -> cursor );
31- if (c != EOF ) {
32- return c ;
24+ assert (tok -> cursor .source != NULL );
25+ assert (tok -> cursor .pos >= tok -> cursor .line_start );
26+ assert (tok -> cursor .pos <= tok -> cursor .line_end );
27+ assert (tok -> cursor .line_end <= tok -> source .len );
28+ if (tok -> cursor .pos < tok -> cursor .line_end ) {
29+ if (tok -> cursor .pos - tok -> cursor .line_start >= INT_MAX ) {
30+ _PyTok_RecordCurrentError (
31+ tok , _PYTOK_ERR_COLUMN_OVERFLOW , NULL );
32+ return EOF ;
33+ }
34+ return Py_CHARMASK (tok -> source .bytes [tok -> cursor .pos ++ ]);
3335 }
3436 if (_PyTok_HasError (tok ) || !_PyTok_ReaderUnderflow (tok )) {
3537 return EOF ;
@@ -52,6 +54,10 @@ _PyTok_LexerMark(const struct _PyTokenizer *tok)
5254static inline void
5355_PyTok_LexerReset (struct _PyTokenizer * tok , _PyTok_Off offset )
5456{
57+ if (offset >= tok -> cursor .line_start && offset < tok -> cursor .line_end ) {
58+ tok -> cursor .pos = offset ;
59+ return ;
60+ }
5561 int result = _PyTok_CursorSetOffset (& tok -> cursor , offset );
5662 if (result < 0 ) {
5763 _PyTok_RecordPending (tok , _PYTOK_ERR_PROPAGATE );
@@ -63,6 +69,7 @@ static inline _PyTok_Frame *
6369_PyTok_CurrentFrame (struct _PyTokenizer * tok )
6470{
6571 assert (tok -> frame_index >= 0 );
72+ assert (tok -> frame_index < tok -> frame_capacity );
6673 assert (tok -> frame_index < _PYTOK_MAX_FRAMES );
6774 return & tok -> frames [tok -> frame_index ];
6875}
@@ -91,50 +98,31 @@ _PyTok_LexerLocation(struct _PyTokenizer *tok, _PyTok_Off offset)
9198 return loc ;
9299}
93100
94- static inline int
101+ int
95102_PyTok_EmitTokenWithLocationEnd (struct _PyTokenizer * tok ,
96103 _PyTok_Token * token , int type ,
97104 _PyTok_Off start , _PyTok_Off end ,
98- _PyTok_Off location_end )
99- {
100- assert ((start < 0 && end < 0 ) || (start >= 0 && end >= start ));
101- assert (start < 0 || location_end >= end );
102- int synthetic = start < 0 ;
103- if (synthetic && type == ENDMARKER ) {
104- start = end = location_end = tok -> cursor .pos ;
105- }
106- token -> type = type ;
107- token -> level = tok -> level ;
108- token -> span = start < 0
109- ? _PyTok_InvalidSpan ()
110- : _PyTok_SpanFromBounds (start , end );
111- token -> start = (_PyTok_Loc ){tok -> cursor .lineno , -1 };
112- token -> end = token -> start ;
113- if (start >= 0 ) {
114- if (_PyTok_CursorLocation (
115- & tok -> cursor , start , _PYTOK_AFFINITY_RIGHT ,
116- & token -> start ) < 0 ) {
117- return record_location_error (tok );
118- }
119- _PyTok_Affinity end_affinity =
120- type == FSTRING_MIDDLE || type == TSTRING_MIDDLE ||
121- start == location_end
122- ? _PYTOK_AFFINITY_RIGHT
123- : _PYTOK_AFFINITY_LEFT ;
124- if (_PyTok_CursorLocation (
125- & tok -> cursor , location_end , end_affinity , & token -> end ) < 0 ) {
126- return record_location_error (tok );
127- }
128- }
129- token -> flags = _PyTok_SourceLineIsImplicit (
130- & tok -> source , tok -> cursor .lineno ) ? _PYTOK_IMPLICIT_NL : 0 ;
131- return type ;
132- }
105+ _PyTok_Off location_end );
133106
134107static inline int
135108_PyTok_EmitToken (struct _PyTokenizer * tok , _PyTok_Token * token , int type ,
136109 _PyTok_Off start , _PyTok_Off end )
137110{
111+ assert (start < 0 || end >= start );
112+ if (start >= tok -> cursor .line_start &&
113+ start < tok -> cursor .line_end &&
114+ end <= tok -> cursor .line_end &&
115+ end - tok -> cursor .line_start <= INT_MAX ) {
116+ token -> type = type ;
117+ token -> level = tok -> level ;
118+ token -> span = _PyTok_SpanFromBounds (start , end );
119+ token -> start = (_PyTok_Loc ){
120+ tok -> cursor .lineno , (int )(start - tok -> cursor .line_start )};
121+ token -> end = (_PyTok_Loc ){
122+ tok -> cursor .lineno , (int )(end - tok -> cursor .line_start )};
123+ token -> flags = tok -> cursor .implicit_newline ? _PYTOK_IMPLICIT_NL : 0 ;
124+ return type ;
125+ }
138126 return _PyTok_EmitTokenWithLocationEnd (
139127 tok , token , type , start , end , end );
140128}
0 commit comments