blob: 80d59735a47848abac1515432c9963c8ae86ea2f [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// 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 Tolnayad4b2472018-08-25 08:25:24 -04009use std;
David Tolnayc5ab8c62017-12-26 16:43:39 -050010use std::fmt::{self, Display};
David Tolnayad4b2472018-08-25 08:25:24 -040011use std::iter::FromIterator;
12
13use proc_macro2::{
14 Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree,
15};
16
17use buffer::Cursor;
18
19/// The result of a Syn parser.
20pub 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)]
30pub struct Error {
31 span: Span,
32 message: String,
33}
34
35impl 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 Tolnay7744d9d2018-08-25 22:15:52 -040043 pub fn span(&self) -> Span {
44 self.span
45 }
46
David Tolnayad4b2472018-08-25 08:25:24 -040047 /// 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)]
80pub 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
88impl Display for Error {
89 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
90 formatter.write_str(&self.message)
91 }
92}
93
94impl std::error::Error for Error {
95 fn description(&self) -> &str {
96 "parse error"
97 }
98}
99
100impl From<LexError> for Error {
101 fn from(err: LexError) -> Self {
102 Error::new(Span::call_site(), format!("{:?}", err))
103 }
104}
David Tolnayc5ab8c62017-12-26 16:43:39 -0500105
David Tolnayb34f5702018-01-06 19:39:49 -0800106/// 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 Tolnay461d98e2018-01-07 11:07:19 -0800111///
112/// *This type is available if Syn is built with the `"parsing"` feature.*
David Tolnayad4b2472018-08-25 08:25:24 -0400113pub type PResult<'a, O> = std::result::Result<(O, Cursor<'a>), Error>;