David Tolnay | 30a3600 | 2017-02-08 14:24:12 -0800 | [diff] [blame] | 1 | //! Adapted from [`nom`](https://github.com/Geal/nom) by removing the |
| 2 | //! `IResult::Incomplete` variant which: |
| 3 | //! |
| 4 | //! - we don't need, |
| 5 | //! - is an unintuitive footgun when working with non-streaming use cases, and |
| 6 | //! - more than doubles compilation time. |
| 7 | //! |
| 8 | //! ## Whitespace handling strategy |
| 9 | //! |
| 10 | //! As (sy)nom is a parser combinator library, the parsers provided here and |
| 11 | //! that you implement yourself are all made up of successively more primitive |
| 12 | //! parsers, eventually culminating in a small number of fundamental parsers |
| 13 | //! that are implemented in Rust. Among these are `punct!` and `keyword!`. |
| 14 | //! |
| 15 | //! All synom fundamental parsers (those not combined out of other parsers) |
| 16 | //! should be written to skip over leading whitespace in their input. This way, |
| 17 | //! as long as every parser eventually boils down to some combination of |
| 18 | //! fundamental parsers, we get correct whitespace handling at all levels for |
| 19 | //! free. |
| 20 | //! |
| 21 | //! For our use case, this strategy is a huge improvement in usability, |
| 22 | //! correctness, and compile time over nom's `ws!` strategy. |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 23 | |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 24 | extern crate unicode_xid; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame^] | 25 | extern crate proc_macro2; |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 26 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 27 | #[cfg(feature = "printing")] |
| 28 | extern crate quote; |
| 29 | |
David Tolnay | 30a3600 | 2017-02-08 14:24:12 -0800 | [diff] [blame] | 30 | #[doc(hidden)] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame^] | 31 | pub use proc_macro2::TokenTree; |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 32 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 33 | #[cfg(feature = "parsing")] |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 34 | #[doc(hidden)] |
| 35 | pub mod helper; |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 36 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 37 | pub mod delimited; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame^] | 38 | pub mod tokens; |
| 39 | pub mod span; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 40 | |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 41 | /// The result of a parser. |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 42 | #[derive(Debug, PartialEq, Eq, Clone)] |
| 43 | pub enum IResult<I, O> { |
David Tolnay | f2222f0 | 2017-01-27 17:09:20 -0800 | [diff] [blame] | 44 | /// Parsing succeeded. The first field contains the rest of the unparsed |
| 45 | /// data and the second field contains the parse result. |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 46 | Done(I, O), |
David Tolnay | f2222f0 | 2017-01-27 17:09:20 -0800 | [diff] [blame] | 47 | /// Parsing failed. |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 48 | Error, |
| 49 | } |
| 50 | |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 51 | impl<'a, O> IResult<&'a [TokenTree], O> { |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 52 | /// Unwraps the result, asserting the the parse is complete. Panics with a |
| 53 | /// message based on the given string if the parse failed or is incomplete. |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 54 | /// |
| 55 | /// ```rust |
| 56 | /// extern crate syn; |
| 57 | /// #[macro_use] extern crate synom; |
| 58 | /// |
| 59 | /// use syn::Ty; |
| 60 | /// use syn::parse::ty; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 61 | /// use synom::delimited::Delimited; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 62 | /// |
| 63 | /// // One or more Rust types separated by commas. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 64 | /// named!(comma_separated_types -> Delimited<Ty, &str>, |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 65 | /// separated_nonempty_list!(punct!(","), ty) |
| 66 | /// ); |
| 67 | /// |
| 68 | /// fn main() { |
| 69 | /// let input = "&str, Map<K, V>, String"; |
| 70 | /// |
| 71 | /// let parsed = comma_separated_types(input).expect("comma-separated types"); |
| 72 | /// |
| 73 | /// assert_eq!(parsed.len(), 3); |
| 74 | /// println!("{:?}", parsed); |
| 75 | /// } |
| 76 | /// ``` |
David Tolnay | f2222f0 | 2017-01-27 17:09:20 -0800 | [diff] [blame] | 77 | pub fn expect(self, name: &str) -> O { |
| 78 | match self { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 79 | IResult::Done(rest, o) => { |
David Tolnay | f2222f0 | 2017-01-27 17:09:20 -0800 | [diff] [blame] | 80 | if rest.is_empty() { |
| 81 | o |
| 82 | } else { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 83 | panic!("unparsed tokens after {}: {:?}", name, /* rest */ ()) |
David Tolnay | f2222f0 | 2017-01-27 17:09:20 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | IResult::Error => panic!("failed to parse {}", name), |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame^] | 91 | pub trait Synom: Sized { |
| 92 | fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self>; |
| 93 | } |
| 94 | |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 95 | /// Define a function from a parser combination. |
| 96 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 97 | /// - **Syntax:** `named!(NAME -> TYPE, PARSER)` or `named!(pub NAME -> TYPE, PARSER)` |
| 98 | /// |
| 99 | /// ```rust |
| 100 | /// # extern crate syn; |
| 101 | /// # #[macro_use] extern crate synom; |
| 102 | /// # use syn::Ty; |
| 103 | /// # use syn::parse::ty; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 104 | /// # use synom::delimited::Delimited; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 105 | /// // One or more Rust types separated by commas. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 106 | /// named!(pub comma_separated_types -> Delimited<Ty, &str>, |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 107 | /// separated_nonempty_list!(punct!(","), ty) |
| 108 | /// ); |
| 109 | /// # fn main() {} |
| 110 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 111 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 112 | macro_rules! named { |
| 113 | ($name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 114 | fn $name(i: &[$crate::TokenTree]) -> $crate::IResult<&[$crate::TokenTree], $o> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 115 | $submac!(i, $($args)*) |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | (pub $name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 120 | pub fn $name(i: &[$crate::TokenTree]) -> $crate::IResult<&[$crate::TokenTree], $o> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 121 | $submac!(i, $($args)*) |
| 122 | } |
| 123 | }; |
| 124 | } |
| 125 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 126 | /// Invoke the given parser function with the passed in arguments. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 127 | /// |
| 128 | /// - **Syntax:** `call!(FUNCTION, ARGS...)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 129 | /// |
| 130 | /// where the signature of the function is `fn(&str, ARGS...) -> IResult<&str, T>` |
| 131 | /// - **Output:** `T`, the result of invoking the function `FUNCTION` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 132 | /// |
| 133 | /// ```rust |
| 134 | /// #[macro_use] extern crate synom; |
| 135 | /// |
| 136 | /// use synom::IResult; |
| 137 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 138 | /// // Parses any string up to but not including the given character, returning |
| 139 | /// // the content up to the given character. The given character is required to |
| 140 | /// // be present in the input string. |
| 141 | /// fn skip_until(input: &str, ch: char) -> IResult<&str, &str> { |
| 142 | /// if let Some(pos) = input.find(ch) { |
| 143 | /// IResult::Done(&input[pos..], &input[..pos]) |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 144 | /// } else { |
| 145 | /// IResult::Error |
| 146 | /// } |
| 147 | /// } |
| 148 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 149 | /// // Parses any string surrounded by tilde characters '~'. Returns the content |
| 150 | /// // between the tilde characters. |
| 151 | /// named!(surrounded_by_tilde -> &str, delimited!( |
| 152 | /// punct!("~"), |
| 153 | /// call!(skip_until, '~'), |
| 154 | /// punct!("~") |
| 155 | /// )); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 156 | /// |
| 157 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 158 | /// let input = "~ abc def ~"; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 159 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 160 | /// let inner = surrounded_by_tilde(input).expect("surrounded by tilde"); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 161 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 162 | /// println!("{:?}", inner); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 163 | /// } |
| 164 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 165 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 166 | macro_rules! call { |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 167 | ($i:expr, $fun:expr $(, $args:expr)*) => { |
| 168 | $fun($i $(, $args)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 169 | }; |
| 170 | } |
| 171 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 172 | /// Transform the result of a parser by applying a function or closure. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 173 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 174 | /// - **Syntax:** `map!(THING, FN)` |
| 175 | /// - **Output:** the return type of function FN applied to THING |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 176 | /// |
| 177 | /// ```rust |
| 178 | /// extern crate syn; |
| 179 | /// #[macro_use] extern crate synom; |
| 180 | /// |
| 181 | /// use syn::{Item, Ident}; |
| 182 | /// use syn::parse::item; |
| 183 | /// |
| 184 | /// fn get_item_ident(item: Item) -> Ident { |
| 185 | /// item.ident |
| 186 | /// } |
| 187 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 188 | /// // Parses an item and returns the name (identifier) of the item only. |
| 189 | /// named!(item_ident -> Ident, |
| 190 | /// map!(item, get_item_ident) |
| 191 | /// ); |
| 192 | /// |
| 193 | /// // Or equivalently: |
| 194 | /// named!(item_ident2 -> Ident, |
| 195 | /// map!(item, |i: Item| i.ident) |
| 196 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 197 | /// |
| 198 | /// fn main() { |
| 199 | /// let input = "fn foo() {}"; |
| 200 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 201 | /// let parsed = item_ident(input).expect("item"); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 202 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 203 | /// assert_eq!(parsed, "foo"); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 204 | /// } |
| 205 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 206 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 207 | macro_rules! map { |
| 208 | ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 209 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 210 | $crate::IResult::Error => $crate::IResult::Error, |
| 211 | $crate::IResult::Done(i, o) => { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 212 | $crate::IResult::Done(i, call!(o, $g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | }; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 216 | |
| 217 | ($i:expr, $f:expr, $g:expr) => { |
| 218 | map!($i, call!($f), $g) |
| 219 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 220 | } |
| 221 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 222 | /// Parses successfully if the given parser fails to parse. Does not consume any |
| 223 | /// of the input. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 224 | /// |
| 225 | /// - **Syntax:** `not!(THING)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 226 | /// - **Output:** `()` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 227 | /// |
| 228 | /// ```rust |
| 229 | /// extern crate syn; |
| 230 | /// #[macro_use] extern crate synom; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 231 | /// use synom::IResult; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 232 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 233 | /// // Parses a shebang line like `#!/bin/bash` and returns the part after `#!`. |
| 234 | /// // Note that a line starting with `#![` is an inner attribute, not a |
| 235 | /// // shebang. |
| 236 | /// named!(shebang -> &str, preceded!( |
| 237 | /// tuple!(tag!("#!"), not!(tag!("["))), |
| 238 | /// take_until!("\n") |
| 239 | /// )); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 240 | /// |
| 241 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 242 | /// let bin_bash = "#!/bin/bash\n"; |
| 243 | /// let parsed = shebang(bin_bash).expect("shebang"); |
| 244 | /// assert_eq!(parsed, "/bin/bash"); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 245 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 246 | /// let inner_attr = "#![feature(specialization)]\n"; |
| 247 | /// let err = shebang(inner_attr); |
| 248 | /// assert_eq!(err, IResult::Error); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 249 | /// } |
| 250 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 251 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 252 | macro_rules! not { |
| 253 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 254 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 255 | $crate::IResult::Done(_, _) => $crate::IResult::Error, |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 256 | $crate::IResult::Error => $crate::IResult::Done($i, ()), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 257 | } |
| 258 | }; |
| 259 | } |
| 260 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 261 | /// Conditionally execute the given parser. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 262 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 263 | /// If you are familiar with nom, this is nom's `cond_with_error` parser. |
| 264 | /// |
| 265 | /// - **Syntax:** `cond!(CONDITION, THING)` |
| 266 | /// - **Output:** `Some(THING)` if the condition is true, else `None` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 267 | /// |
| 268 | /// ```rust |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 269 | /// extern crate syn; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 270 | /// #[macro_use] extern crate synom; |
| 271 | /// |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 272 | /// named!(boolean -> bool, alt!( |
| 273 | /// keyword!("true") => { |_| true } |
| 274 | /// | |
| 275 | /// keyword!("false") => { |_| false } |
| 276 | /// )); |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 277 | /// |
| 278 | /// // Parses a tuple of booleans like `(true, false, false)`, possibly with a |
| 279 | /// // dotdot indicating omitted values like `(true, true, .., true)`. Returns |
| 280 | /// // separate vectors for the booleans before and after the dotdot. The second |
| 281 | /// // vector is None if there was no dotdot. |
| 282 | /// named!(bools_with_dotdot -> (Vec<bool>, Option<Vec<bool>>), do_parse!( |
| 283 | /// punct!("(") >> |
| 284 | /// before: separated_list!(punct!(","), boolean) >> |
| 285 | /// after: option!(do_parse!( |
| 286 | /// // Only allow comma if there are elements before dotdot, i.e. cannot |
| 287 | /// // be `(, .., true)`. |
| 288 | /// cond!(!before.is_empty(), punct!(",")) >> |
| 289 | /// punct!("..") >> |
| 290 | /// after: many0!(preceded!(punct!(","), boolean)) >> |
| 291 | /// // Only allow trailing comma if there are elements after dotdot, |
| 292 | /// // i.e. cannot be `(true, .., )`. |
| 293 | /// cond!(!after.is_empty(), option!(punct!(","))) >> |
| 294 | /// (after) |
| 295 | /// )) >> |
| 296 | /// // Allow trailing comma if there is no dotdot but there are elements. |
| 297 | /// cond!(!before.is_empty() && after.is_none(), option!(punct!(","))) >> |
| 298 | /// punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 299 | /// (before.into_vec(), after) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 300 | /// )); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 301 | /// |
| 302 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 303 | /// let input = "(true, false, false)"; |
| 304 | /// let parsed = bools_with_dotdot(input).expect("bools with dotdot"); |
| 305 | /// assert_eq!(parsed, (vec![true, false, false], None)); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 306 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 307 | /// let input = "(true, true, .., true)"; |
| 308 | /// let parsed = bools_with_dotdot(input).expect("bools with dotdot"); |
| 309 | /// assert_eq!(parsed, (vec![true, true], Some(vec![true]))); |
| 310 | /// |
| 311 | /// let input = "(.., true)"; |
| 312 | /// let parsed = bools_with_dotdot(input).expect("bools with dotdot"); |
| 313 | /// assert_eq!(parsed, (vec![], Some(vec![true]))); |
| 314 | /// |
| 315 | /// let input = "(true, true, ..)"; |
| 316 | /// let parsed = bools_with_dotdot(input).expect("bools with dotdot"); |
| 317 | /// assert_eq!(parsed, (vec![true, true], Some(vec![]))); |
| 318 | /// |
| 319 | /// let input = "(..)"; |
| 320 | /// let parsed = bools_with_dotdot(input).expect("bools with dotdot"); |
| 321 | /// assert_eq!(parsed, (vec![], Some(vec![]))); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 322 | /// } |
| 323 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 324 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 325 | macro_rules! cond { |
| 326 | ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => { |
| 327 | if $cond { |
| 328 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 329 | $crate::IResult::Done(i, o) => $crate::IResult::Done(i, ::std::option::Option::Some(o)), |
| 330 | $crate::IResult::Error => $crate::IResult::Error, |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 331 | } |
| 332 | } else { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 333 | $crate::IResult::Done($i, ::std::option::Option::None) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 334 | } |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | ($i:expr, $cond:expr, $f:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 338 | cond!($i, $cond, call!($f)) |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 339 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 340 | } |
| 341 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 342 | /// Fail to parse if condition is false, otherwise parse the given parser. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 343 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 344 | /// This is typically used inside of `option!` or `alt!`. |
| 345 | /// |
| 346 | /// - **Syntax:** `cond_reduce!(CONDITION, THING)` |
| 347 | /// - **Output:** `THING` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 348 | /// |
| 349 | /// ```rust |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 350 | /// extern crate syn; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 351 | /// #[macro_use] extern crate synom; |
| 352 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 353 | /// #[derive(Debug, PartialEq)] |
| 354 | /// struct VariadicBools { |
| 355 | /// data: Vec<bool>, |
| 356 | /// variadic: bool, |
| 357 | /// } |
| 358 | /// |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 359 | /// named!(boolean -> bool, alt!( |
| 360 | /// keyword!("true") => { |_| true } |
| 361 | /// | |
| 362 | /// keyword!("false") => { |_| false } |
| 363 | /// )); |
| 364 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 365 | /// // Parse one or more comma-separated booleans, possibly ending in "..." to |
| 366 | /// // indicate there may be more. |
| 367 | /// named!(variadic_bools -> VariadicBools, do_parse!( |
| 368 | /// data: separated_nonempty_list!(punct!(","), boolean) >> |
| 369 | /// trailing_comma: option!(punct!(",")) >> |
| 370 | /// // Only allow "..." if there is a comma after the last boolean. Using |
| 371 | /// // `cond_reduce!` is more convenient here than using `cond!`. The |
| 372 | /// // alternatives are: |
| 373 | /// // |
| 374 | /// // - `cond!(c, option!(p))` or `option!(cond!(c, p))` |
| 375 | /// // Gives `Some(Some("..."))` for variadic and `Some(None)` or `None` |
| 376 | /// // which both mean not variadic. |
| 377 | /// // - `cond_reduce!(c, option!(p))` |
| 378 | /// // Incorrect; would fail to parse if there is no trailing comma. |
| 379 | /// // - `option!(cond_reduce!(c, p))` |
| 380 | /// // Gives `Some("...")` for variadic and `None` otherwise. Perfect! |
| 381 | /// variadic: option!(cond_reduce!(trailing_comma.is_some(), punct!("..."))) >> |
| 382 | /// (VariadicBools { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 383 | /// data: data.into_vec(), |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 384 | /// variadic: variadic.is_some(), |
| 385 | /// }) |
| 386 | /// )); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 387 | /// |
| 388 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 389 | /// let input = "true, true"; |
| 390 | /// let parsed = variadic_bools(input).expect("variadic bools"); |
| 391 | /// assert_eq!(parsed, VariadicBools { |
| 392 | /// data: vec![true, true], |
| 393 | /// variadic: false, |
| 394 | /// }); |
| 395 | /// |
| 396 | /// let input = "true, ..."; |
| 397 | /// let parsed = variadic_bools(input).expect("variadic bools"); |
| 398 | /// assert_eq!(parsed, VariadicBools { |
| 399 | /// data: vec![true], |
| 400 | /// variadic: true, |
| 401 | /// }); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 402 | /// } |
| 403 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 404 | #[macro_export] |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 405 | macro_rules! cond_reduce { |
| 406 | ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => { |
| 407 | if $cond { |
| 408 | $submac!($i, $($args)*) |
| 409 | } else { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 410 | $crate::IResult::Error |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 411 | } |
| 412 | }; |
| 413 | |
| 414 | ($i:expr, $cond:expr, $f:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 415 | cond_reduce!($i, $cond, call!($f)) |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 416 | }; |
| 417 | } |
| 418 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 419 | /// Parse two things, returning the value of the second. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 420 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 421 | /// - **Syntax:** `preceded!(BEFORE, THING)` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 422 | /// - **Output:** `THING` |
| 423 | /// |
| 424 | /// ```rust |
| 425 | /// extern crate syn; |
| 426 | /// #[macro_use] extern crate synom; |
| 427 | /// |
| 428 | /// use syn::Expr; |
| 429 | /// use syn::parse::expr; |
| 430 | /// |
| 431 | /// // An expression preceded by ##. |
| 432 | /// named!(pound_pound_expr -> Expr, |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 433 | /// preceded!(punct!("##"), expr) |
| 434 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 435 | /// |
| 436 | /// fn main() { |
| 437 | /// let input = "## 1 + 1"; |
| 438 | /// |
| 439 | /// let parsed = pound_pound_expr(input).expect("pound pound expr"); |
| 440 | /// |
| 441 | /// println!("{:?}", parsed); |
| 442 | /// } |
| 443 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 444 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 445 | macro_rules! preceded { |
| 446 | ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => { |
| 447 | match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 448 | $crate::IResult::Done(remaining, (_, o)) => $crate::IResult::Done(remaining, o), |
| 449 | $crate::IResult::Error => $crate::IResult::Error, |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 450 | } |
| 451 | }; |
| 452 | |
| 453 | ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 454 | preceded!($i, $submac!($($args)*), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 455 | }; |
| 456 | |
| 457 | ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 458 | preceded!($i, call!($f), $submac!($($args)*)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 459 | }; |
| 460 | |
| 461 | ($i:expr, $f:expr, $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 462 | preceded!($i, call!($f), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 463 | }; |
| 464 | } |
| 465 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 466 | /// Parse two things, returning the value of the first. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 467 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 468 | /// - **Syntax:** `terminated!(THING, AFTER)` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 469 | /// - **Output:** `THING` |
| 470 | /// |
| 471 | /// ```rust |
| 472 | /// extern crate syn; |
| 473 | /// #[macro_use] extern crate synom; |
| 474 | /// |
| 475 | /// use syn::Expr; |
| 476 | /// use syn::parse::expr; |
| 477 | /// |
| 478 | /// // An expression terminated by ##. |
| 479 | /// named!(expr_pound_pound -> Expr, |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 480 | /// terminated!(expr, punct!("##")) |
| 481 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 482 | /// |
| 483 | /// fn main() { |
| 484 | /// let input = "1 + 1 ##"; |
| 485 | /// |
| 486 | /// let parsed = expr_pound_pound(input).expect("expr pound pound"); |
| 487 | /// |
| 488 | /// println!("{:?}", parsed); |
| 489 | /// } |
| 490 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 491 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 492 | macro_rules! terminated { |
| 493 | ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => { |
| 494 | match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 495 | $crate::IResult::Done(remaining, (o, _)) => $crate::IResult::Done(remaining, o), |
| 496 | $crate::IResult::Error => $crate::IResult::Error, |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 497 | } |
| 498 | }; |
| 499 | |
| 500 | ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 501 | terminated!($i, $submac!($($args)*), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 502 | }; |
| 503 | |
| 504 | ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 505 | terminated!($i, call!($f), $submac!($($args)*)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 506 | }; |
| 507 | |
| 508 | ($i:expr, $f:expr, $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 509 | terminated!($i, call!($f), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 510 | }; |
| 511 | } |
| 512 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 513 | /// Parse zero or more values using the given parser. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 514 | /// |
| 515 | /// - **Syntax:** `many0!(THING)` |
| 516 | /// - **Output:** `Vec<THING>` |
| 517 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 518 | /// You may also be looking for: |
| 519 | /// |
| 520 | /// - `separated_list!` - zero or more values with separator |
| 521 | /// - `separated_nonempty_list!` - one or more values |
| 522 | /// - `terminated_list!` - zero or more, allows trailing separator |
| 523 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 524 | /// ```rust |
| 525 | /// extern crate syn; |
| 526 | /// #[macro_use] extern crate synom; |
| 527 | /// |
| 528 | /// use syn::Item; |
| 529 | /// use syn::parse::item; |
| 530 | /// |
| 531 | /// named!(items -> Vec<Item>, many0!(item)); |
| 532 | /// |
| 533 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 534 | /// let input = " |
| 535 | /// fn a() {} |
| 536 | /// fn b() {} |
| 537 | /// "; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 538 | /// |
| 539 | /// let parsed = items(input).expect("items"); |
| 540 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 541 | /// assert_eq!(parsed.len(), 2); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 542 | /// println!("{:?}", parsed); |
| 543 | /// } |
| 544 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 545 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 546 | macro_rules! many0 { |
| 547 | ($i:expr, $submac:ident!( $($args:tt)* )) => {{ |
| 548 | let ret; |
| 549 | let mut res = ::std::vec::Vec::new(); |
| 550 | let mut input = $i; |
| 551 | |
| 552 | loop { |
| 553 | if input.is_empty() { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 554 | ret = $crate::IResult::Done(input, res); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 555 | break; |
| 556 | } |
| 557 | |
| 558 | match $submac!(input, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 559 | $crate::IResult::Error => { |
| 560 | ret = $crate::IResult::Done(input, res); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 561 | break; |
| 562 | } |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 563 | $crate::IResult::Done(i, o) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 564 | // loop trip must always consume (otherwise infinite loops) |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 565 | if i.len() == input.len() { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 566 | ret = $crate::IResult::Error; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 567 | break; |
| 568 | } |
| 569 | |
| 570 | res.push(o); |
| 571 | input = i; |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | ret |
| 577 | }}; |
| 578 | |
| 579 | ($i:expr, $f:expr) => { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 580 | $crate::many0($i, $f) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 581 | }; |
| 582 | } |
| 583 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 584 | // Improve compile time by compiling this loop only once per type it is used |
| 585 | // with. |
| 586 | // |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 587 | // Not public API. |
| 588 | #[doc(hidden)] |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 589 | pub fn many0<'a, T>(mut input: &'a [TokenTree], |
| 590 | f: fn(&'a [TokenTree]) -> IResult<&'a [TokenTree], T>) |
| 591 | -> IResult<&'a [TokenTree], Vec<T>> { |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 592 | let mut res = Vec::new(); |
| 593 | |
| 594 | loop { |
| 595 | if input.is_empty() { |
| 596 | return IResult::Done(input, res); |
| 597 | } |
| 598 | |
| 599 | match f(input) { |
| 600 | IResult::Error => { |
| 601 | return IResult::Done(input, res); |
| 602 | } |
| 603 | IResult::Done(i, o) => { |
| 604 | // loop trip must always consume (otherwise infinite loops) |
| 605 | if i.len() == input.len() { |
| 606 | return IResult::Error; |
| 607 | } |
| 608 | |
| 609 | res.push(o); |
| 610 | input = i; |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 616 | /// Parse a value without consuming it from the input data. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 617 | /// |
| 618 | /// - **Syntax:** `peek!(THING)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 619 | /// - **Output:** `THING` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 620 | /// |
| 621 | /// ```rust |
| 622 | /// extern crate syn; |
| 623 | /// #[macro_use] extern crate synom; |
| 624 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 625 | /// use syn::Expr; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 626 | /// use syn::parse::{ident, expr}; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 627 | /// use synom::IResult; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 628 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 629 | /// // Parse an expression that begins with an identifier. |
| 630 | /// named!(ident_expr -> Expr, |
| 631 | /// preceded!(peek!(ident), expr) |
| 632 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 633 | /// |
| 634 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 635 | /// // begins with an identifier |
| 636 | /// let input = "banana + 1"; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 637 | /// let parsed = ident_expr(input).expect("ident"); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 638 | /// println!("{:?}", parsed); |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 639 | /// |
| 640 | /// // does not begin with an identifier |
| 641 | /// let input = "1 + banana"; |
| 642 | /// let err = ident_expr(input); |
| 643 | /// assert_eq!(err, IResult::Error); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 644 | /// } |
| 645 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 646 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 647 | macro_rules! peek { |
| 648 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 649 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 650 | $crate::IResult::Done(_, o) => $crate::IResult::Done($i, o), |
| 651 | $crate::IResult::Error => $crate::IResult::Error, |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 652 | } |
| 653 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 654 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 655 | ($i:expr, $f:expr) => { |
| 656 | peek!($i, call!($f)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 657 | }; |
| 658 | } |
| 659 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 660 | /// Parse the part of the input up to but not including the given string. Fail |
| 661 | /// to parse if the given string is not present in the input. |
| 662 | /// |
| 663 | /// - **Syntax:** `take_until!("...")` |
| 664 | /// - **Output:** `&str` |
| 665 | /// |
| 666 | /// ```rust |
| 667 | /// extern crate syn; |
| 668 | /// #[macro_use] extern crate synom; |
| 669 | /// use synom::IResult; |
| 670 | /// |
| 671 | /// // Parse a single line doc comment: /// ... |
| 672 | /// named!(single_line_doc -> &str, |
| 673 | /// preceded!(punct!("///"), take_until!("\n")) |
| 674 | /// ); |
| 675 | /// |
| 676 | /// fn main() { |
| 677 | /// let comment = "/// comment\n"; |
| 678 | /// let parsed = single_line_doc(comment).expect("single line doc comment"); |
| 679 | /// assert_eq!(parsed, " comment"); |
| 680 | /// } |
| 681 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 682 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 683 | macro_rules! take_until { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 684 | ($i:expr, $substr:expr) => {{ |
| 685 | if $substr.len() > $i.len() { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 686 | $crate::IResult::Error |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 687 | } else { |
David Tolnay | f2c452b | 2016-12-21 22:45:28 -0500 | [diff] [blame] | 688 | let substr_vec: Vec<char> = $substr.chars().collect(); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 689 | let mut window: Vec<char> = vec![]; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 690 | let mut offset = $i.len(); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 691 | let mut parsed = false; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 692 | for (o, c) in $i.char_indices() { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 693 | window.push(c); |
| 694 | if window.len() > substr_vec.len() { |
| 695 | window.remove(0); |
| 696 | } |
| 697 | if window == substr_vec { |
| 698 | parsed = true; |
| 699 | window.pop(); |
| 700 | let window_len: usize = window.iter() |
| 701 | .map(|x| x.len_utf8()) |
| 702 | .fold(0, |x, y| x + y); |
| 703 | offset = o - window_len; |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | if parsed { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 708 | $crate::IResult::Done(&$i[offset..], &$i[..offset]) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 709 | } else { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 710 | $crate::IResult::Error |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | }}; |
| 714 | } |
| 715 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 716 | /// Parse the given string from exactly the current position in the input. You |
| 717 | /// almost always want `punct!` or `keyword!` instead of this. |
| 718 | /// |
| 719 | /// The `tag!` parser is equivalent to `punct!` but does not ignore leading |
| 720 | /// whitespace. Both `punct!` and `keyword!` skip over leading whitespace. See |
| 721 | /// an explanation of synom's whitespace handling strategy in the top-level |
| 722 | /// crate documentation. |
| 723 | /// |
| 724 | /// - **Syntax:** `tag!("...")` |
| 725 | /// - **Output:** `"..."` |
| 726 | /// |
| 727 | /// ```rust |
| 728 | /// extern crate syn; |
| 729 | /// #[macro_use] extern crate synom; |
| 730 | /// |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 731 | /// use syn::Lit; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 732 | /// use syn::parse::string; |
| 733 | /// use synom::IResult; |
| 734 | /// |
| 735 | /// // Parse a proposed syntax for an owned string literal: "abc"s |
| 736 | /// named!(owned_string -> String, |
| 737 | /// map!( |
| 738 | /// terminated!(string, tag!("s")), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 739 | /// |lit: Lit| lit.to_string() |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 740 | /// ) |
| 741 | /// ); |
| 742 | /// |
| 743 | /// fn main() { |
| 744 | /// let input = r#" "abc"s "#; |
| 745 | /// let parsed = owned_string(input).expect("owned string literal"); |
| 746 | /// println!("{:?}", parsed); |
| 747 | /// |
| 748 | /// let input = r#" "abc" s "#; |
| 749 | /// let err = owned_string(input); |
| 750 | /// assert_eq!(err, IResult::Error); |
| 751 | /// } |
| 752 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 753 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 754 | macro_rules! tag { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 755 | ($i:expr, $tag:expr) => { |
David Tolnay | 0b154ea | 2016-10-01 15:42:50 -0700 | [diff] [blame] | 756 | if $i.starts_with($tag) { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 757 | $crate::IResult::Done(&$i[$tag.len()..], &$i[..$tag.len()]) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 758 | } else { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 759 | $crate::IResult::Error |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 760 | } |
| 761 | }; |
| 762 | } |
| 763 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 764 | /// Pattern-match the result of a parser to select which other parser to run. |
| 765 | /// |
| 766 | /// - **Syntax:** `switch!(TARGET, PAT1 => THEN1 | PAT2 => THEN2 | ...)` |
| 767 | /// - **Output:** `T`, the return type of `THEN1` and `THEN2` and ... |
| 768 | /// |
| 769 | /// ```rust |
| 770 | /// extern crate syn; |
| 771 | /// #[macro_use] extern crate synom; |
| 772 | /// |
| 773 | /// use syn::{Ident, Ty}; |
| 774 | /// use syn::parse::{ident, ty}; |
| 775 | /// |
| 776 | /// #[derive(Debug)] |
| 777 | /// enum UnitType { |
| 778 | /// Struct { |
| 779 | /// name: Ident, |
| 780 | /// }, |
| 781 | /// Enum { |
| 782 | /// name: Ident, |
| 783 | /// variant: Ident, |
| 784 | /// }, |
| 785 | /// } |
| 786 | /// |
| 787 | /// // Parse a unit struct or enum: either `struct S;` or `enum E { V }`. |
| 788 | /// named!(unit_type -> UnitType, do_parse!( |
| 789 | /// which: alt!(keyword!("struct") | keyword!("enum")) >> |
| 790 | /// id: ident >> |
| 791 | /// item: switch!(value!(which), |
| 792 | /// "struct" => map!( |
| 793 | /// punct!(";"), |
| 794 | /// move |_| UnitType::Struct { |
| 795 | /// name: id, |
| 796 | /// } |
| 797 | /// ) |
| 798 | /// | |
| 799 | /// "enum" => map!( |
| 800 | /// delimited!(punct!("{"), ident, punct!("}")), |
| 801 | /// move |variant| UnitType::Enum { |
| 802 | /// name: id, |
| 803 | /// variant: variant, |
| 804 | /// } |
| 805 | /// ) |
| 806 | /// ) >> |
| 807 | /// (item) |
| 808 | /// )); |
| 809 | /// |
| 810 | /// fn main() { |
| 811 | /// let input = "struct S;"; |
| 812 | /// let parsed = unit_type(input).expect("unit struct or enum"); |
| 813 | /// println!("{:?}", parsed); |
| 814 | /// |
| 815 | /// let input = "enum E { V }"; |
| 816 | /// let parsed = unit_type(input).expect("unit struct or enum"); |
| 817 | /// println!("{:?}", parsed); |
| 818 | /// } |
| 819 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 820 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 821 | macro_rules! switch { |
| 822 | ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => { |
| 823 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 824 | $crate::IResult::Error => $crate::IResult::Error, |
| 825 | $crate::IResult::Done(i, o) => match o { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 826 | $( |
| 827 | $p => $subrule!(i, $($args2)*), |
| 828 | )* |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 829 | _ => $crate::IResult::Error, |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | }; |
| 833 | } |
| 834 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 835 | /// Produce the given value without parsing anything. Useful as an argument to |
| 836 | /// `switch!`. |
| 837 | /// |
| 838 | /// - **Syntax:** `value!(VALUE)` |
| 839 | /// - **Output:** `VALUE` |
| 840 | /// |
| 841 | /// ```rust |
| 842 | /// extern crate syn; |
| 843 | /// #[macro_use] extern crate synom; |
| 844 | /// |
| 845 | /// use syn::{Ident, Ty}; |
| 846 | /// use syn::parse::{ident, ty}; |
| 847 | /// |
| 848 | /// #[derive(Debug)] |
| 849 | /// enum UnitType { |
| 850 | /// Struct { |
| 851 | /// name: Ident, |
| 852 | /// }, |
| 853 | /// Enum { |
| 854 | /// name: Ident, |
| 855 | /// variant: Ident, |
| 856 | /// }, |
| 857 | /// } |
| 858 | /// |
| 859 | /// // Parse a unit struct or enum: either `struct S;` or `enum E { V }`. |
| 860 | /// named!(unit_type -> UnitType, do_parse!( |
| 861 | /// which: alt!(keyword!("struct") | keyword!("enum")) >> |
| 862 | /// id: ident >> |
| 863 | /// item: switch!(value!(which), |
| 864 | /// "struct" => map!( |
| 865 | /// punct!(";"), |
| 866 | /// move |_| UnitType::Struct { |
| 867 | /// name: id, |
| 868 | /// } |
| 869 | /// ) |
| 870 | /// | |
| 871 | /// "enum" => map!( |
| 872 | /// delimited!(punct!("{"), ident, punct!("}")), |
| 873 | /// move |variant| UnitType::Enum { |
| 874 | /// name: id, |
| 875 | /// variant: variant, |
| 876 | /// } |
| 877 | /// ) |
| 878 | /// ) >> |
| 879 | /// (item) |
| 880 | /// )); |
| 881 | /// |
| 882 | /// fn main() { |
| 883 | /// let input = "struct S;"; |
| 884 | /// let parsed = unit_type(input).expect("unit struct or enum"); |
| 885 | /// println!("{:?}", parsed); |
| 886 | /// |
| 887 | /// let input = "enum E { V }"; |
| 888 | /// let parsed = unit_type(input).expect("unit struct or enum"); |
| 889 | /// println!("{:?}", parsed); |
| 890 | /// } |
| 891 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 892 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 893 | macro_rules! value { |
| 894 | ($i:expr, $res:expr) => { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 895 | $crate::IResult::Done($i, $res) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 896 | }; |
| 897 | } |
| 898 | |
David Tolnay | f2222f0 | 2017-01-27 17:09:20 -0800 | [diff] [blame] | 899 | /// Value surrounded by a pair of delimiters. |
| 900 | /// |
| 901 | /// - **Syntax:** `delimited!(OPEN, THING, CLOSE)` |
| 902 | /// - **Output:** `THING` |
| 903 | /// |
| 904 | /// ```rust |
| 905 | /// extern crate syn; |
| 906 | /// #[macro_use] extern crate synom; |
| 907 | /// |
| 908 | /// use syn::Expr; |
| 909 | /// use syn::parse::expr; |
| 910 | /// |
| 911 | /// // An expression surrounded by [[ ... ]]. |
| 912 | /// named!(double_bracket_expr -> Expr, |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 913 | /// delimited!(punct!("[["), expr, punct!("]]")) |
| 914 | /// ); |
David Tolnay | f2222f0 | 2017-01-27 17:09:20 -0800 | [diff] [blame] | 915 | /// |
| 916 | /// fn main() { |
| 917 | /// let input = "[[ 1 + 1 ]]"; |
| 918 | /// |
| 919 | /// let parsed = double_bracket_expr(input).expect("double bracket expr"); |
| 920 | /// |
| 921 | /// println!("{:?}", parsed); |
| 922 | /// } |
| 923 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 924 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 925 | macro_rules! delimited { |
| 926 | ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => { |
| 927 | match tuple_parser!($i, (), $submac!($($args)*), $($rest)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 928 | $crate::IResult::Error => $crate::IResult::Error, |
| 929 | $crate::IResult::Done(i1, (_, o, _)) => $crate::IResult::Done(i1, o) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 930 | } |
| 931 | }; |
| 932 | |
| 933 | ($i:expr, $f:expr, $($rest:tt)+) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 934 | delimited!($i, call!($f), $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 935 | }; |
| 936 | } |
| 937 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 938 | /// One or more values separated by some separator. Does not allow a trailing |
| 939 | /// separator. |
David Tolnay | f2222f0 | 2017-01-27 17:09:20 -0800 | [diff] [blame] | 940 | /// |
| 941 | /// - **Syntax:** `separated_nonempty_list!(SEPARATOR, THING)` |
| 942 | /// - **Output:** `Vec<THING>` |
| 943 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 944 | /// You may also be looking for: |
| 945 | /// |
| 946 | /// - `separated_list!` - one or more values |
| 947 | /// - `terminated_list!` - zero or more, allows trailing separator |
| 948 | /// - `many0!` - zero or more, no separator |
| 949 | /// |
David Tolnay | f2222f0 | 2017-01-27 17:09:20 -0800 | [diff] [blame] | 950 | /// ```rust |
| 951 | /// extern crate syn; |
| 952 | /// #[macro_use] extern crate synom; |
| 953 | /// |
| 954 | /// use syn::Ty; |
| 955 | /// use syn::parse::ty; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 956 | /// use synom::delimited::Delimited; |
David Tolnay | f2222f0 | 2017-01-27 17:09:20 -0800 | [diff] [blame] | 957 | /// |
| 958 | /// // One or more Rust types separated by commas. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 959 | /// named!(comma_separated_types -> Delimited<Ty, &str>, |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 960 | /// separated_nonempty_list!(punct!(","), ty) |
| 961 | /// ); |
David Tolnay | f2222f0 | 2017-01-27 17:09:20 -0800 | [diff] [blame] | 962 | /// |
| 963 | /// fn main() { |
| 964 | /// let input = "&str, Map<K, V>, String"; |
| 965 | /// |
| 966 | /// let parsed = comma_separated_types(input).expect("comma-separated types"); |
| 967 | /// |
| 968 | /// assert_eq!(parsed.len(), 3); |
| 969 | /// println!("{:?}", parsed); |
| 970 | /// } |
| 971 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 972 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 973 | macro_rules! separated_nonempty_list { |
| 974 | ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{ |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 975 | let mut res = $crate::delimited::Delimited::new(); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 976 | let mut input = $i; |
| 977 | |
| 978 | // get the first element |
| 979 | match $submac!(input, $($args2)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 980 | $crate::IResult::Error => $crate::IResult::Error, |
| 981 | $crate::IResult::Done(i, o) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 982 | if i.len() == input.len() { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 983 | $crate::IResult::Error |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 984 | } else { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 985 | input = i; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 986 | res.push_first(o); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 987 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 988 | while let $crate::IResult::Done(i2, s) = $sep!(input, $($args)*) { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 989 | if i2.len() == input.len() { |
| 990 | break; |
| 991 | } |
| 992 | |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 993 | if let $crate::IResult::Done(i3, o3) = $submac!(i2, $($args2)*) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 994 | res.push_next(o3, s); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 995 | if i3.len() == i2.len() { |
| 996 | break; |
| 997 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 998 | input = i3; |
| 999 | } else { |
| 1000 | break; |
| 1001 | } |
| 1002 | } |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1003 | $crate::IResult::Done(input, res) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | }}; |
| 1008 | |
| 1009 | ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 1010 | separated_nonempty_list!($i, $submac!($($args)*), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1011 | }; |
| 1012 | |
| 1013 | ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 1014 | separated_nonempty_list!($i, call!($f), $submac!($($args)*)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1015 | }; |
| 1016 | |
| 1017 | ($i:expr, $f:expr, $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 1018 | separated_nonempty_list!($i, call!($f), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1019 | }; |
| 1020 | } |
| 1021 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1022 | /// 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] | 1023 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1024 | /// - **Syntax:** `tuple!(A, B, C, ...)` |
| 1025 | /// - **Output:** `(A, B, C, ...)` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 1026 | /// |
| 1027 | /// ```rust |
| 1028 | /// extern crate syn; |
| 1029 | /// #[macro_use] extern crate synom; |
| 1030 | /// |
| 1031 | /// use syn::Ty; |
| 1032 | /// use syn::parse::ty; |
| 1033 | /// |
| 1034 | /// named!(two_types -> (Ty, Ty), tuple!(ty, ty)); |
| 1035 | /// |
| 1036 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1037 | /// let input = "&str Map<K, V>"; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 1038 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1039 | /// let parsed = two_types(input).expect("two types"); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 1040 | /// |
| 1041 | /// println!("{:?}", parsed); |
| 1042 | /// } |
| 1043 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1044 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1045 | macro_rules! tuple { |
| 1046 | ($i:expr, $($rest:tt)*) => { |
| 1047 | tuple_parser!($i, (), $($rest)*) |
| 1048 | }; |
| 1049 | } |
| 1050 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1051 | /// Internal parser, do not use directly. |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1052 | #[doc(hidden)] |
| 1053 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1054 | macro_rules! tuple_parser { |
| 1055 | ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 1056 | tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1057 | }; |
| 1058 | |
| 1059 | ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => { |
| 1060 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1061 | $crate::IResult::Error => $crate::IResult::Error, |
| 1062 | $crate::IResult::Done(i, o) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1063 | tuple_parser!(i, (o), $($rest)*), |
| 1064 | } |
| 1065 | }; |
| 1066 | |
| 1067 | ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => { |
| 1068 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1069 | $crate::IResult::Error => $crate::IResult::Error, |
| 1070 | $crate::IResult::Done(i, o) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1071 | tuple_parser!(i, ($($parsed)* , o), $($rest)*), |
| 1072 | } |
| 1073 | }; |
| 1074 | |
| 1075 | ($i:expr, ($($parsed:tt),*), $e:ident) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 1076 | tuple_parser!($i, ($($parsed),*), call!($e)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1077 | }; |
| 1078 | |
| 1079 | ($i:expr, (), $submac:ident!( $($args:tt)* )) => { |
| 1080 | $submac!($i, $($args)*) |
| 1081 | }; |
| 1082 | |
| 1083 | ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => { |
| 1084 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1085 | $crate::IResult::Error => $crate::IResult::Error, |
| 1086 | $crate::IResult::Done(i, o) => $crate::IResult::Done(i, ($($parsed),*, o)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1087 | } |
| 1088 | }; |
| 1089 | |
| 1090 | ($i:expr, ($($parsed:expr),*)) => { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1091 | $crate::IResult::Done($i, ($($parsed),*)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1092 | }; |
| 1093 | } |
| 1094 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1095 | /// Run a series of parsers, returning the result of the first one which |
| 1096 | /// succeeds. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 1097 | /// |
| 1098 | /// Optionally allows for the result to be transformed. |
| 1099 | /// |
| 1100 | /// - **Syntax:** `alt!(THING1 | THING2 => { FUNC } | ...)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1101 | /// - **Output:** `T`, the return type of `THING1` and `FUNC(THING2)` and ... |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 1102 | /// |
| 1103 | /// ```rust |
| 1104 | /// extern crate syn; |
| 1105 | /// #[macro_use] extern crate synom; |
| 1106 | /// |
| 1107 | /// use syn::Ident; |
| 1108 | /// use syn::parse::ident; |
| 1109 | /// |
| 1110 | /// named!(ident_or_bang -> Ident, |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1111 | /// alt!( |
| 1112 | /// ident |
| 1113 | /// | |
| 1114 | /// punct!("!") => { |_| "BANG".into() } |
| 1115 | /// ) |
| 1116 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 1117 | /// |
| 1118 | /// fn main() { |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 1119 | /// let input = "foo"; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1120 | /// let parsed = ident_or_bang(input).expect("identifier or `!`"); |
| 1121 | /// assert_eq!(parsed, "foo"); |
| 1122 | /// |
| 1123 | /// let input = "!"; |
| 1124 | /// let parsed = ident_or_bang(input).expect("identifier or `!`"); |
| 1125 | /// assert_eq!(parsed, "BANG"); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 1126 | /// } |
| 1127 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1128 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1129 | macro_rules! alt { |
| 1130 | ($i:expr, $e:ident | $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 1131 | alt!($i, call!($e) | $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1132 | }; |
| 1133 | |
| 1134 | ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => { |
| 1135 | match $subrule!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1136 | res @ $crate::IResult::Done(_, _) => res, |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1137 | _ => alt!($i, $($rest)*) |
| 1138 | } |
| 1139 | }; |
| 1140 | |
| 1141 | ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => { |
| 1142 | match $subrule!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1143 | $crate::IResult::Done(i, o) => $crate::IResult::Done(i, $gen(o)), |
| 1144 | $crate::IResult::Error => alt!($i, $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1145 | } |
| 1146 | }; |
| 1147 | |
| 1148 | ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 1149 | alt!($i, call!($e) => { $gen } | $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1150 | }; |
| 1151 | |
| 1152 | ($i:expr, $e:ident => { $gen:expr }) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 1153 | alt!($i, call!($e) => { $gen }) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1154 | }; |
| 1155 | |
| 1156 | ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => { |
| 1157 | match $subrule!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1158 | $crate::IResult::Done(i, o) => $crate::IResult::Done(i, $gen(o)), |
| 1159 | $crate::IResult::Error => $crate::IResult::Error, |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1160 | } |
| 1161 | }; |
| 1162 | |
| 1163 | ($i:expr, $e:ident) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 1164 | alt!($i, call!($e)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1165 | }; |
| 1166 | |
| 1167 | ($i:expr, $subrule:ident!( $($args:tt)*)) => { |
David Tolnay | 5377b17 | 2016-10-25 01:13:12 -0700 | [diff] [blame] | 1168 | $subrule!($i, $($args)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1169 | }; |
| 1170 | } |
| 1171 | |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 1172 | /// Run a series of parsers, one after another, optionally assigning the results |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1173 | /// a name. Fail if any of the parsers fails. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 1174 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1175 | /// Produces the result of evaluating the final expression in parentheses with |
| 1176 | /// all of the previously named results bound. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 1177 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1178 | /// - **Syntax:** `do_parse!(name: THING1 >> THING2 >> (RESULT))` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 1179 | /// - **Output:** `RESULT` |
| 1180 | /// |
| 1181 | /// ```rust |
| 1182 | /// extern crate syn; |
| 1183 | /// #[macro_use] extern crate synom; |
| 1184 | /// |
| 1185 | /// use syn::{Ident, TokenTree}; |
| 1186 | /// use syn::parse::{ident, tt}; |
| 1187 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1188 | /// // Parse a macro invocation like `stringify!($args)`. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 1189 | /// named!(simple_mac -> (Ident, TokenTree), do_parse!( |
| 1190 | /// name: ident >> |
| 1191 | /// punct!("!") >> |
| 1192 | /// body: tt >> |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1193 | /// (name, body) |
| 1194 | /// )); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 1195 | /// |
| 1196 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1197 | /// let input = "stringify!($args)"; |
| 1198 | /// let (name, body) = simple_mac(input).expect("macro invocation"); |
| 1199 | /// println!("{:?}", name); |
| 1200 | /// println!("{:?}", body); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 1201 | /// } |
| 1202 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1203 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1204 | macro_rules! do_parse { |
| 1205 | ($i:expr, ( $($rest:expr),* )) => { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1206 | $crate::IResult::Done($i, ( $($rest),* )) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1207 | }; |
| 1208 | |
| 1209 | ($i:expr, $e:ident >> $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 1210 | do_parse!($i, call!($e) >> $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1211 | }; |
| 1212 | |
| 1213 | ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 1214 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1215 | $crate::IResult::Error => $crate::IResult::Error, |
| 1216 | $crate::IResult::Done(i, _) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1217 | do_parse!(i, $($rest)*), |
| 1218 | } |
| 1219 | }; |
| 1220 | |
| 1221 | ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 1222 | do_parse!($i, $field: call!($e) >> $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1223 | }; |
| 1224 | |
| 1225 | ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 1226 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1227 | $crate::IResult::Error => $crate::IResult::Error, |
| 1228 | $crate::IResult::Done(i, o) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1229 | let $field = o; |
| 1230 | do_parse!(i, $($rest)*) |
| 1231 | }, |
| 1232 | } |
| 1233 | }; |
| 1234 | |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1235 | ($i:expr, mut $field:ident : $e:ident >> $($rest:tt)*) => { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1236 | do_parse!($i, mut $field: call!($e) >> $($rest)*) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1237 | }; |
| 1238 | |
| 1239 | ($i:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 1240 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1241 | $crate::IResult::Error => $crate::IResult::Error, |
| 1242 | $crate::IResult::Done(i, o) => { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1243 | let mut $field = o; |
| 1244 | do_parse!(i, $($rest)*) |
| 1245 | }, |
| 1246 | } |
| 1247 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1248 | } |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 1249 | |
| 1250 | #[macro_export] |
| 1251 | macro_rules! input_end { |
| 1252 | ($i:expr,) => { |
| 1253 | $crate::input_end($i) |
| 1254 | }; |
| 1255 | } |
| 1256 | |
| 1257 | // Not a public API |
| 1258 | #[doc(hidden)] |
| 1259 | pub fn input_end(input: &[TokenTree]) -> IResult<&'static [TokenTree], &'static str> { |
| 1260 | if input.is_empty() { |
| 1261 | IResult::Done(&[], "") |
| 1262 | } else { |
| 1263 | IResult::Error |
| 1264 | } |
| 1265 | } |