summaryrefslogtreecommitdiff
path: root/chain/src/default.rs
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2023-02-12 12:07:34 +0800
committerJSDurand <mmemmew@gmail.com>2023-02-12 12:07:34 +0800
commit987c84f3454c687cca0efe0d471fcf00e052ecab (patch)
tree04b9cf073a12adfb5d07ae308c3809e88cf4ebd2 /chain/src/default.rs
parent265ff8f87dc7392fdf701f811eb2bf54d7bc6678 (diff)
Added the functionality of split or clone.
I need more than the ability to clone nodes: I also need to split the nodes. Now this seems to be correctly added.
Diffstat (limited to 'chain/src/default.rs')
-rw-r--r--chain/src/default.rs33
1 files changed, 32 insertions, 1 deletions
diff --git a/chain/src/default.rs b/chain/src/default.rs
index 5e94623..31c1002 100644
--- a/chain/src/default.rs
+++ b/chain/src/default.rs
@@ -38,6 +38,8 @@ pub enum Error {
CannotPlant,
/// Trying to insert an empty item.
EmptyItem,
+ /// Cannot split a packed node.
+ SplitPack(usize),
/// An invalid situation happens.
Invalid,
}
@@ -56,6 +58,7 @@ impl Display for Error {
Self::CannotClone(fe) => write!(f, "cannot clone due to {fe}"),
Self::CannotPlant => write!(f, "cannot plant a new forest fragment"),
Self::EmptyItem => write!(f, "trying to insert an empty item"),
+ Self::SplitPack(n) => write!(f, "cannot split the packed node {n}"),
Self::Invalid => write!(f, "invalid"),
}
}
@@ -82,6 +85,7 @@ impl From<ForestError> for Error {
ForestError::InvalidGraphError(ge) => ge.into(),
ForestError::NodeNoLabel(n) => Error::NodeNoLabel(n),
ForestError::LabelConversion(ce) => Error::CannotClone(ce),
+ ForestError::SplitPack(n) => Error::SplitPack(n),
}
}
}
@@ -783,7 +787,6 @@ mod test_chain {
let mut chain = DefaultChain::unit(atom)?;
chain.chain(3, 00)?;
- dbg!("hi");
chain.chain(1, 01)?;
chain.chain(2, 02)?;
chain.chain(2, 03)?;
@@ -811,6 +814,34 @@ mod test_chain {
}
#[test]
+ fn test_ambiguity() -> Result<(), Box<dyn std::error::Error>> {
+ let grammar = new_paren_grammar()?;
+
+ let atom = DefaultAtom::from_grammar(grammar)?;
+
+ let mut chain = DefaultChain::unit(atom)?;
+ chain.chain(0, 0)?;
+ chain.chain(2, 1)?;
+ chain.chain(2, 2)?;
+ // chain.chain(2, 3)?;
+ chain.chain(1, 3)?;
+
+ dbg!(chain.current(), chain.history());
+
+ #[cfg(feature = "test-print-viz")]
+ {
+ chain.graph.print_viz("chain.gv")?;
+ chain.atom.print_nfa("nfa.gv")?;
+ chain.forest.print_viz("forest.gv")?;
+ item::default::print_labels(&chain.atom, &chain.forest)?;
+ }
+
+ assert!(matches!(chain.epsilon(), Ok(true)));
+
+ Ok(())
+ }
+
+ #[test]
fn test_speed() -> Result<(), Box<dyn std::error::Error>> {
let grammar = new_notes_grammar_no_regexp()?;