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