autumn 0.4.3

A recursive descent parser combinator library
Documentation
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Commonly used parsers that can be combined with each other
//!
//! The functions here are either parsers in their own right or functions that produce parsers as
//! their output.
//!
//! Specifying characters
//! =====================
//!
//! The following parsers are provided that parse a single character matching a certain category of
//! characters. They all produce a [`Span`](../struct.Span.html) of the character they match.
//!
//!  * [`any_character`](fn.any_character.html) will match any Unicode character.
//!  * [`alphabetic`](fn.alphabetic.html) will match an ASCII alphabetic character.
//!  * [`alphanumeric`](fn.alphanumeric.html) will match an ASCII alphanumeric character.
//!  * [`digit`](fn.digit.html) will match an ASCII decimal digit.
//!  * [`whitespace`](fn.whitespace.html) will match any Unicode whitespace character.
//!    [`space`](fn.space.html) is also provided to match one or more Unicode whitespace
//!    characters.
//!
//! These can be combined to produce many basic parsers.
//!
//! ```rust
//! # use autumn::prelude::*;
//! /// Parses C-like identifiers
//! fn identifier(source: &str, location: Span) -> ParseResult<String> {
//!     alphabetic
//!         .or("_")
//!         .and(alphanumeric.or("_").multiple().maybe())
//!         .copy_string()
//!         .parse(source, location)
//! }
//!
//! /// Parses integers
//! fn integer(source: &str, location: Span) -> ParseResult<String> {
//!     digit.multiple().copy_string().parse(source, location)
//! }
//!
//! /// Parses float literals
//! fn float(source: &str, location: Span) -> ParseResult<String> {
//!     digit
//!         .multiple()
//!         .and(".".and(digit.multiple()).maybe())
//!         .or(".".and(digit.multiple()))
//!         .copy_string()
//!         .parse(source, location)
//! }
//! ```
//!
//! Specific characters or strings
//! ==============================
//!
//! To parse a specific literal character or string, the corresponding `&str` or `String`
//! can be used directly as a parser. These types will all parse themselves from the input and
//! produce a [`Span`](../struct.Span.html) corresponding to the matched characters.
//!
//! The [`empty`](fn.empty.html) parser is also provided to parse no characters and produce an
//! empty [`Concat`](../trait.Concat.html).
//!
//! ```rust
//! # use autumn::prelude::*;
//! /// Parse C18 storage class specifiers
//! fn storage_class(source: &str, location: Span) -> ParseResult<String> {
//!     "auto"
//!         .or("extern")
//!         .or("register")
//!         .or("static")
//!         .or("typedef")
//!         .or("_Thread_local")
//!         .copy_string()
//!         .parse(source, location)
//! }
//! ```
//!
//! Parsing a character rather than a list of characters
//! ----------------------------------------------------
//!
//! The [`character`](fn.character.html) parser consumes any Unicode character and
//! produces a `char` rather than a [`Span`](../struct.Span.html). The
//! [`condition`](../combinators/trait.ParserExt.html#method.condition) combinator can be used to
//! restrict which character is matched.
//!
//! ```rust
//! # use autumn::prelude::*;
//! /// Parse simple single-character operators
//! fn operator(source: &str, location: Span) -> ParseResult<char> {
//!     '+'.or('-').or('*').or('/').or('%').parse(source, location)
//! }
//! ```
//!
//! Closures as parsers
//! ===================
//!
//! Although the Rust compiler will quite happily accept any function with the appropriate
//! signature as a parser it requires additional help to determine that closures may also be
//! parsers. The [`closure`](fn.closure.html) simply ensures that a closure is appropriately
//! considered a parser.
//!
//! ```rust
//! # use autumn::prelude::*;
//! /// Parse an exact number of a specific character
//! fn counted(character: char, count: usize) -> impl Parser<Span> {
//!     closure(move |source, location| {
//!         if count > 0 {
//!             any_character
//!                 .and(counted(character, count - 1))
//!                 .parse(source, location)
//!         } else {
//!             empty.parse(source, location)
//!         }
//!     })
//! }
//! ```
//!
//! Parsers that produce errors
//! ===========================
//!
//! The [`error`](fn.error.html) and [`throw`](fn.throw.html) functions can be used when a
//! parse output needs to be checked to determine if the value is correct or a parse occurs that
//! relates to an error in the source. The [`value`](fn.value.html) parser can be used to produce a
//! matching parser if a check shows that no error has occurred.
//!
//! ```rust
//! # use autumn::prelude::*;
//! #[derive(Clone)]
//! struct InvalidIdentifier(String);
//!
//! /// Parses C-like identifiers
//! fn identifier(
//!     source: &str,
//!     location: Span,
//! ) -> ParseResult<Option<String>, InvalidIdentifier> {
//!     alphabetic
//!         .or("_")
//!         .and(alphanumeric.or("_").multiple())
//!         .copy_string()
//!         .map(Some)
//!         .on_none(
//!             any_character
//!                 .str_condition(|c| !c.chars().any(char::is_whitespace))
//!                 .multiple()
//!                 .copy_string()
//!                 .and_then(|identifier| throw(None, InvalidIdentifier(identifier)))
//!         )
//!         .catch()
//!         .parse(source, location)
//! }
//! ```

