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 | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 9 | //! Adapted from [`nom`](https://github.com/Geal/nom) by removing the |
| 10 | //! `IPResult::Incomplete` variant which: |
| 11 | //! |
| 12 | //! - we don't need, |
| 13 | //! - is an unintuitive footgun when working with non-streaming use cases, and |
| 14 | //! - more than doubles compilation time. |
| 15 | //! |
| 16 | //! ## Whitespace handling strategy |
| 17 | //! |
| 18 | //! As (sy)nom is a parser combinator library, the parsers provided here and |
| 19 | //! that you implement yourself are all made up of successively more primitive |
| 20 | //! parsers, eventually culminating in a small number of fundamental parsers |
| 21 | //! that are implemented in Rust. Among these are `punct!` and `keyword!`. |
| 22 | //! |
| 23 | //! All synom fundamental parsers (those not combined out of other parsers) |
| 24 | //! should be written to skip over leading whitespace in their input. This way, |
| 25 | //! as long as every parser eventually boils down to some combination of |
| 26 | //! fundamental parsers, we get correct whitespace handling at all levels for |
| 27 | //! free. |
| 28 | //! |
| 29 | //! For our use case, this strategy is a huge improvement in usability, |
| 30 | //! correctness, and compile time over nom's `ws!` strategy. |
| 31 | |
| 32 | use proc_macro2::TokenStream; |
| 33 | |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 34 | pub use error::{PResult, ParseError}; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 35 | |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame^] | 36 | use buffer::Cursor; |
| 37 | |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 38 | pub trait Synom: Sized { |
| 39 | fn parse(input: Cursor) -> PResult<Self>; |
| 40 | |
| 41 | fn description() -> Option<&'static str> { |
| 42 | None |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl Synom for TokenStream { |
| 47 | fn parse(input: Cursor) -> PResult<Self> { |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 48 | Ok((input.token_stream(), Cursor::empty())) |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 49 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 50 | |
| 51 | fn description() -> Option<&'static str> { |
| 52 | Some("arbitrary token stream") |
| 53 | } |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 54 | } |