1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//! Wrap [`nom`] parsers in increparse [`Pass`](increparse::Pass)es.
//!
//! 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`](increparse::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`](increparse::EngineConfig::enforce_shrink)
//! is disabled) strictly smaller.
use ;
use IResult;
use LocatedSpan;
use Range;
/// The children a nom parser produces: slice-relative ranges plus the
/// context each child carries into the next round.
pub type NomChildren<C> = ;
/// The input and remaining-output type nom parsers see: a located view of
/// the region's slice of the source.
pub type Located<'a> = ;
/// A [`Pass`] driven by a nom parser.
///
/// See the [crate docs](self) for the conversion rules. Build one with
/// [`nom_pass`].
/// Creates a pass from a nom parser producing [`NomChildren`].