blob: 7576020099870df4a42d54a83a0ce15a2bcba5ae [file] [log] [blame]
Alex Crichton1fd0e8a2018-02-04 21:29:13 -08001//! A "shim crate" intended to multiplex the [`proc_macro`] API on to stable
2//! Rust.
Alex Crichtonbabc99e2017-07-05 18:00:29 -07003//!
4//! Procedural macros in Rust operate over the upstream
Alex Crichton1fd0e8a2018-02-04 21:29:13 -08005//! [`proc_macro::TokenStream`][ts] type. This type currently is quite
6//! conservative and exposed no internal implementation details. Nightly
7//! compilers, however, contain a much richer interface. This richer interface
8//! allows fine-grained inspection of the token stream which avoids
9//! stringification/re-lexing and also preserves span information.
Alex Crichtonbabc99e2017-07-05 18:00:29 -070010//!
Alex Crichton1fd0e8a2018-02-04 21:29:13 -080011//! The upcoming APIs added to [`proc_macro`] upstream are the foundation for
Alex Crichtonbabc99e2017-07-05 18:00:29 -070012//! productive procedural macros in the ecosystem. To help prepare the ecosystem
13//! for using them this crate serves to both compile on stable and nightly and
14//! mirrors the API-to-be. The intention is that procedural macros which switch
15//! to use this crate will be trivially able to switch to the upstream
16//! `proc_macro` crate once its API stabilizes.
17//!
David Tolnayd66ecf62018-01-02 20:05:42 -080018//! In the meantime this crate also has a `nightly` Cargo feature which
Alex Crichton1fd0e8a2018-02-04 21:29:13 -080019//! enables it to reimplement itself with the unstable API of [`proc_macro`].
Alex Crichtonbabc99e2017-07-05 18:00:29 -070020//! This'll allow immediate usage of the beneficial upstream API, particularly
21//! around preserving span information.
Alex Crichton1fd0e8a2018-02-04 21:29:13 -080022//!
David Tolnay6b46deb2018-04-25 21:22:46 -070023//! # Unstable Features
24//!
25//! `proc-macro2` supports exporting some methods from `proc_macro` which are
26//! currently highly unstable, and may not be stabilized in the first pass of
27//! `proc_macro` stabilizations. These features are not exported by default.
28//! Minor versions of `proc-macro2` may make breaking changes to them at any
29//! time.
30//!
31//! To enable these features, the `procmacro2_semver_exempt` config flag must be
32//! passed to rustc.
33//!
34//! ```sh
35//! RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo build
36//! ```
37//!
38//! Note that this must not only be done for your crate, but for any crate that
39//! depends on your crate. This infectious nature is intentional, as it serves
40//! as a reminder that you are outside of the normal semver guarantees.
41//!
Alex Crichton1fd0e8a2018-02-04 21:29:13 -080042//! [`proc_macro`]: https://doc.rust-lang.org/proc_macro/
43//! [ts]: https://doc.rust-lang.org/proc_macro/struct.TokenStream.html
Alex Crichtonbabc99e2017-07-05 18:00:29 -070044
David Tolnay15cc4982018-01-08 08:03:27 -080045// Proc-macro2 types in rustdoc of other crates get linked to here.
David Tolnay2c8e9172018-05-18 20:54:57 -070046#![doc(html_root_url = "https://docs.rs/proc-macro2/0.4.2")]
David Tolnayd66ecf62018-01-02 20:05:42 -080047#![cfg_attr(feature = "nightly", feature(proc_macro))]
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070048
Alex Crichton0e8e7f42018-02-22 06:15:13 -080049#[cfg(feature = "proc-macro")]
Alex Crichton44bffbc2017-05-19 17:51:59 -070050extern crate proc_macro;
David Tolnayb1032662017-05-31 15:52:28 -070051extern crate unicode_xid;
Alex Crichton44bffbc2017-05-19 17:51:59 -070052
Alex Crichtonf3888432018-05-16 09:11:05 -070053use std::cmp::Ordering;
Alex Crichton44bffbc2017-05-19 17:51:59 -070054use std::fmt;
Alex Crichtonf3888432018-05-16 09:11:05 -070055use std::hash::{Hash, Hasher};
Alex Crichton44bffbc2017-05-19 17:51:59 -070056use std::iter::FromIterator;
Alex Crichtonaf5bad42018-03-27 14:45:10 -070057use std::marker;
58use std::rc::Rc;
59use std::str::FromStr;
Alex Crichton44bffbc2017-05-19 17:51:59 -070060
David Tolnayb1032662017-05-31 15:52:28 -070061#[macro_use]
David Tolnayb1032662017-05-31 15:52:28 -070062mod strnom;
Alex Crichton30a4e9e2018-04-27 17:02:19 -070063mod stable;
David Tolnayb1032662017-05-31 15:52:28 -070064
David Tolnayd66ecf62018-01-02 20:05:42 -080065#[cfg(not(feature = "nightly"))]
Alex Crichton30a4e9e2018-04-27 17:02:19 -070066use stable as imp;
Alex Crichtonb15c6352017-05-19 19:36:36 -070067#[path = "unstable.rs"]
David Tolnayd66ecf62018-01-02 20:05:42 -080068#[cfg(feature = "nightly")]
Alex Crichton44bffbc2017-05-19 17:51:59 -070069mod imp;
70
David Tolnay82ba02d2018-05-20 16:22:43 -070071/// An abstract stream of tokens, or more concretely a sequence of token trees.
72///
73/// This type provides interfaces for iterating over token trees and for
74/// collecting token trees into one stream.
75///
76/// Token stream is both the input and output of `#[proc_macro]`,
77/// `#[proc_macro_attribute]` and `#[proc_macro_derive]` definitions.
David Tolnaycb1b85f2017-06-03 16:40:35 -070078#[derive(Clone)]
Alex Crichtonaf5bad42018-03-27 14:45:10 -070079pub struct TokenStream {
80 inner: imp::TokenStream,
81 _marker: marker::PhantomData<Rc<()>>,
82}
Alex Crichton44bffbc2017-05-19 17:51:59 -070083
David Tolnay82ba02d2018-05-20 16:22:43 -070084/// Error returned from `TokenStream::from_str`.
Alex Crichtonaf5bad42018-03-27 14:45:10 -070085pub struct LexError {
86 inner: imp::LexError,
87 _marker: marker::PhantomData<Rc<()>>,
88}
89
90impl TokenStream {
91 fn _new(inner: imp::TokenStream) -> TokenStream {
92 TokenStream {
93 inner: inner,
94 _marker: marker::PhantomData,
95 }
96 }
97
Alex Crichton30a4e9e2018-04-27 17:02:19 -070098 fn _new_stable(inner: stable::TokenStream) -> TokenStream {
99 TokenStream {
100 inner: inner.into(),
101 _marker: marker::PhantomData,
102 }
103 }
104
David Tolnay82ba02d2018-05-20 16:22:43 -0700105 /// Returns an empty `TokenStream` containing no token trees.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700106 pub fn empty() -> TokenStream {
107 TokenStream::_new(imp::TokenStream::empty())
108 }
109
David Tolnay82ba02d2018-05-20 16:22:43 -0700110 /// Checks if this `TokenStream` is empty.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700111 pub fn is_empty(&self) -> bool {
112 self.inner.is_empty()
113 }
114}
Alex Crichton44bffbc2017-05-19 17:51:59 -0700115
David Tolnay82ba02d2018-05-20 16:22:43 -0700116/// Attempts to break the string into tokens and parse those tokens into a token
117/// stream.
118///
119/// May fail for a number of reasons, for example, if the string contains
120/// unbalanced delimiters or characters not existing in the language.
121///
122/// NOTE: Some errors may cause panics instead of returning `LexError`. We
123/// reserve the right to change these errors into `LexError`s later.
Alex Crichton44bffbc2017-05-19 17:51:59 -0700124impl FromStr for TokenStream {
125 type Err = LexError;
126
127 fn from_str(src: &str) -> Result<TokenStream, LexError> {
David Tolnayb28f38a2018-03-31 22:02:29 +0200128 let e = src.parse().map_err(|e| LexError {
129 inner: e,
130 _marker: marker::PhantomData,
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700131 })?;
132 Ok(TokenStream::_new(e))
Alex Crichton44bffbc2017-05-19 17:51:59 -0700133 }
134}
135
Alex Crichton0e8e7f42018-02-22 06:15:13 -0800136#[cfg(feature = "proc-macro")]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700137impl From<proc_macro::TokenStream> for TokenStream {
138 fn from(inner: proc_macro::TokenStream) -> TokenStream {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700139 TokenStream::_new(inner.into())
Alex Crichton44bffbc2017-05-19 17:51:59 -0700140 }
141}
142
Alex Crichton0e8e7f42018-02-22 06:15:13 -0800143#[cfg(feature = "proc-macro")]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700144impl From<TokenStream> for proc_macro::TokenStream {
145 fn from(inner: TokenStream) -> proc_macro::TokenStream {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700146 inner.inner.into()
Alex Crichton44bffbc2017-05-19 17:51:59 -0700147 }
148}
149
Alex Crichtonf3888432018-05-16 09:11:05 -0700150impl Extend<TokenTree> for TokenStream {
151 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
152 self.inner.extend(streams)
153 }
154}
155
David Tolnay82ba02d2018-05-20 16:22:43 -0700156/// Collects a number of token trees into a single stream.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700157impl FromIterator<TokenTree> for TokenStream {
158 fn from_iter<I: IntoIterator<Item = TokenTree>>(streams: I) -> Self {
159 TokenStream::_new(streams.into_iter().collect())
Alex Crichton44bffbc2017-05-19 17:51:59 -0700160 }
161}
162
David Tolnay82ba02d2018-05-20 16:22:43 -0700163/// Prints the token stream as a string that is supposed to be losslessly
164/// convertible back into the same token stream (modulo spans), except for
165/// possibly `TokenTree::Group`s with `Delimiter::None` delimiters and negative
166/// numeric literals.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700167impl fmt::Display for TokenStream {
168 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
169 self.inner.fmt(f)
Alex Crichton44bffbc2017-05-19 17:51:59 -0700170 }
171}
172
David Tolnay82ba02d2018-05-20 16:22:43 -0700173/// Prints token in a form convenient for debugging.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700174impl fmt::Debug for TokenStream {
175 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
176 self.inner.fmt(f)
Alex Crichton44bffbc2017-05-19 17:51:59 -0700177 }
178}
179
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700180impl fmt::Debug for LexError {
181 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
182 self.inner.fmt(f)
Alex Crichton44bffbc2017-05-19 17:51:59 -0700183 }
184}
185
Nika Layzellb35a9a32017-12-30 14:34:35 -0500186// Returned by reference, so we can't easily wrap it.
David Tolnay1ebe3972018-01-02 20:14:20 -0800187#[cfg(procmacro2_semver_exempt)]
Nika Layzellb35a9a32017-12-30 14:34:35 -0500188pub use imp::FileName;
189
David Tolnay82ba02d2018-05-20 16:22:43 -0700190/// The source file of a given `Span`.
David Tolnay1ebe3972018-01-02 20:14:20 -0800191#[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500192#[derive(Clone, PartialEq, Eq)]
193pub struct SourceFile(imp::SourceFile);
194
David Tolnay1ebe3972018-01-02 20:14:20 -0800195#[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500196impl SourceFile {
David Tolnay82ba02d2018-05-20 16:22:43 -0700197 /// Get the path to this source file.
198 ///
199 /// ### Note
200 ///
201 /// If the code span associated with this `SourceFile` was generated by an
202 /// external macro, this may not be an actual path on the filesystem. Use
203 /// [`is_real`] to check.
204 ///
205 /// Also note that even if `is_real` returns `true`, if
206 /// `--remap-path-prefix` was passed on the command line, the path as given
207 /// may not actually be valid.
208 ///
209 /// [`is_real`]: #method.is_real
Nika Layzellb35a9a32017-12-30 14:34:35 -0500210 pub fn path(&self) -> &FileName {
211 self.0.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500212 }
213
David Tolnay82ba02d2018-05-20 16:22:43 -0700214 /// Returns `true` if this source file is a real source file, and not
215 /// generated by an external macro's expansion.
Nika Layzellf8d5f212017-12-11 14:07:02 -0500216 pub fn is_real(&self) -> bool {
217 self.0.is_real()
218 }
219}
220
David Tolnay1ebe3972018-01-02 20:14:20 -0800221#[cfg(procmacro2_semver_exempt)]
Nika Layzellb35a9a32017-12-30 14:34:35 -0500222impl AsRef<FileName> for SourceFile {
223 fn as_ref(&self) -> &FileName {
224 self.0.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500225 }
226}
227
David Tolnay1ebe3972018-01-02 20:14:20 -0800228#[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500229impl fmt::Debug for SourceFile {
230 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
231 self.0.fmt(f)
232 }
233}
234
David Tolnay82ba02d2018-05-20 16:22:43 -0700235/// A line-column pair representing the start or end of a `Span`.
David Tolnay1ebe3972018-01-02 20:14:20 -0800236#[cfg(procmacro2_semver_exempt)]
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500237pub struct LineColumn {
David Tolnay82ba02d2018-05-20 16:22:43 -0700238 /// The 1-indexed line in the source file on which the span starts or ends
239 /// (inclusive).
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500240 pub line: usize,
David Tolnay82ba02d2018-05-20 16:22:43 -0700241 /// The 0-indexed column (in UTF-8 characters) in the source file on which
242 /// the span starts or ends (inclusive).
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500243 pub column: usize,
244}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500245
David Tolnay82ba02d2018-05-20 16:22:43 -0700246/// A region of source code, along with macro expansion information.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700247#[derive(Copy, Clone)]
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700248pub struct Span {
249 inner: imp::Span,
250 _marker: marker::PhantomData<Rc<()>>,
251}
Alex Crichton44bffbc2017-05-19 17:51:59 -0700252
Alex Crichton44bffbc2017-05-19 17:51:59 -0700253impl Span {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700254 fn _new(inner: imp::Span) -> Span {
255 Span {
256 inner: inner,
257 _marker: marker::PhantomData,
258 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700259 }
Alex Crichtone6085b72017-11-21 07:24:25 -0800260
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700261 fn _new_stable(inner: stable::Span) -> Span {
262 Span {
263 inner: inner.into(),
264 _marker: marker::PhantomData,
265 }
266 }
267
David Tolnay82ba02d2018-05-20 16:22:43 -0700268 /// The span of the invocation of the current procedural macro.
269 ///
270 /// Identifiers created with this span will be resolved as if they were
271 /// written directly at the macro call location (call-site hygiene) and
272 /// other code at the macro call site will be able to refer to them as well.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700273 pub fn call_site() -> Span {
274 Span::_new(imp::Span::call_site())
275 }
276
David Tolnay82ba02d2018-05-20 16:22:43 -0700277 /// A span that resolves at the macro definition site.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700278 #[cfg(procmacro2_semver_exempt)]
Alex Crichtone6085b72017-11-21 07:24:25 -0800279 pub fn def_site() -> Span {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700280 Span::_new(imp::Span::def_site())
Alex Crichtone6085b72017-11-21 07:24:25 -0800281 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500282
David Tolnay4e8e3972018-01-05 18:10:22 -0800283 /// Creates a new span with the same line/column information as `self` but
284 /// that resolves symbols as though it were at `other`.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700285 #[cfg(procmacro2_semver_exempt)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800286 pub fn resolved_at(&self, other: Span) -> Span {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700287 Span::_new(self.inner.resolved_at(other.inner))
David Tolnay4e8e3972018-01-05 18:10:22 -0800288 }
289
290 /// Creates a new span with the same name resolution behavior as `self` but
291 /// with the line/column information of `other`.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700292 #[cfg(procmacro2_semver_exempt)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800293 pub fn located_at(&self, other: Span) -> Span {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700294 Span::_new(self.inner.located_at(other.inner))
David Tolnay4e8e3972018-01-05 18:10:22 -0800295 }
296
David Tolnayd66ecf62018-01-02 20:05:42 -0800297 /// This method is only available when the `"nightly"` feature is enabled.
Alex Crichton0e8e7f42018-02-22 06:15:13 -0800298 #[cfg(all(feature = "nightly", feature = "proc-macro"))]
David Tolnay16a17202017-12-31 10:47:24 -0500299 pub fn unstable(self) -> proc_macro::Span {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700300 self.inner.unstable()
David Tolnay16a17202017-12-31 10:47:24 -0500301 }
302
David Tolnay82ba02d2018-05-20 16:22:43 -0700303 /// The original source file into which this span points.
David Tolnay1ebe3972018-01-02 20:14:20 -0800304 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500305 pub fn source_file(&self) -> SourceFile {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700306 SourceFile(self.inner.source_file())
Nika Layzellf8d5f212017-12-11 14:07:02 -0500307 }
308
David Tolnay82ba02d2018-05-20 16:22:43 -0700309 /// Get the starting line/column in the source file for this span.
David Tolnay1ebe3972018-01-02 20:14:20 -0800310 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500311 pub fn start(&self) -> LineColumn {
David Tolnayb28f38a2018-03-31 22:02:29 +0200312 let imp::LineColumn { line, column } = self.inner.start();
313 LineColumn {
314 line: line,
315 column: column,
316 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500317 }
318
David Tolnay82ba02d2018-05-20 16:22:43 -0700319 /// Get the ending line/column in the source file for this span.
David Tolnay1ebe3972018-01-02 20:14:20 -0800320 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500321 pub fn end(&self) -> LineColumn {
David Tolnayb28f38a2018-03-31 22:02:29 +0200322 let imp::LineColumn { line, column } = self.inner.end();
323 LineColumn {
324 line: line,
325 column: column,
326 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500327 }
328
David Tolnay82ba02d2018-05-20 16:22:43 -0700329 /// Create a new span encompassing `self` and `other`.
330 ///
331 /// Returns `None` if `self` and `other` are from different files.
David Tolnay1ebe3972018-01-02 20:14:20 -0800332 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500333 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700334 self.inner.join(other.inner).map(Span::_new)
335 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700336
David Tolnay82ba02d2018-05-20 16:22:43 -0700337 /// Compares to spans to see if they're equal.
Alex Crichtonb2c94622018-04-04 07:36:41 -0700338 #[cfg(procmacro2_semver_exempt)]
339 pub fn eq(&self, other: &Span) -> bool {
340 self.inner.eq(&other.inner)
341 }
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700342}
343
David Tolnay82ba02d2018-05-20 16:22:43 -0700344/// Prints a span in a form convenient for debugging.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700345impl fmt::Debug for Span {
346 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
347 self.inner.fmt(f)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500348 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700349}
350
David Tolnay82ba02d2018-05-20 16:22:43 -0700351/// A single token or a delimited sequence of token trees (e.g. `[1, (), ..]`).
David Tolnay034205f2018-04-22 16:45:28 -0700352#[derive(Clone)]
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700353pub enum TokenTree {
David Tolnay82ba02d2018-05-20 16:22:43 -0700354 /// A token stream surrounded by bracket delimiters.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700355 Group(Group),
David Tolnay82ba02d2018-05-20 16:22:43 -0700356 /// An identifier.
Alex Crichtonf3888432018-05-16 09:11:05 -0700357 Ident(Ident),
David Tolnay82ba02d2018-05-20 16:22:43 -0700358 /// A single punctuation character (`+`, `,`, `$`, etc.).
Alex Crichtonf3888432018-05-16 09:11:05 -0700359 Punct(Punct),
David Tolnay82ba02d2018-05-20 16:22:43 -0700360 /// A literal character (`'a'`), string (`"hello"`), number (`2.3`), etc.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700361 Literal(Literal),
Alex Crichton1a7f7622017-07-05 17:47:15 -0700362}
363
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700364impl TokenTree {
David Tolnay82ba02d2018-05-20 16:22:43 -0700365 /// Returns the span of this tree, delegating to the `span` method of
366 /// the contained token or a delimited stream.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700367 pub fn span(&self) -> Span {
368 match *self {
369 TokenTree::Group(ref t) => t.span(),
Alex Crichtonf3888432018-05-16 09:11:05 -0700370 TokenTree::Ident(ref t) => t.span(),
371 TokenTree::Punct(ref t) => t.span(),
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700372 TokenTree::Literal(ref t) => t.span(),
373 }
374 }
375
David Tolnay82ba02d2018-05-20 16:22:43 -0700376 /// Configures the span for *only this token*.
377 ///
378 /// Note that if this token is a `Group` then this method will not configure
379 /// the span of each of the internal tokens, this will simply delegate to
380 /// the `set_span` method of each variant.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700381 pub fn set_span(&mut self, span: Span) {
382 match *self {
383 TokenTree::Group(ref mut t) => t.set_span(span),
Alex Crichtonf3888432018-05-16 09:11:05 -0700384 TokenTree::Ident(ref mut t) => t.set_span(span),
385 TokenTree::Punct(ref mut t) => t.set_span(span),
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700386 TokenTree::Literal(ref mut t) => t.set_span(span),
387 }
388 }
389}
390
391impl From<Group> for TokenTree {
392 fn from(g: Group) -> TokenTree {
393 TokenTree::Group(g)
394 }
395}
396
Alex Crichtonf3888432018-05-16 09:11:05 -0700397impl From<Ident> for TokenTree {
398 fn from(g: Ident) -> TokenTree {
399 TokenTree::Ident(g)
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700400 }
401}
402
Alex Crichtonf3888432018-05-16 09:11:05 -0700403impl From<Punct> for TokenTree {
404 fn from(g: Punct) -> TokenTree {
405 TokenTree::Punct(g)
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700406 }
407}
408
409impl From<Literal> for TokenTree {
410 fn from(g: Literal) -> TokenTree {
411 TokenTree::Literal(g)
Alex Crichton1a7f7622017-07-05 17:47:15 -0700412 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700413}
414
David Tolnay82ba02d2018-05-20 16:22:43 -0700415/// Prints the token tree as a string that is supposed to be losslessly
416/// convertible back into the same token tree (modulo spans), except for
417/// possibly `TokenTree::Group`s with `Delimiter::None` delimiters and negative
418/// numeric literals.
Alex Crichton44bffbc2017-05-19 17:51:59 -0700419impl fmt::Display for TokenTree {
420 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700421 match *self {
422 TokenTree::Group(ref t) => t.fmt(f),
Alex Crichtonf3888432018-05-16 09:11:05 -0700423 TokenTree::Ident(ref t) => t.fmt(f),
424 TokenTree::Punct(ref t) => t.fmt(f),
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700425 TokenTree::Literal(ref t) => t.fmt(f),
426 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700427 }
428}
429
David Tolnay82ba02d2018-05-20 16:22:43 -0700430/// Prints token tree in a form convenient for debugging.
David Tolnay034205f2018-04-22 16:45:28 -0700431impl fmt::Debug for TokenTree {
432 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
433 // Each of these has the name in the struct type in the derived debug,
434 // so don't bother with an extra layer of indirection
435 match *self {
436 TokenTree::Group(ref t) => t.fmt(f),
Alex Crichtonf3888432018-05-16 09:11:05 -0700437 TokenTree::Ident(ref t) => t.fmt(f),
438 TokenTree::Punct(ref t) => t.fmt(f),
David Tolnay034205f2018-04-22 16:45:28 -0700439 TokenTree::Literal(ref t) => t.fmt(f),
440 }
441 }
442}
443
David Tolnay82ba02d2018-05-20 16:22:43 -0700444/// A delimited token stream.
445///
446/// A `Group` internally contains a `TokenStream` which is surrounded by
447/// `Delimiter`s.
David Tolnay034205f2018-04-22 16:45:28 -0700448#[derive(Clone)]
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700449pub struct Group {
450 delimiter: Delimiter,
451 stream: TokenStream,
452 span: Span,
Alex Crichton44bffbc2017-05-19 17:51:59 -0700453}
454
David Tolnay82ba02d2018-05-20 16:22:43 -0700455/// Describes how a sequence of token trees is delimited.
Michael Layzell5372f4b2017-06-02 10:29:31 -0400456#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700457pub enum Delimiter {
David Tolnay82ba02d2018-05-20 16:22:43 -0700458 /// `( ... )`
Alex Crichton44bffbc2017-05-19 17:51:59 -0700459 Parenthesis,
David Tolnay82ba02d2018-05-20 16:22:43 -0700460 /// `{ ... }`
Alex Crichton44bffbc2017-05-19 17:51:59 -0700461 Brace,
David Tolnay82ba02d2018-05-20 16:22:43 -0700462 /// `[ ... ]`
Alex Crichton44bffbc2017-05-19 17:51:59 -0700463 Bracket,
David Tolnay82ba02d2018-05-20 16:22:43 -0700464 /// `Ø ... Ø`
465 ///
466 /// An implicit delimiter, that may, for example, appear around tokens
467 /// coming from a "macro variable" `$var`. It is important to preserve
468 /// operator priorities in cases like `$var * 3` where `$var` is `1 + 2`.
469 /// Implicit delimiters may not survive roundtrip of a token stream through
470 /// a string.
Alex Crichton44bffbc2017-05-19 17:51:59 -0700471 None,
472}
473
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700474impl Group {
David Tolnay82ba02d2018-05-20 16:22:43 -0700475 /// Creates a new `Group` with the given delimiter and token stream.
476 ///
477 /// This constructor will set the span for this group to
478 /// `Span::call_site()`. To change the span you can use the `set_span`
479 /// method below.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700480 pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
481 Group {
482 delimiter: delimiter,
483 stream: stream,
484 span: Span::call_site(),
485 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700486 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700487
David Tolnay82ba02d2018-05-20 16:22:43 -0700488 /// Returns the delimiter of this `Group`
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700489 pub fn delimiter(&self) -> Delimiter {
490 self.delimiter
Alex Crichton44bffbc2017-05-19 17:51:59 -0700491 }
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700492
David Tolnay82ba02d2018-05-20 16:22:43 -0700493 /// Returns the `TokenStream` of tokens that are delimited in this `Group`.
494 ///
495 /// Note that the returned token stream does not include the delimiter
496 /// returned above.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700497 pub fn stream(&self) -> TokenStream {
498 self.stream.clone()
499 }
500
David Tolnay82ba02d2018-05-20 16:22:43 -0700501 /// Returns the span for the delimiters of this token stream, spanning the
502 /// entire `Group`.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700503 pub fn span(&self) -> Span {
504 self.span
505 }
506
David Tolnay82ba02d2018-05-20 16:22:43 -0700507 /// Configures the span for this `Group`'s delimiters, but not its internal
508 /// tokens.
509 ///
510 /// This method will **not** set the span of all the internal tokens spanned
511 /// by this group, but rather it will only set the span of the delimiter
512 /// tokens at the level of the `Group`.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700513 pub fn set_span(&mut self, span: Span) {
514 self.span = span;
515 }
516}
517
David Tolnay82ba02d2018-05-20 16:22:43 -0700518/// Prints the group as a string that should be losslessly convertible back
519/// into the same group (modulo spans), except for possibly `TokenTree::Group`s
520/// with `Delimiter::None` delimiters.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700521impl fmt::Display for Group {
522 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
523 self.stream.fmt(f)
524 }
525}
526
David Tolnay034205f2018-04-22 16:45:28 -0700527impl fmt::Debug for Group {
528 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
529 let mut debug = fmt.debug_struct("Group");
530 debug.field("delimiter", &self.delimiter);
531 debug.field("stream", &self.stream);
532 #[cfg(procmacro2_semver_exempt)]
533 debug.field("span", &self.span);
534 debug.finish()
535 }
536}
537
David Tolnay82ba02d2018-05-20 16:22:43 -0700538/// An `Punct` is an single punctuation character like `+`, `-` or `#`.
539///
540/// Multicharacter operators like `+=` are represented as two instances of
541/// `Punct` with different forms of `Spacing` returned.
Alex Crichtonf3888432018-05-16 09:11:05 -0700542#[derive(Clone)]
543pub struct Punct {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700544 op: char,
545 spacing: Spacing,
546 span: Span,
Alex Crichton44bffbc2017-05-19 17:51:59 -0700547}
548
David Tolnay82ba02d2018-05-20 16:22:43 -0700549/// Whether an `Punct` is followed immediately by another `Punct` or followed by
550/// another token or whitespace.
Lukas Kalbertodteb3f9302017-08-20 18:58:41 +0200551#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Alex Crichton1a7f7622017-07-05 17:47:15 -0700552pub enum Spacing {
David Tolnay82ba02d2018-05-20 16:22:43 -0700553 /// E.g. `+` is `Alone` in `+ =`, `+ident` or `+()`.
Alex Crichton44bffbc2017-05-19 17:51:59 -0700554 Alone,
David Tolnay82ba02d2018-05-20 16:22:43 -0700555 /// E.g. `+` is `Joint` in `+=` or `'#`.
556 ///
557 /// Additionally, single quote `'` can join with identifiers to form
558 /// lifetimes `'ident`.
Alex Crichton44bffbc2017-05-19 17:51:59 -0700559 Joint,
560}
561
Alex Crichtonf3888432018-05-16 09:11:05 -0700562impl Punct {
David Tolnay82ba02d2018-05-20 16:22:43 -0700563 /// Creates a new `Punct` from the given character and spacing.
564 ///
565 /// The `ch` argument must be a valid punctuation character permitted by the
566 /// language, otherwise the function will panic.
567 ///
568 /// The returned `Punct` will have the default span of `Span::call_site()`
569 /// which can be further configured with the `set_span` method below.
Alex Crichtonf3888432018-05-16 09:11:05 -0700570 pub fn new(op: char, spacing: Spacing) -> Punct {
571 Punct {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700572 op: op,
573 spacing: spacing,
574 span: Span::call_site(),
575 }
576 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700577
David Tolnay82ba02d2018-05-20 16:22:43 -0700578 /// Returns the value of this punctuation character as `char`.
Alex Crichtonf3888432018-05-16 09:11:05 -0700579 pub fn as_char(&self) -> char {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700580 self.op
581 }
582
David Tolnay82ba02d2018-05-20 16:22:43 -0700583 /// Returns the spacing of this punctuation character, indicating whether
584 /// it's immediately followed by another `Punct` in the token stream, so
585 /// they can potentially be combined into a multicharacter operator
586 /// (`Joint`), or it's followed by some other token or whitespace (`Alone`)
587 /// so the operator has certainly ended.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700588 pub fn spacing(&self) -> Spacing {
589 self.spacing
590 }
591
David Tolnay82ba02d2018-05-20 16:22:43 -0700592 /// Returns the span for this punctuation character.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700593 pub fn span(&self) -> Span {
594 self.span
595 }
596
David Tolnay82ba02d2018-05-20 16:22:43 -0700597 /// Configure the span for this punctuation character.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700598 pub fn set_span(&mut self, span: Span) {
599 self.span = span;
600 }
601}
602
David Tolnay82ba02d2018-05-20 16:22:43 -0700603/// Prints the punctuation character as a string that should be losslessly
604/// convertible back into the same character.
Alex Crichtonf3888432018-05-16 09:11:05 -0700605impl fmt::Display for Punct {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700606 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
607 self.op.fmt(f)
608 }
609}
610
Alex Crichtonf3888432018-05-16 09:11:05 -0700611impl fmt::Debug for Punct {
David Tolnay034205f2018-04-22 16:45:28 -0700612 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonf3888432018-05-16 09:11:05 -0700613 let mut debug = fmt.debug_struct("Punct");
David Tolnay034205f2018-04-22 16:45:28 -0700614 debug.field("op", &self.op);
615 debug.field("spacing", &self.spacing);
616 #[cfg(procmacro2_semver_exempt)]
617 debug.field("span", &self.span);
618 debug.finish()
619 }
620}
621
David Tolnay82ba02d2018-05-20 16:22:43 -0700622/// An identifier (`ident`).
Alex Crichtonf3888432018-05-16 09:11:05 -0700623#[derive(Clone)]
624pub struct Ident {
625 inner: imp::Ident,
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700626 _marker: marker::PhantomData<Rc<()>>,
627}
628
Alex Crichtonf3888432018-05-16 09:11:05 -0700629impl Ident {
630 fn _new(inner: imp::Ident) -> Ident {
631 Ident {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700632 inner: inner,
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700633 _marker: marker::PhantomData,
634 }
635 }
636
David Tolnay82ba02d2018-05-20 16:22:43 -0700637 /// Creates a new `Ident` with the given `string` as well as the specified
638 /// `span`.
639 ///
640 /// The `string` argument must be a valid identifier permitted by the
641 /// language, otherwise the function will panic.
642 ///
643 /// Note that `span`, currently in rustc, configures the hygiene information
644 /// for this identifier.
645 ///
646 /// As of this time `Span::call_site()` explicitly opts-in to "call-site"
647 /// hygiene meaning that identifiers created with this span will be resolved
648 /// as if they were written directly at the location of the macro call, and
649 /// other code at the macro call site will be able to refer to them as well.
650 ///
651 /// Later spans like `Span::def_site()` will allow to opt-in to
652 /// "definition-site" hygiene meaning that identifiers created with this
653 /// span will be resolved at the location of the macro definition and other
654 /// code at the macro call site will not be able to refer to them.
655 ///
656 /// Due to the current importance of hygiene this constructor, unlike other
657 /// tokens, requires a `Span` to be specified at construction.
Alex Crichtonf3888432018-05-16 09:11:05 -0700658 pub fn new(string: &str, span: Span) -> Ident {
659 Ident::_new(imp::Ident::new(string, span.inner))
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700660 }
661
David Tolnay82ba02d2018-05-20 16:22:43 -0700662 /// Same as `Ident::new`, but creates a raw identifier (`r#ident`).
Alex Crichtonf3888432018-05-16 09:11:05 -0700663 #[cfg(procmacro2_semver_exempt)]
664 pub fn new_raw(string: &str, span: Span) -> Ident {
665 Ident::_new_raw(string, span)
666 }
667
668 fn _new_raw(string: &str, span: Span) -> Ident {
669 Ident::_new(imp::Ident::new_raw(string, span.inner))
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700670 }
671
David Tolnay82ba02d2018-05-20 16:22:43 -0700672 /// Returns the span of this `Ident`.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700673 pub fn span(&self) -> Span {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700674 Span::_new(self.inner.span())
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700675 }
676
David Tolnay82ba02d2018-05-20 16:22:43 -0700677 /// Configures the span of this `Ident`, possibly changing its hygiene
678 /// context.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700679 pub fn set_span(&mut self, span: Span) {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700680 self.inner.set_span(span.inner);
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700681 }
682}
683
Alex Crichtonf3888432018-05-16 09:11:05 -0700684impl PartialEq for Ident {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700685 fn eq(&self, other: &Ident) -> bool {
Alex Crichtonf3888432018-05-16 09:11:05 -0700686 self.to_string() == other.to_string()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700687 }
688}
689
David Tolnayc0bbcc52018-05-18 10:51:04 -0700690impl<T> PartialEq<T> for Ident
691where
692 T: ?Sized + AsRef<str>,
693{
694 fn eq(&self, other: &T) -> bool {
695 self.to_string() == other.as_ref()
696 }
697}
698
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700699impl Eq for Ident {}
Alex Crichtonf3888432018-05-16 09:11:05 -0700700
701impl PartialOrd for Ident {
702 fn partial_cmp(&self, other: &Ident) -> Option<Ordering> {
703 Some(self.cmp(other))
704 }
705}
706
707impl Ord for Ident {
708 fn cmp(&self, other: &Ident) -> Ordering {
709 self.to_string().cmp(&other.to_string())
710 }
711}
712
713impl Hash for Ident {
714 fn hash<H: Hasher>(&self, hasher: &mut H) {
715 self.to_string().hash(hasher)
716 }
717}
718
David Tolnay82ba02d2018-05-20 16:22:43 -0700719/// Prints the identifier as a string that should be losslessly convertible back
720/// into the same identifier.
Alex Crichtonf3888432018-05-16 09:11:05 -0700721impl fmt::Display for Ident {
722 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
723 self.inner.fmt(f)
724 }
725}
726
727impl fmt::Debug for Ident {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700728 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
729 self.inner.fmt(f)
730 }
731}
732
David Tolnay82ba02d2018-05-20 16:22:43 -0700733/// A literal string (`"hello"`), byte string (`b"hello"`), character (`'a'`),
734/// byte character (`b'a'`), an integer or floating point number with or without
735/// a suffix (`1`, `1u8`, `2.3`, `2.3f32`).
736///
737/// Boolean literals like `true` and `false` do not belong here, they are
738/// `Ident`s.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700739#[derive(Clone)]
740pub struct Literal {
741 inner: imp::Literal,
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700742 _marker: marker::PhantomData<Rc<()>>,
743}
744
David Tolnay82ba02d2018-05-20 16:22:43 -0700745macro_rules! suffixed_int_literals {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700746 ($($name:ident => $kind:ident,)*) => ($(
David Tolnay82ba02d2018-05-20 16:22:43 -0700747 /// Creates a new suffixed integer literal with the specified value.
748 ///
749 /// This function will create an integer like `1u32` where the integer
750 /// value specified is the first part of the token and the integral is
751 /// also suffixed at the end. Literals created from negative numbers may
752 /// not survive rountrips through `TokenStream` or strings and may be
753 /// broken into two tokens (`-` and positive literal).
754 ///
755 /// Literals created through this method have the `Span::call_site()`
756 /// span by default, which can be configured with the `set_span` method
757 /// below.
758 pub fn $name(n: $kind) -> Literal {
759 Literal::_new(imp::Literal::$name(n))
760 }
761 )*)
762}
763
764macro_rules! unsuffixed_int_literals {
765 ($($name:ident => $kind:ident,)*) => ($(
766 /// Creates a new unsuffixed integer literal with the specified value.
767 ///
768 /// This function will create an integer like `1` where the integer
769 /// value specified is the first part of the token. No suffix is
770 /// specified on this token, meaning that invocations like
771 /// `Literal::i8_unsuffixed(1)` are equivalent to
772 /// `Literal::u32_unsuffixed(1)`. Literals created from negative numbers
773 /// may not survive rountrips through `TokenStream` or strings and may
774 /// be broken into two tokens (`-` and positive literal).
775 ///
776 /// Literals created through this method have the `Span::call_site()`
777 /// span by default, which can be configured with the `set_span` method
778 /// below.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700779 pub fn $name(n: $kind) -> Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700780 Literal::_new(imp::Literal::$name(n))
Alex Crichton1a7f7622017-07-05 17:47:15 -0700781 }
782 )*)
783}
784
Alex Crichton852d53d2017-05-19 19:25:08 -0700785impl Literal {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700786 fn _new(inner: imp::Literal) -> Literal {
787 Literal {
788 inner: inner,
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700789 _marker: marker::PhantomData,
790 }
Alex Crichton1a7f7622017-07-05 17:47:15 -0700791 }
792
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700793 fn _new_stable(inner: stable::Literal) -> Literal {
794 Literal {
795 inner: inner.into(),
796 _marker: marker::PhantomData,
797 }
798 }
799
David Tolnay82ba02d2018-05-20 16:22:43 -0700800 suffixed_int_literals! {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700801 u8_suffixed => u8,
802 u16_suffixed => u16,
803 u32_suffixed => u32,
804 u64_suffixed => u64,
805 usize_suffixed => usize,
806 i8_suffixed => i8,
807 i16_suffixed => i16,
808 i32_suffixed => i32,
809 i64_suffixed => i64,
810 isize_suffixed => isize,
David Tolnay82ba02d2018-05-20 16:22:43 -0700811 }
Alex Crichton1a7f7622017-07-05 17:47:15 -0700812
David Tolnay82ba02d2018-05-20 16:22:43 -0700813 unsuffixed_int_literals! {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700814 u8_unsuffixed => u8,
815 u16_unsuffixed => u16,
816 u32_unsuffixed => u32,
817 u64_unsuffixed => u64,
818 usize_unsuffixed => usize,
819 i8_unsuffixed => i8,
820 i16_unsuffixed => i16,
821 i32_unsuffixed => i32,
822 i64_unsuffixed => i64,
823 isize_unsuffixed => isize,
Alex Crichton1a7f7622017-07-05 17:47:15 -0700824 }
825
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700826 pub fn f64_unsuffixed(f: f64) -> Literal {
827 assert!(f.is_finite());
Alex Crichtona914a612018-04-04 07:48:44 -0700828 Literal::_new(imp::Literal::f64_unsuffixed(f))
Alex Crichton1a7f7622017-07-05 17:47:15 -0700829 }
830
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700831 pub fn f64_suffixed(f: f64) -> Literal {
832 assert!(f.is_finite());
Alex Crichtona914a612018-04-04 07:48:44 -0700833 Literal::_new(imp::Literal::f64_suffixed(f))
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700834 }
835
David Tolnay82ba02d2018-05-20 16:22:43 -0700836 /// Creates a new unsuffixed floating-point literal.
837 ///
838 /// This constructor is similar to those like `Literal::i8_unsuffixed` where
839 /// the float's value is emitted directly into the token but no suffix is
840 /// used, so it may be inferred to be a `f64` later in the compiler.
841 /// Literals created from negative numbers may not survive rountrips through
842 /// `TokenStream` or strings and may be broken into two tokens (`-` and
843 /// positive literal).
844 ///
845 /// # Panics
846 ///
847 /// This function requires that the specified float is finite, for example
848 /// if it is infinity or NaN this function will panic.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700849 pub fn f32_unsuffixed(f: f32) -> Literal {
850 assert!(f.is_finite());
Alex Crichtona914a612018-04-04 07:48:44 -0700851 Literal::_new(imp::Literal::f32_unsuffixed(f))
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700852 }
853
854 pub fn f32_suffixed(f: f32) -> Literal {
855 assert!(f.is_finite());
Alex Crichtona914a612018-04-04 07:48:44 -0700856 Literal::_new(imp::Literal::f32_suffixed(f))
Alex Crichton1a7f7622017-07-05 17:47:15 -0700857 }
858
859 pub fn string(string: &str) -> Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700860 Literal::_new(imp::Literal::string(string))
Alex Crichton1a7f7622017-07-05 17:47:15 -0700861 }
862
863 pub fn character(ch: char) -> Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700864 Literal::_new(imp::Literal::character(ch))
Alex Crichton76a5cc82017-05-23 07:01:44 -0700865 }
866
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700867 pub fn byte_string(s: &[u8]) -> Literal {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700868 Literal::_new(imp::Literal::byte_string(s))
Alex Crichton852d53d2017-05-19 19:25:08 -0700869 }
Alex Crichton76a5cc82017-05-23 07:01:44 -0700870
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700871 pub fn span(&self) -> Span {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700872 Span::_new(self.inner.span())
Alex Crichton1a7f7622017-07-05 17:47:15 -0700873 }
874
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700875 pub fn set_span(&mut self, span: Span) {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700876 self.inner.set_span(span.inner);
Alex Crichton31316622017-05-26 12:54:47 -0700877 }
Alex Crichton852d53d2017-05-19 19:25:08 -0700878}
879
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700880impl fmt::Debug for Literal {
881 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
882 self.inner.fmt(f)
Alex Crichton44bffbc2017-05-19 17:51:59 -0700883 }
884}
David Tolnaycb1b85f2017-06-03 16:40:35 -0700885
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700886impl fmt::Display for Literal {
887 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
888 self.inner.fmt(f)
889 }
890}
891
David Tolnay82ba02d2018-05-20 16:22:43 -0700892/// Public implementation details for the `TokenStream` type, such as iterators.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700893pub mod token_stream {
894 use std::fmt;
895 use std::marker;
896 use std::rc::Rc;
897
David Tolnay48ea5042018-04-23 19:17:35 -0700898 use imp;
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700899 pub use TokenStream;
David Tolnayb28f38a2018-03-31 22:02:29 +0200900 use TokenTree;
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700901
David Tolnay82ba02d2018-05-20 16:22:43 -0700902 /// An iterator over `TokenStream`'s `TokenTree`s.
903 ///
904 /// The iteration is "shallow", e.g. the iterator doesn't recurse into
905 /// delimited groups, and returns whole groups as token trees.
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700906 pub struct IntoIter {
907 inner: imp::TokenTreeIter,
908 _marker: marker::PhantomData<Rc<()>>,
909 }
910
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700911 impl Iterator for IntoIter {
912 type Item = TokenTree;
913
914 fn next(&mut self) -> Option<TokenTree> {
915 self.inner.next()
916 }
917 }
918
919 impl fmt::Debug for IntoIter {
920 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
921 self.inner.fmt(f)
922 }
923 }
924
925 impl IntoIterator for TokenStream {
926 type Item = TokenTree;
927 type IntoIter = IntoIter;
928
929 fn into_iter(self) -> IntoIter {
930 IntoIter {
931 inner: self.inner.into_iter(),
932 _marker: marker::PhantomData,
933 }
934 }
935 }
936}