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