summaryrefslogtreecommitdiff
path: root/chain/src/atom/default.rs
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2023-02-03 10:52:35 +0800
committerJSDurand <mmemmew@gmail.com>2023-02-03 10:52:35 +0800
commit265ff8f87dc7392fdf701f811eb2bf54d7bc6678 (patch)
tree35538c8ac7524e0d9f2acff8be21b72994728bc4 /chain/src/atom/default.rs
parentf28155105134b90fd86049c65478d307e0d8dbbc (diff)
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.
Diffstat (limited to 'chain/src/atom/default.rs')
-rw-r--r--chain/src/atom/default.rs19
1 files changed, 15 insertions, 4 deletions
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<String, Box<dyn std::error::Error>> {
+ 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.