From 987c84f3454c687cca0efe0d471fcf00e052ecab Mon Sep 17 00:00:00 2001 From: JSDurand Date: Sun, 12 Feb 2023 12:07:34 +0800 Subject: 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. --- chain/src/default.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'chain/src/default.rs') 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 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)?; @@ -810,6 +813,34 @@ mod test_chain { Ok(()) } + #[test] + fn test_ambiguity() -> Result<(), Box> { + 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> { let grammar = new_notes_grammar_no_regexp()?; -- cgit v1.2.3-18-g5258