Mercurial > lbo > hg > rex
changeset 69:694d965661c7
Implement basic tracing/debugging facility
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 01 Sep 2019 20:44:33 +0200 |
parents | be9b8751f568 |
children | 06019f9a273c |
files | src/matcher.rs src/matching.rs src/state.rs |
diffstat | 3 files changed, 16 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/matcher.rs Sat Aug 31 21:00:53 2019 +0200 +++ b/src/matcher.rs Sun Sep 01 20:44:33 2019 +0200 @@ -3,7 +3,7 @@ #![allow(dead_code)] use std::fmt::Debug; -use std::iter::FromIterator; +use std::iter::{self, FromIterator}; use std::rc::Rc; /// Matchee contains a character and position to match. It's used by the matching logic to check @@ -46,6 +46,11 @@ pub fn finished(&self) -> bool { self.ix == self.src.len() } + pub fn string(&self) -> String { + let matchee = String::from_iter(self.src.iter()); + let pointer = String::from_iter(iter::repeat(' ').take(self.ix).chain(iter::once('^'))); + format!("{}\n{}", matchee, pointer) + } } /// A Matcher matches parts of a Matchee (where a Matchee is a string to be matched). While
--- a/src/matching.rs Sat Aug 31 21:00:53 2019 +0200 +++ b/src/matching.rs Sun Sep 01 20:44:33 2019 +0200 @@ -24,6 +24,7 @@ submatches: Rc<RefCell<Vec<Option<usize>>>>, // We need to clone the submatches queue only rarely (when a submatch starts or ends). submatches_todo: Rc<Vec<usize>>, + debug: bool, } impl MatchState { @@ -33,6 +34,7 @@ matchee: Matchee::from_string(s), submatches: Rc::new(RefCell::new(vec![None; s.len()])), submatches_todo: Rc::new(Vec::with_capacity(4)), + debug: false, } } fn fork(&self, next: StateRef, advance: usize) -> MatchState { @@ -65,6 +67,13 @@ self.submatches.borrow_mut()[begin] = Some(self.matchee.pos()); } } + fn debug(&self, sg: &StateGraph) -> String { + let m = self.matchee.string(); + let out = sg[self.node].out.map_or("".to_owned(), |ix| sg[ix].to_string()); + let out1 = sg[self.node].out1.map_or("".to_owned(), |ix| sg[ix].to_string()); + let full = format!("{} (<{}> next: 1. <{:?}> {} 2. <{:?}> {})\n", m, self.node, sg[self.node].out, out, sg[self.node].out1, out1); + full + } } /// do_match starts the matching process. It tries to match the supplied compiled regex against the