1 //! Combinators applying parsers in sequence
2
3 #[cfg(test)]
4 mod tests;
5
6 use crate::error::ParseError;
7 use crate::internal::{IResult, Parser};
8
9 /// Gets an object from the first parser,
10 /// then gets another object from the second parser.
11 ///
12 /// # Arguments
13 /// * `first` The first parser to apply.
14 /// * `second` The second parser to apply.
15 /// ```rust
16 /// # use nom::{Err, error::ErrorKind, Needed};
17 /// # use nom::Needed::Size;
18 /// use nom::sequence::pair;
19 /// use nom::bytes::complete::tag;
20 ///
21 /// let mut parser = pair(tag("abc"), tag("efg"));
22 ///
23 /// assert_eq!(parser("abcefg"), Ok(("", ("abc", "efg"))));
24 /// assert_eq!(parser("abcefghij"), Ok(("hij", ("abc", "efg"))));
25 /// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag))));
26 /// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag))));
27 /// ```
pair<I, O1, O2, E: ParseError<I>, F, G>( mut first: F, mut second: G, ) -> impl FnMut(I) -> IResult<I, (O1, O2), E> where F: Parser<I, O1, E>, G: Parser<I, O2, E>,28 pub fn pair<I, O1, O2, E: ParseError<I>, F, G>(
29 mut first: F,
30 mut second: G,
31 ) -> impl FnMut(I) -> IResult<I, (O1, O2), E>
32 where
33 F: Parser<I, O1, E>,
34 G: Parser<I, O2, E>,
35 {
36 move |input: I| {
37 let (input, o1) = first.parse(input)?;
38 second.parse(input).map(|(i, o2)| (i, (o1, o2)))
39 }
40 }
41
42 /// Matches an object from the first parser and discards it,
43 /// then gets an object from the second parser.
44 ///
45 /// # Arguments
46 /// * `first` The opening parser.
47 /// * `second` The second parser to get object.
48 /// ```rust
49 /// # use nom::{Err, error::ErrorKind, Needed};
50 /// # use nom::Needed::Size;
51 /// use nom::sequence::preceded;
52 /// use nom::bytes::complete::tag;
53 ///
54 /// let mut parser = preceded(tag("abc"), tag("efg"));
55 ///
56 /// assert_eq!(parser("abcefg"), Ok(("", "efg")));
57 /// assert_eq!(parser("abcefghij"), Ok(("hij", "efg")));
58 /// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag))));
59 /// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag))));
60 /// ```
preceded<I, O1, O2, E: ParseError<I>, F, G>( mut first: F, mut second: G, ) -> impl FnMut(I) -> IResult<I, O2, E> where F: Parser<I, O1, E>, G: Parser<I, O2, E>,61 pub fn preceded<I, O1, O2, E: ParseError<I>, F, G>(
62 mut first: F,
63 mut second: G,
64 ) -> impl FnMut(I) -> IResult<I, O2, E>
65 where
66 F: Parser<I, O1, E>,
67 G: Parser<I, O2, E>,
68 {
69 move |input: I| {
70 let (input, _) = first.parse(input)?;
71 second.parse(input)
72 }
73 }
74
75 /// Gets an object from the first parser,
76 /// then matches an object from the second parser and discards it.
77 ///
78 /// # Arguments
79 /// * `first` The first parser to apply.
80 /// * `second` The second parser to match an object.
81 /// ```rust
82 /// # use nom::{Err, error::ErrorKind, Needed};
83 /// # use nom::Needed::Size;
84 /// use nom::sequence::terminated;
85 /// use nom::bytes::complete::tag;
86 ///
87 /// let mut parser = terminated(tag("abc"), tag("efg"));
88 ///
89 /// assert_eq!(parser("abcefg"), Ok(("", "abc")));
90 /// assert_eq!(parser("abcefghij"), Ok(("hij", "abc")));
91 /// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag))));
92 /// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag))));
93 /// ```
terminated<I, O1, O2, E: ParseError<I>, F, G>( mut first: F, mut second: G, ) -> impl FnMut(I) -> IResult<I, O1, E> where F: Parser<I, O1, E>, G: Parser<I, O2, E>,94 pub fn terminated<I, O1, O2, E: ParseError<I>, F, G>(
95 mut first: F,
96 mut second: G,
97 ) -> impl FnMut(I) -> IResult<I, O1, E>
98 where
99 F: Parser<I, O1, E>,
100 G: Parser<I, O2, E>,
101 {
102 move |input: I| {
103 let (input, o1) = first.parse(input)?;
104 second.parse(input).map(|(i, _)| (i, o1))
105 }
106 }
107
108 /// Gets an object from the first parser,
109 /// then matches an object from the sep_parser and discards it,
110 /// then gets another object from the second parser.
111 ///
112 /// # Arguments
113 /// * `first` The first parser to apply.
114 /// * `sep` The separator parser to apply.
115 /// * `second` The second parser to apply.
116 /// ```rust
117 /// # use nom::{Err, error::ErrorKind, Needed};
118 /// # use nom::Needed::Size;
119 /// use nom::sequence::separated_pair;
120 /// use nom::bytes::complete::tag;
121 ///
122 /// let mut parser = separated_pair(tag("abc"), tag("|"), tag("efg"));
123 ///
124 /// assert_eq!(parser("abc|efg"), Ok(("", ("abc", "efg"))));
125 /// assert_eq!(parser("abc|efghij"), Ok(("hij", ("abc", "efg"))));
126 /// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag))));
127 /// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag))));
128 /// ```
separated_pair<I, O1, O2, O3, E: ParseError<I>, F, G, H>( mut first: F, mut sep: G, mut second: H, ) -> impl FnMut(I) -> IResult<I, (O1, O3), E> where F: Parser<I, O1, E>, G: Parser<I, O2, E>, H: Parser<I, O3, E>,129 pub fn separated_pair<I, O1, O2, O3, E: ParseError<I>, F, G, H>(
130 mut first: F,
131 mut sep: G,
132 mut second: H,
133 ) -> impl FnMut(I) -> IResult<I, (O1, O3), E>
134 where
135 F: Parser<I, O1, E>,
136 G: Parser<I, O2, E>,
137 H: Parser<I, O3, E>,
138 {
139 move |input: I| {
140 let (input, o1) = first.parse(input)?;
141 let (input, _) = sep.parse(input)?;
142 second.parse(input).map(|(i, o2)| (i, (o1, o2)))
143 }
144 }
145
146 /// Matches an object from the first parser and discards it,
147 /// then gets an object from the second parser,
148 /// and finally matches an object from the third parser and discards it.
149 ///
150 /// # Arguments
151 /// * `first` The first parser to apply and discard.
152 /// * `second` The second parser to apply.
153 /// * `third` The third parser to apply and discard.
154 /// ```rust
155 /// # use nom::{Err, error::ErrorKind, Needed};
156 /// # use nom::Needed::Size;
157 /// use nom::sequence::delimited;
158 /// use nom::bytes::complete::tag;
159 ///
160 /// let mut parser = delimited(tag("("), tag("abc"), tag(")"));
161 ///
162 /// assert_eq!(parser("(abc)"), Ok(("", "abc")));
163 /// assert_eq!(parser("(abc)def"), Ok(("def", "abc")));
164 /// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag))));
165 /// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag))));
166 /// ```
delimited<I, O1, O2, O3, E: ParseError<I>, F, G, H>( mut first: F, mut second: G, mut third: H, ) -> impl FnMut(I) -> IResult<I, O2, E> where F: Parser<I, O1, E>, G: Parser<I, O2, E>, H: Parser<I, O3, E>,167 pub fn delimited<I, O1, O2, O3, E: ParseError<I>, F, G, H>(
168 mut first: F,
169 mut second: G,
170 mut third: H,
171 ) -> impl FnMut(I) -> IResult<I, O2, E>
172 where
173 F: Parser<I, O1, E>,
174 G: Parser<I, O2, E>,
175 H: Parser<I, O3, E>,
176 {
177 move |input: I| {
178 let (input, _) = first.parse(input)?;
179 let (input, o2) = second.parse(input)?;
180 third.parse(input).map(|(i, _)| (i, o2))
181 }
182 }
183
184 /// Helper trait for the tuple combinator.
185 ///
186 /// This trait is implemented for tuples of parsers of up to 21 elements.
187 pub trait Tuple<I, O, E> {
188 /// Parses the input and returns a tuple of results of each parser.
parse(&mut self, input: I) -> IResult<I, O, E>189 fn parse(&mut self, input: I) -> IResult<I, O, E>;
190 }
191
192 impl<Input, Output, Error: ParseError<Input>, F: Parser<Input, Output, Error>>
193 Tuple<Input, (Output,), Error> for (F,)
194 {
parse(&mut self, input: Input) -> IResult<Input, (Output,), Error>195 fn parse(&mut self, input: Input) -> IResult<Input, (Output,), Error> {
196 self.0.parse(input).map(|(i, o)| (i, (o,)))
197 }
198 }
199
200 macro_rules! tuple_trait(
201 ($name1:ident $ty1:ident, $name2: ident $ty2:ident, $($name:ident $ty:ident),*) => (
202 tuple_trait!(__impl $name1 $ty1, $name2 $ty2; $($name $ty),*);
203 );
204 (__impl $($name:ident $ty: ident),+; $name1:ident $ty1:ident, $($name2:ident $ty2:ident),*) => (
205 tuple_trait_impl!($($name $ty),+);
206 tuple_trait!(__impl $($name $ty),+ , $name1 $ty1; $($name2 $ty2),*);
207 );
208 (__impl $($name:ident $ty: ident),+; $name1:ident $ty1:ident) => (
209 tuple_trait_impl!($($name $ty),+);
210 tuple_trait_impl!($($name $ty),+, $name1 $ty1);
211 );
212 );
213
214 macro_rules! tuple_trait_impl(
215 ($($name:ident $ty: ident),+) => (
216 impl<
217 Input: Clone, $($ty),+ , Error: ParseError<Input>,
218 $($name: Parser<Input, $ty, Error>),+
219 > Tuple<Input, ( $($ty),+ ), Error> for ( $($name),+ ) {
220
221 fn parse(&mut self, input: Input) -> IResult<Input, ( $($ty),+ ), Error> {
222 tuple_trait_inner!(0, self, input, (), $($name)+)
223
224 }
225 }
226 );
227 );
228
229 macro_rules! tuple_trait_inner(
230 ($it:tt, $self:expr, $input:expr, (), $head:ident $($id:ident)+) => ({
231 let (i, o) = $self.$it.parse($input.clone())?;
232
233 succ!($it, tuple_trait_inner!($self, i, ( o ), $($id)+))
234 });
235 ($it:tt, $self:expr, $input:expr, ($($parsed:tt)*), $head:ident $($id:ident)+) => ({
236 let (i, o) = $self.$it.parse($input.clone())?;
237
238 succ!($it, tuple_trait_inner!($self, i, ($($parsed)* , o), $($id)+))
239 });
240 ($it:tt, $self:expr, $input:expr, ($($parsed:tt)*), $head:ident) => ({
241 let (i, o) = $self.$it.parse($input.clone())?;
242
243 Ok((i, ($($parsed)* , o)))
244 });
245 );
246
247 tuple_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,
248 FnM M, FnN N, FnO O, FnP P, FnQ Q, FnR R, FnS S, FnT T, FnU U);
249
250 ///Applies a tuple of parsers one by one and returns their results as a tuple.
251 ///
252 /// ```rust
253 /// # use nom::{Err, error::ErrorKind};
254 /// use nom::sequence::tuple;
255 /// use nom::character::complete::{alpha1, digit1};
256 /// let mut parser = tuple((alpha1, digit1, alpha1));
257 ///
258 /// assert_eq!(parser("abc123def"), Ok(("", ("abc", "123", "def"))));
259 /// assert_eq!(parser("123def"), Err(Err::Error(("123def", ErrorKind::Alpha))));
260 /// ```
tuple<I, O, E: ParseError<I>, List: Tuple<I, O, E>>( mut l: List, ) -> impl FnMut(I) -> IResult<I, O, E>261 pub fn tuple<I, O, E: ParseError<I>, List: Tuple<I, O, E>>(
262 mut l: List,
263 ) -> impl FnMut(I) -> IResult<I, O, E> {
264 move |i: I| l.parse(i)
265 }
266