Skip to main content

FoldSink

Trait FoldSink 

pub trait FoldSink {
    // Required methods
    fn shift(&mut self, token: &Token);
    fn reduce(&mut self, shape: &RuleShape, size: usize);
}
Expand description

A monomorphic, UNBOXED reduce-time fold — the Rust-native peer to ReduceCallback that does NOT route values through the type-erased NodeValue::Foreign (Arc) box the language bindings need. The concrete sink owns its own typed value stack (e.g. Vec<f64>), so a pure-Rust fold pays no per-reduction heap allocation and no per-reduction Vec<NodeValue> children shaping: Self::shift converts a token leaf to a value and pushes it; Self::reduce keeps the shaped children of one rule (shape.to_include, ?-collapse via expand1_inline), folds them, and replaces them with the single result. It drives the identical token stream as the tree build (the reduce value never steers the parser), so it accepts/rejects identically — only the value representation differs. Driven by crate::Lark::parse_fold; the concrete sink exposes its own accessor for the accepted root value.

Splice is the sink’s responsibility on this raw seam. A _-transparent (splice) rule — what */+/[...] groups desugar to — reduces like any other, so a sink that just folds each reduction to one value sees a splice child nested (one folded value) where the tree build sees its members flattened into the parent. That is invisible to an associative fold (a sum) but diverges from parse().fold() for an order/arity-sensitive one. To flatten exactly as the tree does, a sink must track how many stack values each symbol spans (a splice symbol spans its members) and expand a to_expand child in place — which is what crate::Lark::parse_fold_fn does for you. Prefer that unless you specifically want the raw, un-flattened seam.

Required Methods§

fn shift(&mut self, token: &Token)

A shifted token leaf — convert it to a value and push it on the stack.

fn reduce(&mut self, shape: &RuleShape, size: usize)

One reduction of size raw children (the top size stack entries): keep the shaped children, fold them, and push the single result in their place.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§