David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 9 | use std; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 10 | use std::fmt::{self, Display}; |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 11 | use std::iter::FromIterator; |
| 12 | |
| 13 | use proc_macro2::{ |
| 14 | Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree, |
| 15 | }; |
| 16 | |
| 17 | use buffer::Cursor; |
| 18 | |
| 19 | /// The result of a Syn parser. |
| 20 | pub type Result<T> = std::result::Result<T, Error>; |
| 21 | |
| 22 | /// Error returned when a Syn parser cannot parse the input tokens. |
| 23 | /// |
| 24 | /// Refer to the [module documentation] for details about parsing in Syn. |
| 25 | /// |
| 26 | /// [module documentation]: index.html |
| 27 | /// |
| 28 | /// *This type is available if Syn is built with the `"parsing"` feature.* |
| 29 | #[derive(Debug)] |
| 30 | pub struct Error { |
| 31 | span: Span, |
| 32 | message: String, |
| 33 | } |
| 34 | |
| 35 | impl Error { |
| 36 | pub fn new<T: Display>(span: Span, message: T) -> Self { |
| 37 | Error { |
| 38 | span: span, |
| 39 | message: message.to_string(), |
| 40 | } |
| 41 | } |
| 42 | |
David Tolnay | 7744d9d | 2018-08-25 22:15:52 -0400 | [diff] [blame] | 43 | pub fn span(&self) -> Span { |
| 44 | self.span |
| 45 | } |
| 46 | |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 47 | /// Render the error as an invocation of [`compile_error!`]. |
| 48 | /// |
| 49 | /// The [`parse_macro_input!`] macro provides a convenient way to invoke |
| 50 | /// this method correctly in a procedural macro. |
| 51 | /// |
| 52 | /// [`compile_error!`]: https://doc.rust-lang.org/std/macro.compile_error.html |
| 53 | /// [`parse_macro_input!`]: ../macro.parse_macro_input.html |
| 54 | pub fn into_compile_error(self) -> TokenStream { |
| 55 | // compile_error!($message) |
| 56 | TokenStream::from_iter(vec![ |
| 57 | TokenTree::Ident(Ident::new("compile_error", self.span)), |
| 58 | TokenTree::Punct({ |
| 59 | let mut punct = Punct::new('!', Spacing::Alone); |
| 60 | punct.set_span(self.span); |
| 61 | punct |
| 62 | }), |
| 63 | TokenTree::Group({ |
| 64 | let mut group = Group::new(Delimiter::Brace, { |
| 65 | TokenStream::from_iter(vec![TokenTree::Literal({ |
| 66 | let mut string = Literal::string(&self.message); |
| 67 | string.set_span(self.span); |
| 68 | string |
| 69 | })]) |
| 70 | }); |
| 71 | group.set_span(self.span); |
| 72 | group |
| 73 | }), |
| 74 | ]) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Not public API. |
| 79 | #[doc(hidden)] |
| 80 | pub fn new_at<T: Display>(scope: Span, cursor: Cursor, message: T) -> Error { |
| 81 | if cursor.eof() { |
| 82 | Error::new(scope, format!("unexpected end of input, {}", message)) |
| 83 | } else { |
| 84 | Error::new(cursor.span(), message) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | impl Display for Error { |
| 89 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 90 | formatter.write_str(&self.message) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl std::error::Error for Error { |
| 95 | fn description(&self) -> &str { |
| 96 | "parse error" |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | impl From<LexError> for Error { |
| 101 | fn from(err: LexError) -> Self { |
| 102 | Error::new(Span::call_site(), format!("{:?}", err)) |
| 103 | } |
| 104 | } |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 105 | |
David Tolnay | b34f570 | 2018-01-06 19:39:49 -0800 | [diff] [blame] | 106 | /// The result of a `Synom` parser. |
| 107 | /// |
| 108 | /// Refer to the [module documentation] for details about parsing in Syn. |
| 109 | /// |
| 110 | /// [module documentation]: index.html |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 111 | /// |
| 112 | /// *This type is available if Syn is built with the `"parsing"` feature.* |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 113 | pub type PResult<'a, O> = std::result::Result<(O, Cursor<'a>), Error>; |