From 265ff8f87dc7392fdf701f811eb2bf54d7bc6678 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Fri, 3 Feb 2023 10:52:35 +0800 Subject: Finally produced the first correct forest Finally the prototype parser has produced the first correct forest. It is my first time to generate a correct forest, in fact, ever since the beginning of this project. --- chain/src/atom/default.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'chain/src/atom/default.rs') diff --git a/chain/src/atom/default.rs b/chain/src/atom/default.rs index e88cfc9..9c91296 100644 --- a/chain/src/atom/default.rs +++ b/chain/src/atom/default.rs @@ -48,18 +48,31 @@ pub struct DefaultAtom { impl DefaultAtom { /// Return the string description of a rule position. pub fn rule_pos_string(&self, pos: usize) -> Result> { + if pos == self.grammar.total() { + return Ok("End of rules".to_owned()); + } + let rule_num = self.grammar.get_rule_num(pos)?; assert!(rule_num < self.grammar.non_num()); - let display_tnt = |tnt| self.name_of_tnt(tnt).unwrap_or_else(|e| format!("{e}")); + let display_tnt = |tnt| { + format!( + " {} ", + self.name_of_tnt(match tnt { + TNT::Non(_) => tnt, + TNT::Ter(t) => self.unpack_tnt(t).unwrap(), + }) + .unwrap_or_else(|e| format!("{e}")) + ) + }; Ok(self.regexp.get(rule_num).unwrap().to_string_with_dot( display_tnt, if rule_num == 0 { pos } else { - pos - self.grammar.nth_accumulator(rule_num - 1)? + pos - self.grammar.nth_accumulator(rule_num)? }, )?) } @@ -394,8 +407,6 @@ impl DefaultAtom { } } - // dbg!(&accumulators); - for nt in 0..nt_num { let children: std::collections::HashMap<_, _> = nfa // This is safe because of the above assertion. -- cgit v1.2.3-18-g5258