/rust/registry/src/index.crates.io-1949cf8c6b5b557f/combine-4.6.7/src/parser/token.rs
Line | Count | Source |
1 | | //! Parsers working with single stream items. |
2 | | |
3 | | use crate::{ |
4 | | error::{ |
5 | | self, ErrorInfo, ParseError, |
6 | | ParseResult::{self, *}, |
7 | | ResultExt, StreamError, Tracked, |
8 | | }, |
9 | | lib::marker::PhantomData, |
10 | | stream::{uncons, Stream, StreamOnce}, |
11 | | Parser, |
12 | | }; |
13 | | |
14 | | #[derive(Copy, Clone)] |
15 | | pub struct Any<Input>(PhantomData<fn(Input) -> Input>); |
16 | | |
17 | | impl<Input> Parser<Input> for Any<Input> |
18 | | where |
19 | | Input: Stream, |
20 | | { |
21 | | type Output = Input::Token; |
22 | | type PartialState = (); |
23 | | |
24 | | #[inline] |
25 | 5.81M | fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Input::Token, Input::Error> { |
26 | 5.81M | uncons(input) |
27 | 5.81M | } <combine::parser::token::Any<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>> as combine::parser::Parser<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>>::parse_lazy Line | Count | Source | 25 | 5.81M | fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Input::Token, Input::Error> { | 26 | 5.81M | uncons(input) | 27 | 5.81M | } |
Unexecuted instantiation: <combine::parser::token::Any<_> as combine::parser::Parser<_>>::parse_lazy |
28 | | } |
29 | | |
30 | | /// Parses any token. |
31 | | /// |
32 | | /// ``` |
33 | | /// # extern crate combine; |
34 | | /// # use combine::*; |
35 | | /// # fn main() { |
36 | | /// let mut char_parser = any(); |
37 | | /// assert_eq!(char_parser.parse("!").map(|x| x.0), Ok('!')); |
38 | | /// assert!(char_parser.parse("").is_err()); |
39 | | /// let mut byte_parser = any(); |
40 | | /// assert_eq!(byte_parser.parse(&b"!"[..]).map(|x| x.0), Ok(b'!')); |
41 | | /// assert!(byte_parser.parse(&b""[..]).is_err()); |
42 | | /// # } |
43 | | /// ``` |
44 | 6.56M | pub fn any<Input>() -> Any<Input> |
45 | 6.56M | where |
46 | 6.56M | Input: Stream, |
47 | | { |
48 | 6.56M | Any(PhantomData) |
49 | 6.56M | } combine::parser::token::any::<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>> Line | Count | Source | 44 | 6.56M | pub fn any<Input>() -> Any<Input> | 45 | 6.56M | where | 46 | 6.56M | Input: Stream, | 47 | | { | 48 | 6.56M | Any(PhantomData) | 49 | 6.56M | } |
Unexecuted instantiation: combine::parser::token::any::<_> |
50 | | |
51 | | #[derive(Copy, Clone)] |
52 | | pub struct Satisfy<Input, P> { |
53 | | predicate: P, |
54 | | _marker: PhantomData<Input>, |
55 | | } |
56 | | |
57 | 219k | fn satisfy_impl<Input, P, R>(input: &mut Input, mut predicate: P) -> ParseResult<R, Input::Error> |
58 | 219k | where |
59 | 219k | Input: Stream, |
60 | 219k | P: FnMut(Input::Token) -> Option<R>, |
61 | | { |
62 | 219k | let position = input.position(); |
63 | 219k | match uncons(input) { |
64 | 216k | PeekOk(c) | CommitOk(c) => match predicate(c) { |
65 | 215k | Some(c) => CommitOk(c), |
66 | 244 | None => PeekErr(Input::Error::empty(position).into()), |
67 | | }, |
68 | 242 | PeekErr(err) => PeekErr(err), |
69 | 3.42k | CommitErr(err) => CommitErr(err), |
70 | | } |
71 | 219k | } combine::parser::token::satisfy_impl::<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, <combine::parser::token::Satisfy<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, combine::parser::byte::crlf<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}> as combine::parser::Parser<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>>::parse_lazy::{closure#0}, u8> Line | Count | Source | 57 | 111k | fn satisfy_impl<Input, P, R>(input: &mut Input, mut predicate: P) -> ParseResult<R, Input::Error> | 58 | 111k | where | 59 | 111k | Input: Stream, | 60 | 111k | P: FnMut(Input::Token) -> Option<R>, | 61 | | { | 62 | 111k | let position = input.position(); | 63 | 111k | match uncons(input) { | 64 | 108k | PeekOk(c) | CommitOk(c) => match predicate(c) { | 65 | 108k | Some(c) => CommitOk(c), | 66 | 216 | None => PeekErr(Input::Error::empty(position).into()), | 67 | | }, | 68 | 197 | PeekErr(err) => PeekErr(err), | 69 | 2.57k | CommitErr(err) => CommitErr(err), | 70 | | } | 71 | 111k | } |
combine::parser::token::satisfy_impl::<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, <combine::parser::token::Satisfy<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, combine::parser::byte::newline<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}> as combine::parser::Parser<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>>::parse_lazy::{closure#0}, u8> Line | Count | Source | 57 | 108k | fn satisfy_impl<Input, P, R>(input: &mut Input, mut predicate: P) -> ParseResult<R, Input::Error> | 58 | 108k | where | 59 | 108k | Input: Stream, | 60 | 108k | P: FnMut(Input::Token) -> Option<R>, | 61 | | { | 62 | 108k | let position = input.position(); | 63 | 108k | match uncons(input) { | 64 | 107k | PeekOk(c) | CommitOk(c) => match predicate(c) { | 65 | 107k | Some(c) => CommitOk(c), | 66 | 28 | None => PeekErr(Input::Error::empty(position).into()), | 67 | | }, | 68 | 45 | PeekErr(err) => PeekErr(err), | 69 | 850 | CommitErr(err) => CommitErr(err), | 70 | | } | 71 | 108k | } |
Unexecuted instantiation: combine::parser::token::satisfy_impl::<_, _, _> |
72 | | |
73 | | impl<Input, P> Parser<Input> for Satisfy<Input, P> |
74 | | where |
75 | | Input: Stream, |
76 | | P: FnMut(Input::Token) -> bool, |
77 | | { |
78 | | type Output = Input::Token; |
79 | | type PartialState = (); |
80 | | |
81 | | #[inline] |
82 | 219k | fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Self::Output, Input::Error> { |
83 | 219k | satisfy_impl(input, |c| { |
84 | 216k | if (self.predicate)(c.clone()) { |
85 | 215k | Some(c) |
86 | | } else { |
87 | 244 | None |
88 | | } |
89 | 216k | }) <combine::parser::token::Satisfy<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, combine::parser::byte::crlf<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}> as combine::parser::Parser<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>>::parse_lazy::{closure#0} Line | Count | Source | 83 | 108k | satisfy_impl(input, |c| { | 84 | 108k | if (self.predicate)(c.clone()) { | 85 | 108k | Some(c) | 86 | | } else { | 87 | 216 | None | 88 | | } | 89 | 108k | }) |
<combine::parser::token::Satisfy<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, combine::parser::byte::newline<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}> as combine::parser::Parser<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>>::parse_lazy::{closure#0} Line | Count | Source | 83 | 107k | satisfy_impl(input, |c| { | 84 | 107k | if (self.predicate)(c.clone()) { | 85 | 107k | Some(c) | 86 | | } else { | 87 | 28 | None | 88 | | } | 89 | 107k | }) |
Unexecuted instantiation: <combine::parser::token::Satisfy<_, _> as combine::parser::Parser<_>>::parse_lazy::{closure#0} |
90 | 219k | } <combine::parser::token::Satisfy<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, combine::parser::byte::crlf<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}> as combine::parser::Parser<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>>::parse_lazy Line | Count | Source | 82 | 111k | fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Self::Output, Input::Error> { | 83 | 111k | satisfy_impl(input, |c| { | 84 | | if (self.predicate)(c.clone()) { | 85 | | Some(c) | 86 | | } else { | 87 | | None | 88 | | } | 89 | | }) | 90 | 111k | } |
<combine::parser::token::Satisfy<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, combine::parser::byte::newline<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}> as combine::parser::Parser<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>>::parse_lazy Line | Count | Source | 82 | 108k | fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Self::Output, Input::Error> { | 83 | 108k | satisfy_impl(input, |c| { | 84 | | if (self.predicate)(c.clone()) { | 85 | | Some(c) | 86 | | } else { | 87 | | None | 88 | | } | 89 | | }) | 90 | 108k | } |
Unexecuted instantiation: <combine::parser::token::Satisfy<_, _> as combine::parser::Parser<_>>::parse_lazy |
91 | | } |
92 | | |
93 | | /// Parses a token and succeeds depending on the result of `predicate`. |
94 | | /// |
95 | | /// ``` |
96 | | /// # extern crate combine; |
97 | | /// # use combine::*; |
98 | | /// # fn main() { |
99 | | /// let mut parser = satisfy(|c| c == '!' || c == '?'); |
100 | | /// assert_eq!(parser.parse("!").map(|x| x.0), Ok('!')); |
101 | | /// assert_eq!(parser.parse("?").map(|x| x.0), Ok('?')); |
102 | | /// # } |
103 | | /// ``` |
104 | 246k | pub fn satisfy<Input, P>(predicate: P) -> Satisfy<Input, P> |
105 | 246k | where |
106 | 246k | Input: Stream, |
107 | 246k | P: FnMut(Input::Token) -> bool, |
108 | | { |
109 | 246k | Satisfy { |
110 | 246k | predicate, |
111 | 246k | _marker: PhantomData, |
112 | 246k | } |
113 | 246k | } combine::parser::token::satisfy::<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, combine::parser::byte::crlf<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}> Line | Count | Source | 104 | 123k | pub fn satisfy<Input, P>(predicate: P) -> Satisfy<Input, P> | 105 | 123k | where | 106 | 123k | Input: Stream, | 107 | 123k | P: FnMut(Input::Token) -> bool, | 108 | | { | 109 | 123k | Satisfy { | 110 | 123k | predicate, | 111 | 123k | _marker: PhantomData, | 112 | 123k | } | 113 | 123k | } |
combine::parser::token::satisfy::<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, combine::parser::byte::newline<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}> Line | Count | Source | 104 | 123k | pub fn satisfy<Input, P>(predicate: P) -> Satisfy<Input, P> | 105 | 123k | where | 106 | 123k | Input: Stream, | 107 | 123k | P: FnMut(Input::Token) -> bool, | 108 | | { | 109 | 123k | Satisfy { | 110 | 123k | predicate, | 111 | 123k | _marker: PhantomData, | 112 | 123k | } | 113 | 123k | } |
Unexecuted instantiation: combine::parser::token::satisfy::<_, _> |
114 | | |
115 | | #[derive(Copy, Clone)] |
116 | | pub struct SatisfyMap<Input, P> { |
117 | | predicate: P, |
118 | | _marker: PhantomData<Input>, |
119 | | } |
120 | | |
121 | | impl<Input, P, R> Parser<Input> for SatisfyMap<Input, P> |
122 | | where |
123 | | Input: Stream, |
124 | | P: FnMut(Input::Token) -> Option<R>, |
125 | | { |
126 | | type Output = R; |
127 | | type PartialState = (); |
128 | | #[inline] |
129 | 0 | fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Self::Output, Input::Error> { |
130 | 0 | satisfy_impl(input, &mut self.predicate) |
131 | 0 | } |
132 | | } |
133 | | |
134 | | /// Parses a token and passes it to `predicate`. If `predicate` returns `Some` the parser succeeds |
135 | | /// and returns the value inside the `Option`. If `predicate` returns `None` the parser fails |
136 | | /// without consuming any input. |
137 | | /// |
138 | | /// ``` |
139 | | /// # extern crate combine; |
140 | | /// # use combine::*; |
141 | | /// # fn main() { |
142 | | /// #[derive(Debug, PartialEq)] |
143 | | /// enum YesNo { |
144 | | /// Yes, |
145 | | /// No, |
146 | | /// } |
147 | | /// let mut parser = satisfy_map(|c| { |
148 | | /// match c { |
149 | | /// 'Y' => Some(YesNo::Yes), |
150 | | /// 'N' => Some(YesNo::No), |
151 | | /// _ => None, |
152 | | /// } |
153 | | /// }); |
154 | | /// assert_eq!(parser.parse("Y").map(|x| x.0), Ok(YesNo::Yes)); |
155 | | /// assert!(parser.parse("A").map(|x| x.0).is_err()); |
156 | | /// # } |
157 | | /// ``` |
158 | 0 | pub fn satisfy_map<Input, P, R>(predicate: P) -> SatisfyMap<Input, P> |
159 | 0 | where |
160 | 0 | Input: Stream, |
161 | 0 | P: FnMut(Input::Token) -> Option<R>, |
162 | | { |
163 | 0 | SatisfyMap { |
164 | 0 | predicate, |
165 | 0 | _marker: PhantomData, |
166 | 0 | } |
167 | 0 | } |
168 | | |
169 | | #[derive(Copy, Clone)] |
170 | | pub struct Token<Input> |
171 | | where |
172 | | Input: Stream, |
173 | | Input::Token: PartialEq, |
174 | | { |
175 | | c: Input::Token, |
176 | | _marker: PhantomData<Input>, |
177 | | } |
178 | | |
179 | | impl<Input> Parser<Input> for Token<Input> |
180 | | where |
181 | | Input: Stream, |
182 | | Input::Token: PartialEq + Clone, |
183 | | { |
184 | | type Output = Input::Token; |
185 | | type PartialState = (); |
186 | | |
187 | | #[inline] |
188 | 0 | fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Input::Token, Input::Error> { |
189 | 0 | satisfy_impl(input, |c| if c == self.c { Some(c) } else { None }) |
190 | 0 | } |
191 | 0 | fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) { |
192 | 0 | errors.error.add_expected(error::Token(self.c.clone())); |
193 | 0 | } |
194 | | } |
195 | | |
196 | | /// Parses a character and succeeds if the character is equal to `c`. |
197 | | /// |
198 | | /// ``` |
199 | | /// # extern crate combine; |
200 | | /// # use combine::*; |
201 | | /// # fn main() { |
202 | | /// let result = token('!') |
203 | | /// .parse("!") |
204 | | /// .map(|x| x.0); |
205 | | /// assert_eq!(result, Ok('!')); |
206 | | /// # } |
207 | | /// ``` |
208 | 0 | pub fn token<Input>(c: Input::Token) -> Token<Input> |
209 | 0 | where |
210 | 0 | Input: Stream, |
211 | 0 | Input::Token: PartialEq, |
212 | | { |
213 | 0 | Token { |
214 | 0 | c, |
215 | 0 | _marker: PhantomData, |
216 | 0 | } |
217 | 0 | } |
218 | | |
219 | | #[derive(Clone)] |
220 | | pub struct Tokens<C, E, T, Input> |
221 | | where |
222 | | Input: Stream, |
223 | | { |
224 | | cmp: C, |
225 | | expected: E, |
226 | | tokens: T, |
227 | | _marker: PhantomData<Input>, |
228 | | } |
229 | | |
230 | | impl<Input, C, E, T> Parser<Input> for Tokens<C, E, T, Input> |
231 | | where |
232 | | C: FnMut(T::Item, Input::Token) -> bool, |
233 | | E: for<'s> ErrorInfo<'s, Input::Token, Input::Range>, |
234 | | T: Clone + IntoIterator, |
235 | | Input: Stream, |
236 | | { |
237 | | type Output = T; |
238 | | type PartialState = (); |
239 | | #[inline] |
240 | 0 | fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<T, Input::Error> { |
241 | 0 | let start = input.position(); |
242 | 0 | let mut committed = false; |
243 | 0 | for c in self.tokens.clone() { |
244 | 0 | match crate::stream::uncons(input) { |
245 | 0 | CommitOk(other) | PeekOk(other) => { |
246 | 0 | if !(self.cmp)(c, other.clone()) { |
247 | 0 | return if committed { |
248 | 0 | let mut errors = <Input as StreamOnce>::Error::from_error( |
249 | 0 | start, |
250 | 0 | StreamError::unexpected_token(other), |
251 | | ); |
252 | 0 | errors.add_expected(&self.expected); |
253 | 0 | CommitErr(errors) |
254 | | } else { |
255 | 0 | PeekErr(<Input as StreamOnce>::Error::empty(start).into()) |
256 | | }; |
257 | 0 | } |
258 | 0 | committed = true; |
259 | | } |
260 | 0 | PeekErr(mut error) => { |
261 | 0 | error.error.set_position(start); |
262 | 0 | return if committed { |
263 | 0 | CommitErr(error.error) |
264 | | } else { |
265 | 0 | PeekErr(error) |
266 | | }; |
267 | | } |
268 | 0 | CommitErr(mut error) => { |
269 | 0 | error.set_position(start); |
270 | 0 | return CommitErr(error); |
271 | | } |
272 | | } |
273 | | } |
274 | 0 | if committed { |
275 | 0 | CommitOk(self.tokens.clone()) |
276 | | } else { |
277 | 0 | PeekOk(self.tokens.clone()) |
278 | | } |
279 | 0 | } |
280 | 0 | fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) { |
281 | 0 | errors.error.add_expected(&self.expected); |
282 | 0 | } |
283 | | } |
284 | | |
285 | | /// Parses multiple tokens. |
286 | | /// |
287 | | /// Consumes items from the input and compares them to the values from `tokens` using the |
288 | | /// comparison function `cmp`. Succeeds if all the items from `tokens` are matched in the input |
289 | | /// stream and fails otherwise with `expected` used as part of the error. |
290 | | /// |
291 | | /// ``` |
292 | | /// # extern crate combine; |
293 | | /// # use combine::*; |
294 | | /// # use combine::error; |
295 | | /// # fn main() { |
296 | | /// let result = tokens(|l, r| l.eq_ignore_ascii_case(&r), "abc", "abc".chars()) |
297 | | /// .parse("AbC") |
298 | | /// .map(|x| x.0.as_str()); |
299 | | /// assert_eq!(result, Ok("abc")); |
300 | | /// let result = tokens( |
301 | | /// |&l, r| (if l < r { r - l } else { l - r }) <= 2, |
302 | | /// error::Range(&b"025"[..]), |
303 | | /// &b"025"[..] |
304 | | /// ) |
305 | | /// .parse(&b"123"[..]) |
306 | | /// .map(|x| x.0); |
307 | | /// assert_eq!(result, Ok(&b"025"[..])); |
308 | | /// # } |
309 | | /// ``` |
310 | 0 | pub fn tokens<C, E, T, Input>(cmp: C, expected: E, tokens: T) -> Tokens<C, E, T, Input> |
311 | 0 | where |
312 | 0 | C: FnMut(T::Item, Input::Token) -> bool, |
313 | 0 | T: Clone + IntoIterator, |
314 | 0 | Input: Stream, |
315 | | { |
316 | 0 | Tokens { |
317 | 0 | cmp, |
318 | 0 | expected, |
319 | 0 | tokens, |
320 | 0 | _marker: PhantomData, |
321 | 0 | } |
322 | 0 | } |
323 | | |
324 | | #[derive(Clone)] |
325 | | pub struct TokensCmp<C, T, Input> |
326 | | where |
327 | | Input: Stream, |
328 | | { |
329 | | cmp: C, |
330 | | tokens: T, |
331 | | _marker: PhantomData<Input>, |
332 | | } |
333 | | |
334 | | impl<Input, C, T> Parser<Input> for TokensCmp<C, T, Input> |
335 | | where |
336 | | C: FnMut(T::Item, Input::Token) -> bool, |
337 | | T: Clone + IntoIterator, |
338 | | Input: Stream, |
339 | | { |
340 | | type Output = T; |
341 | | type PartialState = (); |
342 | | |
343 | | #[inline] |
344 | 0 | fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<T, Input::Error> { |
345 | 0 | let start = input.position(); |
346 | 0 | let mut committed = false; |
347 | 0 | for c in self.tokens.clone() { |
348 | 0 | match crate::stream::uncons(input) { |
349 | 0 | CommitOk(other) | PeekOk(other) => { |
350 | 0 | if !(self.cmp)(c, other.clone()) { |
351 | 0 | return if committed { |
352 | 0 | let errors = <Input as StreamOnce>::Error::from_error( |
353 | 0 | start, |
354 | 0 | StreamError::unexpected_token(other), |
355 | | ); |
356 | 0 | CommitErr(errors) |
357 | | } else { |
358 | 0 | PeekErr(<Input as StreamOnce>::Error::empty(start).into()) |
359 | | }; |
360 | 0 | } |
361 | 0 | committed = true; |
362 | | } |
363 | 0 | PeekErr(mut error) => { |
364 | 0 | error.error.set_position(start); |
365 | 0 | return if committed { |
366 | 0 | CommitErr(error.error) |
367 | | } else { |
368 | 0 | PeekErr(error) |
369 | | }; |
370 | | } |
371 | 0 | CommitErr(mut error) => { |
372 | 0 | error.set_position(start); |
373 | 0 | return CommitErr(error); |
374 | | } |
375 | | } |
376 | | } |
377 | 0 | if committed { |
378 | 0 | CommitOk(self.tokens.clone()) |
379 | | } else { |
380 | 0 | PeekOk(self.tokens.clone()) |
381 | | } |
382 | 0 | } |
383 | | } |
384 | | |
385 | | /// Parses multiple tokens. |
386 | | /// |
387 | | /// Consumes items from the input and compares them to the values from `tokens` using the |
388 | | /// comparison function `cmp`. Succeeds if all the items from `tokens` are matched in the input |
389 | | /// stream and fails otherwise. |
390 | | /// |
391 | | /// ``` |
392 | | /// # extern crate combine; |
393 | | /// # use combine::*; |
394 | | /// # fn main() { |
395 | | /// # #[allow(deprecated)] |
396 | | /// # use std::ascii::AsciiExt; |
397 | | /// let result = tokens_cmp("abc".chars(), |l, r| l.eq_ignore_ascii_case(&r)) |
398 | | /// .parse("AbC") |
399 | | /// .map(|x| x.0.as_str()); |
400 | | /// assert_eq!(result, Ok("abc")); |
401 | | /// let result = tokens_cmp( |
402 | | /// &b"025"[..], |
403 | | /// |&l, r| (if l < r { r - l } else { l - r }) <= 2, |
404 | | /// ) |
405 | | /// .parse(&b"123"[..]) |
406 | | /// .map(|x| x.0); |
407 | | /// assert_eq!(result, Ok(&b"025"[..])); |
408 | | /// # } |
409 | | /// ``` |
410 | 0 | pub fn tokens_cmp<C, T, I>(tokens: T, cmp: C) -> TokensCmp<C, T, I> |
411 | 0 | where |
412 | 0 | C: FnMut(T::Item, I::Token) -> bool, |
413 | 0 | T: Clone + IntoIterator, |
414 | 0 | I: Stream, |
415 | | { |
416 | 0 | TokensCmp { |
417 | 0 | cmp, |
418 | 0 | tokens, |
419 | 0 | _marker: PhantomData, |
420 | 0 | } |
421 | 0 | } |
422 | | |
423 | | #[derive(Copy, Clone)] |
424 | | pub struct Position<Input> |
425 | | where |
426 | | Input: Stream, |
427 | | { |
428 | | _marker: PhantomData<Input>, |
429 | | } |
430 | | |
431 | | impl<Input> Parser<Input> for Position<Input> |
432 | | where |
433 | | Input: Stream, |
434 | | { |
435 | | type Output = Input::Position; |
436 | | type PartialState = (); |
437 | | |
438 | | #[inline] |
439 | 0 | fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Input::Position, Input::Error> { |
440 | 0 | PeekOk(input.position()) |
441 | 0 | } |
442 | | } |
443 | | |
444 | | /// Parser which just returns the current position in the stream. |
445 | | /// |
446 | | /// ``` |
447 | | /// # extern crate combine; |
448 | | /// # use combine::*; |
449 | | /// # use combine::stream::position::{self, SourcePosition}; |
450 | | /// # fn main() { |
451 | | /// let result = (position(), token('!'), position()) |
452 | | /// .parse(position::Stream::new("!")) |
453 | | /// .map(|x| x.0); |
454 | | /// assert_eq!(result, Ok((SourcePosition { line: 1, column: 1 }, |
455 | | /// '!', |
456 | | /// SourcePosition { line: 1, column: 2 }))); |
457 | | /// # } |
458 | | /// ``` |
459 | 0 | pub fn position<Input>() -> Position<Input> |
460 | 0 | where |
461 | 0 | Input: Stream, |
462 | | { |
463 | 0 | Position { |
464 | 0 | _marker: PhantomData, |
465 | 0 | } |
466 | 0 | } |
467 | | |
468 | | #[derive(Copy, Clone)] |
469 | | pub struct OneOf<T, Input> |
470 | | where |
471 | | Input: Stream, |
472 | | { |
473 | | tokens: T, |
474 | | _marker: PhantomData<Input>, |
475 | | } |
476 | | |
477 | | impl<Input, T> Parser<Input> for OneOf<T, Input> |
478 | | where |
479 | | T: Clone + IntoIterator<Item = Input::Token>, |
480 | | Input: Stream, |
481 | | Input::Token: PartialEq, |
482 | | { |
483 | | type Output = Input::Token; |
484 | | type PartialState = (); |
485 | | |
486 | | #[inline] |
487 | 0 | fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Input::Token, Input::Error> { |
488 | 0 | satisfy(|c| self.tokens.clone().into_iter().any(|t| t == c)).parse_lazy(input) |
489 | 0 | } |
490 | | |
491 | 0 | fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) { |
492 | 0 | for expected in self.tokens.clone() { |
493 | 0 | errors.error.add_expected(error::Token(expected)); |
494 | 0 | } |
495 | 0 | } |
496 | | } |
497 | | |
498 | | /// Extract one token and succeeds if it is part of `tokens`. |
499 | | /// |
500 | | /// ``` |
501 | | /// # extern crate combine; |
502 | | /// # use combine::*; |
503 | | /// # fn main() { |
504 | | /// let result = many(one_of("abc".chars())) |
505 | | /// .parse("abd"); |
506 | | /// assert_eq!(result, Ok((String::from("ab"), "d"))); |
507 | | /// # } |
508 | | /// ``` |
509 | 0 | pub fn one_of<T, Input>(tokens: T) -> OneOf<T, Input> |
510 | 0 | where |
511 | 0 | T: Clone + IntoIterator, |
512 | 0 | Input: Stream, |
513 | 0 | Input::Token: PartialEq<T::Item>, |
514 | | { |
515 | 0 | OneOf { |
516 | 0 | tokens, |
517 | 0 | _marker: PhantomData, |
518 | 0 | } |
519 | 0 | } |
520 | | |
521 | | #[derive(Copy, Clone)] |
522 | | pub struct NoneOf<T, Input> |
523 | | where |
524 | | Input: Stream, |
525 | | { |
526 | | tokens: T, |
527 | | _marker: PhantomData<Input>, |
528 | | } |
529 | | |
530 | | impl<Input, T> Parser<Input> for NoneOf<T, Input> |
531 | | where |
532 | | T: Clone + IntoIterator<Item = Input::Token>, |
533 | | Input: Stream, |
534 | | Input::Token: PartialEq, |
535 | | { |
536 | | type Output = Input::Token; |
537 | | type PartialState = (); |
538 | | |
539 | | #[inline] |
540 | 0 | fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Input::Token, Input::Error> { |
541 | 0 | satisfy(|c| self.tokens.clone().into_iter().all(|t| t != c)).parse_lazy(input) |
542 | 0 | } |
543 | | } |
544 | | |
545 | | /// Extract one token and succeeds if it is not part of `tokens`. |
546 | | /// |
547 | | /// ``` |
548 | | /// # extern crate combine; |
549 | | /// # use combine::*; |
550 | | /// # use combine::stream::easy; |
551 | | /// # use combine::stream::position; |
552 | | /// # fn main() { |
553 | | /// let mut parser = many1(none_of(b"abc".iter().cloned())); |
554 | | /// let result = parser.easy_parse(position::Stream::new(&b"xyb"[..])) |
555 | | /// .map(|(output, input)| (output, input.input)); |
556 | | /// assert_eq!(result, Ok((b"xy"[..].to_owned(), &b"b"[..]))); |
557 | | /// |
558 | | /// let result = parser.easy_parse(position::Stream::new(&b"ab"[..])); |
559 | | /// assert_eq!(result, Err(easy::Errors { |
560 | | /// position: 0, |
561 | | /// errors: vec![ |
562 | | /// easy::Error::Unexpected(easy::Info::Token(b'a')), |
563 | | /// ] |
564 | | /// })); |
565 | | /// # } |
566 | | /// ``` |
567 | 0 | pub fn none_of<T, Input>(tokens: T) -> NoneOf<T, Input> |
568 | 0 | where |
569 | 0 | T: Clone + IntoIterator, |
570 | 0 | Input: Stream, |
571 | 0 | Input::Token: PartialEq<T::Item>, |
572 | | { |
573 | 0 | NoneOf { |
574 | 0 | tokens, |
575 | 0 | _marker: PhantomData, |
576 | 0 | } |
577 | 0 | } |
578 | | |
579 | | #[derive(Copy, Clone)] |
580 | | pub struct Value<Input, T>(T, PhantomData<fn(Input) -> Input>); |
581 | | impl<Input, T> Parser<Input> for Value<Input, T> |
582 | | where |
583 | | Input: Stream, |
584 | | T: Clone, |
585 | | { |
586 | | type Output = T; |
587 | | type PartialState = (); |
588 | | #[inline] |
589 | 5.77M | fn parse_lazy(&mut self, _: &mut Input) -> ParseResult<T, Input::Error> { |
590 | 5.77M | PeekOk(self.0.clone()) |
591 | 5.77M | } <combine::parser::token::Value<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, u8> as combine::parser::Parser<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>>::parse_lazy Line | Count | Source | 589 | 5.77M | fn parse_lazy(&mut self, _: &mut Input) -> ParseResult<T, Input::Error> { | 590 | 5.77M | PeekOk(self.0.clone()) | 591 | 5.77M | } |
Unexecuted instantiation: <combine::parser::token::Value<_, _> as combine::parser::Parser<_>>::parse_lazy |
592 | | } |
593 | | |
594 | | /// Always returns the value `v` without consuming any input. |
595 | | /// |
596 | | /// ``` |
597 | | /// # extern crate combine; |
598 | | /// # use combine::*; |
599 | | /// # fn main() { |
600 | | /// let result = value(42) |
601 | | /// .parse("hello world") |
602 | | /// .map(|x| x.0); |
603 | | /// assert_eq!(result, Ok(42)); |
604 | | /// # } |
605 | | /// ``` |
606 | 5.77M | pub fn value<Input, T>(v: T) -> Value<Input, T> |
607 | 5.77M | where |
608 | 5.77M | Input: Stream, |
609 | 5.77M | T: Clone, |
610 | | { |
611 | 5.77M | Value(v, PhantomData) |
612 | 5.77M | } combine::parser::token::value::<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, u8> Line | Count | Source | 606 | 5.77M | pub fn value<Input, T>(v: T) -> Value<Input, T> | 607 | 5.77M | where | 608 | 5.77M | Input: Stream, | 609 | 5.77M | T: Clone, | 610 | | { | 611 | 5.77M | Value(v, PhantomData) | 612 | 5.77M | } |
Unexecuted instantiation: combine::parser::token::value::<_, _> |
613 | | |
614 | | #[derive(Copy, Clone)] |
615 | | pub struct Produce<Input, F>(F, PhantomData<fn(Input) -> Input>); |
616 | | impl<Input, F, R> Parser<Input> for Produce<Input, F> |
617 | | where |
618 | | Input: Stream, |
619 | | F: FnMut() -> R, |
620 | | { |
621 | | type Output = R; |
622 | | type PartialState = (); |
623 | | #[inline] |
624 | 41.0k | fn parse_lazy(&mut self, _: &mut Input) -> ParseResult<R, Input::Error> { |
625 | 41.0k | PeekOk((self.0)()) |
626 | 41.0k | } <combine::parser::token::Produce<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, redis::parser::value<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}::{closure#1}::{closure#3}::{closure#0}::{closure#0}> as combine::parser::Parser<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>>::parse_lazy Line | Count | Source | 624 | 31.1k | fn parse_lazy(&mut self, _: &mut Input) -> ParseResult<R, Input::Error> { | 625 | 31.1k | PeekOk((self.0)()) | 626 | 31.1k | } |
<combine::parser::token::Produce<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, redis::parser::value<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}::{closure#1}::{closure#5}::{closure#0}::{closure#0}> as combine::parser::Parser<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>>::parse_lazy Line | Count | Source | 624 | 1.76k | fn parse_lazy(&mut self, _: &mut Input) -> ParseResult<R, Input::Error> { | 625 | 1.76k | PeekOk((self.0)()) | 626 | 1.76k | } |
<combine::parser::token::Produce<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, redis::parser::value<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}::{closure#1}::{closure#9}::{closure#0}::{closure#0}> as combine::parser::Parser<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>>::parse_lazy Line | Count | Source | 624 | 1.91k | fn parse_lazy(&mut self, _: &mut Input) -> ParseResult<R, Input::Error> { | 625 | 1.91k | PeekOk((self.0)()) | 626 | 1.91k | } |
<combine::parser::token::Produce<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, redis::parser::value<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}::{closure#1}::{closure#10}::{closure#0}::{closure#0}> as combine::parser::Parser<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>>::parse_lazy Line | Count | Source | 624 | 6.15k | fn parse_lazy(&mut self, _: &mut Input) -> ParseResult<R, Input::Error> { | 625 | 6.15k | PeekOk((self.0)()) | 626 | 6.15k | } |
Unexecuted instantiation: <combine::parser::token::Produce<_, _> as combine::parser::Parser<_>>::parse_lazy |
627 | | } |
628 | | |
629 | | /// Always returns the value produced by calling `f`. |
630 | | /// |
631 | | /// Can be used when `value` is unable to be used for lack of `Clone` implementation on the value. |
632 | | /// |
633 | | /// ``` |
634 | | /// # use combine::*; |
635 | | /// #[derive(Debug, PartialEq)] |
636 | | /// struct NoClone; |
637 | | /// let result = produce(|| vec![NoClone]) |
638 | | /// .parse("hello world") |
639 | | /// .map(|x| x.0); |
640 | | /// assert_eq!(result, Ok(vec![NoClone])); |
641 | | /// ``` |
642 | 41.0k | pub fn produce<Input, F, R>(f: F) -> Produce<Input, F> |
643 | 41.0k | where |
644 | 41.0k | Input: Stream, |
645 | 41.0k | F: FnMut() -> R, |
646 | | { |
647 | 41.0k | Produce(f, PhantomData) |
648 | 41.0k | } combine::parser::token::produce::<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, redis::parser::value<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}::{closure#1}::{closure#3}::{closure#0}::{closure#0}, redis::types::Value> Line | Count | Source | 642 | 31.1k | pub fn produce<Input, F, R>(f: F) -> Produce<Input, F> | 643 | 31.1k | where | 644 | 31.1k | Input: Stream, | 645 | 31.1k | F: FnMut() -> R, | 646 | | { | 647 | 31.1k | Produce(f, PhantomData) | 648 | 31.1k | } |
combine::parser::token::produce::<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, redis::parser::value<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}::{closure#1}::{closure#5}::{closure#0}::{closure#0}, redis::types::Value> Line | Count | Source | 642 | 1.76k | pub fn produce<Input, F, R>(f: F) -> Produce<Input, F> | 643 | 1.76k | where | 644 | 1.76k | Input: Stream, | 645 | 1.76k | F: FnMut() -> R, | 646 | | { | 647 | 1.76k | Produce(f, PhantomData) | 648 | 1.76k | } |
combine::parser::token::produce::<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, redis::parser::value<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}::{closure#1}::{closure#9}::{closure#0}::{closure#0}, redis::types::Value> Line | Count | Source | 642 | 1.91k | pub fn produce<Input, F, R>(f: F) -> Produce<Input, F> | 643 | 1.91k | where | 644 | 1.91k | Input: Stream, | 645 | 1.91k | F: FnMut() -> R, | 646 | | { | 647 | 1.91k | Produce(f, PhantomData) | 648 | 1.91k | } |
combine::parser::token::produce::<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>, redis::parser::value<combine::stream::easy::Stream<combine::stream::MaybePartialStream<&[u8]>>>::{closure#0}::{closure#1}::{closure#10}::{closure#0}::{closure#0}, redis::types::Value> Line | Count | Source | 642 | 6.15k | pub fn produce<Input, F, R>(f: F) -> Produce<Input, F> | 643 | 6.15k | where | 644 | 6.15k | Input: Stream, | 645 | 6.15k | F: FnMut() -> R, | 646 | | { | 647 | 6.15k | Produce(f, PhantomData) | 648 | 6.15k | } |
Unexecuted instantiation: combine::parser::token::produce::<_, _, _> |
649 | | |
650 | | #[derive(Copy, Clone)] |
651 | | pub struct Eof<Input>(PhantomData<Input>); |
652 | | impl<Input> Parser<Input> for Eof<Input> |
653 | | where |
654 | | Input: Stream, |
655 | | { |
656 | | type Output = (); |
657 | | type PartialState = (); |
658 | | |
659 | | #[inline] |
660 | 0 | fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<(), Input::Error> { |
661 | 0 | let before = input.checkpoint(); |
662 | 0 | match input.uncons() { |
663 | 0 | Err(ref err) if err.is_unexpected_end_of_input() => PeekOk(()), |
664 | | _ => { |
665 | 0 | ctry!(input.reset(before).committed()); |
666 | 0 | PeekErr(<Input as StreamOnce>::Error::empty(input.position()).into()) |
667 | | } |
668 | | } |
669 | 0 | } |
670 | | |
671 | 0 | fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) { |
672 | 0 | errors.error.add_expected("end of input"); |
673 | 0 | } |
674 | | } |
675 | | |
676 | | /// Succeeds only if the stream is at end of input, fails otherwise. |
677 | | /// |
678 | | /// ``` |
679 | | /// # extern crate combine; |
680 | | /// # use combine::*; |
681 | | /// # use combine::stream::easy; |
682 | | /// # use combine::stream::position::{self, SourcePosition}; |
683 | | /// # fn main() { |
684 | | /// let mut parser = eof(); |
685 | | /// assert_eq!(parser.easy_parse(position::Stream::new("")), Ok(((), position::Stream::new("")))); |
686 | | /// assert_eq!(parser.easy_parse(position::Stream::new("x")), Err(easy::Errors { |
687 | | /// position: SourcePosition::default(), |
688 | | /// errors: vec![ |
689 | | /// easy::Error::Unexpected('x'.into()), |
690 | | /// easy::Error::Expected("end of input".into()) |
691 | | /// ] |
692 | | /// })); |
693 | | /// # } |
694 | | /// ``` |
695 | 0 | pub fn eof<Input>() -> Eof<Input> |
696 | 0 | where |
697 | 0 | Input: Stream, |
698 | | { |
699 | 0 | Eof(PhantomData) |
700 | 0 | } |