blob: 0afd63ab92a9e8f0553a942da5433486d0a700ee [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
David Tolnayad4b2472018-08-25 08:25:24 -040078pub fn new_at<T: Display>(scope: Span, cursor: Cursor, message: T) -> Error {
79 if cursor.eof() {
80 Error::new(scope, format!("unexpected end of input, {}", message))
81 } else {
82 Error::new(cursor.span(), message)
83 }
84}
85
86impl Display for Error {
87 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
88 formatter.write_str(&self.message)
89 }
90}
91
92impl std::error::Error for Error {
93 fn description(&self) -> &str {
94 "parse error"
95 }
96}
97
98impl From<LexError> for Error {
99 fn from(err: LexError) -> Self {
100 Error::new(Span::call_site(), format!("{:?}", err))
101 }
102}