blob: a7dd13c0c8c1758065ef3f8e030da3802d0f295c [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
David Tolnayca7cd972018-01-11 14:23:06 -0800152use proc_macro;
153use proc_macro2;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500154
David Tolnay203557a2017-12-27 23:59:33 -0500155pub use error::{PResult, ParseError};
David Tolnayc5ab8c62017-12-26 16:43:39 -0500156
David Tolnayca7cd972018-01-11 14:23:06 -0800157use buffer::{Cursor, TokenBuffer};
David Tolnaydfc886b2018-01-06 08:03:09 -0800158
David Tolnayca7cd972018-01-11 14:23:06 -0800159/// Parsing interface implemented by all types that can be parsed in a default
160/// way from a token stream.
David Tolnayb34f5702018-01-06 19:39:49 -0800161///
162/// Refer to the [module documentation] for details about parsing in Syn.
163///
164/// [module documentation]: index.html
David Tolnay461d98e2018-01-07 11:07:19 -0800165///
166/// *This trait is available if Syn is built with the `"parsing"` feature.*
David Tolnayc5ab8c62017-12-26 16:43:39 -0500167pub trait Synom: Sized {
168 fn parse(input: Cursor) -> PResult<Self>;
169
170 fn description() -> Option<&'static str> {
171 None
172 }
173}
174
David Tolnayca7cd972018-01-11 14:23:06 -0800175impl Synom for proc_macro2::TokenStream {
David Tolnayc5ab8c62017-12-26 16:43:39 -0500176 fn parse(input: Cursor) -> PResult<Self> {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500177 Ok((input.token_stream(), Cursor::empty()))
David Tolnayc5ab8c62017-12-26 16:43:39 -0500178 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800179
180 fn description() -> Option<&'static str> {
181 Some("arbitrary token stream")
182 }
David Tolnayc5ab8c62017-12-26 16:43:39 -0500183}
David Tolnayca7cd972018-01-11 14:23:06 -0800184
185/// Parser that can parse Rust tokens into a particular syntax tree node.
186///
187/// Refer to the [module documentation] for details about parsing in Syn.
188///
189/// [module documentation]: index.html
190///
191/// *This trait is available if Syn is built with the `"parsing"` feature.*
192pub trait Parser: Sized {
193 type Output;
194
David Tolnay9c2da0c2018-01-11 21:03:06 -0800195 /// Parse a proc-macro2 token stream into the chosen syntax tree node.
David Tolnayca7cd972018-01-11 14:23:06 -0800196 fn parse2(self, tokens: proc_macro2::TokenStream) -> Result<Self::Output, ParseError>;
197
David Tolnay9c2da0c2018-01-11 21:03:06 -0800198 /// Parse tokens of source code into the chosen syntax tree node.
David Tolnayca7cd972018-01-11 14:23:06 -0800199 fn parse(self, tokens: proc_macro::TokenStream) -> Result<Self::Output, ParseError> {
200 self.parse2(tokens.into())
201 }
202
David Tolnay9c2da0c2018-01-11 21:03:06 -0800203 /// Parse a string of Rust code into the chosen syntax tree node.
204 ///
205 /// # Hygiene
206 ///
207 /// Every span in the resulting syntax tree will be set to resolve at the
208 /// macro call site.
David Tolnayca7cd972018-01-11 14:23:06 -0800209 fn parse_str(self, s: &str) -> Result<Self::Output, ParseError> {
210 match s.parse() {
211 Ok(tts) => self.parse2(tts),
212 Err(_) => Err(ParseError::new("error while lexing input string")),
213 }
214 }
215}
216
217impl<F, T> Parser for F where F: FnOnce(Cursor) -> PResult<T> {
218 type Output = T;
219
220 fn parse2(self, tokens: proc_macro2::TokenStream) -> Result<T, ParseError> {
221 let buf = TokenBuffer::new2(tokens);
222 let (t, rest) = self(buf.begin())?;
223 if rest.eof() {
224 Ok(t)
225 } else if rest == buf.begin() {
226 // parsed nothing
227 Err(ParseError::new("failed to parse anything"))
228 } else {
229 Err(ParseError::new("failed to parse all tokens"))
230 }
231 }
232}