@@ -123,50 +123,45 @@ growable_comment_array_deallocate(growable_comment_array *arr) {
123
123
}
124
124
125
125
static int
126
- _get_keyword_or_name_type (Parser * p , const char * name , int name_len )
126
+ _get_keyword_or_name_type (Parser * p , struct token * new_token )
127
127
{
128
+ int name_len = new_token -> end_col_offset - new_token -> col_offset ;
128
129
assert (name_len > 0 );
130
+
129
131
if (name_len >= p -> n_keyword_lists ||
130
132
p -> keywords [name_len ] == NULL ||
131
133
p -> keywords [name_len ]-> type == -1 ) {
132
134
return NAME ;
133
135
}
134
136
for (KeywordToken * k = p -> keywords [name_len ]; k != NULL && k -> type != -1 ; k ++ ) {
135
- if (strncmp (k -> str , name , name_len ) == 0 ) {
137
+ if (strncmp (k -> str , new_token -> start , name_len ) == 0 ) {
136
138
return k -> type ;
137
139
}
138
140
}
139
141
return NAME ;
140
142
}
141
143
142
144
static int
143
- initialize_token (Parser * p , Token * token , const char * start , const char * end , int token_type ) {
144
- assert (token != NULL );
145
+ initialize_token (Parser * p , Token * parser_token , struct token * new_token , int token_type ) {
146
+ assert (parser_token != NULL );
145
147
146
- token -> type = (token_type == NAME ) ? _get_keyword_or_name_type (p , start , ( int )( end - start ) ) : token_type ;
147
- token -> bytes = PyBytes_FromStringAndSize (start , end - start );
148
- if (token -> bytes == NULL ) {
148
+ parser_token -> type = (token_type == NAME ) ? _get_keyword_or_name_type (p , new_token ) : token_type ;
149
+ parser_token -> bytes = PyBytes_FromStringAndSize (new_token -> start , new_token -> end - new_token -> start );
150
+ if (parser_token -> bytes == NULL ) {
149
151
return -1 ;
150
152
}
151
-
152
- if (_PyArena_AddPyObject (p -> arena , token -> bytes ) < 0 ) {
153
- Py_DECREF (token -> bytes );
153
+ if (_PyArena_AddPyObject (p -> arena , parser_token -> bytes ) < 0 ) {
154
+ Py_DECREF (parser_token -> bytes );
154
155
return -1 ;
155
156
}
156
157
157
- token -> level = p -> tok -> level ;
158
-
159
- const char * line_start = token_type == STRING ? p -> tok -> multi_line_start : p -> tok -> line_start ;
160
- int lineno = token_type == STRING ? p -> tok -> first_lineno : p -> tok -> lineno ;
161
- int end_lineno = p -> tok -> lineno ;
162
-
163
- int col_offset = (start != NULL && start >= line_start ) ? (int )(start - line_start ) : -1 ;
164
- int end_col_offset = (end != NULL && end >= p -> tok -> line_start ) ? (int )(end - p -> tok -> line_start ) : -1 ;
165
-
166
- token -> lineno = lineno ;
167
- token -> col_offset = p -> tok -> lineno == p -> starting_lineno ? p -> starting_col_offset + col_offset : col_offset ;
168
- token -> end_lineno = end_lineno ;
169
- token -> end_col_offset = p -> tok -> lineno == p -> starting_lineno ? p -> starting_col_offset + end_col_offset : end_col_offset ;
158
+ parser_token -> level = new_token -> level ;
159
+ parser_token -> lineno = new_token -> lineno ;
160
+ parser_token -> col_offset = p -> tok -> lineno == p -> starting_lineno ? p -> starting_col_offset + new_token -> col_offset
161
+ : new_token -> col_offset ;
162
+ parser_token -> end_lineno = new_token -> end_lineno ;
163
+ parser_token -> end_col_offset = p -> tok -> lineno == p -> starting_lineno ? p -> starting_col_offset + new_token -> end_col_offset
164
+ : new_token -> end_col_offset ;
170
165
171
166
p -> fill += 1 ;
172
167
@@ -202,26 +197,25 @@ _resize_tokens_array(Parser *p) {
202
197
int
203
198
_PyPegen_fill_token (Parser * p )
204
199
{
205
- const char * start ;
206
- const char * end ;
207
- int type = _PyTokenizer_Get (p -> tok , & start , & end );
200
+ struct token new_token ;
201
+ int type = _PyTokenizer_Get (p -> tok , & new_token );
208
202
209
203
// Record and skip '# type: ignore' comments
210
204
while (type == TYPE_IGNORE ) {
211
- Py_ssize_t len = end - start ;
205
+ Py_ssize_t len = new_token . end_col_offset - new_token . col_offset ;
212
206
char * tag = PyMem_Malloc (len + 1 );
213
207
if (tag == NULL ) {
214
208
PyErr_NoMemory ();
215
209
return -1 ;
216
210
}
217
- strncpy (tag , start , len );
211
+ strncpy (tag , new_token . start , len );
218
212
tag [len ] = '\0' ;
219
213
// Ownership of tag passes to the growable array
220
214
if (!growable_comment_array_add (& p -> type_ignore_comments , p -> tok -> lineno , tag )) {
221
215
PyErr_NoMemory ();
222
216
return -1 ;
223
217
}
224
- type = _PyTokenizer_Get (p -> tok , & start , & end );
218
+ type = _PyTokenizer_Get (p -> tok , & new_token );
225
219
}
226
220
227
221
// If we have reached the end and we are in single input mode we need to insert a newline and reset the parsing
@@ -244,7 +238,7 @@ _PyPegen_fill_token(Parser *p)
244
238
}
245
239
246
240
Token * t = p -> tokens [p -> fill ];
247
- return initialize_token (p , t , start , end , type );
241
+ return initialize_token (p , t , & new_token , type );
248
242
}
249
243
250
244
#if defined(Py_DEBUG )
0 commit comments