Skip to main content

Crate increparse_nom

Crate increparse_nom 

Source
Expand description

Wrap nom parsers in increparse Passes.

nom reports offsets relative to the input slice it was handed; increparse needs absolute spans carrying a source revision — and the incremental tree only reuses regions whose spans match exactly. Getting that translation wrong by one byte silently degrades re-parsing into re-parsing-everything. This crate does it once, correctly, so your nom parsers can stay in slice-relative coordinates.

A pass is a function from a LocatedSpan of the region’s text to an IResult whose output is the parsed children: each child is a slice-relative Range<usize> plus a context value for the next round. The wrapper converts the result to an Outcome:

  • Ok(children) → Outcome::Expand with rebased absolute spans,
  • Ok(vec![]) → Outcome::Done (nothing left to parse in the region),
  • Err(_) → Outcome::Failed (the error is dropped; the region stays in the tree as a leaf and later passes may retry it).

§Examples

use increparse::{Outcome, Pass, Span};
use increparse_nom::{nom_pass, NomChildren};
use nom::IResult;
use nom::bytes::complete::tag;
use nom_locate::LocatedSpan;
use std::ops::Range;

fn split_at_hi(i: LocatedSpan<&str>) -> IResult<LocatedSpan<&str>, NomChildren<()>> {
    let (i, _) = tag("hi")(i)?;
    let end = i.location_offset();
    Ok((i, vec![(0..end, ())]))
}

let pass = nom_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 nom saw only a slice.
        assert_eq!(children[0].0, Span::new(0, 2, 0));
    }
    _ => 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§

NomPass
A Pass driven by a nom parser.

Functions§

nom_pass
Creates a pass from a nom parser producing NomChildren.

Type Aliases§

Located
The input and remaining-output type nom parsers see: a located view of the region’s slice of the source.
NomChildren
The children a nom parser produces: slice-relative ranges plus the context each child carries into the next round.