David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 1 | use std; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 2 | use std::fmt::{self, Display}; |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 3 | use std::iter::FromIterator; |
| 4 | |
| 5 | use proc_macro2::{ |
| 6 | Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree, |
| 7 | }; |
Alex Crichton | 3ea5ca3 | 2018-11-16 02:43:46 -0800 | [diff] [blame] | 8 | #[cfg(feature = "printing")] |
| 9 | use quote::ToTokens; |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 10 | |
David Tolnay | a72f262 | 2018-11-24 13:39:45 -0800 | [diff] [blame] | 11 | #[cfg(feature = "parsing")] |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 12 | use buffer::Cursor; |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 13 | use thread::ThreadBound; |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 14 | |
| 15 | /// The result of a Syn parser. |
| 16 | pub type Result<T> = std::result::Result<T, Error>; |
| 17 | |
| 18 | /// Error returned when a Syn parser cannot parse the input tokens. |
| 19 | /// |
| 20 | /// Refer to the [module documentation] for details about parsing in Syn. |
| 21 | /// |
| 22 | /// [module documentation]: index.html |
| 23 | /// |
| 24 | /// *This type is available if Syn is built with the `"parsing"` feature.* |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 25 | #[derive(Debug)] |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 26 | pub struct Error { |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 27 | // Span is implemented as an index into a thread-local interner to keep the |
| 28 | // size small. It is not safe to access from a different thread. We want |
| 29 | // errors to be Send and Sync to play nicely with the Failure crate, so pin |
| 30 | // the span we're given to its original thread and assume it is |
| 31 | // Span::call_site if accessed from any other thread. |
Alex Crichton | 3ea5ca3 | 2018-11-16 02:43:46 -0800 | [diff] [blame] | 32 | start_span: ThreadBound<Span>, |
| 33 | end_span: ThreadBound<Span>, |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 34 | message: String, |
| 35 | } |
| 36 | |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 37 | #[cfg(test)] |
David Tolnay | 5f79480 | 2018-11-24 14:51:21 -0800 | [diff] [blame] | 38 | struct _Test |
| 39 | where |
| 40 | Error: Send + Sync; |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 41 | |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 42 | impl Error { |
David Tolnay | 7c8efe9 | 2018-09-01 10:00:50 -0700 | [diff] [blame] | 43 | /// Usually the [`ParseStream::error`] method will be used instead, which |
| 44 | /// automatically uses the correct span from the current position of the |
| 45 | /// parse stream. |
| 46 | /// |
| 47 | /// Use `Error::new` when the error needs to be triggered on some span other |
| 48 | /// than where the parse stream is currently positioned. |
| 49 | /// |
| 50 | /// [`ParseStream::error`]: struct.ParseBuffer.html#method.error |
| 51 | /// |
| 52 | /// # Example |
| 53 | /// |
David Tolnay | 95989db | 2019-01-01 15:05:57 -0500 | [diff] [blame^] | 54 | /// ```edition2018 |
David Tolnay | fd5b117 | 2018-12-31 17:54:36 -0500 | [diff] [blame] | 55 | /// use syn::{Error, Ident, LitStr, Result, Token}; |
David Tolnay | 67fea04 | 2018-11-24 14:50:20 -0800 | [diff] [blame] | 56 | /// use syn::parse::ParseStream; |
David Tolnay | 7c8efe9 | 2018-09-01 10:00:50 -0700 | [diff] [blame] | 57 | /// |
| 58 | /// // Parses input that looks like `name = "string"` where the key must be |
| 59 | /// // the identifier `name` and the value may be any string literal. |
| 60 | /// // Returns the string literal. |
| 61 | /// fn parse_name(input: ParseStream) -> Result<LitStr> { |
| 62 | /// let name_token: Ident = input.parse()?; |
| 63 | /// if name_token != "name" { |
| 64 | /// // Trigger an error not on the current position of the stream, |
| 65 | /// // but on the position of the unexpected identifier. |
| 66 | /// return Err(Error::new(name_token.span(), "expected `name`")); |
| 67 | /// } |
| 68 | /// input.parse::<Token![=]>()?; |
| 69 | /// let s: LitStr = input.parse()?; |
| 70 | /// Ok(s) |
| 71 | /// } |
David Tolnay | 7c8efe9 | 2018-09-01 10:00:50 -0700 | [diff] [blame] | 72 | /// ``` |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 73 | pub fn new<T: Display>(span: Span, message: T) -> Self { |
| 74 | Error { |
Alex Crichton | 3ea5ca3 | 2018-11-16 02:43:46 -0800 | [diff] [blame] | 75 | start_span: ThreadBound::new(span), |
| 76 | end_span: ThreadBound::new(span), |
| 77 | message: message.to_string(), |
| 78 | } |
| 79 | } |
| 80 | |
David Tolnay | 3aa72f5 | 2018-11-16 04:21:54 -0800 | [diff] [blame] | 81 | /// Creates an error with the specified message spanning the given syntax |
| 82 | /// tree node. |
Alex Crichton | 3ea5ca3 | 2018-11-16 02:43:46 -0800 | [diff] [blame] | 83 | /// |
David Tolnay | 3aa72f5 | 2018-11-16 04:21:54 -0800 | [diff] [blame] | 84 | /// Unlike the `Error::new` constructor, this constructor takes an argument |
| 85 | /// `tokens` which is a syntax tree node. This allows the resulting `Error` |
| 86 | /// to attempt to span all tokens inside of `tokens`. While you would |
| 87 | /// typically be able to use the `Spanned` trait with the above `Error::new` |
| 88 | /// constructor, implementation limitations today mean that |
| 89 | /// `Error::new_spanned` may provide a higher-quality error message on |
| 90 | /// stable Rust. |
Alex Crichton | 3ea5ca3 | 2018-11-16 02:43:46 -0800 | [diff] [blame] | 91 | /// |
| 92 | /// When in doubt it's recommended to stick to `Error::new` (or |
| 93 | /// `ParseStream::error`)! |
| 94 | #[cfg(feature = "printing")] |
| 95 | pub fn new_spanned<T: ToTokens, U: Display>(tokens: T, message: U) -> Self { |
| 96 | let mut iter = tokens.into_token_stream().into_iter(); |
David Tolnay | 708ef13 | 2018-11-16 04:10:53 -0800 | [diff] [blame] | 97 | let start = iter.next().map_or_else(Span::call_site, |t| t.span()); |
| 98 | let end = iter.last().map_or(start, |t| t.span()); |
Alex Crichton | 3ea5ca3 | 2018-11-16 02:43:46 -0800 | [diff] [blame] | 99 | Error { |
| 100 | start_span: ThreadBound::new(start), |
| 101 | end_span: ThreadBound::new(end), |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 102 | message: message.to_string(), |
| 103 | } |
| 104 | } |
| 105 | |
David Tolnay | e790073 | 2018-11-11 10:25:47 -0800 | [diff] [blame] | 106 | /// The source location of the error. |
| 107 | /// |
| 108 | /// Spans are not thread-safe so this function returns `Span::call_site()` |
| 109 | /// if called from a different thread than the one on which the `Error` was |
| 110 | /// originally created. |
David Tolnay | 7744d9d | 2018-08-25 22:15:52 -0400 | [diff] [blame] | 111 | pub fn span(&self) -> Span { |
Alex Crichton | 3ea5ca3 | 2018-11-16 02:43:46 -0800 | [diff] [blame] | 112 | let start = match self.start_span.get() { |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 113 | Some(span) => *span, |
Alex Crichton | 3ea5ca3 | 2018-11-16 02:43:46 -0800 | [diff] [blame] | 114 | None => return Span::call_site(), |
| 115 | }; |
| 116 | |
| 117 | #[cfg(procmacro2_semver_exempt)] |
| 118 | { |
| 119 | let end = match self.end_span.get() { |
| 120 | Some(span) => *span, |
| 121 | None => return Span::call_site(), |
| 122 | }; |
David Tolnay | 2f12a6d | 2018-11-16 04:06:06 -0800 | [diff] [blame] | 123 | start.join(end).unwrap_or(start) |
Alex Crichton | 3ea5ca3 | 2018-11-16 02:43:46 -0800 | [diff] [blame] | 124 | } |
| 125 | #[cfg(not(procmacro2_semver_exempt))] |
| 126 | { |
David Tolnay | 2f12a6d | 2018-11-16 04:06:06 -0800 | [diff] [blame] | 127 | start |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 128 | } |
David Tolnay | 7744d9d | 2018-08-25 22:15:52 -0400 | [diff] [blame] | 129 | } |
| 130 | |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 131 | /// Render the error as an invocation of [`compile_error!`]. |
| 132 | /// |
| 133 | /// The [`parse_macro_input!`] macro provides a convenient way to invoke |
| 134 | /// this method correctly in a procedural macro. |
| 135 | /// |
| 136 | /// [`compile_error!`]: https://doc.rust-lang.org/std/macro.compile_error.html |
| 137 | /// [`parse_macro_input!`]: ../macro.parse_macro_input.html |
David Tolnay | 24e21f9 | 2018-09-06 02:08:07 -0700 | [diff] [blame] | 138 | pub fn to_compile_error(&self) -> TokenStream { |
David Tolnay | 5f79480 | 2018-11-24 14:51:21 -0800 | [diff] [blame] | 139 | let start = self |
| 140 | .start_span |
| 141 | .get() |
| 142 | .cloned() |
| 143 | .unwrap_or_else(Span::call_site); |
David Tolnay | caf11b4 | 2018-11-16 04:09:43 -0800 | [diff] [blame] | 144 | let end = self.end_span.get().cloned().unwrap_or_else(Span::call_site); |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 145 | |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 146 | // compile_error!($message) |
| 147 | TokenStream::from_iter(vec![ |
Alex Crichton | 3ea5ca3 | 2018-11-16 02:43:46 -0800 | [diff] [blame] | 148 | TokenTree::Ident(Ident::new("compile_error", start)), |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 149 | TokenTree::Punct({ |
| 150 | let mut punct = Punct::new('!', Spacing::Alone); |
Alex Crichton | 3ea5ca3 | 2018-11-16 02:43:46 -0800 | [diff] [blame] | 151 | punct.set_span(start); |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 152 | punct |
| 153 | }), |
| 154 | TokenTree::Group({ |
| 155 | let mut group = Group::new(Delimiter::Brace, { |
| 156 | TokenStream::from_iter(vec![TokenTree::Literal({ |
| 157 | let mut string = Literal::string(&self.message); |
Alex Crichton | 3ea5ca3 | 2018-11-16 02:43:46 -0800 | [diff] [blame] | 158 | string.set_span(end); |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 159 | string |
| 160 | })]) |
| 161 | }); |
Alex Crichton | 3ea5ca3 | 2018-11-16 02:43:46 -0800 | [diff] [blame] | 162 | group.set_span(end); |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 163 | group |
| 164 | }), |
| 165 | ]) |
| 166 | } |
| 167 | } |
| 168 | |
David Tolnay | a72f262 | 2018-11-24 13:39:45 -0800 | [diff] [blame] | 169 | #[cfg(feature = "parsing")] |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 170 | pub fn new_at<T: Display>(scope: Span, cursor: Cursor, message: T) -> Error { |
| 171 | if cursor.eof() { |
| 172 | Error::new(scope, format!("unexpected end of input, {}", message)) |
| 173 | } else { |
| 174 | Error::new(cursor.span(), message) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | impl Display for Error { |
| 179 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 180 | formatter.write_str(&self.message) |
| 181 | } |
| 182 | } |
| 183 | |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 184 | impl Clone for Error { |
| 185 | fn clone(&self) -> Self { |
David Tolnay | 5f79480 | 2018-11-24 14:51:21 -0800 | [diff] [blame] | 186 | let start = self |
| 187 | .start_span |
| 188 | .get() |
| 189 | .cloned() |
| 190 | .unwrap_or_else(Span::call_site); |
David Tolnay | caf11b4 | 2018-11-16 04:09:43 -0800 | [diff] [blame] | 191 | let end = self.end_span.get().cloned().unwrap_or_else(Span::call_site); |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 192 | Error { |
Alex Crichton | 3ea5ca3 | 2018-11-16 02:43:46 -0800 | [diff] [blame] | 193 | start_span: ThreadBound::new(start), |
| 194 | end_span: ThreadBound::new(end), |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 195 | message: self.message.clone(), |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 200 | impl std::error::Error for Error { |
| 201 | fn description(&self) -> &str { |
| 202 | "parse error" |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | impl From<LexError> for Error { |
| 207 | fn from(err: LexError) -> Self { |
| 208 | Error::new(Span::call_site(), format!("{:?}", err)) |
| 209 | } |
| 210 | } |