Browse Source

adding error branch

pull/1/head
Jason Williams 6 years ago
parent
commit
0903a7abce
  1. 2
      src/bin/bin.rs
  2. 22
      src/lib/syntax/lexer.rs

2
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")
}

22
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<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
@ -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 {
'"' | '\'' => {

Loading…
Cancel
Save