use crate::combinators::{Boxed, BoxedParserExt, ConcatParserExt, ParserExt, TextParserExt};
use crate::location::Span;
use crate::{Concat, ParseResult, Parser};

/// Creates a parser that consumes no input and produces the given value as the result of parsing
///
/// See [parsers that produce errors](index.html#parsers-that-produce-errors).
///
/// This can be used if a particular value will be produced within an
/// [`and_then`](../combinators/trait.ParserExt.html#method.and_then) combinator.
///
/// ```rust
/// # use autumn::prelude::*;
/// fn alphabet(source: &str, location: Span) -> ParseResult<String, &'static str> {
///     "abcde"
///         .and(digit)
///         .copy_string()
///         .and_then(|text| {
///             if text.ends_with("0") {
///                 error(text, "Token must not end with 0")
///             } else {
///                 value(text)
///             }
///         })
///         .parse(source, location)
/// }
/// ```
pub fn value<'p, T: Clone + 'p, E: 'p>(value: T) -> Boxed<dyn Parser<T, E> + 'p> {
    closure(move |source, location| success(value.clone(), source, location)).boxed()
}

/// Creates a parser that consumes no input and produces the given error as the result of parsing
///
/// See [parsers that produce errors](index.html#parsers-that-produce-errors).
///
/// This can be used if a particular value will be produced within an
/// [`and_then`](../combinators/trait.ParserExt.html#method.and_then) combinator.
///
/// ```rust
/// # use autumn::prelude::*;
/// fn alphabet(source: &str, location: Span) -> ParseResult<String, &'static str> {
///     "abcde"
///         .and(digit)
///         .copy_string()
///         .and_then(|text| {
///             if text.ends_with("0") {
///                 error(text, "Token must not end with 0")
///             } else {
///                 value(text)
///             }
///         })
///         .parse(source, location)
/// }
/// ```
pub fn error<'p, T: Clone + 'p, E: Clone + 'p>(value: T, error: E) -> Boxed<dyn Parser<T, E> + 'p> {
    closure(move |source, location| failure(value.clone(), error.clone(), source, location)).boxed()
}

/// Creates a parser that consumes no input and produces the given error as an exception
///
/// See [parsers that produce errors](index.html#parsers-that-produce-errors).
///
/// This can be used if a particular value will be produced within an
/// [`and_then`](../combinators/trait.ParserExt.html#method.and_then) combinator. The exception
/// produced can be turned into an error with the
/// [`catch`](../combinators/trait.ParserExt.html#method.catch) combinator.
///
/// ```rust
/// # use autumn::prelude::*;
/// fn alphabet(source: &str, location: Span) -> ParseResult<String, &'static str> {
///     "abcde"
///         .and(digit)
///         .copy_string()
///         .and_then(|text| {
///             if text.ends_with("0") {
///                 throw(text, "Token must not end with 0")
///             } else {
///                 value(text)
///             }
///         })
///         .catch()
///         .parse(source, location)
/// }
/// ```
pub fn throw<'p, T: Clone + 'p, E: Clone + 'p>(value: T, error: E) -> Boxed<dyn Parser<T, E> + 'p> {
    closure(move |source, location| exception(value.clone(), error.clone(), source, location))
        .boxed()
}

fn success<T, E>(value: T, source: &str, location: Span) -> ParseResult<T, E> {
    ParseResult::success(value, source, location)
}

fn failure<'s, T, E>(value: T, error: E, source: &'s str, location: Span) -> ParseResult<'s, T, E> {
    ParseResult::error(value, error, source, location)
}

fn exception<'s, T, E>(
    value: T,
    error: E,
    source: &'s str,
    location: Span,
) -> ParseResult<'s, T, E> {
    ParseResult::exception(value, error, source, location)
}

/// A parser that consumes no input and produces an empty [`Concat`](../trait.Concat.html)
pub fn empty<T: Concat, E>(source: &str, location: Span) -> ParseResult<T, E> {
    ParseResult::success(T::empty_at(location), source, location)
}

/// Converts a parser-like closure into an actual parser
///
/// Although the Rust compiler will quite happily accept any function with the appropriate
/// signature as a parser it requires additional help to determine that closures may also be
/// parsers. The [`closure`](fn.closure.html) simply ensures that a closure is appropriately
/// considered a parser.
///
/// ```rust
/// # use autumn::prelude::*;
/// /// Parse an exact number of a specific character
/// fn counted(character: char, count: usize) -> impl Parser<Span> {
///     closure(move |source, location| {
///         if count > 0 {
///             any_character
///                 .and(counted(character, count - 1))
///                 .parse(source, location)
///         } else {
///             empty.parse(source, location)
///         }
///     })
/// }
/// ```
pub fn closure<F, T, E>(function: F) -> impl Parser<T, E>
where
    F: for<'s> Fn(&'s str, Span) -> ParseResult<'s, T, E>,
{
    function
}

