@@ -82,23 +82,13 @@ impl<'a> Lexer<'a> {
8282 if let Some ( char) = self . next ( ) {
8383 match self . scan_char ( & char) {
8484 Ok ( Some ( ( token_type, value) ) ) => self . add_token ( token_type, value, start) ,
85- Err ( err) => {
86- self . errors . push ( EngineErrorKind :: LexerError ( LexerError {
87- #[ cfg( feature = "cli" ) ]
88- source_file : self . get_source_sliced ( start, self . cursor ) ,
89- start,
90- end : self . cursor ,
91- kind : err,
92- } ) ) ;
93- }
85+ Err ( err) => self . add_error ( start, err) ,
9486 _ => { }
9587 }
9688 }
9789 }
9890
99- let start = self . cursor ;
100- self . cursor . next_line ( ) ;
101- self . add_token ( LexerTokenKind :: EOL , None , start) ;
91+ self . add_token ( LexerTokenKind :: EOF , None , self . cursor ) ;
10292
10393 & self . tokens
10494 }
@@ -258,6 +248,20 @@ impl<'a> Lexer<'a> {
258248 } ) ;
259249 }
260250
251+ fn add_error (
252+ & mut self ,
253+ start : Cursor ,
254+ err : LexerErrorKind
255+ ) {
256+ self . errors . push ( EngineErrorKind :: LexerError ( LexerError {
257+ #[ cfg( feature = "cli" ) ]
258+ source_file : self . get_source_sliced ( start, self . cursor ) ,
259+ start,
260+ end : self . cursor ,
261+ kind : err,
262+ } ) )
263+ }
264+
261265 /// Consumes a single-line comment (aka skips to the end of the line and returns nothing)
262266 fn consume_single_line_comment ( & mut self ) -> LexerResult < ( ) > {
263267 self . eat_until ( & [ '\n' ] , false ) ;
@@ -280,7 +284,15 @@ impl<'a> Lexer<'a> {
280284 /// Attempts to return a [`TokenType::String`]
281285 fn consume_string ( & mut self ) -> LexerResult < ( LexerTokenKind , Option < Box < LexerLiteral > > ) > {
282286 let string = self . eat_until ( & [ '"' , '\n' ] , true ) . unwrap_or_default ( ) ;
283- self . expect_char ( & '"' ) ?;
287+
288+ if let Err ( err) = self . expect_char ( & '"' ) {
289+ if let LexerErrorKind :: ExpectedCharacter { found : Some ( found) , .. } = & err {
290+ self . cursor_next ( found) ;
291+ }
292+
293+ return Err ( err) ;
294+ }
295+
284296 Ok ( (
285297 LexerTokenKind :: String ,
286298 Some ( Box :: from ( LexerLiteral :: String ( Box :: from ( string) ) ) ) ,
@@ -366,7 +378,7 @@ impl<'a> Lexer<'a> {
366378 }
367379
368380 _ => {
369- return Err ( LexerErrorKind :: UnexpectedCharacter {
381+ return Err ( LexerErrorKind :: ExpectedCharacter {
370382 expected : "0..9" . to_string ( ) ,
371383 found : Some ( char) ,
372384 } )
@@ -404,7 +416,7 @@ impl<'a> Lexer<'a> {
404416 LexerErrorKind :: IntegerOverflow ( collector)
405417 }
406418
407- IntErrorKind :: InvalidDigit => LexerErrorKind :: UnexpectedCharacter {
419+ IntErrorKind :: InvalidDigit => LexerErrorKind :: ExpectedCharacter {
408420 expected : "0..9" . to_string ( ) ,
409421 found : None ,
410422 } ,
@@ -468,7 +480,7 @@ impl<'a> Lexer<'a> {
468480
469481 fn expect_char ( & mut self , expected : & char ) -> LexerResult < char > {
470482 self . expect ( expected)
471- . map_err ( |found| LexerErrorKind :: UnexpectedCharacter {
483+ . map_err ( |found| LexerErrorKind :: ExpectedCharacter {
472484 expected : expected. to_string ( ) ,
473485 found,
474486 } )
@@ -491,7 +503,11 @@ impl<'a> ComponentIter<'a, char, char, Chars<'a>> for Lexer<'a> {
491503 & mut self . chars
492504 }
493505
494- fn cursor_next ( & mut self , item : & char ) {
495- self . cursor . next ( item) ;
506+ fn cursor_next ( & mut self , char : & char ) {
507+ if char == & '\n' {
508+ self . cursor . next_line ( ) ;
509+ } else {
510+ self . cursor . next_col ( ) ;
511+ }
496512 }
497513}
0 commit comments