refactor(tazjin/rlox): Thread through scanner errors

... 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
This commit is contained in:
Vincent Ambo 2020-11-28 18:20:10 +01:00 committed by tazjin
parent af793325c0
commit 36cf7bef24
3 changed files with 29 additions and 9 deletions

View file

@ -1,9 +1,24 @@
use crate::scanner;
use crate::errors::{report, Error};
use crate::scanner::{self, Token};
// Run some Lox code and print it to stdout
pub fn run(code: &str) {
let chars: Vec<char> = code.chars().collect();
for token in scanner::scan(&chars) {
match scanner::scan(&chars) {
Ok(tokens) => print_tokens(tokens),
Err(errors) => report_errors(errors),
}
}
fn print_tokens<'a>(tokens: Vec<Token<'a>>) {
for token in tokens {
println!("{:?}", token);
}
}
fn report_errors(errors: Vec<Error>) {
for error in errors {
report(&error);
}
}