@@ -52,6 +52,9 @@ impl<'a> LuaLexer<'a> {
5252 if self . tokens . is_empty ( ) {
5353 self . current_token = LuaTokenKind :: TkEof ;
5454 } else {
55+ // Initialize line from first token
56+ self . line = self . tokens [ 0 ] . line ;
57+ self . lastline = 1 ; // lastline starts at 1
5558 self . current_token = self . tokens [ 0 ] . kind ;
5659 }
5760
@@ -128,14 +131,19 @@ impl<'a> LuaLexer<'a> {
128131 self . lastline = self . line ;
129132
130133 let mut next_index = self . token_index + 1 ;
131- self . skip_trivia_and_update_line ( & mut next_index) ;
134+ // Skip trivia tokens to find next real token
135+ self . skip_trivia ( & mut next_index) ;
132136 self . token_index = next_index;
133137
134138 if self . token_index >= self . tokens . len ( ) {
135139 self . current_token = LuaTokenKind :: TkEof ;
136140 return ;
137141 }
138142
143+ // CRITICAL FIX: Update line from token's ending line number
144+ // This ensures multi-line tokens (long strings, multi-line comments)
145+ // correctly update linenumber (matches Lua C's behavior in llex)
146+ self . line = self . tokens [ self . token_index ] . line ;
139147 self . current_token = self . tokens [ self . token_index ] . kind ;
140148 }
141149
@@ -164,24 +172,6 @@ impl<'a> LuaLexer<'a> {
164172 kind = self . tokens [ * index] . kind ;
165173 }
166174 }
167-
168- fn skip_trivia_and_update_line ( & mut self , index : & mut usize ) {
169- if index >= & mut self . tokens . len ( ) {
170- return ;
171- }
172-
173- let mut kind = self . tokens [ * index] . kind ;
174- while is_trivia_kind ( kind) {
175- if kind == LuaTokenKind :: TkEndOfLine {
176- self . line += 1 ;
177- }
178- * index += 1 ;
179- if * index >= self . tokens . len ( ) {
180- break ;
181- }
182- kind = self . tokens [ * index] . kind ;
183- }
184- }
185175}
186176
187177fn is_trivia_kind ( kind : LuaTokenKind ) -> bool {
0 commit comments