diff options
Diffstat (limited to 'chain/src/item/default/mod.rs')
-rw-r--r-- | chain/src/item/default/mod.rs | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/chain/src/item/default/mod.rs b/chain/src/item/default/mod.rs index 22069d2..3903162 100644 --- a/chain/src/item/default/mod.rs +++ b/chain/src/item/default/mod.rs @@ -628,7 +628,7 @@ impl<T: GraphLabel> DefaultForest<ForestLabel<T>> { /// case we clone it only when its degree is larger than one. /// /// Return either the original node or the cloned node at the end. - pub(crate) fn plant_if_needed( + pub(crate) fn plant_at_start( &mut self, node: usize, mut fragment: Self, @@ -691,6 +691,34 @@ impl<T: GraphLabel> DefaultForest<ForestLabel<T>> { Ok(node) } + + /// Extract the node from `pavi`. + /// + /// If pavi is a parent, this returns the child pointed to by the + /// represented edge. + /// + /// If pavi is a virtual node, this simply returns the virtual + /// node. + /// + /// If pavi is empty, this returns the root, unless the forest is + /// empty. + /// + /// # Guarantee + /// + /// The returned node is guaranteed to be a valid node. + pub(crate) fn node_from_pavi(&self, pavi: PaVi) -> Result<usize, Error> { + match pavi { + PaVi::Parent(node, edge) => Ok(self.nth_child(node, edge)?), + PaVi::Virtual(_, _, node) => { + if node >= self.nodes_len() { + return Err(Error::IndexOutOfBounds(node, self.nodes_len())); + } + + Ok(node) + } + PaVi::Empty => Ok(self.root().ok_or(Error::IndexOutOfBounds(0, 0))?), + } + } } pub mod splone; @@ -831,6 +859,7 @@ impl DefaultForest<ForestLabel<GrammarLabel>> { /// `child` is the index of the child node in question, and /// `index` means that the child is the `index`-th child of the /// node. + #[allow(dead_code)] pub(crate) fn position_search( &self, node: usize, @@ -1052,7 +1081,7 @@ impl DefaultForest<ForestLabel<GrammarLabel>> { // } /// Set the position of every node. - pub(crate) fn set_pos(&mut self, pos: usize) -> Result<(), Error> { + pub(crate) fn set_pos(&mut self, pos: usize, allp: bool) -> Result<(), Error> { let mut builder = PLGBuilderMut::from_graph_mut(&mut self.graph); for node in 0..builder.nodes_len() { @@ -1064,7 +1093,7 @@ impl DefaultForest<ForestLabel<GrammarLabel>> { label_label.set_start(pos); - if matches!(label_label.label().tnt(), Some(TNT::Ter(_))) { + if allp || matches!(label_label.label().tnt(), Some(TNT::Ter(_))) { label_label.set_end(pos + 1); } else if builder.degree(node)? == 1 && label_label.label().rule().is_some() { let child = builder.children_of(node)?.next().unwrap(); |