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