|
|
@ -120,7 +120,10 @@ impl<'a> Lexer<'a> { |
|
|
|
|
|
|
|
|
|
|
|
/// next fetches the next token and return it, or a LexerError if there are no more.
|
|
|
|
/// next fetches the next token and return it, or a LexerError if there are no more.
|
|
|
|
fn next(&mut self) -> Result<char, LexerError> { |
|
|
|
fn next(&mut self) -> Result<char, LexerError> { |
|
|
|
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
|
|
|
|
/// 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
|
|
|
|
// No need to return a reference, we can return a copy
|
|
|
|
match self.buffer.peek() { |
|
|
|
match self.buffer.peek() { |
|
|
|
Some(v) => Ok(*v), |
|
|
|
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> { |
|
|
|
pub fn lex(&mut self) -> Result<(), LexerError> { |
|
|
|
loop { |
|
|
|
loop { |
|
|
|
let ch = match self.next() { |
|
|
|
// Check if we've reached the end
|
|
|
|
Ok(ch) => ch, |
|
|
|
match self.preview_next() { |
|
|
|
Err(lexer_error) => return Err(lexer_error), |
|
|
|
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; |
|
|
|
self.column_number += 1; |
|
|
|
match ch { |
|
|
|
match ch { |
|
|
|
'"' | '\'' => { |
|
|
|
'"' | '\'' => { |
|
|
|