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