blob: de16e290f5e61c8acaf043489cd64d632118a160 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// 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 Tolnayc5ab8c62017-12-26 16:43:39 -05009//! 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
32use proc_macro2::TokenStream;
33
34pub use cursor::*;
35
David Tolnay203557a2017-12-27 23:59:33 -050036pub use error::{PResult, ParseError};
David Tolnayc5ab8c62017-12-26 16:43:39 -050037
38pub trait Synom: Sized {
39 fn parse(input: Cursor) -> PResult<Self>;
40
41 fn description() -> Option<&'static str> {
42 None
43 }
44}
45
46impl Synom for TokenStream {
47 fn parse(input: Cursor) -> PResult<Self> {
David Tolnayf4aa6b42017-12-31 16:40:33 -050048 Ok((input.token_stream(), Cursor::empty()))
David Tolnayc5ab8c62017-12-26 16:43:39 -050049 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -080050
51 fn description() -> Option<&'static str> {
52 Some("arbitrary token stream")
53 }
David Tolnayc5ab8c62017-12-26 16:43:39 -050054}