David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
David Tolnay | b34f570 | 2018-01-06 19:39:49 -0800 | [diff] [blame] | 9 | //! Parsing interface for parsing a token stream into a syntax tree node. |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 10 | //! |
David Tolnay | b34f570 | 2018-01-06 19:39:49 -0800 | [diff] [blame] | 11 | //! All syntax tree nodes that can be parsed from a token stream need to |
| 12 | //! implement the [`Synom`] trait. This trait is usually implemented using the |
| 13 | //! [`nom`]-style parser combinator macros provided by Syn, but may also be |
| 14 | //! implemented without macros be using the low-level [`Cursor`] API directly. |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 15 | //! |
David Tolnay | b34f570 | 2018-01-06 19:39:49 -0800 | [diff] [blame] | 16 | //! [`Synom`]: trait.Synom.html |
| 17 | //! [`nom`]: https://github.com/Geal/nom |
| 18 | //! [`Cursor`]: ../buffer/index.html |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 19 | //! |
David Tolnay | b34f570 | 2018-01-06 19:39:49 -0800 | [diff] [blame] | 20 | //! The following parser combinator macros are available and a `Synom` parsing |
| 21 | //! example is provided for each one. |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 22 | //! |
David Tolnay | b34f570 | 2018-01-06 19:39:49 -0800 | [diff] [blame] | 23 | //! - [`alt!`](../macro.alt.html) |
| 24 | //! - [`braces!`](../macro.braces.html) |
| 25 | //! - [`brackets!`](../macro.brackets.html) |
| 26 | //! - [`call!`](../macro.call.html) |
| 27 | //! - [`cond!`](../macro.cond.html) |
| 28 | //! - [`cond_reduce!`](../macro.cond_reduce.html) |
| 29 | //! - [`do_parse!`](../macro.do_parse.html) |
| 30 | //! - [`epsilon!`](../macro.epsilon.html) |
| 31 | //! - [`input_end!`](../macro.input_end.html) |
| 32 | //! - [`keyword!`](../macro.keyword.html) |
| 33 | //! - [`many0!`](../macro.many0.html) |
| 34 | //! - [`map!`](../macro.map.html) |
| 35 | //! - [`not!`](../macro.not.html) |
| 36 | //! - [`option!`](../macro.option.html) |
| 37 | //! - [`parens!`](../macro.parens.html) |
| 38 | //! - [`punct!`](../macro.punct.html) |
| 39 | //! - [`reject!`](../macro.reject.html) |
| 40 | //! - [`switch!`](../macro.switch.html) |
| 41 | //! - [`syn!`](../macro.syn.html) |
| 42 | //! - [`tuple!`](../macro.tuple.html) |
| 43 | //! - [`value!`](../macro.value.html) |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame^] | 44 | //! |
| 45 | //! *This module is available if Syn is built with the `"parsing"` feature.* |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 46 | |
| 47 | use proc_macro2::TokenStream; |
| 48 | |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 49 | pub use error::{PResult, ParseError}; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 50 | |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 51 | use buffer::Cursor; |
| 52 | |
David Tolnay | b34f570 | 2018-01-06 19:39:49 -0800 | [diff] [blame] | 53 | /// Parsing interface implemented by all types that can be parsed from a token |
| 54 | /// stream. |
| 55 | /// |
| 56 | /// Refer to the [module documentation] for details about parsing in Syn. |
| 57 | /// |
| 58 | /// [module documentation]: index.html |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame^] | 59 | /// |
| 60 | /// *This trait is available if Syn is built with the `"parsing"` feature.* |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 61 | pub trait Synom: Sized { |
| 62 | fn parse(input: Cursor) -> PResult<Self>; |
| 63 | |
| 64 | fn description() -> Option<&'static str> { |
| 65 | None |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl Synom for TokenStream { |
| 70 | fn parse(input: Cursor) -> PResult<Self> { |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 71 | Ok((input.token_stream(), Cursor::empty())) |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 72 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 73 | |
| 74 | fn description() -> Option<&'static str> { |
| 75 | Some("arbitrary token stream") |
| 76 | } |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 77 | } |