diff options
author | Vincent Ambo <mail@tazj.in> | 2020-11-28T17·20+0100 |
---|---|---|
committer | tazjin <mail@tazj.in> | 2020-11-28T17·26+0000 |
commit | 36cf7bef24bbd5ffccc8bac47a8c4d10dd5a265c (patch) | |
tree | ccec3463fde0fc088f575d534745a1648844578e /users/tazjin/rlox/src/scanner.rs | |
parent | af793325c03d88add6efbcc9425a3db488f2cff9 (diff) |
refactor(tazjin/rlox): Thread through scanner errors r/1956
... and show them to users, very crudely. Change-Id: If4491b14db1124313f6ab7e5fbfdce9fea501d11 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2193 Reviewed-by: tazjin <mail@tazj.in> Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/rlox/src/scanner.rs')
-rw-r--r-- | users/tazjin/rlox/src/scanner.rs | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/users/tazjin/rlox/src/scanner.rs b/users/tazjin/rlox/src/scanner.rs index e0b04981b4a4..2eed89bd6beb 100644 --- a/users/tazjin/rlox/src/scanner.rs +++ b/users/tazjin/rlox/src/scanner.rs @@ -254,19 +254,18 @@ impl<'a> Scanner<'a> { self.add_token(token_kind); } - fn scan_tokens(mut self) -> Vec<Token<'a>> { + fn scan_tokens(&mut self) { while !self.is_at_end() { self.start = self.current; self.scan_token(); } self.add_token(TokenKind::Eof); - return self.tokens; } } -pub fn scan<'a>(input: &'a [char]) -> Vec<Token<'a>> { - let scanner = Scanner { +pub fn scan<'a>(input: &'a [char]) -> Result<Vec<Token<'a>>, Vec<Error>> { + let mut scanner = Scanner { source: &input, tokens: vec![], errors: vec![], @@ -275,5 +274,11 @@ pub fn scan<'a>(input: &'a [char]) -> Vec<Token<'a>> { line: 0, }; - return scanner.scan_tokens(); + scanner.scan_tokens(); + + if !scanner.errors.is_empty() { + return Err(scanner.errors); + } + + return Ok(scanner.tokens); } |