use crate::types::{InsnId, Pos};
pub trait ParseTracer {
fn on_trace(&mut self, label: &str, pos: Pos, ip: InsnId);
fn on_call(&mut self, _rule_name: &str, _pos: Pos) {}
fn on_return(&mut self, _rule_name: &str, _pos: Pos, _succeeded: bool) {}
}
#[cfg(feature = "std")]
#[derive(Default)]
pub struct PrintTracer {
pub indent: usize,
}
#[cfg(feature = "std")]
impl ParseTracer for PrintTracer {
fn on_trace(&mut self, label: &str, pos: Pos, _ip: InsnId) {
eprintln!("{:>width$}@ {pos}: {label}", "", width = self.indent * 2);
}
fn on_call(&mut self, rule: &str, pos: Pos) {
eprintln!(
"{:>width$}→ {rule} (pos {pos})",
"",
width = self.indent * 2
);
self.indent += 1;
}
fn on_return(&mut self, rule: &str, pos: Pos, ok: bool) {
self.indent = self.indent.saturating_sub(1);
let mark = if ok { "✓" } else { "✗" };
eprintln!(
"{:>width$}{mark} {rule} (pos {pos})",
"",
width = self.indent * 2
);
}
}