Skip to main content

Pass

Trait Pass 

Source
pub trait Pass {
    type Ctx;

    // Required method
    fn parse(
        &self,
        source: &str,
        span: Span,
        ctx: &Self::Ctx,
    ) -> Outcome<Self::Ctx>;

    // Provided method
    fn name(&self) -> &'static str { ... }
}
Expand description

One stage of a parse schedule.

A pass is a pure function from a source region plus its context to an Outcome: the region either expands into smaller child regions, is accepted as parsed, or fails (later passes may retry it).

increparse is deliberately combinator-agnostic: a pass can wrap any parsing technique — nom, chumsky, a PEG, regexes, or hand-rolled scanning — behind this trait. The engine only cares about the outcome.

§Contract

  • Passes must be pure: they must not mutate shared state and must produce the same output for the same (source, span, ctx) input.
  • Every child span returned from Outcome::Expand must be contained in the input span and on the same source revision. By default a child may cover its parent exactly; with EngineConfig::enforce_shrink it must be strictly smaller. The engine rejects outcomes that violate this, marking the node Failed.
  • Passes must be Send + Sync so the engine can run batches on any Executor.

§Examples

use increparse::{Outcome, Pass, Span};

/// Accepts any region whose text starts with "ok".
struct AcceptOk;

impl Pass for AcceptOk {
    type Ctx = ();

    fn parse(&self, source: &str, span: Span, _ctx: &()) -> Outcome<()> {
        let text = &source[span.to_range()];
        if text.starts_with("ok") {
            Outcome::Done
        } else {
            Outcome::Failed
        }
    }
}

Required Associated Types§

Source

type Ctx

Context threaded into every node this pass processes.

Required Methods§

Source

fn parse(&self, source: &str, span: Span, ctx: &Self::Ctx) -> Outcome<Self::Ctx>

Attempts to parse source[span] given the node’s context.

source is a snapshot of the full source text the tree was built against; span selects this node’s region within it.

Provided Methods§

Source

fn name(&self) -> &'static str

A human-readable name used in diagnostics; defaults to the type name.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

Source§

impl<C, F> Pass for PassFn<C, F>
where F: for<'a> Fn(&'a str, Span, &'a C) -> Outcome<C> + Send + Sync,

Source§

type Ctx = C