1#[cfg(test)]
4mod tests;
5
6use crate::error::ParseError;
7use crate::internal::{IResult, Parser};
8use crate::{Check, OutputM, OutputMode, PResult};
9
10pub fn pair<I, O1, O2, E: ParseError<I>, F, G>(
31 first: F,
32 second: G,
33) -> impl Parser<I, Output = (O1, O2), Error = E>
34where
35 F: Parser<I, Output = O1, Error = E>,
36 G: Parser<I, Output = O2, Error = E>,
37{
38 first.and(second)
39}
40
41pub fn preceded<I, O, E: ParseError<I>, F, G>(
62 first: F,
63 second: G,
64) -> impl Parser<I, Output = O, Error = E>
65where
66 F: Parser<I, Error = E>,
67 G: Parser<I, Output = O, Error = E>,
68{
69 Preceded {
70 f: first,
71 g: second,
72 }
73}
74
75pub struct Preceded<F, G> {
77 f: F,
78 g: G,
79}
80
81impl<I, E: ParseError<I>, F: Parser<I, Error = E>, G: Parser<I, Error = E>> Parser<I>
82 for Preceded<F, G>
83{
84 type Output = <G as Parser<I>>::Output;
85 type Error = E;
86
87 #[inline(always)]
88 fn process<OM: OutputMode>(&mut self, i: I) -> PResult<OM, I, Self::Output, Self::Error> {
89 let (i, _) = self
90 .f
91 .process::<OutputM<Check, OM::Error, OM::Incomplete>>(i)?;
92 let (i, o2) = self.g.process::<OM>(i)?;
93
94 Ok((i, o2))
95 }
96}
97
98pub fn terminated<I, O, E: ParseError<I>, F, G>(
119 first: F,
120 second: G,
121) -> impl Parser<I, Output = O, Error = E>
122where
123 F: Parser<I, Output = O, Error = E>,
124 G: Parser<I, Error = E>,
125{
126 Terminated {
127 f: first,
128 g: second,
129 }
130}
131
132pub struct Terminated<F, G> {
134 f: F,
135 g: G,
136}
137
138impl<I, E: ParseError<I>, F: Parser<I, Error = E>, G: Parser<I, Error = E>> Parser<I>
139 for Terminated<F, G>
140{
141 type Output = <F as Parser<I>>::Output;
142 type Error = E;
143
144 #[inline(always)]
145 fn process<OM: OutputMode>(&mut self, i: I) -> PResult<OM, I, Self::Output, Self::Error> {
146 let (i, o1) = self.f.process::<OM>(i)?;
147 let (i, _) = self
148 .g
149 .process::<OutputM<Check, OM::Error, OM::Incomplete>>(i)?;
150
151 Ok((i, o1))
152 }
153}
154
155pub fn separated_pair<I, O1, O2, E: ParseError<I>, F, G, H>(
178 first: F,
179 sep: G,
180 second: H,
181) -> impl Parser<I, Output = (O1, O2), Error = E>
182where
183 F: Parser<I, Output = O1, Error = E>,
184 G: Parser<I, Error = E>,
185 H: Parser<I, Output = O2, Error = E>,
186{
187 first.and(preceded(sep, second))
188}
189
190pub fn delimited<I, O, E: ParseError<I>, F, G, H>(
213 first: F,
214 second: G,
215 third: H,
216) -> impl Parser<I, Output = O, Error = E>
217where
218 F: Parser<I, Error = E>,
219 G: Parser<I, Output = O, Error = E>,
220 H: Parser<I, Error = E>,
221{
222 preceded(first, terminated(second, third))
223}
224
225#[deprecated(since = "8.0.0", note = "`Parser` is directly implemented for tuples")]
229#[allow(deprecated)]
230pub trait Tuple<I, O, E> {
231 fn parse_tuple(&mut self, input: I) -> IResult<I, O, E>;
233}
234
235#[allow(deprecated)]
236impl<Input, Output, Error: ParseError<Input>, F: Parser<Input, Output = Output, Error = Error>>
237 Tuple<Input, (Output,), Error> for (F,)
238{
239 fn parse_tuple(&mut self, input: Input) -> IResult<Input, (Output,), Error> {
240 self.0.parse(input).map(|(i, o)| (i, (o,)))
241 }
242}
243
244macro_rules! tuple_trait(
245 ($name1:ident $ty1:ident, $name2: ident $ty2:ident, $($name:ident $ty:ident),*) => (
246 tuple_trait!(__impl $name1 $ty1, $name2 $ty2; $($name $ty),*);
247 );
248 (__impl $($name:ident $ty: ident),+; $name1:ident $ty1:ident, $($name2:ident $ty2:ident),*) => (
249 tuple_trait_impl!($($name $ty),+);
250 tuple_trait!(__impl $($name $ty),+ , $name1 $ty1; $($name2 $ty2),*);
251 );
252 (__impl $($name:ident $ty: ident),+; $name1:ident $ty1:ident) => (
253 tuple_trait_impl!($($name $ty),+);
254 tuple_trait_impl!($($name $ty),+, $name1 $ty1);
255 );
256);
257
258macro_rules! tuple_trait_impl(
259 ($($name:ident $ty: ident),+) => (
260 #[allow(deprecated)]
261 impl<
262 Input: Clone, $($ty),+ , Error: ParseError<Input>,
263 $($name: Parser<Input, Output = $ty, Error = Error>),+
264 > Tuple<Input, ( $($ty),+ ), Error> for ( $($name),+ ) {
265 fn parse_tuple(&mut self, input: Input) -> IResult<Input, ( $($ty),+ ), Error> {
266 tuple_trait_inner!(0, self, input, (), $($name)+)
267
268 }
269 }
270 );
271);
272
273macro_rules! tuple_trait_inner(
274 ($it:tt, $self:expr, $input:expr, (), $head:ident $($id:ident)+) => ({
275 let (i, o) = $self.$it.parse($input)?;
276
277 succ!($it, tuple_trait_inner!($self, i, ( o ), $($id)+))
278 });
279 ($it:tt, $self:expr, $input:expr, ($($parsed:tt)*), $head:ident $($id:ident)+) => ({
280 let (i, o) = $self.$it.parse($input)?;
281
282 succ!($it, tuple_trait_inner!($self, i, ($($parsed)* , o), $($id)+))
283 });
284 ($it:tt, $self:expr, $input:expr, ($($parsed:tt)*), $head:ident) => ({
285 let (i, o) = $self.$it.parse($input)?;
286
287 Ok((i, ($($parsed)* , o)))
288 });
289);
290
291tuple_trait!(FnA A, FnB B, FnC C, FnD D, FnE E, FnF F, FnG G, FnH H, FnI I, FnJ J, FnK K, FnL L,
292 FnM M, FnN N, FnO O, FnP P, FnQ Q, FnR R, FnS S, FnT T, FnU U);
293
294#[allow(deprecated)]
298impl<I, E: ParseError<I>> Tuple<I, (), E> for () {
299 fn parse_tuple(&mut self, input: I) -> IResult<I, (), E> {
300 Ok((input, ()))
301 }
302}
303
304#[deprecated(since = "8.0.0", note = "`Parser` is directly implemented for tuples")]
316#[allow(deprecated)]
317pub fn tuple<I, O, E: ParseError<I>, List: Tuple<I, O, E>>(
318 mut l: List,
319) -> impl FnMut(I) -> IResult<I, O, E> {
320 move |i: I| l.parse_tuple(i)
321}