1 //! A wrapper around the procedural macro API of the compiler's [`proc_macro`]
2 //! crate. This library serves two purposes:
4 //! [`proc_macro`]: https://doc.rust-lang.org/proc_macro/
6 //! - **Bring proc-macro-like functionality to other contexts like build.rs and
7 //! main.rs.** Types from `proc_macro` are entirely specific to procedural
8 //! macros and cannot ever exist in code outside of a procedural macro.
9 //! Meanwhile `proc_macro2` types may exist anywhere including non-macro code.
10 //! By developing foundational libraries like [syn] and [quote] against
11 //! `proc_macro2` rather than `proc_macro`, the procedural macro ecosystem
12 //! becomes easily applicable to many other use cases and we avoid
13 //! reimplementing non-macro equivalents of those libraries.
15 //! - **Make procedural macros unit testable.** As a consequence of being
16 //! specific to procedural macros, nothing that uses `proc_macro` can be
17 //! executed from a unit test. In order for helper libraries or components of
18 //! a macro to be testable in isolation, they must be implemented using
21 //! [syn]: https://github.com/dtolnay/syn
22 //! [quote]: https://github.com/dtolnay/quote
26 //! The skeleton of a typical procedural macro typically looks like this:
29 //! extern crate proc_macro;
31 //! # const IGNORE: &str = stringify! {
32 //! #[proc_macro_derive(MyDerive)]
34 //! # #[cfg(wrap_proc_macro)]
35 //! pub fn my_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
36 //! let input = proc_macro2::TokenStream::from(input);
38 //! let output: proc_macro2::TokenStream = {
39 //! /* transform input */
43 //! proc_macro::TokenStream::from(output)
47 //! If parsing with [Syn], you'll use [`parse_macro_input!`] instead to
48 //! propagate parse errors correctly back to the compiler when parsing fails.
50 //! [`parse_macro_input!`]: https://docs.rs/syn/1.0/syn/macro.parse_macro_input.html
52 //! # Unstable features
54 //! The default feature set of proc-macro2 tracks the most recent stable
55 //! compiler API. Functionality in `proc_macro` that is not yet stable is not
56 //! exposed by proc-macro2 by default.
58 //! To opt into the additional APIs available in the most recent nightly
59 //! compiler, the `procmacro2_semver_exempt` config flag must be passed to
60 //! rustc. We will polyfill those nightly-only APIs back to Rust 1.31.0. As
61 //! these are unstable APIs that track the nightly compiler, minor versions of
62 //! proc-macro2 may make breaking changes to them at any time.
65 //! RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo build
68 //! Note that this must not only be done for your crate, but for any crate that
69 //! depends on your crate. This infectious nature is intentional, as it serves
70 //! as a reminder that you are outside of the normal semver guarantees.
72 //! Semver exempt methods are marked as such in the proc-macro2 documentation.
76 //! Most types in this crate are `!Sync` because the underlying compiler
77 //! types make use of thread-local memory, meaning they cannot be accessed from
78 //! a different thread.
80 // Proc-macro2 types in rustdoc of other crates get linked to here.
81 #![doc(html_root_url = "https://docs.rs/proc-macro2/1.0.28")]
82 #![cfg_attr(any(proc_macro_span, super_unstable), feature(proc_macro_span))]
83 #![cfg_attr(super_unstable, feature(proc_macro_raw_ident, proc_macro_def_site))]
84 #![cfg_attr(doc_cfg, feature(doc_cfg))]
85 #![allow(clippy::needless_doctest_main, clippy::vec_init_then_push)]
87 #[cfg(use_proc_macro)]
88 extern crate proc_macro
;
93 #[cfg(wrap_proc_macro)]
96 // Public for proc_macro2::fallback::force() and unforce(), but those are quite
97 // a niche use case so we omit it from rustdoc.
101 #[cfg(not(wrap_proc_macro))]
102 use crate::fallback
as imp
;
103 #[path = "wrapper.rs"]
104 #[cfg(wrap_proc_macro)]
107 use crate::marker
::Marker
;
108 use std
::cmp
::Ordering
;
109 use std
::error
::Error
;
110 use std
::fmt
::{self, Debug, Display}
;
111 use std
::hash
::{Hash, Hasher}
;
112 use std
::iter
::FromIterator
;
113 use std
::ops
::RangeBounds
;
114 #[cfg(procmacro2_semver_exempt)]
115 use std
::path
::PathBuf
;
116 use std
::str::FromStr
;
118 /// An abstract stream of tokens, or more concretely a sequence of token trees.
120 /// This type provides interfaces for iterating over token trees and for
121 /// collecting token trees into one stream.
123 /// Token stream is both the input and output of `#[proc_macro]`,
124 /// `#[proc_macro_attribute]` and `#[proc_macro_derive]` definitions.
126 pub struct TokenStream
{
127 inner
: imp
::TokenStream
,
131 /// Error returned from `TokenStream::from_str`.
132 pub struct LexError
{
133 inner
: imp
::LexError
,
138 fn _new(inner
: imp
::TokenStream
) -> TokenStream
{
145 fn _new_stable(inner
: fallback
::TokenStream
) -> TokenStream
{
152 /// Returns an empty `TokenStream` containing no token trees.
153 pub fn new() -> TokenStream
{
154 TokenStream
::_new(imp
::TokenStream
::new())
157 /// Checks if this `TokenStream` is empty.
158 pub fn is_empty(&self) -> bool
{
159 self.inner
.is_empty()
163 /// `TokenStream::default()` returns an empty stream,
164 /// i.e. this is equivalent with `TokenStream::new()`.
165 impl Default
for TokenStream
{
166 fn default() -> Self {
171 /// Attempts to break the string into tokens and parse those tokens into a token
174 /// May fail for a number of reasons, for example, if the string contains
175 /// unbalanced delimiters or characters not existing in the language.
177 /// NOTE: Some errors may cause panics instead of returning `LexError`. We
178 /// reserve the right to change these errors into `LexError`s later.
179 impl FromStr
for TokenStream
{
182 fn from_str(src
: &str) -> Result
<TokenStream
, LexError
> {
183 let e
= src
.parse().map_err(|e
| LexError
{
187 Ok(TokenStream
::_new(e
))
191 #[cfg(use_proc_macro)]
192 impl From
<proc_macro
::TokenStream
> for TokenStream
{
193 fn from(inner
: proc_macro
::TokenStream
) -> TokenStream
{
194 TokenStream
::_new(inner
.into())
198 #[cfg(use_proc_macro)]
199 impl From
<TokenStream
> for proc_macro
::TokenStream
{
200 fn from(inner
: TokenStream
) -> proc_macro
::TokenStream
{
205 impl From
<TokenTree
> for TokenStream
{
206 fn from(token
: TokenTree
) -> Self {
207 TokenStream
::_new(imp
::TokenStream
::from(token
))
211 impl Extend
<TokenTree
> for TokenStream
{
212 fn extend
<I
: IntoIterator
<Item
= TokenTree
>>(&mut self, streams
: I
) {
213 self.inner
.extend(streams
)
217 impl Extend
<TokenStream
> for TokenStream
{
218 fn extend
<I
: IntoIterator
<Item
= TokenStream
>>(&mut self, streams
: I
) {
220 .extend(streams
.into_iter().map(|stream
| stream
.inner
))
224 /// Collects a number of token trees into a single stream.
225 impl FromIterator
<TokenTree
> for TokenStream
{
226 fn from_iter
<I
: IntoIterator
<Item
= TokenTree
>>(streams
: I
) -> Self {
227 TokenStream
::_new(streams
.into_iter().collect())
230 impl FromIterator
<TokenStream
> for TokenStream
{
231 fn from_iter
<I
: IntoIterator
<Item
= TokenStream
>>(streams
: I
) -> Self {
232 TokenStream
::_new(streams
.into_iter().map(|i
| i
.inner
).collect())
236 /// Prints the token stream as a string that is supposed to be losslessly
237 /// convertible back into the same token stream (modulo spans), except for
238 /// possibly `TokenTree::Group`s with `Delimiter::None` delimiters and negative
239 /// numeric literals.
240 impl Display
for TokenStream
{
241 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
242 Display
::fmt(&self.inner
, f
)
246 /// Prints token in a form convenient for debugging.
247 impl Debug
for TokenStream
{
248 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
249 Debug
::fmt(&self.inner
, f
)
254 pub fn span(&self) -> Span
{
255 Span
::_new(self.inner
.span())
259 impl Debug
for LexError
{
260 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
261 Debug
::fmt(&self.inner
, f
)
265 impl Display
for LexError
{
266 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
267 Display
::fmt(&self.inner
, f
)
271 impl Error
for LexError {}
273 /// The source file of a given `Span`.
275 /// This type is semver exempt and not exposed by default.
276 #[cfg(procmacro2_semver_exempt)]
277 #[cfg_attr(doc_cfg, doc(cfg(procmacro2_semver_exempt)))]
278 #[derive(Clone, PartialEq, Eq)]
279 pub struct SourceFile
{
280 inner
: imp
::SourceFile
,
284 #[cfg(procmacro2_semver_exempt)]
286 fn _new(inner
: imp
::SourceFile
) -> Self {
293 /// Get the path to this source file.
297 /// If the code span associated with this `SourceFile` was generated by an
298 /// external macro, this may not be an actual path on the filesystem. Use
299 /// [`is_real`] to check.
301 /// Also note that even if `is_real` returns `true`, if
302 /// `--remap-path-prefix` was passed on the command line, the path as given
303 /// may not actually be valid.
305 /// [`is_real`]: #method.is_real
306 pub fn path(&self) -> PathBuf
{
310 /// Returns `true` if this source file is a real source file, and not
311 /// generated by an external macro's expansion.
312 pub fn is_real(&self) -> bool
{
317 #[cfg(procmacro2_semver_exempt)]
318 impl Debug
for SourceFile
{
319 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
320 Debug
::fmt(&self.inner
, f
)
324 /// A line-column pair representing the start or end of a `Span`.
326 /// This type is semver exempt and not exposed by default.
327 #[cfg(span_locations)]
328 #[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
329 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
330 pub struct LineColumn
{
331 /// The 1-indexed line in the source file on which the span starts or ends
334 /// The 0-indexed column (in UTF-8 characters) in the source file on which
335 /// the span starts or ends (inclusive).
339 #[cfg(span_locations)]
340 impl Ord
for LineColumn
{
341 fn cmp(&self, other
: &Self) -> Ordering
{
344 .then(self.column
.cmp(&other
.column
))
348 #[cfg(span_locations)]
349 impl PartialOrd
for LineColumn
{
350 fn partial_cmp(&self, other
: &Self) -> Option
<Ordering
> {
351 Some(self.cmp(other
))
355 /// A region of source code, along with macro expansion information.
356 #[derive(Copy, Clone)]
363 fn _new(inner
: imp
::Span
) -> Span
{
370 fn _new_stable(inner
: fallback
::Span
) -> Span
{
377 /// The span of the invocation of the current procedural macro.
379 /// Identifiers created with this span will be resolved as if they were
380 /// written directly at the macro call location (call-site hygiene) and
381 /// other code at the macro call site will be able to refer to them as well.
382 pub fn call_site() -> Span
{
383 Span
::_new(imp
::Span
::call_site())
386 /// The span located at the invocation of the procedural macro, but with
387 /// local variables, labels, and `$crate` resolved at the definition site
388 /// of the macro. This is the same hygiene behavior as `macro_rules`.
390 /// This function requires Rust 1.45 or later.
392 pub fn mixed_site() -> Span
{
393 Span
::_new(imp
::Span
::mixed_site())
396 /// A span that resolves at the macro definition site.
398 /// This method is semver exempt and not exposed by default.
399 #[cfg(procmacro2_semver_exempt)]
400 #[cfg_attr(doc_cfg, doc(cfg(procmacro2_semver_exempt)))]
401 pub fn def_site() -> Span
{
402 Span
::_new(imp
::Span
::def_site())
405 /// Creates a new span with the same line/column information as `self` but
406 /// that resolves symbols as though it were at `other`.
407 pub fn resolved_at(&self, other
: Span
) -> Span
{
408 Span
::_new(self.inner
.resolved_at(other
.inner
))
411 /// Creates a new span with the same name resolution behavior as `self` but
412 /// with the line/column information of `other`.
413 pub fn located_at(&self, other
: Span
) -> Span
{
414 Span
::_new(self.inner
.located_at(other
.inner
))
417 /// Convert `proc_macro2::Span` to `proc_macro::Span`.
419 /// This method is available when building with a nightly compiler, or when
420 /// building with rustc 1.29+ *without* semver exempt features.
424 /// Panics if called from outside of a procedural macro. Unlike
425 /// `proc_macro2::Span`, the `proc_macro::Span` type can only exist within
426 /// the context of a procedural macro invocation.
427 #[cfg(wrap_proc_macro)]
428 pub fn unwrap(self) -> proc_macro
::Span
{
432 // Soft deprecated. Please use Span::unwrap.
433 #[cfg(wrap_proc_macro)]
435 pub fn unstable(self) -> proc_macro
::Span
{
439 /// The original source file into which this span points.
441 /// This method is semver exempt and not exposed by default.
442 #[cfg(procmacro2_semver_exempt)]
443 #[cfg_attr(doc_cfg, doc(cfg(procmacro2_semver_exempt)))]
444 pub fn source_file(&self) -> SourceFile
{
445 SourceFile
::_new(self.inner
.source_file())
448 /// Get the starting line/column in the source file for this span.
450 /// This method requires the `"span-locations"` feature to be enabled.
452 /// When executing in a procedural macro context, the returned line/column
453 /// are only meaningful if compiled with a nightly toolchain. The stable
454 /// toolchain does not have this information available. When executing
455 /// outside of a procedural macro, such as main.rs or build.rs, the
456 /// line/column are always meaningful regardless of toolchain.
457 #[cfg(span_locations)]
458 #[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
459 pub fn start(&self) -> LineColumn
{
460 let imp
::LineColumn { line, column }
= self.inner
.start();
461 LineColumn { line, column }
464 /// Get the ending line/column in the source file for this span.
466 /// This method requires the `"span-locations"` feature to be enabled.
468 /// When executing in a procedural macro context, the returned line/column
469 /// are only meaningful if compiled with a nightly toolchain. The stable
470 /// toolchain does not have this information available. When executing
471 /// outside of a procedural macro, such as main.rs or build.rs, the
472 /// line/column are always meaningful regardless of toolchain.
473 #[cfg(span_locations)]
474 #[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
475 pub fn end(&self) -> LineColumn
{
476 let imp
::LineColumn { line, column }
= self.inner
.end();
477 LineColumn { line, column }
480 /// Create a new span encompassing `self` and `other`.
482 /// Returns `None` if `self` and `other` are from different files.
484 /// Warning: the underlying [`proc_macro::Span::join`] method is
485 /// nightly-only. When called from within a procedural macro not using a
486 /// nightly compiler, this method will always return `None`.
488 /// [`proc_macro::Span::join`]: https://doc.rust-lang.org/proc_macro/struct.Span.html#method.join
489 pub fn join(&self, other
: Span
) -> Option
<Span
> {
490 self.inner
.join(other
.inner
).map(Span
::_new
)
493 /// Compares two spans to see if they're equal.
495 /// This method is semver exempt and not exposed by default.
496 #[cfg(procmacro2_semver_exempt)]
497 #[cfg_attr(doc_cfg, doc(cfg(procmacro2_semver_exempt)))]
498 pub fn eq(&self, other
: &Span
) -> bool
{
499 self.inner
.eq(&other
.inner
)
503 /// Prints a span in a form convenient for debugging.
504 impl Debug
for Span
{
505 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
506 Debug
::fmt(&self.inner
, f
)
510 /// A single token or a delimited sequence of token trees (e.g. `[1, (), ..]`).
513 /// A token stream surrounded by bracket delimiters.
517 /// A single punctuation character (`+`, `,`, `$`, etc.).
519 /// A literal character (`'a'`), string (`"hello"`), number (`2.3`), etc.
524 /// Returns the span of this tree, delegating to the `span` method of
525 /// the contained token or a delimited stream.
526 pub fn span(&self) -> Span
{
528 TokenTree
::Group(t
) => t
.span(),
529 TokenTree
::Ident(t
) => t
.span(),
530 TokenTree
::Punct(t
) => t
.span(),
531 TokenTree
::Literal(t
) => t
.span(),
535 /// Configures the span for *only this token*.
537 /// Note that if this token is a `Group` then this method will not configure
538 /// the span of each of the internal tokens, this will simply delegate to
539 /// the `set_span` method of each variant.
540 pub fn set_span(&mut self, span
: Span
) {
542 TokenTree
::Group(t
) => t
.set_span(span
),
543 TokenTree
::Ident(t
) => t
.set_span(span
),
544 TokenTree
::Punct(t
) => t
.set_span(span
),
545 TokenTree
::Literal(t
) => t
.set_span(span
),
550 impl From
<Group
> for TokenTree
{
551 fn from(g
: Group
) -> TokenTree
{
556 impl From
<Ident
> for TokenTree
{
557 fn from(g
: Ident
) -> TokenTree
{
562 impl From
<Punct
> for TokenTree
{
563 fn from(g
: Punct
) -> TokenTree
{
568 impl From
<Literal
> for TokenTree
{
569 fn from(g
: Literal
) -> TokenTree
{
570 TokenTree
::Literal(g
)
574 /// Prints the token tree as a string that is supposed to be losslessly
575 /// convertible back into the same token tree (modulo spans), except for
576 /// possibly `TokenTree::Group`s with `Delimiter::None` delimiters and negative
577 /// numeric literals.
578 impl Display
for TokenTree
{
579 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
581 TokenTree
::Group(t
) => Display
::fmt(t
, f
),
582 TokenTree
::Ident(t
) => Display
::fmt(t
, f
),
583 TokenTree
::Punct(t
) => Display
::fmt(t
, f
),
584 TokenTree
::Literal(t
) => Display
::fmt(t
, f
),
589 /// Prints token tree in a form convenient for debugging.
590 impl Debug
for TokenTree
{
591 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
592 // Each of these has the name in the struct type in the derived debug,
593 // so don't bother with an extra layer of indirection
595 TokenTree
::Group(t
) => Debug
::fmt(t
, f
),
596 TokenTree
::Ident(t
) => {
597 let mut debug
= f
.debug_struct("Ident");
598 debug
.field("sym", &format_args
!("{}", t
));
599 imp
::debug_span_field_if_nontrivial(&mut debug
, t
.span().inner
);
602 TokenTree
::Punct(t
) => Debug
::fmt(t
, f
),
603 TokenTree
::Literal(t
) => Debug
::fmt(t
, f
),
608 /// A delimited token stream.
610 /// A `Group` internally contains a `TokenStream` which is surrounded by
617 /// Describes how a sequence of token trees is delimited.
618 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
628 /// An implicit delimiter, that may, for example, appear around tokens
629 /// coming from a "macro variable" `$var`. It is important to preserve
630 /// operator priorities in cases like `$var * 3` where `$var` is `1 + 2`.
631 /// Implicit delimiters may not survive roundtrip of a token stream through
637 fn _new(inner
: imp
::Group
) -> Self {
641 fn _new_stable(inner
: fallback
::Group
) -> Self {
647 /// Creates a new `Group` with the given delimiter and token stream.
649 /// This constructor will set the span for this group to
650 /// `Span::call_site()`. To change the span you can use the `set_span`
652 pub fn new(delimiter
: Delimiter
, stream
: TokenStream
) -> Group
{
654 inner
: imp
::Group
::new(delimiter
, stream
.inner
),
658 /// Returns the delimiter of this `Group`
659 pub fn delimiter(&self) -> Delimiter
{
660 self.inner
.delimiter()
663 /// Returns the `TokenStream` of tokens that are delimited in this `Group`.
665 /// Note that the returned token stream does not include the delimiter
667 pub fn stream(&self) -> TokenStream
{
668 TokenStream
::_new(self.inner
.stream())
671 /// Returns the span for the delimiters of this token stream, spanning the
675 /// pub fn span(&self) -> Span {
678 pub fn span(&self) -> Span
{
679 Span
::_new(self.inner
.span())
682 /// Returns the span pointing to the opening delimiter of this group.
685 /// pub fn span_open(&self) -> Span {
688 pub fn span_open(&self) -> Span
{
689 Span
::_new(self.inner
.span_open())
692 /// Returns the span pointing to the closing delimiter of this group.
695 /// pub fn span_close(&self) -> Span {
698 pub fn span_close(&self) -> Span
{
699 Span
::_new(self.inner
.span_close())
702 /// Configures the span for this `Group`'s delimiters, but not its internal
705 /// This method will **not** set the span of all the internal tokens spanned
706 /// by this group, but rather it will only set the span of the delimiter
707 /// tokens at the level of the `Group`.
708 pub fn set_span(&mut self, span
: Span
) {
709 self.inner
.set_span(span
.inner
)
713 /// Prints the group as a string that should be losslessly convertible back
714 /// into the same group (modulo spans), except for possibly `TokenTree::Group`s
715 /// with `Delimiter::None` delimiters.
716 impl Display
for Group
{
717 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
718 Display
::fmt(&self.inner
, formatter
)
722 impl Debug
for Group
{
723 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
724 Debug
::fmt(&self.inner
, formatter
)
728 /// A `Punct` is a single punctuation character like `+`, `-` or `#`.
730 /// Multicharacter operators like `+=` are represented as two instances of
731 /// `Punct` with different forms of `Spacing` returned.
739 /// Whether a `Punct` is followed immediately by another `Punct` or followed by
740 /// another token or whitespace.
741 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
743 /// E.g. `+` is `Alone` in `+ =`, `+ident` or `+()`.
745 /// E.g. `+` is `Joint` in `+=` or `'` is `Joint` in `'#`.
747 /// Additionally, single quote `'` can join with identifiers to form
748 /// lifetimes `'ident`.
753 /// Creates a new `Punct` from the given character and spacing.
755 /// The `ch` argument must be a valid punctuation character permitted by the
756 /// language, otherwise the function will panic.
758 /// The returned `Punct` will have the default span of `Span::call_site()`
759 /// which can be further configured with the `set_span` method below.
760 pub fn new(ch
: char, spacing
: Spacing
) -> Punct
{
764 span
: Span
::call_site(),
768 /// Returns the value of this punctuation character as `char`.
769 pub fn as_char(&self) -> char {
773 /// Returns the spacing of this punctuation character, indicating whether
774 /// it's immediately followed by another `Punct` in the token stream, so
775 /// they can potentially be combined into a multicharacter operator
776 /// (`Joint`), or it's followed by some other token or whitespace (`Alone`)
777 /// so the operator has certainly ended.
778 pub fn spacing(&self) -> Spacing
{
782 /// Returns the span for this punctuation character.
783 pub fn span(&self) -> Span
{
787 /// Configure the span for this punctuation character.
788 pub fn set_span(&mut self, span
: Span
) {
793 /// Prints the punctuation character as a string that should be losslessly
794 /// convertible back into the same character.
795 impl Display
for Punct
{
796 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
797 Display
::fmt(&self.ch
, f
)
801 impl Debug
for Punct
{
802 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
803 let mut debug
= fmt
.debug_struct("Punct");
804 debug
.field("char", &self.ch
);
805 debug
.field("spacing", &self.spacing
);
806 imp
::debug_span_field_if_nontrivial(&mut debug
, self.span
.inner
);
811 /// A word of Rust code, which may be a keyword or legal variable name.
813 /// An identifier consists of at least one Unicode code point, the first of
814 /// which has the XID_Start property and the rest of which have the XID_Continue
817 /// - The empty string is not an identifier. Use `Option<Ident>`.
818 /// - A lifetime is not an identifier. Use `syn::Lifetime` instead.
820 /// An identifier constructed with `Ident::new` is permitted to be a Rust
821 /// keyword, though parsing one through its [`Parse`] implementation rejects
822 /// Rust keywords. Use `input.call(Ident::parse_any)` when parsing to match the
823 /// behaviour of `Ident::new`.
825 /// [`Parse`]: https://docs.rs/syn/1.0/syn/parse/trait.Parse.html
829 /// A new ident can be created from a string using the `Ident::new` function.
830 /// A span must be provided explicitly which governs the name resolution
831 /// behavior of the resulting identifier.
834 /// use proc_macro2::{Ident, Span};
837 /// let call_ident = Ident::new("calligraphy", Span::call_site());
839 /// println!("{}", call_ident);
843 /// An ident can be interpolated into a token stream using the `quote!` macro.
846 /// use proc_macro2::{Ident, Span};
847 /// use quote::quote;
850 /// let ident = Ident::new("demo", Span::call_site());
852 /// // Create a variable binding whose name is this ident.
853 /// let expanded = quote! { let #ident = 10; };
855 /// // Create a variable binding with a slightly different name.
856 /// let temp_ident = Ident::new(&format!("new_{}", ident), Span::call_site());
857 /// let expanded = quote! { let #temp_ident = 10; };
861 /// A string representation of the ident is available through the `to_string()`
865 /// # use proc_macro2::{Ident, Span};
867 /// # let ident = Ident::new("another_identifier", Span::call_site());
869 /// // Examine the ident as a string.
870 /// let ident_string = ident.to_string();
871 /// if ident_string.len() > 60 {
872 /// println!("Very long identifier: {}", ident_string)
882 fn _new(inner
: imp
::Ident
) -> Ident
{
889 /// Creates a new `Ident` with the given `string` as well as the specified
892 /// The `string` argument must be a valid identifier permitted by the
893 /// language, otherwise the function will panic.
895 /// Note that `span`, currently in rustc, configures the hygiene information
896 /// for this identifier.
898 /// As of this time `Span::call_site()` explicitly opts-in to "call-site"
899 /// hygiene meaning that identifiers created with this span will be resolved
900 /// as if they were written directly at the location of the macro call, and
901 /// other code at the macro call site will be able to refer to them as well.
903 /// Later spans like `Span::def_site()` will allow to opt-in to
904 /// "definition-site" hygiene meaning that identifiers created with this
905 /// span will be resolved at the location of the macro definition and other
906 /// code at the macro call site will not be able to refer to them.
908 /// Due to the current importance of hygiene this constructor, unlike other
909 /// tokens, requires a `Span` to be specified at construction.
913 /// Panics if the input string is neither a keyword nor a legal variable
914 /// name. If you are not sure whether the string contains an identifier and
915 /// need to handle an error case, use
916 /// <a href="https://docs.rs/syn/1.0/syn/fn.parse_str.html"><code
917 /// style="padding-right:0;">syn::parse_str</code></a><code
918 /// style="padding-left:0;">::<Ident></code>
919 /// rather than `Ident::new`.
920 pub fn new(string
: &str, span
: Span
) -> Ident
{
921 Ident
::_new(imp
::Ident
::new(string
, span
.inner
))
924 /// Same as `Ident::new`, but creates a raw identifier (`r#ident`).
926 /// This method is semver exempt and not exposed by default.
927 #[cfg(procmacro2_semver_exempt)]
928 #[cfg_attr(doc_cfg, doc(cfg(procmacro2_semver_exempt)))]
929 pub fn new_raw(string
: &str, span
: Span
) -> Ident
{
930 Ident
::_new_raw(string
, span
)
933 fn _new_raw(string
: &str, span
: Span
) -> Ident
{
934 Ident
::_new(imp
::Ident
::new_raw(string
, span
.inner
))
937 /// Returns the span of this `Ident`.
938 pub fn span(&self) -> Span
{
939 Span
::_new(self.inner
.span())
942 /// Configures the span of this `Ident`, possibly changing its hygiene
944 pub fn set_span(&mut self, span
: Span
) {
945 self.inner
.set_span(span
.inner
);
949 impl PartialEq
for Ident
{
950 fn eq(&self, other
: &Ident
) -> bool
{
951 self.inner
== other
.inner
955 impl<T
> PartialEq
<T
> for Ident
957 T
: ?Sized
+ AsRef
<str>,
959 fn eq(&self, other
: &T
) -> bool
{
966 impl PartialOrd
for Ident
{
967 fn partial_cmp(&self, other
: &Ident
) -> Option
<Ordering
> {
968 Some(self.cmp(other
))
973 fn cmp(&self, other
: &Ident
) -> Ordering
{
974 self.to_string().cmp(&other
.to_string())
978 impl Hash
for Ident
{
979 fn hash
<H
: Hasher
>(&self, hasher
: &mut H
) {
980 self.to_string().hash(hasher
)
984 /// Prints the identifier as a string that should be losslessly convertible back
985 /// into the same identifier.
986 impl Display
for Ident
{
987 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
988 Display
::fmt(&self.inner
, f
)
992 impl Debug
for Ident
{
993 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
994 Debug
::fmt(&self.inner
, f
)
998 /// A literal string (`"hello"`), byte string (`b"hello"`), character (`'a'`),
999 /// byte character (`b'a'`), an integer or floating point number with or without
1000 /// a suffix (`1`, `1u8`, `2.3`, `2.3f32`).
1002 /// Boolean literals like `true` and `false` do not belong here, they are
1005 pub struct Literal
{
1006 inner
: imp
::Literal
,
1010 macro_rules
! suffixed_int_literals
{
1011 ($
($name
:ident
=> $kind
:ident
,)*) => ($
(
1012 /// Creates a new suffixed integer literal with the specified value.
1014 /// This function will create an integer like `1u32` where the integer
1015 /// value specified is the first part of the token and the integral is
1016 /// also suffixed at the end. Literals created from negative numbers may
1017 /// not survive roundtrips through `TokenStream` or strings and may be
1018 /// broken into two tokens (`-` and positive literal).
1020 /// Literals created through this method have the `Span::call_site()`
1021 /// span by default, which can be configured with the `set_span` method
1023 pub fn $
name(n
: $kind
) -> Literal
{
1024 Literal
::_new(imp
::Literal
::$
name(n
))
1029 macro_rules
! unsuffixed_int_literals
{
1030 ($
($name
:ident
=> $kind
:ident
,)*) => ($
(
1031 /// Creates a new unsuffixed integer literal with the specified value.
1033 /// This function will create an integer like `1` where the integer
1034 /// value specified is the first part of the token. No suffix is
1035 /// specified on this token, meaning that invocations like
1036 /// `Literal::i8_unsuffixed(1)` are equivalent to
1037 /// `Literal::u32_unsuffixed(1)`. Literals created from negative numbers
1038 /// may not survive roundtrips through `TokenStream` or strings and may
1039 /// be broken into two tokens (`-` and positive literal).
1041 /// Literals created through this method have the `Span::call_site()`
1042 /// span by default, which can be configured with the `set_span` method
1044 pub fn $
name(n
: $kind
) -> Literal
{
1045 Literal
::_new(imp
::Literal
::$
name(n
))
1051 fn _new(inner
: imp
::Literal
) -> Literal
{
1058 fn _new_stable(inner
: fallback
::Literal
) -> Literal
{
1060 inner
: inner
.into(),
1065 suffixed_int_literals
! {
1067 u16_suffixed
=> u16,
1068 u32_suffixed
=> u32,
1069 u64_suffixed
=> u64,
1070 u128_suffixed
=> u128
,
1071 usize_suffixed
=> usize,
1073 i16_suffixed
=> i16,
1074 i32_suffixed
=> i32,
1075 i64_suffixed
=> i64,
1076 i128_suffixed
=> i128
,
1077 isize_suffixed
=> isize,
1080 unsuffixed_int_literals
! {
1081 u8_unsuffixed
=> u8,
1082 u16_unsuffixed
=> u16,
1083 u32_unsuffixed
=> u32,
1084 u64_unsuffixed
=> u64,
1085 u128_unsuffixed
=> u128
,
1086 usize_unsuffixed
=> usize,
1087 i8_unsuffixed
=> i8,
1088 i16_unsuffixed
=> i16,
1089 i32_unsuffixed
=> i32,
1090 i64_unsuffixed
=> i64,
1091 i128_unsuffixed
=> i128
,
1092 isize_unsuffixed
=> isize,
1095 /// Creates a new unsuffixed floating-point literal.
1097 /// This constructor is similar to those like `Literal::i8_unsuffixed` where
1098 /// the float's value is emitted directly into the token but no suffix is
1099 /// used, so it may be inferred to be a `f64` later in the compiler.
1100 /// Literals created from negative numbers may not survive rountrips through
1101 /// `TokenStream` or strings and may be broken into two tokens (`-` and
1102 /// positive literal).
1106 /// This function requires that the specified float is finite, for example
1107 /// if it is infinity or NaN this function will panic.
1108 pub fn f64_unsuffixed(f
: f64) -> Literal
{
1109 assert
!(f
.is_finite());
1110 Literal
::_new(imp
::Literal
::f64_unsuffixed(f
))
1113 /// Creates a new suffixed floating-point literal.
1115 /// This constructor will create a literal like `1.0f64` where the value
1116 /// specified is the preceding part of the token and `f64` is the suffix of
1117 /// the token. This token will always be inferred to be an `f64` in the
1118 /// compiler. Literals created from negative numbers may not survive
1119 /// rountrips through `TokenStream` or strings and may be broken into two
1120 /// tokens (`-` and positive literal).
1124 /// This function requires that the specified float is finite, for example
1125 /// if it is infinity or NaN this function will panic.
1126 pub fn f64_suffixed(f
: f64) -> Literal
{
1127 assert
!(f
.is_finite());
1128 Literal
::_new(imp
::Literal
::f64_suffixed(f
))
1131 /// Creates a new unsuffixed floating-point literal.
1133 /// This constructor is similar to those like `Literal::i8_unsuffixed` where
1134 /// the float's value is emitted directly into the token but no suffix is
1135 /// used, so it may be inferred to be a `f64` later in the compiler.
1136 /// Literals created from negative numbers may not survive rountrips through
1137 /// `TokenStream` or strings and may be broken into two tokens (`-` and
1138 /// positive literal).
1142 /// This function requires that the specified float is finite, for example
1143 /// if it is infinity or NaN this function will panic.
1144 pub fn f32_unsuffixed(f
: f32) -> Literal
{
1145 assert
!(f
.is_finite());
1146 Literal
::_new(imp
::Literal
::f32_unsuffixed(f
))
1149 /// Creates a new suffixed floating-point literal.
1151 /// This constructor will create a literal like `1.0f32` where the value
1152 /// specified is the preceding part of the token and `f32` is the suffix of
1153 /// the token. This token will always be inferred to be an `f32` in the
1154 /// compiler. Literals created from negative numbers may not survive
1155 /// rountrips through `TokenStream` or strings and may be broken into two
1156 /// tokens (`-` and positive literal).
1160 /// This function requires that the specified float is finite, for example
1161 /// if it is infinity or NaN this function will panic.
1162 pub fn f32_suffixed(f
: f32) -> Literal
{
1163 assert
!(f
.is_finite());
1164 Literal
::_new(imp
::Literal
::f32_suffixed(f
))
1168 pub fn string(string
: &str) -> Literal
{
1169 Literal
::_new(imp
::Literal
::string(string
))
1172 /// Character literal.
1173 pub fn character(ch
: char) -> Literal
{
1174 Literal
::_new(imp
::Literal
::character(ch
))
1177 /// Byte string literal.
1178 pub fn byte_string(s
: &[u8]) -> Literal
{
1179 Literal
::_new(imp
::Literal
::byte_string(s
))
1182 /// Returns the span encompassing this literal.
1183 pub fn span(&self) -> Span
{
1184 Span
::_new(self.inner
.span())
1187 /// Configures the span associated for this literal.
1188 pub fn set_span(&mut self, span
: Span
) {
1189 self.inner
.set_span(span
.inner
);
1192 /// Returns a `Span` that is a subset of `self.span()` containing only
1193 /// the source bytes in range `range`. Returns `None` if the would-be
1194 /// trimmed span is outside the bounds of `self`.
1196 /// Warning: the underlying [`proc_macro::Literal::subspan`] method is
1197 /// nightly-only. When called from within a procedural macro not using a
1198 /// nightly compiler, this method will always return `None`.
1200 /// [`proc_macro::Literal::subspan`]: https://doc.rust-lang.org/proc_macro/struct.Literal.html#method.subspan
1201 pub fn subspan
<R
: RangeBounds
<usize>>(&self, range
: R
) -> Option
<Span
> {
1202 self.inner
.subspan(range
).map(Span
::_new
)
1206 impl FromStr
for Literal
{
1207 type Err
= LexError
;
1209 fn from_str(repr
: &str) -> Result
<Self, LexError
> {
1210 repr
.parse().map(Literal
::_new
).map_err(|inner
| LexError
{
1217 impl Debug
for Literal
{
1218 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1219 Debug
::fmt(&self.inner
, f
)
1223 impl Display
for Literal
{
1224 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1225 Display
::fmt(&self.inner
, f
)
1229 /// Public implementation details for the `TokenStream` type, such as iterators.
1230 pub mod token_stream
{
1231 use crate::marker
::Marker
;
1232 use crate::{imp, TokenTree}
;
1233 use std
::fmt
::{self, Debug}
;
1235 pub use crate::TokenStream
;
1237 /// An iterator over `TokenStream`'s `TokenTree`s.
1239 /// The iteration is "shallow", e.g. the iterator doesn't recurse into
1240 /// delimited groups, and returns whole groups as token trees.
1242 pub struct IntoIter
{
1243 inner
: imp
::TokenTreeIter
,
1247 impl Iterator
for IntoIter
{
1248 type Item
= TokenTree
;
1250 fn next(&mut self) -> Option
<TokenTree
> {
1255 impl Debug
for IntoIter
{
1256 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1257 Debug
::fmt(&self.inner
, f
)
1261 impl IntoIterator
for TokenStream
{
1262 type Item
= TokenTree
;
1263 type IntoIter
= IntoIter
;
1265 fn into_iter(self) -> IntoIter
{
1267 inner
: self.inner
.into_iter(),