/// Takes a reference to a parser and produces a complete parser
///
/// This is the most simple mechanism to use a reference to a parser as a parser. We can't
/// implement parsers directly without conflicting with the implementation of parsers on
/// implementors of the Fn trait.
pub fn ref_parser<'p, T: 'p, E: 'p, P: Parser<T, E>>(parser: &'p P) -> impl Parser<T, E> + 'p {
    closure(move |source, location| parser.parse(source, location))
}

/// Parsers a single character from the input as an element of a
/// [`Span`](../struct.Span.html)
///
/// See [specifying characters](index.html#specifying-characters).
pub fn any_character<'s, E>(source: &'s str, mut location: Span) -> ParseResult<'s, Span, E> {
    if let Some((_, next)) = source.char_indices().next() {
        location.after(next);
        ParseResult::success(location, &source[next.len_utf8()..], location)
    } else {
        ParseResult::none(location)
    }
}

/// Parses a single character from the input
pub fn character<E>(source: &str, mut location: Span) -> ParseResult<char, E> {
    if let Some((_, next)) = source.char_indices().next() {
        location.after(next);
        ParseResult::success(next, &source[next.len_utf8()..], location)
    } else {
        ParseResult::none(location)
    }
}

fn char_condition<'s, E>(
    condition: &impl Fn(char) -> bool,
    source: &'s str,
    location: Span,
) -> ParseResult<'s, Span, E> {
    any_character
        .str_condition(|s| s.chars().all(condition))
        .parse(source, location)
}

impl<E> Parser<char, E> for char {
    fn parse<'s>(&self, source: &'s str, location: Span) -> ParseResult<'s, char, E> {
        let expected = *self;
        character
            .condition(move |c| *c == expected)
            .parse(source, location)
    }
}

impl<E> Parser<char, E> for &char {
    fn parse<'s>(&self, source: &'s str, location: Span) -> ParseResult<'s, char, E> {
        (*self).parse(source, location)
    }
}

/// Parses a single ASCII digit character from the input
///
/// See [specifying characters](index.html#specifying-characters).
pub fn digit<E>(source: &str, location: Span) -> ParseResult<Span, E> {
    char_condition(&|c| c.is_ascii_digit(), source, location)
}

/// Parses a single ASCII alphabetic character from the input
///
/// See [specifying characters](index.html#specifying-characters).
pub fn alphabetic<E>(source: &str, location: Span) -> ParseResult<Span, E> {
    char_condition(&|c| c.is_ascii_alphabetic(), source, location)
}

/// Parses a single ASCII alphabetic or digit character from the input
///
/// See [specifying characters](index.html#specifying-characters).
pub fn alphanumeric<E>(source: &str, location: Span) -> ParseResult<Span, E> {
    char_condition(&|c| c.is_ascii_alphanumeric(), source, location)
}

/// Parses a single Unicode whitespace character from the input
///
/// See [specifying characters](index.html#specifying-characters).
pub fn whitespace<E>(source: &str, location: Span) -> ParseResult<Span, E> {
    char_condition(&|c| c.is_whitespace(), source, location)
}

/// Parses one or more Unicode whitespace characters from the input
///
/// See [specifying characters](index.html#specifying-characters).
pub fn space<E>(source: &str, location: Span) -> ParseResult<Span, E> {
    whitespace.multiple().parse(source, location)
}

fn exact_rec<'s, E>(exact: &str, source: &'s str, location: Span) -> ParseResult<'s, Span, E> {
    if let Some(next) = exact.chars().next() {
        let remaining = &exact[next.len_utf8()..];
        let next = &exact[..next.len_utf8()];
        any_character
            .str_condition(|s| s == next)
            .parse(source, location)
            .and_then(&|parsed, source, location| {
                exact_rec::<E>(remaining, source, location)
                    .map(&|remaining| parsed.concat(remaining))
            })
    } else {
        empty.parse(source, location)
    }
}

impl<E> Parser<Span, E> for str {
    fn parse<'s>(&self, source: &'s str, location: Span) -> ParseResult<'s, Span, E> {
        exact_rec(self, source, location)
    }
}

impl<E> Parser<Span, E> for &str {
    fn parse<'s>(&self, source: &'s str, location: Span) -> ParseResult<'s, Span, E> {
        exact_rec(self, source, location)
    }
}

impl<E> Parser<Span, E> for String {
    fn parse<'s>(&self, source: &'s str, location: Span) -> ParseResult<'s, Span, E> {
        exact_rec(self, source, location)
    }
}