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() { pub fn main() {
let buffer = read_to_string("test.js").unwrap(); let buffer = read_to_string("test.js").unwrap();
let mut lexer = Lexer::new(&buffer); 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. /// 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 {
'"' | '\'' => { '"' | '\'' => {

Loading…
Cancel
Save