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