diff --git a/src/bin/bin.rs b/src/bin/bin.rs index 8ba7ab81c6..dfed6e9f78 100644 --- a/src/bin/bin.rs +++ b/src/bin/bin.rs @@ -5,5 +5,5 @@ use std::fs::read_to_string; pub fn main() { let buffer = read_to_string("test.js").unwrap(); let mut lexer = Lexer::new(&buffer); - lexer.lex().unwrap() + lexer.lex().expect("finished") } diff --git a/src/lib/syntax/lexer.rs b/src/lib/syntax/lexer.rs index c526e2be17..59ea7b3b7e 100644 --- a/src/lib/syntax/lexer.rs +++ b/src/lib/syntax/lexer.rs @@ -120,7 +120,10 @@ impl<'a> Lexer<'a> { /// next fetches the next token and return it, or a LexerError if there are no more. fn next(&mut self) -> Result { - self.buffer.next().ok_or(LexerError::new("next failed")) + match self.buffer.next() { + Some(char) => Ok(char), + None => Err(LexerError::new("finished")), + } } /// read_line attempts to read until the end of the line and returns the String object or a LexerError @@ -146,7 +149,7 @@ impl<'a> Lexer<'a> { // No need to return a reference, we can return a copy match self.buffer.peek() { Some(v) => Ok(*v), - None => Err(LexerError::new("uidhi")), + None => Err(LexerError::new("finished")), } } @@ -161,10 +164,17 @@ impl<'a> Lexer<'a> { pub fn lex(&mut self) -> Result<(), LexerError> { loop { - let ch = match self.next() { - Ok(ch) => ch, - Err(lexer_error) => return Err(lexer_error), - }; + // Check if we've reached the end + match self.preview_next() { + Ok(_) => (), // If there are still characters, carry on + Err(LexerError { details }) => { + if details == "finished" { + // If there are no more characters left in the Chars iterator, we should just return + return Ok(()); + } + } + } + let ch = self.next()?; self.column_number += 1; match ch { '"' | '\'' => {