@@ -56,11 +56,47 @@ impl IndentationLevel {
5656 }
5757}
5858
59+ #[ derive( Debug ) ]
60+ struct Indentations {
61+ indent_stack : Vec < IndentationLevel > ,
62+ }
63+
64+ impl Indentations {
65+ pub fn is_empty ( & self ) -> bool {
66+ self . indent_stack . len ( ) == 1
67+ }
68+
69+ pub fn push ( & mut self , indent : IndentationLevel ) {
70+ self . indent_stack . push ( indent) ;
71+ }
72+
73+ pub fn pop ( & mut self ) -> Option < IndentationLevel > {
74+ if self . is_empty ( ) {
75+ return None ;
76+ }
77+ self . indent_stack . pop ( )
78+ }
79+
80+ pub fn current ( & self ) -> & IndentationLevel {
81+ self . indent_stack
82+ . last ( )
83+ . expect ( "Indetations must have at least one level" )
84+ }
85+ }
86+
87+ impl Default for Indentations {
88+ fn default ( ) -> Self {
89+ Self {
90+ indent_stack : vec ! [ IndentationLevel :: default ( ) ] ,
91+ }
92+ }
93+ }
94+
5995pub struct Lexer < T : Iterator < Item = char > > {
6096 chars : T ,
6197 at_begin_of_line : bool ,
6298 nesting : usize , // Amount of parenthesis
63- indentation_stack : Vec < IndentationLevel > ,
99+ indentations : Indentations ,
64100 pending : Vec < Spanned > ,
65101 chr0 : Option < char > ,
66102 chr1 : Option < char > ,
@@ -157,7 +193,7 @@ where
157193 chars : input,
158194 at_begin_of_line : true ,
159195 nesting : 0 ,
160- indentation_stack : vec ! [ Default :: default ( ) ] ,
196+ indentations : Indentations :: default ( ) ,
161197 pending : Vec :: new ( ) ,
162198 location : start,
163199 chr0 : None ,
@@ -732,15 +768,15 @@ where
732768 }
733769
734770 // Determine indent or dedent:
735- let current_indentation = self . indentation_stack . last ( ) . unwrap ( ) ;
771+ let current_indentation = self . indentations . current ( ) ;
736772 let ordering = indentation_level. compare_strict ( current_indentation, self . get_pos ( ) ) ?;
737773 match ordering {
738774 Ordering :: Equal => {
739775 // Same same
740776 }
741777 Ordering :: Greater => {
742778 // New indentation level:
743- self . indentation_stack . push ( indentation_level) ;
779+ self . indentations . push ( indentation_level) ;
744780 let tok_pos = self . get_pos ( ) ;
745781 self . emit ( ( tok_pos, Tok :: Indent , tok_pos) ) ;
746782 }
@@ -749,12 +785,12 @@ where
749785 // Pop off other levels until col is found:
750786
751787 loop {
752- let current_indentation = self . indentation_stack . last ( ) . unwrap ( ) ;
788+ let current_indentation = self . indentations . current ( ) ;
753789 let ordering =
754790 indentation_level. compare_strict ( current_indentation, self . get_pos ( ) ) ?;
755791 match ordering {
756792 Ordering :: Less => {
757- self . indentation_stack . pop ( ) ;
793+ self . indentations . pop ( ) ;
758794 let tok_pos = self . get_pos ( ) ;
759795 self . emit ( ( tok_pos, Tok :: Dedent , tok_pos) ) ;
760796 }
@@ -817,8 +853,8 @@ where
817853 }
818854
819855 // Next, flush the indentation stack to zero.
820- while self . indentation_stack . len ( ) > 1 {
821- self . indentation_stack . pop ( ) ;
856+ while ! self . indentations . is_empty ( ) {
857+ self . indentations . pop ( ) ;
822858 self . emit ( ( tok_pos, Tok :: Dedent , tok_pos) ) ;
823859 }
824860
@@ -1267,7 +1303,7 @@ where
12671303 "Lex token {:?}, nesting={:?}, indent stack: {:?}" ,
12681304 token,
12691305 self . nesting,
1270- self . indentation_stack
1306+ self . indentations ,
12711307 ) ;
12721308
12731309 match token {
0 commit comments