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 | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 9 | use buffer::Cursor; |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 10 | use std::error::Error; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 11 | use std::fmt::{self, Display}; |
| 12 | |
David Tolnay | b34f570 | 2018-01-06 19:39:49 -0800 | [diff] [blame] | 13 | /// The result of a `Synom` parser. |
| 14 | /// |
| 15 | /// Refer to the [module documentation] for details about parsing in Syn. |
| 16 | /// |
| 17 | /// [module documentation]: index.html |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 18 | /// |
| 19 | /// *This type is available if Syn is built with the `"parsing"` feature.* |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 20 | pub type PResult<'a, O> = Result<(O, Cursor<'a>), ParseError>; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 21 | |
| 22 | /// An error with a default error message. |
| 23 | /// |
| 24 | /// NOTE: We should provide better error messages in the future. |
David Tolnay | fe68c90 | 2018-05-20 18:53:58 -0700 | [diff] [blame^] | 25 | pub fn parse_error<'a, O>() -> PResult<'a, O> { |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 26 | Err(ParseError(None)) |
| 27 | } |
| 28 | |
David Tolnay | b34f570 | 2018-01-06 19:39:49 -0800 | [diff] [blame] | 29 | /// Error returned when a `Synom` parser cannot parse the input tokens. |
| 30 | /// |
| 31 | /// Refer to the [module documentation] for details about parsing in Syn. |
| 32 | /// |
| 33 | /// [module documentation]: index.html |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 34 | /// |
| 35 | /// *This type is available if Syn is built with the `"parsing"` feature.* |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 36 | #[derive(Debug)] |
| 37 | pub struct ParseError(Option<String>); |
| 38 | |
| 39 | impl Error for ParseError { |
| 40 | fn description(&self) -> &str { |
| 41 | match self.0 { |
| 42 | Some(ref desc) => desc, |
| 43 | None => "failed to parse", |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl Display for ParseError { |
| 49 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
David Tolnay | b34f570 | 2018-01-06 19:39:49 -0800 | [diff] [blame] | 50 | Display::fmt(self.description(), f) |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 54 | impl ParseError { |
| 55 | // For syn use only. Not public API. |
| 56 | #[doc(hidden)] |
| 57 | pub fn new<T: Into<String>>(msg: T) -> Self { |
| 58 | ParseError(Some(msg.into())) |
| 59 | } |
| 60 | } |