blob: adaf08039d9e35383d87653b6c990d6166ea98f0 [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
43 /// Render the error as an invocation of [`compile_error!`].
44 ///
45 /// The [`parse_macro_input!`] macro provides a convenient way to invoke
46 /// this method correctly in a procedural macro.
47 ///
48 /// [`compile_error!`]: https://doc.rust-lang.org/std/macro.compile_error.html
49 /// [`parse_macro_input!`]: ../macro.parse_macro_input.html
50 pub fn into_compile_error(self) -> TokenStream {
51 // compile_error!($message)
52 TokenStream::from_iter(vec![
53 TokenTree::Ident(Ident::new("compile_error", self.span)),
54 TokenTree::Punct({
55 let mut punct = Punct::new('!', Spacing::Alone);
56 punct.set_span(self.span);
57 punct
58 }),
59 TokenTree::Group({
60 let mut group = Group::new(Delimiter::Brace, {
61 TokenStream::from_iter(vec![TokenTree::Literal({
62 let mut string = Literal::string(&self.message);
63 string.set_span(self.span);
64 string
65 })])
66 });
67 group.set_span(self.span);
68 group
69 }),
70 ])
71 }
72}
73
74// Not public API.
75#[doc(hidden)]
76pub fn new_at<T: Display>(scope: Span, cursor: Cursor, message: T) -> Error {
77 if cursor.eof() {
78 Error::new(scope, format!("unexpected end of input, {}", message))
79 } else {
80 Error::new(cursor.span(), message)
81 }
82}
83
84impl Display for Error {
85 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
86 formatter.write_str(&self.message)
87 }
88}
89
90impl std::error::Error for Error {
91 fn description(&self) -> &str {
92 "parse error"
93 }
94}
95
96impl From<LexError> for Error {
97 fn from(err: LexError) -> Self {
98 Error::new(Span::call_site(), format!("{:?}", err))
99 }
100}
David Tolnayc5ab8c62017-12-26 16:43:39 -0500101
David Tolnayb34f5702018-01-06 19:39:49 -0800102/// The result of a `Synom` parser.
103///
104/// Refer to the [module documentation] for details about parsing in Syn.
105///
106/// [module documentation]: index.html
David Tolnay461d98e2018-01-07 11:07:19 -0800107///
108/// *This type is available if Syn is built with the `"parsing"` feature.*
David Tolnayad4b2472018-08-25 08:25:24 -0400109pub type PResult<'a, O> = std::result::Result<(O, Cursor<'a>), Error>;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500110
111/// An error with a default error message.
112///
113/// NOTE: We should provide better error messages in the future.
David Tolnayfe68c902018-05-20 18:53:58 -0700114pub fn parse_error<'a, O>() -> PResult<'a, O> {
David Tolnayad4b2472018-08-25 08:25:24 -0400115 Err(Error::new(Span::call_site(), "parse error"))
David Tolnayc5ab8c62017-12-26 16:43:39 -0500116}