blob: 935b7a8ea71d98c76b420735dbcbbdb902217181 [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 Tolnayb34f5702018-01-06 19:39:49 -08009//! Parsing interface for parsing a token stream into a syntax tree node.
David Tolnayc5ab8c62017-12-26 16:43:39 -050010//!
David Tolnayca7cd972018-01-11 14:23:06 -080011//! Parsing in Syn is built on parser functions that take in a [`Cursor`] and
12//! produce a [`PResult<T>`] where `T` is some syntax tree node. `Cursor` is a
13//! cheaply copyable cursor over a range of tokens in a token stream, and
14//! `PResult` is a result that packages together a parsed syntax tree node `T`
15//! with a stream of remaining unparsed tokens after `T` represented as another
16//! `Cursor`, or a [`ParseError`] if parsing failed.
David Tolnayc5ab8c62017-12-26 16:43:39 -050017//!
David Tolnayb34f5702018-01-06 19:39:49 -080018//! [`Cursor`]: ../buffer/index.html
David Tolnayca7cd972018-01-11 14:23:06 -080019//! [`PResult<T>`]: type.PResult.html
20//! [`ParseError`]: struct.ParseError.html
21//!
22//! This `Cursor`- and `PResult`-based interface is convenient for parser
23//! combinators and parser implementations, but not necessarily when you just
24//! have some tokens that you want to parse. For that we expose the following
25//! two entry points.
26//!
27//! ## The `syn::parse*` functions
28//!
29//! The [`syn::parse`], [`syn::parse2`], and [`syn::parse_str`] functions serve
30//! as an entry point for parsing syntax tree nodes that can be parsed in an
31//! obvious default way. These functions can return any syntax tree node that
32//! implements the [`Synom`] trait, which includes most types in Syn.
33//!
34//! [`syn::parse`]: ../fn.parse.html
35//! [`syn::parse2`]: ../fn.parse2.html
36//! [`syn::parse_str`]: ../fn.parse_str.html
37//! [`Synom`]: trait.Synom.html
38//!
39//! ```
40//! use syn::Type;
41//!
42//! # fn run_parser() -> Result<(), syn::synom::ParseError> {
43//! let t: Type = syn::parse_str("std::collections::HashMap<String, Value>")?;
44//! # Ok(())
45//! # }
46//! #
47//! # fn main() {
48//! # run_parser().unwrap();
49//! # }
50//! ```
51//!
David Tolnayb4ee14e2018-01-11 15:23:25 -080052//! The [`parse_quote!`] macro also uses this approach.
53//!
54//! [`parse_quote!`]: ../macro.parse_quote.html
55//!
David Tolnayca7cd972018-01-11 14:23:06 -080056//! ## The `Parser` trait
57//!
58//! Some types can be parsed in several ways depending on context. For example
59//! an [`Attribute`] can be either "outer" like `#[...]` or "inner" like
60//! `#![...]` and parsing the wrong one would be a bug. Similarly [`Punctuated`]
61//! may or may not allow trailing punctuation, and parsing it the wrong way
62//! would either reject valid input or accept invalid input.
63//!
64//! [`Attribute`]: ../struct.Attribute.html
65//! [`Punctuated`]: ../punctuated/index.html
66//!
67//! The `Synom` trait is not implemented in these cases because there is no good
68//! behavior to consider the default.
69//!
70//! ```ignore
71//! // Can't parse `Punctuated` without knowing whether trailing punctuation
72//! // should be allowed in this context.
73//! let path: Punctuated<PathSegment, Token![::]> = syn::parse(tokens)?;
74//! ```
75//!
76//! In these cases the types provide a choice of parser functions rather than a
77//! single `Synom` implementation, and those parser functions can be invoked
78//! through the [`Parser`] trait.
79//!
80//! [`Parser`]: trait.Parser.html
81//!
82//! ```
83//! # #[macro_use]
84//! # extern crate syn;
85//! #
86//! # extern crate proc_macro2;
87//! # use proc_macro2::TokenStream;
88//! #
89//! use syn::synom::Parser;
90//! use syn::punctuated::Punctuated;
91//! use syn::{PathSegment, Expr, Attribute};
92//!
93//! # fn run_parsers() -> Result<(), syn::synom::ParseError> {
94//! # let tokens = TokenStream::empty().into();
95//! // Parse a nonempty sequence of path segments separated by `::` punctuation
96//! // with no trailing punctuation.
97//! let parser = Punctuated::<PathSegment, Token![::]>::parse_separated_nonempty;
98//! let path = parser.parse(tokens)?;
99//!
100//! # let tokens = TokenStream::empty().into();
101//! // Parse a possibly empty sequence of expressions terminated by commas with
102//! // an optional trailing punctuation.
103//! let parser = Punctuated::<Expr, Token![,]>::parse_terminated;
104//! let args = parser.parse(tokens)?;
105//!
106//! # let tokens = TokenStream::empty().into();
107//! // Parse zero or more outer attributes but not inner attributes.
108//! named!(outer_attrs -> Vec<Attribute>, many0!(Attribute::parse_outer));
109//! let attrs = outer_attrs.parse(tokens)?;
110//! #
111//! # Ok(())
112//! # }
113//! #
114//! # fn main() {}
115//! ```
116//!
117//! # Implementing a parser function
118//!
119//! Parser functions are usually implemented using the [`nom`]-style parser
120//! combinator macros provided by Syn, but may also be implemented without
121//! macros be using the low-level [`Cursor`] API directly.
122//!
123//! [`nom`]: https://github.com/Geal/nom
David Tolnayc5ab8c62017-12-26 16:43:39 -0500124//!
David Tolnayb34f5702018-01-06 19:39:49 -0800125//! The following parser combinator macros are available and a `Synom` parsing
126//! example is provided for each one.
David Tolnayc5ab8c62017-12-26 16:43:39 -0500127//!
David Tolnayb34f5702018-01-06 19:39:49 -0800128//! - [`alt!`](../macro.alt.html)
129//! - [`braces!`](../macro.braces.html)
130//! - [`brackets!`](../macro.brackets.html)
131//! - [`call!`](../macro.call.html)
132//! - [`cond!`](../macro.cond.html)
133//! - [`cond_reduce!`](../macro.cond_reduce.html)
134//! - [`do_parse!`](../macro.do_parse.html)
135//! - [`epsilon!`](../macro.epsilon.html)
136//! - [`input_end!`](../macro.input_end.html)
137//! - [`keyword!`](../macro.keyword.html)
138//! - [`many0!`](../macro.many0.html)
139//! - [`map!`](../macro.map.html)
140//! - [`not!`](../macro.not.html)
141//! - [`option!`](../macro.option.html)
142//! - [`parens!`](../macro.parens.html)
143//! - [`punct!`](../macro.punct.html)
144//! - [`reject!`](../macro.reject.html)
145//! - [`switch!`](../macro.switch.html)
146//! - [`syn!`](../macro.syn.html)
147//! - [`tuple!`](../macro.tuple.html)
148//! - [`value!`](../macro.value.html)
David Tolnay461d98e2018-01-07 11:07:19 -0800149//!
150//! *This module is available if Syn is built with the `"parsing"` feature.*
David Tolnayc5ab8c62017-12-26 16:43:39 -0500151
Kartikaya Gupta6434beb2018-02-22 17:07:00 -0500152#[cfg(feature = "proc-macro")]
David Tolnayca7cd972018-01-11 14:23:06 -0800153use proc_macro;
154use proc_macro2;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500155
David Tolnay203557a2017-12-27 23:59:33 -0500156pub use error::{PResult, ParseError};
David Tolnayc5ab8c62017-12-26 16:43:39 -0500157
David Tolnayca7cd972018-01-11 14:23:06 -0800158use buffer::{Cursor, TokenBuffer};
David Tolnaydfc886b2018-01-06 08:03:09 -0800159
David Tolnayca7cd972018-01-11 14:23:06 -0800160/// Parsing interface implemented by all types that can be parsed in a default
161/// way from a token stream.
David Tolnayb34f5702018-01-06 19:39:49 -0800162///
163/// Refer to the [module documentation] for details about parsing in Syn.
164///
165/// [module documentation]: index.html
David Tolnay461d98e2018-01-07 11:07:19 -0800166///
167/// *This trait is available if Syn is built with the `"parsing"` feature.*
David Tolnayc5ab8c62017-12-26 16:43:39 -0500168pub trait Synom: Sized {
169 fn parse(input: Cursor) -> PResult<Self>;
170
Federico Mena Quintero13864cf2018-02-28 11:52:45 -0600171 /// A short name of the type being parsed.
172 ///
173 /// The description should only be used for a simple name. It should not
174 /// contain newlines or sentence-ending punctuation, to facilitate embedding in
175 /// larger user-facing strings. Syn will use this description when building
176 /// error messages about parse failures.
177 ///
178 /// # Examples
179 ///
David Tolnayfe35c522018-03-28 11:30:27 +0200180 /// ```
181 /// # use syn::buffer::Cursor;
182 /// # use syn::synom::{Synom, PResult};
183 /// #
David Tolnayecb65e72018-03-28 11:31:12 +0200184 /// struct ExprMacro {
Federico Mena Quintero13864cf2018-02-28 11:52:45 -0600185 /// // ...
186 /// }
187 ///
David Tolnayecb65e72018-03-28 11:31:12 +0200188 /// impl Synom for ExprMacro {
Federico Mena Quintero13864cf2018-02-28 11:52:45 -0600189 /// # fn parse(input: Cursor) -> PResult<Self> { unimplemented!() }
190 /// // fn parse(...) -> ... { ... }
191 ///
192 /// fn description() -> Option<&'static str> {
David Tolnayecb65e72018-03-28 11:31:12 +0200193 /// // Will result in messages like
194 /// //
195 /// // "failed to parse macro invocation expression: $reason"
196 /// Some("macro invocation expression")
Federico Mena Quintero13864cf2018-02-28 11:52:45 -0600197 /// }
198 /// }
199 /// ```
David Tolnayc5ab8c62017-12-26 16:43:39 -0500200 fn description() -> Option<&'static str> {
201 None
202 }
203}
204
David Tolnayca7cd972018-01-11 14:23:06 -0800205impl Synom for proc_macro2::TokenStream {
David Tolnayc5ab8c62017-12-26 16:43:39 -0500206 fn parse(input: Cursor) -> PResult<Self> {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500207 Ok((input.token_stream(), Cursor::empty()))
David Tolnayc5ab8c62017-12-26 16:43:39 -0500208 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800209
210 fn description() -> Option<&'static str> {
211 Some("arbitrary token stream")
212 }
David Tolnayc5ab8c62017-12-26 16:43:39 -0500213}
David Tolnayca7cd972018-01-11 14:23:06 -0800214
215/// Parser that can parse Rust tokens into a particular syntax tree node.
216///
217/// Refer to the [module documentation] for details about parsing in Syn.
218///
219/// [module documentation]: index.html
220///
221/// *This trait is available if Syn is built with the `"parsing"` feature.*
222pub trait Parser: Sized {
223 type Output;
224
David Tolnay9c2da0c2018-01-11 21:03:06 -0800225 /// Parse a proc-macro2 token stream into the chosen syntax tree node.
David Tolnayca7cd972018-01-11 14:23:06 -0800226 fn parse2(self, tokens: proc_macro2::TokenStream) -> Result<Self::Output, ParseError>;
227
David Tolnay9c2da0c2018-01-11 21:03:06 -0800228 /// Parse tokens of source code into the chosen syntax tree node.
Kartikaya Gupta6434beb2018-02-22 17:07:00 -0500229 #[cfg(feature = "proc-macro")]
David Tolnayca7cd972018-01-11 14:23:06 -0800230 fn parse(self, tokens: proc_macro::TokenStream) -> Result<Self::Output, ParseError> {
231 self.parse2(tokens.into())
232 }
233
David Tolnay9c2da0c2018-01-11 21:03:06 -0800234 /// Parse a string of Rust code into the chosen syntax tree node.
235 ///
236 /// # Hygiene
237 ///
238 /// Every span in the resulting syntax tree will be set to resolve at the
239 /// macro call site.
David Tolnayca7cd972018-01-11 14:23:06 -0800240 fn parse_str(self, s: &str) -> Result<Self::Output, ParseError> {
241 match s.parse() {
242 Ok(tts) => self.parse2(tts),
243 Err(_) => Err(ParseError::new("error while lexing input string")),
244 }
245 }
246}
247
248impl<F, T> Parser for F where F: FnOnce(Cursor) -> PResult<T> {
249 type Output = T;
250
251 fn parse2(self, tokens: proc_macro2::TokenStream) -> Result<T, ParseError> {
252 let buf = TokenBuffer::new2(tokens);
253 let (t, rest) = self(buf.begin())?;
254 if rest.eof() {
255 Ok(t)
256 } else if rest == buf.begin() {
257 // parsed nothing
258 Err(ParseError::new("failed to parse anything"))
259 } else {
260 Err(ParseError::new("failed to parse all tokens"))
261 }
262 }
263}