Skip to main content

Crate increparse_chumsky

Crate increparse_chumsky 

Source
Expand description

Wrap chumsky parsers in increparse Passes.

chumsky 0.10 reports spans as SimpleSpans relative to the input slice it parsed; increparse needs absolute spans carrying a source revision — and the incremental tree only reuses regions whose spans match exactly. This crate does the translation once, correctly, so your chumsky parsers can stay in slice-relative coordinates.

A pass is a function from the region’s slice to a chumsky ParseResult whose output is the parsed children: each child is a slice-relative SimpleSpan plus a context value for the next round. The wrapper converts the result to an Outcome:

  • parse succeeded with children → Outcome::Expand with rebased absolute spans,
  • succeeded with no children → Outcome::Done,
  • parse failed → Outcome::Failed (errors dropped; the region stays in the tree as a leaf and later passes may retry it).

Note: chumsky’s Parser::parse implicitly requires the whole input to be consumed. To claim children and ignore the rest of the region, end your parser with .then_ignore(any().repeated()).

Because parsers are typically built per-call (they are cheap), the closure receives the slice and runs its parser itself:

§Examples

use chumsky::prelude::*;
use increparse::{Outcome, Pass, Span};
use increparse_chumsky::{chumsky_pass, ChumChildren};

// Parsers are built per call, tied to the input's lifetime.
fn split_at_hi(slice: &str) -> ParseResult<ChumChildren<()>, Rich<'_, char>> {
    fn parser<'a>() -> impl Parser<'a, &'a str, ChumChildren<()>, extra::Err<Rich<'a, char>>> {
        just("hi")
            .map_with(|_out, e| vec![(e.span(), ())])
            .then_ignore(any().repeated())
    }
    parser().parse(slice)
}

let pass = chumsky_pass(split_at_hi);
let source = "hi there";
match pass.parse(source, Span::new(0, source.len(), 0), &()) {
    Outcome::Expand(children) => {
        // The child span is absolute, even though chumsky saw a slice.
        assert_eq!(children[0].0.start, 0);
        assert_eq!(children[0].0.end, 2);
    }
    _ => panic!("expected expansion"),
}

Spans you emit are validated by the engine like any other: they must be contained in the region and (unless EngineConfig::enforce_shrink is disabled) strictly smaller.

Structs§

ChumskyPass
A Pass driven by a chumsky parser.

Functions§

chumsky_pass
Creates a pass from a closure that parses a slice into ChumChildren.

Type Aliases§

ChumChildren
The children a chumsky parser produces: slice-relative spans plus the context each child carries into the next round.