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