David Tolnay | 30a3600 | 2017-02-08 14:24:12 -0800 | [diff] [blame] | 1 | //! Adapted from [`nom`](https://github.com/Geal/nom) by removing the |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 2 | //! `IPResult::Incomplete` variant which: |
David Tolnay | 30a3600 | 2017-02-08 14:24:12 -0800 | [diff] [blame] | 3 | //! |
| 4 | //! - we don't need, |
| 5 | //! - is an unintuitive footgun when working with non-streaming use cases, and |
| 6 | //! - more than doubles compilation time. |
| 7 | //! |
| 8 | //! ## Whitespace handling strategy |
| 9 | //! |
| 10 | //! As (sy)nom is a parser combinator library, the parsers provided here and |
| 11 | //! that you implement yourself are all made up of successively more primitive |
| 12 | //! parsers, eventually culminating in a small number of fundamental parsers |
| 13 | //! that are implemented in Rust. Among these are `punct!` and `keyword!`. |
| 14 | //! |
| 15 | //! All synom fundamental parsers (those not combined out of other parsers) |
| 16 | //! should be written to skip over leading whitespace in their input. This way, |
| 17 | //! as long as every parser eventually boils down to some combination of |
| 18 | //! fundamental parsers, we get correct whitespace handling at all levels for |
| 19 | //! free. |
| 20 | //! |
| 21 | //! For our use case, this strategy is a huge improvement in usability, |
| 22 | //! correctness, and compile time over nom's `ws!` strategy. |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 23 | |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 24 | extern crate unicode_xid; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 25 | extern crate proc_macro2; |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 26 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 27 | #[cfg(feature = "printing")] |
| 28 | extern crate quote; |
| 29 | |
David Tolnay | 30a3600 | 2017-02-08 14:24:12 -0800 | [diff] [blame] | 30 | #[doc(hidden)] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 31 | pub use proc_macro2::{TokenTree, TokenStream}; |
| 32 | |
| 33 | use std::convert::From; |
| 34 | use std::error::Error; |
| 35 | use std::fmt; |
| 36 | |
| 37 | use proc_macro2::LexError; |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 38 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 39 | #[cfg(feature = "parsing")] |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 40 | #[doc(hidden)] |
| 41 | pub mod helper; |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 42 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 43 | pub mod delimited; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 44 | pub mod tokens; |
| 45 | pub mod span; |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame^] | 46 | pub mod cursor; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 47 | |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 48 | /// A cursor into a Vec<TokenTree>. |
| 49 | /// |
| 50 | /// NOTE: This type is currently unnecessary, but will make future refactorings |
| 51 | /// which change this type easier. |
| 52 | pub type Cursor<'a> = &'a [TokenTree]; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 53 | |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 54 | /// The result of a parser |
| 55 | pub type PResult<'a, O> = Result<(Cursor<'a>, O), ParseError>; |
| 56 | |
| 57 | /// An error with a default error message. |
| 58 | /// |
| 59 | /// NOTE: We should provide better error messages in the future. |
| 60 | pub fn parse_error<O>() -> PResult<'static, O> { |
Michael Layzell | ad763b7 | 2017-06-01 00:25:31 -0400 | [diff] [blame] | 61 | Err(ParseError(None)) |
David Tolnay | f2222f0 | 2017-01-27 17:09:20 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 64 | pub trait Synom: Sized { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 65 | fn parse(input: Cursor) -> PResult<Self>; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 66 | |
| 67 | fn description() -> Option<&'static str> { |
| 68 | None |
| 69 | } |
| 70 | |
| 71 | fn parse_all(input: TokenStream) -> Result<Self, ParseError> { |
| 72 | let tokens = input.into_iter().collect::<Vec<_>>(); |
Michael Layzell | ad763b7 | 2017-06-01 00:25:31 -0400 | [diff] [blame] | 73 | let result = Self::parse(&tokens); |
| 74 | let err = match result { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 75 | Ok((rest, t)) => { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 76 | if rest.is_empty() { |
| 77 | return Ok(t) |
| 78 | } else if rest.len() == tokens.len() { |
| 79 | // parsed nothing |
| 80 | "failed to parse" |
| 81 | } else { |
| 82 | "unparsed tokens after" |
| 83 | } |
| 84 | } |
Michael Layzell | ad763b7 | 2017-06-01 00:25:31 -0400 | [diff] [blame] | 85 | Err(ref err) => err.description(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 86 | }; |
| 87 | match Self::description() { |
Michael Layzell | ad763b7 | 2017-06-01 00:25:31 -0400 | [diff] [blame] | 88 | Some(s) => Err(ParseError(Some(format!("{} {}", err, s)))), |
| 89 | None => Err(ParseError(Some(err.to_string()))), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | fn parse_str_all(input: &str) -> Result<Self, ParseError> { |
| 94 | Self::parse_all(input.parse()?) |
| 95 | } |
| 96 | |
| 97 | fn parse_all_unwrap(input: TokenStream) -> Self { |
| 98 | // TODO: eventually try to provide super nice error messages here as |
| 99 | // this is what most users will hit. Hopefully the compiler will give us |
| 100 | // an interface one day to give an extra-good error message here. |
| 101 | Self::parse_all(input).unwrap() |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | #[derive(Debug)] |
Michael Layzell | ad763b7 | 2017-06-01 00:25:31 -0400 | [diff] [blame] | 106 | pub struct ParseError(Option<String>); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 107 | |
| 108 | impl Error for ParseError { |
| 109 | fn description(&self) -> &str { |
Michael Layzell | ad763b7 | 2017-06-01 00:25:31 -0400 | [diff] [blame] | 110 | match self.0 { |
| 111 | Some(ref desc) => desc, |
| 112 | None => "failed to parse", |
| 113 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | impl fmt::Display for ParseError { |
| 118 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Michael Layzell | ad763b7 | 2017-06-01 00:25:31 -0400 | [diff] [blame] | 119 | <str as fmt::Display>::fmt(self.description(), f) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | impl From<LexError> for ParseError { |
| 124 | fn from(_: LexError) -> ParseError { |
Michael Layzell | ad763b7 | 2017-06-01 00:25:31 -0400 | [diff] [blame] | 125 | ParseError(Some("error while lexing input string".to_owned())) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | impl Synom for TokenStream { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 130 | fn parse(input: &[TokenTree]) -> PResult<Self> { |
| 131 | Ok((&[], input.iter().cloned().collect())) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 132 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 135 | /// Define a function from a parser combination. |
| 136 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 137 | /// - **Syntax:** `named!(NAME -> TYPE, PARSER)` or `named!(pub NAME -> TYPE, PARSER)` |
| 138 | /// |
| 139 | /// ```rust |
| 140 | /// # extern crate syn; |
| 141 | /// # #[macro_use] extern crate synom; |
| 142 | /// # use syn::Ty; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 143 | /// # use synom::delimited::Delimited; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 144 | /// # use synom::tokens::Comma; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 145 | /// // One or more Rust types separated by commas. |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 146 | /// named!(pub comma_separated_types -> Delimited<Ty, Comma>, |
| 147 | /// call!(Delimited::parse_separated_nonempty) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 148 | /// ); |
| 149 | /// # fn main() {} |
| 150 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 151 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 152 | macro_rules! named { |
| 153 | ($name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 154 | fn $name(i: $crate::Cursor) -> $crate::PResult<$o> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 155 | $submac!(i, $($args)*) |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | (pub $name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 160 | pub fn $name(i: $crate::Cursor) -> $crate::PResult<$o> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 161 | $submac!(i, $($args)*) |
| 162 | } |
| 163 | }; |
| 164 | } |
| 165 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 166 | /// Invoke the given parser function with the passed in arguments. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 167 | /// |
| 168 | /// - **Syntax:** `call!(FUNCTION, ARGS...)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 169 | /// |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 170 | /// where the signature of the function is `fn(&[U], ARGS...) -> IPResult<&[U], T>` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 171 | /// - **Output:** `T`, the result of invoking the function `FUNCTION` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 172 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 173 | macro_rules! call { |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 174 | ($i:expr, $fun:expr $(, $args:expr)*) => { |
| 175 | $fun($i $(, $args)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 176 | }; |
| 177 | } |
| 178 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 179 | /// Transform the result of a parser by applying a function or closure. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 180 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 181 | /// - **Syntax:** `map!(THING, FN)` |
| 182 | /// - **Output:** the return type of function FN applied to THING |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 183 | /// |
| 184 | /// ```rust |
| 185 | /// extern crate syn; |
| 186 | /// #[macro_use] extern crate synom; |
| 187 | /// |
| 188 | /// use syn::{Item, Ident}; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 189 | /// |
| 190 | /// fn get_item_ident(item: Item) -> Ident { |
| 191 | /// item.ident |
| 192 | /// } |
| 193 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 194 | /// // Parses an item and returns the name (identifier) of the item only. |
| 195 | /// named!(item_ident -> Ident, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 196 | /// map!(syn!(Item), get_item_ident) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 197 | /// ); |
| 198 | /// |
| 199 | /// // Or equivalently: |
| 200 | /// named!(item_ident2 -> Ident, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 201 | /// map!(syn!(Item), |i: Item| i.ident) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 202 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 203 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 204 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 205 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 206 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 207 | macro_rules! map { |
| 208 | ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 209 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 210 | ::std::result::Result::Err(err) => |
| 211 | ::std::result::Result::Err(err), |
| 212 | ::std::result::Result::Ok((i, o)) => |
| 213 | ::std::result::Result::Ok((i, call!(o, $g))), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 214 | } |
| 215 | }; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 216 | |
| 217 | ($i:expr, $f:expr, $g:expr) => { |
| 218 | map!($i, call!($f), $g) |
| 219 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 220 | } |
| 221 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 222 | /// Parses successfully if the given parser fails to parse. Does not consume any |
| 223 | /// of the input. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 224 | /// |
| 225 | /// - **Syntax:** `not!(THING)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 226 | /// - **Output:** `()` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 227 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 228 | macro_rules! not { |
| 229 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 230 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 231 | ::std::result::Result::Ok(_) => $crate::parse_error(), |
| 232 | ::std::result::Result::Err(_) => |
| 233 | ::std::result::Result::Ok(($i, ())), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 234 | } |
| 235 | }; |
| 236 | } |
| 237 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 238 | /// Conditionally execute the given parser. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 239 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 240 | /// If you are familiar with nom, this is nom's `cond_with_error` parser. |
| 241 | /// |
| 242 | /// - **Syntax:** `cond!(CONDITION, THING)` |
| 243 | /// - **Output:** `Some(THING)` if the condition is true, else `None` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 244 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 245 | macro_rules! cond { |
| 246 | ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => { |
| 247 | if $cond { |
| 248 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 249 | ::std::result::Result::Ok((i, o)) => |
| 250 | ::std::result::Result::Ok((i, ::std::option::Option::Some(o))), |
| 251 | ::std::result::Result::Err(x) => ::std::result::Result::Err(x), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 252 | } |
| 253 | } else { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 254 | ::std::result::Result::Ok(($i, ::std::option::Option::None)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 255 | } |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | ($i:expr, $cond:expr, $f:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 259 | cond!($i, $cond, call!($f)) |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 260 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 261 | } |
| 262 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 263 | /// Fail to parse if condition is false, otherwise parse the given parser. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 264 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 265 | /// This is typically used inside of `option!` or `alt!`. |
| 266 | /// |
| 267 | /// - **Syntax:** `cond_reduce!(CONDITION, THING)` |
| 268 | /// - **Output:** `THING` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 269 | #[macro_export] |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 270 | macro_rules! cond_reduce { |
| 271 | ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => { |
| 272 | if $cond { |
| 273 | $submac!($i, $($args)*) |
| 274 | } else { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 275 | $crate::parse_error() |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 276 | } |
| 277 | }; |
| 278 | |
| 279 | ($i:expr, $cond:expr, $f:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 280 | cond_reduce!($i, $cond, call!($f)) |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 281 | }; |
| 282 | } |
| 283 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 284 | /// Parse two things, returning the value of the first. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 285 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 286 | /// - **Syntax:** `terminated!(THING, AFTER)` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 287 | /// - **Output:** `THING` |
| 288 | /// |
| 289 | /// ```rust |
| 290 | /// extern crate syn; |
| 291 | /// #[macro_use] extern crate synom; |
| 292 | /// |
| 293 | /// use syn::Expr; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 294 | /// use synom::tokens::Pound; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 295 | /// |
| 296 | /// // An expression terminated by ##. |
| 297 | /// named!(expr_pound_pound -> Expr, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 298 | /// terminated!(syn!(Expr), tuple!(syn!(Pound), syn!(Pound))) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 299 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 300 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 301 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 302 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 303 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 304 | macro_rules! terminated { |
| 305 | ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => { |
| 306 | match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 307 | ::std::result::Result::Ok((i, (o, _))) => |
| 308 | ::std::result::Result::Ok((i, o)), |
| 309 | ::std::result::Result::Err(err) => |
| 310 | ::std::result::Result::Err(err), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 311 | } |
| 312 | }; |
| 313 | |
| 314 | ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 315 | terminated!($i, $submac!($($args)*), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 316 | }; |
| 317 | |
| 318 | ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 319 | terminated!($i, call!($f), $submac!($($args)*)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | ($i:expr, $f:expr, $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 323 | terminated!($i, call!($f), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 324 | }; |
| 325 | } |
| 326 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 327 | /// Parse zero or more values using the given parser. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 328 | /// |
| 329 | /// - **Syntax:** `many0!(THING)` |
| 330 | /// - **Output:** `Vec<THING>` |
| 331 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 332 | /// You may also be looking for: |
| 333 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 334 | /// - `call!(Delimited::parse_separated)` - zero or more values with separator |
| 335 | /// - `call!(Delimited::parse_separated_nonempty)` - one or more values |
| 336 | /// - `call!(Delimited::parse_terminated)` - zero or more, allows trailing separator |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 337 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 338 | /// ```rust |
| 339 | /// extern crate syn; |
| 340 | /// #[macro_use] extern crate synom; |
| 341 | /// |
| 342 | /// use syn::Item; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 343 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 344 | /// named!(items -> Vec<Item>, many0!(syn!(Item))); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 345 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 346 | /// # fn main() {} |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 347 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 348 | macro_rules! many0 { |
| 349 | ($i:expr, $submac:ident!( $($args:tt)* )) => {{ |
| 350 | let ret; |
| 351 | let mut res = ::std::vec::Vec::new(); |
| 352 | let mut input = $i; |
| 353 | |
| 354 | loop { |
| 355 | if input.is_empty() { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 356 | ret = ::std::result::Result::Ok((input, res)); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 357 | break; |
| 358 | } |
| 359 | |
| 360 | match $submac!(input, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 361 | ::std::result::Result::Err(_) => { |
| 362 | ret = ::std::result::Result::Ok((input, res)); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 363 | break; |
| 364 | } |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 365 | ::std::result::Result::Ok((i, o)) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 366 | // loop trip must always consume (otherwise infinite loops) |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 367 | if i.len() == input.len() { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 368 | ret = $crate::parse_error(); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 369 | break; |
| 370 | } |
| 371 | |
| 372 | res.push(o); |
| 373 | input = i; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | ret |
| 379 | }}; |
| 380 | |
| 381 | ($i:expr, $f:expr) => { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 382 | $crate::many0($i, $f) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 383 | }; |
| 384 | } |
| 385 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 386 | // Improve compile time by compiling this loop only once per type it is used |
| 387 | // with. |
| 388 | // |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 389 | // Not public API. |
| 390 | #[doc(hidden)] |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 391 | pub fn many0<'a, T>(mut input: Cursor, f: fn(Cursor) -> PResult<T>) -> PResult<Vec<T>> { |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 392 | let mut res = Vec::new(); |
| 393 | |
| 394 | loop { |
| 395 | if input.is_empty() { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 396 | return Ok((input, res)); |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | match f(input) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 400 | Err(_) => { |
| 401 | return Ok((input, res)); |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 402 | } |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 403 | Ok((i, o)) => { |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 404 | // loop trip must always consume (otherwise infinite loops) |
| 405 | if i.len() == input.len() { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 406 | return parse_error(); |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | res.push(o); |
| 410 | input = i; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 416 | /// Parse a value without consuming it from the input data. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 417 | /// |
| 418 | /// - **Syntax:** `peek!(THING)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 419 | /// - **Output:** `THING` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 420 | /// |
| 421 | /// ```rust |
| 422 | /// extern crate syn; |
| 423 | /// #[macro_use] extern crate synom; |
| 424 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 425 | /// use syn::{Expr, Ident}; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 426 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 427 | /// // Parse an expression that begins with an identifier. |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 428 | /// named!(ident_expr -> (Ident, Expr), |
| 429 | /// tuple!(peek!(syn!(Ident)), syn!(Expr)) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 430 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 431 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 432 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 433 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 434 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 435 | macro_rules! peek { |
| 436 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 437 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 438 | ::std::result::Result::Ok((_, o)) => ::std::result::Result::Ok(($i, o)), |
| 439 | ::std::result::Result::Err(err) => ::std::result::Result::Err(err), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 440 | } |
| 441 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 442 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 443 | ($i:expr, $f:expr) => { |
| 444 | peek!($i, call!($f)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 445 | }; |
| 446 | } |
| 447 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 448 | /// Pattern-match the result of a parser to select which other parser to run. |
| 449 | /// |
| 450 | /// - **Syntax:** `switch!(TARGET, PAT1 => THEN1 | PAT2 => THEN2 | ...)` |
| 451 | /// - **Output:** `T`, the return type of `THEN1` and `THEN2` and ... |
| 452 | /// |
| 453 | /// ```rust |
| 454 | /// extern crate syn; |
| 455 | /// #[macro_use] extern crate synom; |
| 456 | /// |
| 457 | /// use syn::{Ident, Ty}; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 458 | /// use synom::tokens::*; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 459 | /// |
| 460 | /// #[derive(Debug)] |
| 461 | /// enum UnitType { |
| 462 | /// Struct { |
| 463 | /// name: Ident, |
| 464 | /// }, |
| 465 | /// Enum { |
| 466 | /// name: Ident, |
| 467 | /// variant: Ident, |
| 468 | /// }, |
| 469 | /// } |
| 470 | /// |
| 471 | /// // Parse a unit struct or enum: either `struct S;` or `enum E { V }`. |
| 472 | /// named!(unit_type -> UnitType, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 473 | /// which: alt!( |
| 474 | /// syn!(Struct) => { |_| 0 } |
| 475 | /// | |
| 476 | /// syn!(Enum) => { |_| 1 } |
| 477 | /// ) >> |
| 478 | /// id: syn!(Ident) >> |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 479 | /// item: switch!(value!(which), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 480 | /// 0 => map!( |
| 481 | /// syn!(Semi), |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 482 | /// move |_| UnitType::Struct { |
| 483 | /// name: id, |
| 484 | /// } |
| 485 | /// ) |
| 486 | /// | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 487 | /// 1 => map!( |
| 488 | /// braces!(syn!(Ident)), |
| 489 | /// move |(variant, _)| UnitType::Enum { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 490 | /// name: id, |
| 491 | /// variant: variant, |
| 492 | /// } |
| 493 | /// ) |
| 494 | /// ) >> |
| 495 | /// (item) |
| 496 | /// )); |
| 497 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 498 | /// # fn main() {} |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 499 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 500 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 501 | macro_rules! switch { |
| 502 | ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => { |
| 503 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 504 | ::std::result::Result::Err(err) => ::std::result::Result::Err(err), |
| 505 | ::std::result::Result::Ok((i, o)) => match o { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 506 | $( |
| 507 | $p => $subrule!(i, $($args2)*), |
| 508 | )* |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 509 | _ => $crate::parse_error(), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | }; |
| 513 | } |
| 514 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 515 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 516 | /// Produce the given value without parsing anything. Useful as an argument to |
| 517 | /// `switch!`. |
| 518 | /// |
| 519 | /// - **Syntax:** `value!(VALUE)` |
| 520 | /// - **Output:** `VALUE` |
| 521 | /// |
| 522 | /// ```rust |
| 523 | /// extern crate syn; |
| 524 | /// #[macro_use] extern crate synom; |
| 525 | /// |
| 526 | /// use syn::{Ident, Ty}; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 527 | /// use synom::tokens::*; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 528 | /// |
| 529 | /// #[derive(Debug)] |
| 530 | /// enum UnitType { |
| 531 | /// Struct { |
| 532 | /// name: Ident, |
| 533 | /// }, |
| 534 | /// Enum { |
| 535 | /// name: Ident, |
| 536 | /// variant: Ident, |
| 537 | /// }, |
| 538 | /// } |
| 539 | /// |
| 540 | /// // Parse a unit struct or enum: either `struct S;` or `enum E { V }`. |
| 541 | /// named!(unit_type -> UnitType, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 542 | /// which: alt!( |
| 543 | /// syn!(Struct) => { |_| 0 } |
| 544 | /// | |
| 545 | /// syn!(Enum) => { |_| 1 } |
| 546 | /// ) >> |
| 547 | /// id: syn!(Ident) >> |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 548 | /// item: switch!(value!(which), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 549 | /// 0 => map!( |
| 550 | /// syn!(Semi), |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 551 | /// move |_| UnitType::Struct { |
| 552 | /// name: id, |
| 553 | /// } |
| 554 | /// ) |
| 555 | /// | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 556 | /// 1 => map!( |
| 557 | /// braces!(syn!(Ident)), |
| 558 | /// move |(variant, _)| UnitType::Enum { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 559 | /// name: id, |
| 560 | /// variant: variant, |
| 561 | /// } |
| 562 | /// ) |
| 563 | /// ) >> |
| 564 | /// (item) |
| 565 | /// )); |
| 566 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 567 | /// # fn main() {} |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 568 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 569 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 570 | macro_rules! value { |
| 571 | ($i:expr, $res:expr) => { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 572 | ::std::result::Result::Ok(($i, $res)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 573 | }; |
| 574 | } |
| 575 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 576 | /// Run a series of parsers and produce all of the results in a tuple. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 577 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 578 | /// - **Syntax:** `tuple!(A, B, C, ...)` |
| 579 | /// - **Output:** `(A, B, C, ...)` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 580 | /// |
| 581 | /// ```rust |
| 582 | /// extern crate syn; |
| 583 | /// #[macro_use] extern crate synom; |
| 584 | /// |
| 585 | /// use syn::Ty; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 586 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 587 | /// named!(two_types -> (Ty, Ty), tuple!(syn!(Ty), syn!(Ty))); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 588 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 589 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 590 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 591 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 592 | macro_rules! tuple { |
| 593 | ($i:expr, $($rest:tt)*) => { |
| 594 | tuple_parser!($i, (), $($rest)*) |
| 595 | }; |
| 596 | } |
| 597 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 598 | /// Internal parser, do not use directly. |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 599 | #[doc(hidden)] |
| 600 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 601 | macro_rules! tuple_parser { |
| 602 | ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 603 | tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 604 | }; |
| 605 | |
| 606 | ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => { |
| 607 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 608 | ::std::result::Result::Err(err) => |
| 609 | ::std::result::Result::Err(err), |
| 610 | ::std::result::Result::Ok((i, o)) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 611 | tuple_parser!(i, (o), $($rest)*), |
| 612 | } |
| 613 | }; |
| 614 | |
| 615 | ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => { |
| 616 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 617 | ::std::result::Result::Err(err) => |
| 618 | ::std::result::Result::Err(err), |
| 619 | ::std::result::Result::Ok((i, o)) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 620 | tuple_parser!(i, ($($parsed)* , o), $($rest)*), |
| 621 | } |
| 622 | }; |
| 623 | |
| 624 | ($i:expr, ($($parsed:tt),*), $e:ident) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 625 | tuple_parser!($i, ($($parsed),*), call!($e)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 626 | }; |
| 627 | |
| 628 | ($i:expr, (), $submac:ident!( $($args:tt)* )) => { |
| 629 | $submac!($i, $($args)*) |
| 630 | }; |
| 631 | |
| 632 | ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => { |
| 633 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 634 | ::std::result::Result::Err(err) => |
| 635 | ::std::result::Result::Err(err), |
| 636 | ::std::result::Result::Ok((i, o)) => |
| 637 | ::std::result::Result::Ok((i, ($($parsed),*, o))), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 638 | } |
| 639 | }; |
| 640 | |
| 641 | ($i:expr, ($($parsed:expr),*)) => { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 642 | ::std::result::Result::Ok(($i, ($($parsed),*))) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 643 | }; |
| 644 | } |
| 645 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 646 | /// Run a series of parsers, returning the result of the first one which |
| 647 | /// succeeds. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 648 | /// |
| 649 | /// Optionally allows for the result to be transformed. |
| 650 | /// |
| 651 | /// - **Syntax:** `alt!(THING1 | THING2 => { FUNC } | ...)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 652 | /// - **Output:** `T`, the return type of `THING1` and `FUNC(THING2)` and ... |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 653 | /// |
| 654 | /// ```rust |
| 655 | /// extern crate syn; |
| 656 | /// #[macro_use] extern crate synom; |
| 657 | /// |
| 658 | /// use syn::Ident; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 659 | /// use synom::tokens::Bang; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 660 | /// |
| 661 | /// named!(ident_or_bang -> Ident, |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 662 | /// alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 663 | /// syn!(Ident) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 664 | /// | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 665 | /// syn!(Bang) => { |_| "BANG".into() } |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 666 | /// ) |
| 667 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 668 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 669 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 670 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 671 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 672 | macro_rules! alt { |
| 673 | ($i:expr, $e:ident | $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 674 | alt!($i, call!($e) | $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 675 | }; |
| 676 | |
| 677 | ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => { |
| 678 | match $subrule!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 679 | res @ ::std::result::Result::Ok(_) => res, |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 680 | _ => alt!($i, $($rest)*) |
| 681 | } |
| 682 | }; |
| 683 | |
| 684 | ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => { |
| 685 | match $subrule!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 686 | ::std::result::Result::Ok((i, o)) => |
| 687 | ::std::result::Result::Ok((i, $gen(o))), |
| 688 | ::std::result::Result::Err(_) => alt!($i, $($rest)*), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 689 | } |
| 690 | }; |
| 691 | |
| 692 | ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 693 | alt!($i, call!($e) => { $gen } | $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 694 | }; |
| 695 | |
| 696 | ($i:expr, $e:ident => { $gen:expr }) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 697 | alt!($i, call!($e) => { $gen }) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 698 | }; |
| 699 | |
| 700 | ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => { |
| 701 | match $subrule!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 702 | ::std::result::Result::Ok((i, o)) => |
| 703 | ::std::result::Result::Ok((i, $gen(o))), |
| 704 | ::std::result::Result::Err(err) => |
| 705 | ::std::result::Result::Err(err), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 706 | } |
| 707 | }; |
| 708 | |
| 709 | ($i:expr, $e:ident) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 710 | alt!($i, call!($e)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 711 | }; |
| 712 | |
| 713 | ($i:expr, $subrule:ident!( $($args:tt)*)) => { |
David Tolnay | 5377b17 | 2016-10-25 01:13:12 -0700 | [diff] [blame] | 714 | $subrule!($i, $($args)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 715 | }; |
| 716 | } |
| 717 | |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 718 | /// Run a series of parsers, one after another, optionally assigning the results |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 719 | /// a name. Fail if any of the parsers fails. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 720 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 721 | /// Produces the result of evaluating the final expression in parentheses with |
| 722 | /// all of the previously named results bound. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 723 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 724 | /// - **Syntax:** `do_parse!(name: THING1 >> THING2 >> (RESULT))` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 725 | /// - **Output:** `RESULT` |
| 726 | /// |
| 727 | /// ```rust |
| 728 | /// extern crate syn; |
| 729 | /// #[macro_use] extern crate synom; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 730 | /// extern crate proc_macro2; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 731 | /// |
| 732 | /// use syn::{Ident, TokenTree}; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 733 | /// use synom::tokens::{Bang, Paren}; |
| 734 | /// use proc_macro2::TokenStream; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 735 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 736 | /// // Parse a macro invocation like `stringify!($args)`. |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 737 | /// named!(simple_mac -> (Ident, (TokenStream, Paren)), do_parse!( |
| 738 | /// name: syn!(Ident) >> |
| 739 | /// syn!(Bang) >> |
| 740 | /// body: parens!(syn!(TokenStream)) >> |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 741 | /// (name, body) |
| 742 | /// )); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 743 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 744 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 745 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 746 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 747 | macro_rules! do_parse { |
| 748 | ($i:expr, ( $($rest:expr),* )) => { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 749 | ::std::result::Result::Ok(($i, ( $($rest),* ))) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 750 | }; |
| 751 | |
| 752 | ($i:expr, $e:ident >> $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 753 | do_parse!($i, call!($e) >> $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 754 | }; |
| 755 | |
| 756 | ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 757 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 758 | ::std::result::Result::Err(err) => |
| 759 | ::std::result::Result::Err(err), |
| 760 | ::std::result::Result::Ok((i, _)) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 761 | do_parse!(i, $($rest)*), |
| 762 | } |
| 763 | }; |
| 764 | |
| 765 | ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 766 | do_parse!($i, $field: call!($e) >> $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 767 | }; |
| 768 | |
| 769 | ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 770 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 771 | ::std::result::Result::Err(err) => |
| 772 | ::std::result::Result::Err(err), |
| 773 | ::std::result::Result::Ok((i, o)) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 774 | let $field = o; |
| 775 | do_parse!(i, $($rest)*) |
| 776 | }, |
| 777 | } |
| 778 | }; |
| 779 | |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 780 | ($i:expr, mut $field:ident : $e:ident >> $($rest:tt)*) => { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 781 | do_parse!($i, mut $field: call!($e) >> $($rest)*) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 782 | }; |
| 783 | |
| 784 | ($i:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 785 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 786 | ::std::result::Result::Err(err) => |
| 787 | ::std::result::Result::Err(err), |
| 788 | ::std::result::Result::Ok((i, o)) => { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 789 | let mut $field = o; |
| 790 | do_parse!(i, $($rest)*) |
| 791 | }, |
| 792 | } |
| 793 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 794 | } |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 795 | |
| 796 | #[macro_export] |
| 797 | macro_rules! input_end { |
| 798 | ($i:expr,) => { |
| 799 | $crate::input_end($i) |
| 800 | }; |
| 801 | } |
| 802 | |
| 803 | // Not a public API |
| 804 | #[doc(hidden)] |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 805 | pub fn input_end(input: Cursor) -> PResult<'static, &'static str> { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 806 | if input.is_empty() { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 807 | Ok((&[], "")) |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 808 | } else { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 809 | parse_error() |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 810 | } |
| 811 | } |