22
33use super :: TokenType ;
44use super :: tables:: * ;
5- use alloc:: collections:: VecDeque ;
65use alloc:: vec:: Vec ;
76use core:: cmp:: Ordering ;
87
@@ -17,7 +16,7 @@ Scanner State
1716pub ( super ) struct Scanner < ' a > {
1817 pub src : & ' a [ u8 ] ,
1918 pub pos : usize ,
20- pub pending : VecDeque < ( TokenType , usize , usize , usize ) > ,
19+ pub pending : Vec < ( TokenType , usize , usize , usize ) > ,
2120 pub indent_stack : Vec < usize > ,
2221 pub nesting : u32 ,
2322 pub line : usize ,
@@ -28,7 +27,7 @@ impl<'a> Scanner<'a> {
2827 pub fn new ( src : & ' a [ u8 ] ) -> Self {
2928 Self {
3029 src, pos : 0 ,
31- pending : VecDeque :: new ( ) ,
30+ pending : Vec :: new ( ) ,
3231 indent_stack : Vec :: new ( ) ,
3332 nesting : 0 , line : 0 ,
3433 fstring_stack : Vec :: new ( ) ,
@@ -192,8 +191,8 @@ impl<'a> Scanner<'a> {
192191 let quote_len = if triple { 3 } else { 1 } ;
193192 self . pos = prefix_end + quote_len;
194193 let body_start = self . pos ;
195- self . pending . push_back ( ( TokenType :: FstringStart , self . line , start, body_start) ) ;
196194 self . scan_fstring_body ( quote, triple, body_start) ;
195+ self . pending . push ( ( TokenType :: FstringStart , self . line , start, body_start) ) ;
197196 }
198197
199198 fn scan_fstring_body ( & mut self , quote : u8 , triple : bool , body_start : usize ) {
@@ -208,26 +207,29 @@ impl<'a> Scanner<'a> {
208207 self . src [ pos] == quote
209208 } ;
210209 if closes {
210+ let ql = if triple { 3 } else { 1 } ;
211+ self . pending . push ( ( TokenType :: FstringEnd , self . line , pos, pos + ql) ) ;
211212 if pos > self . pos {
212- self . pending . push_back ( ( TokenType :: FstringMiddle , self . line , body_start, pos) ) ;
213+ self . pending . push ( ( TokenType :: FstringMiddle , self . line , body_start, pos) ) ;
213214 }
214- let ql = if triple { 3 } else { 1 } ;
215- self . pending . push_back ( ( TokenType :: FstringEnd , self . line , pos, pos + ql) ) ;
216215 self . pos = pos + ql;
217216 return ;
218217 }
219218 match self . src [ pos] {
220219 b'\\' => pos = ( pos + 2 ) . min ( self . src . len ( ) ) ,
221220 b'{' if self . src . get ( pos + 1 ) != Some ( & b'{' ) => {
222- if pos > self . pos {
223- self . pending . push_back ( ( TokenType :: FstringMiddle , self . line , body_start, pos) ) ;
224- }
225221 if self . fstring_stack . len ( ) >= MAX_FSTRING_DEPTH {
226- self . pending . push_back ( ( TokenType :: Endmarker , self . line , pos, pos) ) ;
222+ self . pending . push ( ( TokenType :: Endmarker , self . line , pos, pos) ) ;
223+ if pos > self . pos {
224+ self . pending . push ( ( TokenType :: FstringMiddle , self . line , body_start, pos) ) ;
225+ }
227226 self . pos = pos + 1 ;
228227 return ;
229228 }
230- self . pending . push_back ( ( TokenType :: Lbrace , self . line , pos, pos + 1 ) ) ;
229+ self . pending . push ( ( TokenType :: Lbrace , self . line , pos, pos + 1 ) ) ;
230+ if pos > self . pos {
231+ self . pending . push ( ( TokenType :: FstringMiddle , self . line , body_start, pos) ) ;
232+ }
231233 self . fstring_stack . push ( ( quote, triple, pos + 1 , self . nesting ) ) ;
232234 self . pos = pos + 1 ;
233235 return ;
@@ -249,7 +251,7 @@ impl<'a> Scanner<'a> {
249251 self . line += 1 ;
250252
251253 if self . nesting > 0 {
252- self . pending . push_back ( ( TokenType :: Nl , current_line, start, self . pos ) ) ;
254+ self . pending . push ( ( TokenType :: Nl , current_line, start, self . pos ) ) ;
253255 return ;
254256 }
255257
@@ -259,41 +261,45 @@ impl<'a> Scanner<'a> {
259261 let mut p = self . pos ;
260262 while p < self . src . len ( ) && ( self . src [ p] == b' ' || self . src [ p] == b'\t' ) {
261263 has_space |= self . src [ p] == b' ' ;
262- has_tab |= self . src [ p] == b'\t' ;
264+ has_tab |= self . src [ p] == b'\t' ;
263265 level += 1 ; p += 1 ;
264266 }
265267
266268 if has_space && has_tab {
267- self . pending . push_back ( ( TokenType :: Newline , current_line, start, self . pos ) ) ;
268- self . pending . push_back ( ( TokenType :: Endmarker , current_line, start, self . pos ) ) ;
269+ self . pending . push ( ( TokenType :: Endmarker , current_line, start, self . pos ) ) ; // sale 2.º
270+ self . pending . push ( ( TokenType :: Newline , current_line, start, self . pos ) ) ; // sale 1.º
269271 return ;
270272 }
271273
272274 if matches ! ( self . src. get( p) , Some ( b'\n' | b'\r' | b'#' ) ) {
273- self . pending . push_back ( ( TokenType :: Nl , current_line, start, self . pos ) ) ;
275+ self . pending . push ( ( TokenType :: Nl , current_line, start, self . pos ) ) ;
274276 return ;
275277 }
276278
277279 let line_pos = self . pos + level;
278280 let current = * self . indent_stack . last ( ) . unwrap_or ( & 0 ) ;
279- self . pending . push_back ( ( TokenType :: Newline , current_line, start, self . pos ) ) ;
280281
281282 match level. cmp ( & current) {
282283 Ordering :: Greater => {
283284 if self . indent_stack . len ( ) >= MAX_INDENT_DEPTH {
284- self . pending . push_back ( ( TokenType :: Endmarker , current_line, start, self . pos ) ) ;
285+ self . pending . push ( ( TokenType :: Endmarker , current_line, start, self . pos ) ) ; // sale 2.º
286+ self . pending . push ( ( TokenType :: Newline , current_line, start, self . pos ) ) ; // sale 1.º
285287 return ;
286288 }
287289 self . indent_stack . push ( level) ;
288- self . pending . push_back ( ( TokenType :: Indent , self . line , line_pos, line_pos) ) ;
290+ self . pending . push ( ( TokenType :: Indent , self . line , line_pos, line_pos) ) ; // sale 2.º
291+ self . pending . push ( ( TokenType :: Newline , current_line, start, self . pos ) ) ; // sale 1.º
289292 }
290293 Ordering :: Less => {
291294 while self . indent_stack . last ( ) . is_some_and ( |& t| t > level) {
292295 self . indent_stack . pop ( ) ;
293- self . pending . push_back ( ( TokenType :: Dedent , self . line , line_pos, line_pos) ) ;
296+ self . pending . push ( ( TokenType :: Dedent , self . line , line_pos, line_pos) ) ; // salen últimos
294297 }
298+ self . pending . push ( ( TokenType :: Newline , current_line, start, self . pos ) ) ; // sale 1.º
299+ }
300+ Ordering :: Equal => {
301+ self . pending . push ( ( TokenType :: Newline , current_line, start, self . pos ) ) ;
295302 }
296- Ordering :: Equal => { }
297303 }
298304 }
299305
@@ -307,17 +313,17 @@ impl<'a> Scanner<'a> {
307313 if let Some ( & ( _, _, _, saved_nesting) ) = self . fstring_stack . last ( ) {
308314 if self . nesting > saved_nesting {
309315 self . nesting -= 1 ;
310- self . pending . push_back ( ( TokenType :: Rbrace , self . line , start, end) ) ;
316+ self . pending . push ( ( TokenType :: Rbrace , self . line , start, end) ) ;
311317 } else {
312318 let ( quote, triple, _, _) = self . fstring_stack . pop ( ) . unwrap ( ) ;
313- self . pending . push_back ( ( TokenType :: Rbrace , self . line , start, end) ) ;
314319 self . pos = end;
315320 self . scan_fstring_body ( quote, triple, end) ;
321+ self . pending . push ( ( TokenType :: Rbrace , self . line , start, end) ) ;
316322 return ;
317323 }
318324 } else {
319325 self . nesting = self . nesting . saturating_sub ( 1 ) ;
320- self . pending . push_back ( ( TokenType :: Rbrace , self . line , start, end) ) ;
326+ self . pending . push ( ( TokenType :: Rbrace , self . line , start, end) ) ;
321327 }
322328 self . pos = end;
323329 }
@@ -328,7 +334,7 @@ impl<'a> Scanner<'a> {
328334 */
329335
330336 pub fn next_token ( & mut self ) -> Option < ( TokenType , usize , usize , usize ) > {
331- if let Some ( tok) = self . pending . pop_front ( ) {
337+ if let Some ( tok) = self . pending . pop ( ) {
332338 return Some ( tok) ;
333339 }
334340
@@ -343,7 +349,7 @@ impl<'a> Scanner<'a> {
343349 if b == b'\n' {
344350 self . pos += 1 ;
345351 self . handle_newline ( start) ;
346- return self . pending . pop_front ( ) ;
352+ return self . pending . pop ( ) ;
347353 }
348354
349355 // Comment
@@ -365,7 +371,7 @@ impl<'a> Scanner<'a> {
365371 if q == b'"' || q == b'\'' {
366372 let pe = self . pos ;
367373 self . start_fstring ( start, pe) ;
368- return self . pending . pop_front ( ) ;
374+ return self . pending . pop ( ) ;
369375 }
370376 }
371377 }
@@ -414,7 +420,7 @@ impl<'a> Scanner<'a> {
414420 // Close brace (f-string aware)
415421 if b == b'}' {
416422 self . close_brace ( start) ;
417- return self . pending . pop_front ( ) ;
423+ return self . pending . pop ( ) ;
418424 }
419425
420426 // Multi-char operators: 3-char
0 commit comments