From fbaa420ed550e9c3e7cdc09d4a8ec22bfbd782a6 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Mon, 27 Feb 2023 12:36:41 +0800 Subject: before a major refactor I decide to adopt a new approach of recording and updating item derivation forests. Since this affects a lot of things, I decide to commit before the refactor, so that I can create a branch for that refactor. --- chain/src/lib.rs | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) (limited to 'chain/src/lib.rs') diff --git a/chain/src/lib.rs b/chain/src/lib.rs index a7740c2..d7fc519 100644 --- a/chain/src/lib.rs +++ b/chain/src/lib.rs @@ -16,7 +16,7 @@ use graph::{error::Error as GError, LabelExtGraph}; use item::default::Error as ForestError; -use item::PaSe; +use item::PaVi; /// An edge in the Chain-Rule machine. #[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] @@ -24,18 +24,25 @@ pub struct Edge { /// The position in the atomic languages. label: usize, /// The source of the associated forest edge. - forest_source: PaSe, + forest_source: PaVi, + /// The bottom source on which we shall perform reduction. + /// + /// If this equals `forest_source`, no extra reduction needs to be + /// done. + true_source: PaVi, /// Whether or not this edge is "accepting". accepting: bool, } impl Edge { /// Construct a new edge. - pub fn new(label: usize, forest_source: PaSe, accepting: bool) -> Self { + pub fn new(label: usize, forest_source: PaVi, accepting: bool) -> Self { + let true_source = forest_source; Self { label, forest_source, accepting, + true_source, } } @@ -50,9 +57,21 @@ impl Edge { } /// Return the associated forest edge of the edge. - pub fn forest_source(&self) -> PaSe { + pub fn forest_source(&self) -> PaVi { self.forest_source } + + /// Return the associated bottom edge of the edge from which + /// onwards we shall perform the reduction. + pub fn set_true_source(&mut self, true_source: PaVi) { + self.true_source = true_source; + } + + /// Return the associated bottom edge of the edge from which + /// onwards we shall perform the reduction. + pub fn true_source(&self) -> PaVi { + self.true_source + } } impl core::fmt::Display for Edge { @@ -137,7 +156,7 @@ pub enum TwoLayers { /// the source of the associated forest edge of the second layer, /// and the third is a list of edges, which are the common first /// layers. - Two(usize, PaSe, bool, Vec<(Roi, usize)>), + Two(usize, PaVi, bool, Vec<(Roi, usize)>), } /// The type of a collapsed iterator. @@ -239,12 +258,15 @@ pub trait Chain: LabelExtGraph { /// An iterator that iterates all layers that need to be merged. type DerIter: Iterator; + // FIXME: Add a parameter to control whether to manipulate the + // forests or not. + /// Take the derivative by a terminal `t` at position `pos`. fn derive(&mut self, t: usize, pos: usize) -> Result; /// Take the union of all derivatives. fn union(&mut self, der_iter: Self::DerIter) -> Result, Self::Error> { - // REVIEW: Find a way to avoid allocations. + // REVIEW: Think about the possibilities to avoid allocations. Collapsed::<_, Self>::collapse(der_iter, self) .collect::, Self::Error>>() .map(|mut v| { @@ -276,6 +298,10 @@ pub trait Chain: LabelExtGraph { Ok(()) } + + /// Signal to the parser that the end of the input is reached, so + /// that the parser knows to generate suitable forests. + fn end_of_input(&mut self) -> Result<(), Self::Error>; } pub mod default; -- cgit v1.2.3-18-g5258