main: print parse stats when parse errors

This commit is contained in:
2025-10-24 05:25:38 -04:00
parent 89e4afcc42
commit 287b57abd7

View File

@@ -184,6 +184,13 @@ fn subst() -> Result<(), Box<dyn Error>> {
} }
} }
fn plural(count: usize) -> &'static str {
match count {
1 => "",
_ => "s",
}
}
fn parse(document: &str) { fn parse(document: &str) {
let mut parser = Parser::new(Lexer::new(document)); let mut parser = Parser::new(Lexer::new(document));
let verbose = !matches!( let verbose = !matches!(
@@ -193,11 +200,21 @@ fn parse(document: &str) {
for idx in 0.. { for idx in 0.. {
match parser.parse::<Expr>(0) { match parser.parse::<Expr>(0) {
Err(e @ ParseError::EOF(s)) if s.tail == document.len() as _ => { Err(e @ ParseError::EOF(s)) if s.tail == document.len() as _ => {
println!("\x1b[92m{e} (total {} bytes)\x1b[0m", document.len()); println!(
"\x1b[92m{e} (total {} byte{}, {idx} expression{})\x1b[0m",
document.len(),
plural(document.len()),
plural(idx),
);
break; break;
} }
Err(e @ ParseError::EOF(_)) => { Err(e @ ParseError::EOF(_)) => {
println!("\x1b[93m{e} (total {} bytes)\x1b[0m", document.len()); println!(
"\x1b[93m{e} (total {} byte{}, {idx} expression{})\x1b[0m",
document.len(),
plural(document.len()),
plural(idx),
);
break; break;
} }
Err(e) => { Err(e) => {