17ac06127Sopenharmony_ci//! [![github]](https://github.com/dtolnay/proc-macro2) [![crates-io]](https://crates.io/crates/proc-macro2) [![docs-rs]](crate)
27ac06127Sopenharmony_ci//!
37ac06127Sopenharmony_ci//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
47ac06127Sopenharmony_ci//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
57ac06127Sopenharmony_ci//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
67ac06127Sopenharmony_ci//!
77ac06127Sopenharmony_ci//! <br>
87ac06127Sopenharmony_ci//!
97ac06127Sopenharmony_ci//! A wrapper around the procedural macro API of the compiler's [`proc_macro`]
107ac06127Sopenharmony_ci//! crate. This library serves two purposes:
117ac06127Sopenharmony_ci//!
127ac06127Sopenharmony_ci//! [`proc_macro`]: https://doc.rust-lang.org/proc_macro/
137ac06127Sopenharmony_ci//!
147ac06127Sopenharmony_ci//! - **Bring proc-macro-like functionality to other contexts like build.rs and
157ac06127Sopenharmony_ci//!   main.rs.** Types from `proc_macro` are entirely specific to procedural
167ac06127Sopenharmony_ci//!   macros and cannot ever exist in code outside of a procedural macro.
177ac06127Sopenharmony_ci//!   Meanwhile `proc_macro2` types may exist anywhere including non-macro code.
187ac06127Sopenharmony_ci//!   By developing foundational libraries like [syn] and [quote] against
197ac06127Sopenharmony_ci//!   `proc_macro2` rather than `proc_macro`, the procedural macro ecosystem
207ac06127Sopenharmony_ci//!   becomes easily applicable to many other use cases and we avoid
217ac06127Sopenharmony_ci//!   reimplementing non-macro equivalents of those libraries.
227ac06127Sopenharmony_ci//!
237ac06127Sopenharmony_ci//! - **Make procedural macros unit testable.** As a consequence of being
247ac06127Sopenharmony_ci//!   specific to procedural macros, nothing that uses `proc_macro` can be
257ac06127Sopenharmony_ci//!   executed from a unit test. In order for helper libraries or components of
267ac06127Sopenharmony_ci//!   a macro to be testable in isolation, they must be implemented using
277ac06127Sopenharmony_ci//!   `proc_macro2`.
287ac06127Sopenharmony_ci//!
297ac06127Sopenharmony_ci//! [syn]: https://github.com/dtolnay/syn
307ac06127Sopenharmony_ci//! [quote]: https://github.com/dtolnay/quote
317ac06127Sopenharmony_ci//!
327ac06127Sopenharmony_ci//! # Usage
337ac06127Sopenharmony_ci//!
347ac06127Sopenharmony_ci//! The skeleton of a typical procedural macro typically looks like this:
357ac06127Sopenharmony_ci//!
367ac06127Sopenharmony_ci//! ```
377ac06127Sopenharmony_ci//! extern crate proc_macro;
387ac06127Sopenharmony_ci//!
397ac06127Sopenharmony_ci//! # const IGNORE: &str = stringify! {
407ac06127Sopenharmony_ci//! #[proc_macro_derive(MyDerive)]
417ac06127Sopenharmony_ci//! # };
427ac06127Sopenharmony_ci//! # #[cfg(wrap_proc_macro)]
437ac06127Sopenharmony_ci//! pub fn my_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
447ac06127Sopenharmony_ci//!     let input = proc_macro2::TokenStream::from(input);
457ac06127Sopenharmony_ci//!
467ac06127Sopenharmony_ci//!     let output: proc_macro2::TokenStream = {
477ac06127Sopenharmony_ci//!         /* transform input */
487ac06127Sopenharmony_ci//!         # input
497ac06127Sopenharmony_ci//!     };
507ac06127Sopenharmony_ci//!
517ac06127Sopenharmony_ci//!     proc_macro::TokenStream::from(output)
527ac06127Sopenharmony_ci//! }
537ac06127Sopenharmony_ci//! ```
547ac06127Sopenharmony_ci//!
557ac06127Sopenharmony_ci//! If parsing with [Syn], you'll use [`parse_macro_input!`] instead to
567ac06127Sopenharmony_ci//! propagate parse errors correctly back to the compiler when parsing fails.
577ac06127Sopenharmony_ci//!
587ac06127Sopenharmony_ci//! [`parse_macro_input!`]: https://docs.rs/syn/2.0/syn/macro.parse_macro_input.html
597ac06127Sopenharmony_ci//!
607ac06127Sopenharmony_ci//! # Unstable features
617ac06127Sopenharmony_ci//!
627ac06127Sopenharmony_ci//! The default feature set of proc-macro2 tracks the most recent stable
637ac06127Sopenharmony_ci//! compiler API. Functionality in `proc_macro` that is not yet stable is not
647ac06127Sopenharmony_ci//! exposed by proc-macro2 by default.
657ac06127Sopenharmony_ci//!
667ac06127Sopenharmony_ci//! To opt into the additional APIs available in the most recent nightly
677ac06127Sopenharmony_ci//! compiler, the `procmacro2_semver_exempt` config flag must be passed to
687ac06127Sopenharmony_ci//! rustc. We will polyfill those nightly-only APIs back to Rust 1.56.0. As
697ac06127Sopenharmony_ci//! these are unstable APIs that track the nightly compiler, minor versions of
707ac06127Sopenharmony_ci//! proc-macro2 may make breaking changes to them at any time.
717ac06127Sopenharmony_ci//!
727ac06127Sopenharmony_ci//! ```sh
737ac06127Sopenharmony_ci//! RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo build
747ac06127Sopenharmony_ci//! ```
757ac06127Sopenharmony_ci//!
767ac06127Sopenharmony_ci//! Note that this must not only be done for your crate, but for any crate that
777ac06127Sopenharmony_ci//! depends on your crate. This infectious nature is intentional, as it serves
787ac06127Sopenharmony_ci//! as a reminder that you are outside of the normal semver guarantees.
797ac06127Sopenharmony_ci//!
807ac06127Sopenharmony_ci//! Semver exempt methods are marked as such in the proc-macro2 documentation.
817ac06127Sopenharmony_ci//!
827ac06127Sopenharmony_ci//! # Thread-Safety
837ac06127Sopenharmony_ci//!
847ac06127Sopenharmony_ci//! Most types in this crate are `!Sync` because the underlying compiler
857ac06127Sopenharmony_ci//! types make use of thread-local memory, meaning they cannot be accessed from
867ac06127Sopenharmony_ci//! a different thread.
877ac06127Sopenharmony_ci
887ac06127Sopenharmony_ci// Proc-macro2 types in rustdoc of other crates get linked to here.
897ac06127Sopenharmony_ci#![doc(html_root_url = "https://docs.rs/proc-macro2/1.0.76")]
907ac06127Sopenharmony_ci#![cfg_attr(any(proc_macro_span, super_unstable), feature(proc_macro_span))]
917ac06127Sopenharmony_ci#![cfg_attr(super_unstable, feature(proc_macro_def_site))]
927ac06127Sopenharmony_ci#![cfg_attr(doc_cfg, feature(doc_cfg))]
937ac06127Sopenharmony_ci#![deny(unsafe_op_in_unsafe_fn)]
947ac06127Sopenharmony_ci#![allow(
957ac06127Sopenharmony_ci    clippy::cast_lossless,
967ac06127Sopenharmony_ci    clippy::cast_possible_truncation,
977ac06127Sopenharmony_ci    clippy::checked_conversions,
987ac06127Sopenharmony_ci    clippy::doc_markdown,
997ac06127Sopenharmony_ci    clippy::items_after_statements,
1007ac06127Sopenharmony_ci    clippy::iter_without_into_iter,
1017ac06127Sopenharmony_ci    clippy::let_underscore_untyped,
1027ac06127Sopenharmony_ci    clippy::manual_assert,
1037ac06127Sopenharmony_ci    clippy::manual_range_contains,
1047ac06127Sopenharmony_ci    clippy::missing_safety_doc,
1057ac06127Sopenharmony_ci    clippy::must_use_candidate,
1067ac06127Sopenharmony_ci    clippy::needless_doctest_main,
1077ac06127Sopenharmony_ci    clippy::new_without_default,
1087ac06127Sopenharmony_ci    clippy::return_self_not_must_use,
1097ac06127Sopenharmony_ci    clippy::shadow_unrelated,
1107ac06127Sopenharmony_ci    clippy::trivially_copy_pass_by_ref,
1117ac06127Sopenharmony_ci    clippy::unnecessary_wraps,
1127ac06127Sopenharmony_ci    clippy::unused_self,
1137ac06127Sopenharmony_ci    clippy::used_underscore_binding,
1147ac06127Sopenharmony_ci    clippy::vec_init_then_push
1157ac06127Sopenharmony_ci)]
1167ac06127Sopenharmony_ci
1177ac06127Sopenharmony_ci#[cfg(all(procmacro2_semver_exempt, wrap_proc_macro, not(super_unstable)))]
1187ac06127Sopenharmony_cicompile_error! {"\
1197ac06127Sopenharmony_ci    Something is not right. If you've tried to turn on \
1207ac06127Sopenharmony_ci    procmacro2_semver_exempt, you need to ensure that it \
1217ac06127Sopenharmony_ci    is turned on for the compilation of the proc-macro2 \
1227ac06127Sopenharmony_ci    build script as well.
1237ac06127Sopenharmony_ci"}
1247ac06127Sopenharmony_ci
1257ac06127Sopenharmony_ci#[cfg(all(
1267ac06127Sopenharmony_ci    procmacro2_nightly_testing,
1277ac06127Sopenharmony_ci    feature = "proc-macro",
1287ac06127Sopenharmony_ci    not(proc_macro_span)
1297ac06127Sopenharmony_ci))]
1307ac06127Sopenharmony_cicompile_error! {"\
1317ac06127Sopenharmony_ci    Build script probe failed to compile.
1327ac06127Sopenharmony_ci"}
1337ac06127Sopenharmony_ci
1347ac06127Sopenharmony_ciextern crate alloc;
1357ac06127Sopenharmony_ci
1367ac06127Sopenharmony_ci#[cfg(feature = "proc-macro")]
1377ac06127Sopenharmony_ciextern crate proc_macro;
1387ac06127Sopenharmony_ci
1397ac06127Sopenharmony_cimod marker;
1407ac06127Sopenharmony_cimod parse;
1417ac06127Sopenharmony_cimod rcvec;
1427ac06127Sopenharmony_ci
1437ac06127Sopenharmony_ci#[cfg(wrap_proc_macro)]
1447ac06127Sopenharmony_cimod detection;
1457ac06127Sopenharmony_ci
1467ac06127Sopenharmony_ci// Public for proc_macro2::fallback::force() and unforce(), but those are quite
1477ac06127Sopenharmony_ci// a niche use case so we omit it from rustdoc.
1487ac06127Sopenharmony_ci#[doc(hidden)]
1497ac06127Sopenharmony_cipub mod fallback;
1507ac06127Sopenharmony_ci
1517ac06127Sopenharmony_cipub mod extra;
1527ac06127Sopenharmony_ci
1537ac06127Sopenharmony_ci#[cfg(not(wrap_proc_macro))]
1547ac06127Sopenharmony_ciuse crate::fallback as imp;
1557ac06127Sopenharmony_ci#[path = "wrapper.rs"]
1567ac06127Sopenharmony_ci#[cfg(wrap_proc_macro)]
1577ac06127Sopenharmony_cimod imp;
1587ac06127Sopenharmony_ci
1597ac06127Sopenharmony_ci#[cfg(span_locations)]
1607ac06127Sopenharmony_cimod location;
1617ac06127Sopenharmony_ci
1627ac06127Sopenharmony_ciuse crate::extra::DelimSpan;
1637ac06127Sopenharmony_ciuse crate::marker::Marker;
1647ac06127Sopenharmony_ciuse core::cmp::Ordering;
1657ac06127Sopenharmony_ciuse core::fmt::{self, Debug, Display};
1667ac06127Sopenharmony_ciuse core::hash::{Hash, Hasher};
1677ac06127Sopenharmony_ciuse core::ops::RangeBounds;
1687ac06127Sopenharmony_ciuse core::str::FromStr;
1697ac06127Sopenharmony_ciuse std::error::Error;
1707ac06127Sopenharmony_ci#[cfg(procmacro2_semver_exempt)]
1717ac06127Sopenharmony_ciuse std::path::PathBuf;
1727ac06127Sopenharmony_ci
1737ac06127Sopenharmony_ci#[cfg(span_locations)]
1747ac06127Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
1757ac06127Sopenharmony_cipub use crate::location::LineColumn;
1767ac06127Sopenharmony_ci
1777ac06127Sopenharmony_ci/// An abstract stream of tokens, or more concretely a sequence of token trees.
1787ac06127Sopenharmony_ci///
1797ac06127Sopenharmony_ci/// This type provides interfaces for iterating over token trees and for
1807ac06127Sopenharmony_ci/// collecting token trees into one stream.
1817ac06127Sopenharmony_ci///
1827ac06127Sopenharmony_ci/// Token stream is both the input and output of `#[proc_macro]`,
1837ac06127Sopenharmony_ci/// `#[proc_macro_attribute]` and `#[proc_macro_derive]` definitions.
1847ac06127Sopenharmony_ci#[derive(Clone)]
1857ac06127Sopenharmony_cipub struct TokenStream {
1867ac06127Sopenharmony_ci    inner: imp::TokenStream,
1877ac06127Sopenharmony_ci    _marker: Marker,
1887ac06127Sopenharmony_ci}
1897ac06127Sopenharmony_ci
1907ac06127Sopenharmony_ci/// Error returned from `TokenStream::from_str`.
1917ac06127Sopenharmony_cipub struct LexError {
1927ac06127Sopenharmony_ci    inner: imp::LexError,
1937ac06127Sopenharmony_ci    _marker: Marker,
1947ac06127Sopenharmony_ci}
1957ac06127Sopenharmony_ci
1967ac06127Sopenharmony_ciimpl TokenStream {
1977ac06127Sopenharmony_ci    fn _new(inner: imp::TokenStream) -> Self {
1987ac06127Sopenharmony_ci        TokenStream {
1997ac06127Sopenharmony_ci            inner,
2007ac06127Sopenharmony_ci            _marker: Marker,
2017ac06127Sopenharmony_ci        }
2027ac06127Sopenharmony_ci    }
2037ac06127Sopenharmony_ci
2047ac06127Sopenharmony_ci    fn _new_fallback(inner: fallback::TokenStream) -> Self {
2057ac06127Sopenharmony_ci        TokenStream {
2067ac06127Sopenharmony_ci            inner: inner.into(),
2077ac06127Sopenharmony_ci            _marker: Marker,
2087ac06127Sopenharmony_ci        }
2097ac06127Sopenharmony_ci    }
2107ac06127Sopenharmony_ci
2117ac06127Sopenharmony_ci    /// Returns an empty `TokenStream` containing no token trees.
2127ac06127Sopenharmony_ci    pub fn new() -> Self {
2137ac06127Sopenharmony_ci        TokenStream::_new(imp::TokenStream::new())
2147ac06127Sopenharmony_ci    }
2157ac06127Sopenharmony_ci
2167ac06127Sopenharmony_ci    /// Checks if this `TokenStream` is empty.
2177ac06127Sopenharmony_ci    pub fn is_empty(&self) -> bool {
2187ac06127Sopenharmony_ci        self.inner.is_empty()
2197ac06127Sopenharmony_ci    }
2207ac06127Sopenharmony_ci}
2217ac06127Sopenharmony_ci
2227ac06127Sopenharmony_ci/// `TokenStream::default()` returns an empty stream,
2237ac06127Sopenharmony_ci/// i.e. this is equivalent with `TokenStream::new()`.
2247ac06127Sopenharmony_ciimpl Default for TokenStream {
2257ac06127Sopenharmony_ci    fn default() -> Self {
2267ac06127Sopenharmony_ci        TokenStream::new()
2277ac06127Sopenharmony_ci    }
2287ac06127Sopenharmony_ci}
2297ac06127Sopenharmony_ci
2307ac06127Sopenharmony_ci/// Attempts to break the string into tokens and parse those tokens into a token
2317ac06127Sopenharmony_ci/// stream.
2327ac06127Sopenharmony_ci///
2337ac06127Sopenharmony_ci/// May fail for a number of reasons, for example, if the string contains
2347ac06127Sopenharmony_ci/// unbalanced delimiters or characters not existing in the language.
2357ac06127Sopenharmony_ci///
2367ac06127Sopenharmony_ci/// NOTE: Some errors may cause panics instead of returning `LexError`. We
2377ac06127Sopenharmony_ci/// reserve the right to change these errors into `LexError`s later.
2387ac06127Sopenharmony_ciimpl FromStr for TokenStream {
2397ac06127Sopenharmony_ci    type Err = LexError;
2407ac06127Sopenharmony_ci
2417ac06127Sopenharmony_ci    fn from_str(src: &str) -> Result<TokenStream, LexError> {
2427ac06127Sopenharmony_ci        let e = src.parse().map_err(|e| LexError {
2437ac06127Sopenharmony_ci            inner: e,
2447ac06127Sopenharmony_ci            _marker: Marker,
2457ac06127Sopenharmony_ci        })?;
2467ac06127Sopenharmony_ci        Ok(TokenStream::_new(e))
2477ac06127Sopenharmony_ci    }
2487ac06127Sopenharmony_ci}
2497ac06127Sopenharmony_ci
2507ac06127Sopenharmony_ci#[cfg(feature = "proc-macro")]
2517ac06127Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "proc-macro")))]
2527ac06127Sopenharmony_ciimpl From<proc_macro::TokenStream> for TokenStream {
2537ac06127Sopenharmony_ci    fn from(inner: proc_macro::TokenStream) -> Self {
2547ac06127Sopenharmony_ci        TokenStream::_new(inner.into())
2557ac06127Sopenharmony_ci    }
2567ac06127Sopenharmony_ci}
2577ac06127Sopenharmony_ci
2587ac06127Sopenharmony_ci#[cfg(feature = "proc-macro")]
2597ac06127Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "proc-macro")))]
2607ac06127Sopenharmony_ciimpl From<TokenStream> for proc_macro::TokenStream {
2617ac06127Sopenharmony_ci    fn from(inner: TokenStream) -> Self {
2627ac06127Sopenharmony_ci        inner.inner.into()
2637ac06127Sopenharmony_ci    }
2647ac06127Sopenharmony_ci}
2657ac06127Sopenharmony_ci
2667ac06127Sopenharmony_ciimpl From<TokenTree> for TokenStream {
2677ac06127Sopenharmony_ci    fn from(token: TokenTree) -> Self {
2687ac06127Sopenharmony_ci        TokenStream::_new(imp::TokenStream::from(token))
2697ac06127Sopenharmony_ci    }
2707ac06127Sopenharmony_ci}
2717ac06127Sopenharmony_ci
2727ac06127Sopenharmony_ciimpl Extend<TokenTree> for TokenStream {
2737ac06127Sopenharmony_ci    fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
2747ac06127Sopenharmony_ci        self.inner.extend(streams);
2757ac06127Sopenharmony_ci    }
2767ac06127Sopenharmony_ci}
2777ac06127Sopenharmony_ci
2787ac06127Sopenharmony_ciimpl Extend<TokenStream> for TokenStream {
2797ac06127Sopenharmony_ci    fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
2807ac06127Sopenharmony_ci        self.inner
2817ac06127Sopenharmony_ci            .extend(streams.into_iter().map(|stream| stream.inner));
2827ac06127Sopenharmony_ci    }
2837ac06127Sopenharmony_ci}
2847ac06127Sopenharmony_ci
2857ac06127Sopenharmony_ci/// Collects a number of token trees into a single stream.
2867ac06127Sopenharmony_ciimpl FromIterator<TokenTree> for TokenStream {
2877ac06127Sopenharmony_ci    fn from_iter<I: IntoIterator<Item = TokenTree>>(streams: I) -> Self {
2887ac06127Sopenharmony_ci        TokenStream::_new(streams.into_iter().collect())
2897ac06127Sopenharmony_ci    }
2907ac06127Sopenharmony_ci}
2917ac06127Sopenharmony_ciimpl FromIterator<TokenStream> for TokenStream {
2927ac06127Sopenharmony_ci    fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
2937ac06127Sopenharmony_ci        TokenStream::_new(streams.into_iter().map(|i| i.inner).collect())
2947ac06127Sopenharmony_ci    }
2957ac06127Sopenharmony_ci}
2967ac06127Sopenharmony_ci
2977ac06127Sopenharmony_ci/// Prints the token stream as a string that is supposed to be losslessly
2987ac06127Sopenharmony_ci/// convertible back into the same token stream (modulo spans), except for
2997ac06127Sopenharmony_ci/// possibly `TokenTree::Group`s with `Delimiter::None` delimiters and negative
3007ac06127Sopenharmony_ci/// numeric literals.
3017ac06127Sopenharmony_ciimpl Display for TokenStream {
3027ac06127Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3037ac06127Sopenharmony_ci        Display::fmt(&self.inner, f)
3047ac06127Sopenharmony_ci    }
3057ac06127Sopenharmony_ci}
3067ac06127Sopenharmony_ci
3077ac06127Sopenharmony_ci/// Prints token in a form convenient for debugging.
3087ac06127Sopenharmony_ciimpl Debug for TokenStream {
3097ac06127Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3107ac06127Sopenharmony_ci        Debug::fmt(&self.inner, f)
3117ac06127Sopenharmony_ci    }
3127ac06127Sopenharmony_ci}
3137ac06127Sopenharmony_ci
3147ac06127Sopenharmony_ciimpl LexError {
3157ac06127Sopenharmony_ci    pub fn span(&self) -> Span {
3167ac06127Sopenharmony_ci        Span::_new(self.inner.span())
3177ac06127Sopenharmony_ci    }
3187ac06127Sopenharmony_ci}
3197ac06127Sopenharmony_ci
3207ac06127Sopenharmony_ciimpl Debug for LexError {
3217ac06127Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3227ac06127Sopenharmony_ci        Debug::fmt(&self.inner, f)
3237ac06127Sopenharmony_ci    }
3247ac06127Sopenharmony_ci}
3257ac06127Sopenharmony_ci
3267ac06127Sopenharmony_ciimpl Display for LexError {
3277ac06127Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3287ac06127Sopenharmony_ci        Display::fmt(&self.inner, f)
3297ac06127Sopenharmony_ci    }
3307ac06127Sopenharmony_ci}
3317ac06127Sopenharmony_ci
3327ac06127Sopenharmony_ciimpl Error for LexError {}
3337ac06127Sopenharmony_ci
3347ac06127Sopenharmony_ci/// The source file of a given `Span`.
3357ac06127Sopenharmony_ci///
3367ac06127Sopenharmony_ci/// This type is semver exempt and not exposed by default.
3377ac06127Sopenharmony_ci#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))]
3387ac06127Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(procmacro2_semver_exempt)))]
3397ac06127Sopenharmony_ci#[derive(Clone, PartialEq, Eq)]
3407ac06127Sopenharmony_cipub struct SourceFile {
3417ac06127Sopenharmony_ci    inner: imp::SourceFile,
3427ac06127Sopenharmony_ci    _marker: Marker,
3437ac06127Sopenharmony_ci}
3447ac06127Sopenharmony_ci
3457ac06127Sopenharmony_ci#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))]
3467ac06127Sopenharmony_ciimpl SourceFile {
3477ac06127Sopenharmony_ci    fn _new(inner: imp::SourceFile) -> Self {
3487ac06127Sopenharmony_ci        SourceFile {
3497ac06127Sopenharmony_ci            inner,
3507ac06127Sopenharmony_ci            _marker: Marker,
3517ac06127Sopenharmony_ci        }
3527ac06127Sopenharmony_ci    }
3537ac06127Sopenharmony_ci
3547ac06127Sopenharmony_ci    /// Get the path to this source file.
3557ac06127Sopenharmony_ci    ///
3567ac06127Sopenharmony_ci    /// ### Note
3577ac06127Sopenharmony_ci    ///
3587ac06127Sopenharmony_ci    /// If the code span associated with this `SourceFile` was generated by an
3597ac06127Sopenharmony_ci    /// external macro, this may not be an actual path on the filesystem. Use
3607ac06127Sopenharmony_ci    /// [`is_real`] to check.
3617ac06127Sopenharmony_ci    ///
3627ac06127Sopenharmony_ci    /// Also note that even if `is_real` returns `true`, if
3637ac06127Sopenharmony_ci    /// `--remap-path-prefix` was passed on the command line, the path as given
3647ac06127Sopenharmony_ci    /// may not actually be valid.
3657ac06127Sopenharmony_ci    ///
3667ac06127Sopenharmony_ci    /// [`is_real`]: #method.is_real
3677ac06127Sopenharmony_ci    pub fn path(&self) -> PathBuf {
3687ac06127Sopenharmony_ci        self.inner.path()
3697ac06127Sopenharmony_ci    }
3707ac06127Sopenharmony_ci
3717ac06127Sopenharmony_ci    /// Returns `true` if this source file is a real source file, and not
3727ac06127Sopenharmony_ci    /// generated by an external macro's expansion.
3737ac06127Sopenharmony_ci    pub fn is_real(&self) -> bool {
3747ac06127Sopenharmony_ci        self.inner.is_real()
3757ac06127Sopenharmony_ci    }
3767ac06127Sopenharmony_ci}
3777ac06127Sopenharmony_ci
3787ac06127Sopenharmony_ci#[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))]
3797ac06127Sopenharmony_ciimpl Debug for SourceFile {
3807ac06127Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3817ac06127Sopenharmony_ci        Debug::fmt(&self.inner, f)
3827ac06127Sopenharmony_ci    }
3837ac06127Sopenharmony_ci}
3847ac06127Sopenharmony_ci
3857ac06127Sopenharmony_ci/// A region of source code, along with macro expansion information.
3867ac06127Sopenharmony_ci#[derive(Copy, Clone)]
3877ac06127Sopenharmony_cipub struct Span {
3887ac06127Sopenharmony_ci    inner: imp::Span,
3897ac06127Sopenharmony_ci    _marker: Marker,
3907ac06127Sopenharmony_ci}
3917ac06127Sopenharmony_ci
3927ac06127Sopenharmony_ciimpl Span {
3937ac06127Sopenharmony_ci    fn _new(inner: imp::Span) -> Self {
3947ac06127Sopenharmony_ci        Span {
3957ac06127Sopenharmony_ci            inner,
3967ac06127Sopenharmony_ci            _marker: Marker,
3977ac06127Sopenharmony_ci        }
3987ac06127Sopenharmony_ci    }
3997ac06127Sopenharmony_ci
4007ac06127Sopenharmony_ci    fn _new_fallback(inner: fallback::Span) -> Self {
4017ac06127Sopenharmony_ci        Span {
4027ac06127Sopenharmony_ci            inner: inner.into(),
4037ac06127Sopenharmony_ci            _marker: Marker,
4047ac06127Sopenharmony_ci        }
4057ac06127Sopenharmony_ci    }
4067ac06127Sopenharmony_ci
4077ac06127Sopenharmony_ci    /// The span of the invocation of the current procedural macro.
4087ac06127Sopenharmony_ci    ///
4097ac06127Sopenharmony_ci    /// Identifiers created with this span will be resolved as if they were
4107ac06127Sopenharmony_ci    /// written directly at the macro call location (call-site hygiene) and
4117ac06127Sopenharmony_ci    /// other code at the macro call site will be able to refer to them as well.
4127ac06127Sopenharmony_ci    pub fn call_site() -> Self {
4137ac06127Sopenharmony_ci        Span::_new(imp::Span::call_site())
4147ac06127Sopenharmony_ci    }
4157ac06127Sopenharmony_ci
4167ac06127Sopenharmony_ci    /// The span located at the invocation of the procedural macro, but with
4177ac06127Sopenharmony_ci    /// local variables, labels, and `$crate` resolved at the definition site
4187ac06127Sopenharmony_ci    /// of the macro. This is the same hygiene behavior as `macro_rules`.
4197ac06127Sopenharmony_ci    pub fn mixed_site() -> Self {
4207ac06127Sopenharmony_ci        Span::_new(imp::Span::mixed_site())
4217ac06127Sopenharmony_ci    }
4227ac06127Sopenharmony_ci
4237ac06127Sopenharmony_ci    /// A span that resolves at the macro definition site.
4247ac06127Sopenharmony_ci    ///
4257ac06127Sopenharmony_ci    /// This method is semver exempt and not exposed by default.
4267ac06127Sopenharmony_ci    #[cfg(procmacro2_semver_exempt)]
4277ac06127Sopenharmony_ci    #[cfg_attr(doc_cfg, doc(cfg(procmacro2_semver_exempt)))]
4287ac06127Sopenharmony_ci    pub fn def_site() -> Self {
4297ac06127Sopenharmony_ci        Span::_new(imp::Span::def_site())
4307ac06127Sopenharmony_ci    }
4317ac06127Sopenharmony_ci
4327ac06127Sopenharmony_ci    /// Creates a new span with the same line/column information as `self` but
4337ac06127Sopenharmony_ci    /// that resolves symbols as though it were at `other`.
4347ac06127Sopenharmony_ci    pub fn resolved_at(&self, other: Span) -> Span {
4357ac06127Sopenharmony_ci        Span::_new(self.inner.resolved_at(other.inner))
4367ac06127Sopenharmony_ci    }
4377ac06127Sopenharmony_ci
4387ac06127Sopenharmony_ci    /// Creates a new span with the same name resolution behavior as `self` but
4397ac06127Sopenharmony_ci    /// with the line/column information of `other`.
4407ac06127Sopenharmony_ci    pub fn located_at(&self, other: Span) -> Span {
4417ac06127Sopenharmony_ci        Span::_new(self.inner.located_at(other.inner))
4427ac06127Sopenharmony_ci    }
4437ac06127Sopenharmony_ci
4447ac06127Sopenharmony_ci    /// Convert `proc_macro2::Span` to `proc_macro::Span`.
4457ac06127Sopenharmony_ci    ///
4467ac06127Sopenharmony_ci    /// This method is available when building with a nightly compiler, or when
4477ac06127Sopenharmony_ci    /// building with rustc 1.29+ *without* semver exempt features.
4487ac06127Sopenharmony_ci    ///
4497ac06127Sopenharmony_ci    /// # Panics
4507ac06127Sopenharmony_ci    ///
4517ac06127Sopenharmony_ci    /// Panics if called from outside of a procedural macro. Unlike
4527ac06127Sopenharmony_ci    /// `proc_macro2::Span`, the `proc_macro::Span` type can only exist within
4537ac06127Sopenharmony_ci    /// the context of a procedural macro invocation.
4547ac06127Sopenharmony_ci    #[cfg(wrap_proc_macro)]
4557ac06127Sopenharmony_ci    pub fn unwrap(self) -> proc_macro::Span {
4567ac06127Sopenharmony_ci        self.inner.unwrap()
4577ac06127Sopenharmony_ci    }
4587ac06127Sopenharmony_ci
4597ac06127Sopenharmony_ci    // Soft deprecated. Please use Span::unwrap.
4607ac06127Sopenharmony_ci    #[cfg(wrap_proc_macro)]
4617ac06127Sopenharmony_ci    #[doc(hidden)]
4627ac06127Sopenharmony_ci    pub fn unstable(self) -> proc_macro::Span {
4637ac06127Sopenharmony_ci        self.unwrap()
4647ac06127Sopenharmony_ci    }
4657ac06127Sopenharmony_ci
4667ac06127Sopenharmony_ci    /// The original source file into which this span points.
4677ac06127Sopenharmony_ci    ///
4687ac06127Sopenharmony_ci    /// This method is semver exempt and not exposed by default.
4697ac06127Sopenharmony_ci    #[cfg(all(procmacro2_semver_exempt, any(not(wrap_proc_macro), super_unstable)))]
4707ac06127Sopenharmony_ci    #[cfg_attr(doc_cfg, doc(cfg(procmacro2_semver_exempt)))]
4717ac06127Sopenharmony_ci    pub fn source_file(&self) -> SourceFile {
4727ac06127Sopenharmony_ci        SourceFile::_new(self.inner.source_file())
4737ac06127Sopenharmony_ci    }
4747ac06127Sopenharmony_ci
4757ac06127Sopenharmony_ci    /// Get the starting line/column in the source file for this span.
4767ac06127Sopenharmony_ci    ///
4777ac06127Sopenharmony_ci    /// This method requires the `"span-locations"` feature to be enabled.
4787ac06127Sopenharmony_ci    ///
4797ac06127Sopenharmony_ci    /// When executing in a procedural macro context, the returned line/column
4807ac06127Sopenharmony_ci    /// are only meaningful if compiled with a nightly toolchain. The stable
4817ac06127Sopenharmony_ci    /// toolchain does not have this information available. When executing
4827ac06127Sopenharmony_ci    /// outside of a procedural macro, such as main.rs or build.rs, the
4837ac06127Sopenharmony_ci    /// line/column are always meaningful regardless of toolchain.
4847ac06127Sopenharmony_ci    #[cfg(span_locations)]
4857ac06127Sopenharmony_ci    #[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
4867ac06127Sopenharmony_ci    pub fn start(&self) -> LineColumn {
4877ac06127Sopenharmony_ci        self.inner.start()
4887ac06127Sopenharmony_ci    }
4897ac06127Sopenharmony_ci
4907ac06127Sopenharmony_ci    /// Get the ending line/column in the source file for this span.
4917ac06127Sopenharmony_ci    ///
4927ac06127Sopenharmony_ci    /// This method requires the `"span-locations"` feature to be enabled.
4937ac06127Sopenharmony_ci    ///
4947ac06127Sopenharmony_ci    /// When executing in a procedural macro context, the returned line/column
4957ac06127Sopenharmony_ci    /// are only meaningful if compiled with a nightly toolchain. The stable
4967ac06127Sopenharmony_ci    /// toolchain does not have this information available. When executing
4977ac06127Sopenharmony_ci    /// outside of a procedural macro, such as main.rs or build.rs, the
4987ac06127Sopenharmony_ci    /// line/column are always meaningful regardless of toolchain.
4997ac06127Sopenharmony_ci    #[cfg(span_locations)]
5007ac06127Sopenharmony_ci    #[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
5017ac06127Sopenharmony_ci    pub fn end(&self) -> LineColumn {
5027ac06127Sopenharmony_ci        self.inner.end()
5037ac06127Sopenharmony_ci    }
5047ac06127Sopenharmony_ci
5057ac06127Sopenharmony_ci    /// Create a new span encompassing `self` and `other`.
5067ac06127Sopenharmony_ci    ///
5077ac06127Sopenharmony_ci    /// Returns `None` if `self` and `other` are from different files.
5087ac06127Sopenharmony_ci    ///
5097ac06127Sopenharmony_ci    /// Warning: the underlying [`proc_macro::Span::join`] method is
5107ac06127Sopenharmony_ci    /// nightly-only. When called from within a procedural macro not using a
5117ac06127Sopenharmony_ci    /// nightly compiler, this method will always return `None`.
5127ac06127Sopenharmony_ci    ///
5137ac06127Sopenharmony_ci    /// [`proc_macro::Span::join`]: https://doc.rust-lang.org/proc_macro/struct.Span.html#method.join
5147ac06127Sopenharmony_ci    pub fn join(&self, other: Span) -> Option<Span> {
5157ac06127Sopenharmony_ci        self.inner.join(other.inner).map(Span::_new)
5167ac06127Sopenharmony_ci    }
5177ac06127Sopenharmony_ci
5187ac06127Sopenharmony_ci    /// Compares two spans to see if they're equal.
5197ac06127Sopenharmony_ci    ///
5207ac06127Sopenharmony_ci    /// This method is semver exempt and not exposed by default.
5217ac06127Sopenharmony_ci    #[cfg(procmacro2_semver_exempt)]
5227ac06127Sopenharmony_ci    #[cfg_attr(doc_cfg, doc(cfg(procmacro2_semver_exempt)))]
5237ac06127Sopenharmony_ci    pub fn eq(&self, other: &Span) -> bool {
5247ac06127Sopenharmony_ci        self.inner.eq(&other.inner)
5257ac06127Sopenharmony_ci    }
5267ac06127Sopenharmony_ci
5277ac06127Sopenharmony_ci    /// Returns the source text behind a span. This preserves the original
5287ac06127Sopenharmony_ci    /// source code, including spaces and comments. It only returns a result if
5297ac06127Sopenharmony_ci    /// the span corresponds to real source code.
5307ac06127Sopenharmony_ci    ///
5317ac06127Sopenharmony_ci    /// Note: The observable result of a macro should only rely on the tokens
5327ac06127Sopenharmony_ci    /// and not on this source text. The result of this function is a best
5337ac06127Sopenharmony_ci    /// effort to be used for diagnostics only.
5347ac06127Sopenharmony_ci    pub fn source_text(&self) -> Option<String> {
5357ac06127Sopenharmony_ci        self.inner.source_text()
5367ac06127Sopenharmony_ci    }
5377ac06127Sopenharmony_ci}
5387ac06127Sopenharmony_ci
5397ac06127Sopenharmony_ci/// Prints a span in a form convenient for debugging.
5407ac06127Sopenharmony_ciimpl Debug for Span {
5417ac06127Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5427ac06127Sopenharmony_ci        Debug::fmt(&self.inner, f)
5437ac06127Sopenharmony_ci    }
5447ac06127Sopenharmony_ci}
5457ac06127Sopenharmony_ci
5467ac06127Sopenharmony_ci/// A single token or a delimited sequence of token trees (e.g. `[1, (), ..]`).
5477ac06127Sopenharmony_ci#[derive(Clone)]
5487ac06127Sopenharmony_cipub enum TokenTree {
5497ac06127Sopenharmony_ci    /// A token stream surrounded by bracket delimiters.
5507ac06127Sopenharmony_ci    Group(Group),
5517ac06127Sopenharmony_ci    /// An identifier.
5527ac06127Sopenharmony_ci    Ident(Ident),
5537ac06127Sopenharmony_ci    /// A single punctuation character (`+`, `,`, `$`, etc.).
5547ac06127Sopenharmony_ci    Punct(Punct),
5557ac06127Sopenharmony_ci    /// A literal character (`'a'`), string (`"hello"`), number (`2.3`), etc.
5567ac06127Sopenharmony_ci    Literal(Literal),
5577ac06127Sopenharmony_ci}
5587ac06127Sopenharmony_ci
5597ac06127Sopenharmony_ciimpl TokenTree {
5607ac06127Sopenharmony_ci    /// Returns the span of this tree, delegating to the `span` method of
5617ac06127Sopenharmony_ci    /// the contained token or a delimited stream.
5627ac06127Sopenharmony_ci    pub fn span(&self) -> Span {
5637ac06127Sopenharmony_ci        match self {
5647ac06127Sopenharmony_ci            TokenTree::Group(t) => t.span(),
5657ac06127Sopenharmony_ci            TokenTree::Ident(t) => t.span(),
5667ac06127Sopenharmony_ci            TokenTree::Punct(t) => t.span(),
5677ac06127Sopenharmony_ci            TokenTree::Literal(t) => t.span(),
5687ac06127Sopenharmony_ci        }
5697ac06127Sopenharmony_ci    }
5707ac06127Sopenharmony_ci
5717ac06127Sopenharmony_ci    /// Configures the span for *only this token*.
5727ac06127Sopenharmony_ci    ///
5737ac06127Sopenharmony_ci    /// Note that if this token is a `Group` then this method will not configure
5747ac06127Sopenharmony_ci    /// the span of each of the internal tokens, this will simply delegate to
5757ac06127Sopenharmony_ci    /// the `set_span` method of each variant.
5767ac06127Sopenharmony_ci    pub fn set_span(&mut self, span: Span) {
5777ac06127Sopenharmony_ci        match self {
5787ac06127Sopenharmony_ci            TokenTree::Group(t) => t.set_span(span),
5797ac06127Sopenharmony_ci            TokenTree::Ident(t) => t.set_span(span),
5807ac06127Sopenharmony_ci            TokenTree::Punct(t) => t.set_span(span),
5817ac06127Sopenharmony_ci            TokenTree::Literal(t) => t.set_span(span),
5827ac06127Sopenharmony_ci        }
5837ac06127Sopenharmony_ci    }
5847ac06127Sopenharmony_ci}
5857ac06127Sopenharmony_ci
5867ac06127Sopenharmony_ciimpl From<Group> for TokenTree {
5877ac06127Sopenharmony_ci    fn from(g: Group) -> Self {
5887ac06127Sopenharmony_ci        TokenTree::Group(g)
5897ac06127Sopenharmony_ci    }
5907ac06127Sopenharmony_ci}
5917ac06127Sopenharmony_ci
5927ac06127Sopenharmony_ciimpl From<Ident> for TokenTree {
5937ac06127Sopenharmony_ci    fn from(g: Ident) -> Self {
5947ac06127Sopenharmony_ci        TokenTree::Ident(g)
5957ac06127Sopenharmony_ci    }
5967ac06127Sopenharmony_ci}
5977ac06127Sopenharmony_ci
5987ac06127Sopenharmony_ciimpl From<Punct> for TokenTree {
5997ac06127Sopenharmony_ci    fn from(g: Punct) -> Self {
6007ac06127Sopenharmony_ci        TokenTree::Punct(g)
6017ac06127Sopenharmony_ci    }
6027ac06127Sopenharmony_ci}
6037ac06127Sopenharmony_ci
6047ac06127Sopenharmony_ciimpl From<Literal> for TokenTree {
6057ac06127Sopenharmony_ci    fn from(g: Literal) -> Self {
6067ac06127Sopenharmony_ci        TokenTree::Literal(g)
6077ac06127Sopenharmony_ci    }
6087ac06127Sopenharmony_ci}
6097ac06127Sopenharmony_ci
6107ac06127Sopenharmony_ci/// Prints the token tree as a string that is supposed to be losslessly
6117ac06127Sopenharmony_ci/// convertible back into the same token tree (modulo spans), except for
6127ac06127Sopenharmony_ci/// possibly `TokenTree::Group`s with `Delimiter::None` delimiters and negative
6137ac06127Sopenharmony_ci/// numeric literals.
6147ac06127Sopenharmony_ciimpl Display for TokenTree {
6157ac06127Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6167ac06127Sopenharmony_ci        match self {
6177ac06127Sopenharmony_ci            TokenTree::Group(t) => Display::fmt(t, f),
6187ac06127Sopenharmony_ci            TokenTree::Ident(t) => Display::fmt(t, f),
6197ac06127Sopenharmony_ci            TokenTree::Punct(t) => Display::fmt(t, f),
6207ac06127Sopenharmony_ci            TokenTree::Literal(t) => Display::fmt(t, f),
6217ac06127Sopenharmony_ci        }
6227ac06127Sopenharmony_ci    }
6237ac06127Sopenharmony_ci}
6247ac06127Sopenharmony_ci
6257ac06127Sopenharmony_ci/// Prints token tree in a form convenient for debugging.
6267ac06127Sopenharmony_ciimpl Debug for TokenTree {
6277ac06127Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6287ac06127Sopenharmony_ci        // Each of these has the name in the struct type in the derived debug,
6297ac06127Sopenharmony_ci        // so don't bother with an extra layer of indirection
6307ac06127Sopenharmony_ci        match self {
6317ac06127Sopenharmony_ci            TokenTree::Group(t) => Debug::fmt(t, f),
6327ac06127Sopenharmony_ci            TokenTree::Ident(t) => {
6337ac06127Sopenharmony_ci                let mut debug = f.debug_struct("Ident");
6347ac06127Sopenharmony_ci                debug.field("sym", &format_args!("{}", t));
6357ac06127Sopenharmony_ci                imp::debug_span_field_if_nontrivial(&mut debug, t.span().inner);
6367ac06127Sopenharmony_ci                debug.finish()
6377ac06127Sopenharmony_ci            }
6387ac06127Sopenharmony_ci            TokenTree::Punct(t) => Debug::fmt(t, f),
6397ac06127Sopenharmony_ci            TokenTree::Literal(t) => Debug::fmt(t, f),
6407ac06127Sopenharmony_ci        }
6417ac06127Sopenharmony_ci    }
6427ac06127Sopenharmony_ci}
6437ac06127Sopenharmony_ci
6447ac06127Sopenharmony_ci/// A delimited token stream.
6457ac06127Sopenharmony_ci///
6467ac06127Sopenharmony_ci/// A `Group` internally contains a `TokenStream` which is surrounded by
6477ac06127Sopenharmony_ci/// `Delimiter`s.
6487ac06127Sopenharmony_ci#[derive(Clone)]
6497ac06127Sopenharmony_cipub struct Group {
6507ac06127Sopenharmony_ci    inner: imp::Group,
6517ac06127Sopenharmony_ci}
6527ac06127Sopenharmony_ci
6537ac06127Sopenharmony_ci/// Describes how a sequence of token trees is delimited.
6547ac06127Sopenharmony_ci#[derive(Copy, Clone, Debug, Eq, PartialEq)]
6557ac06127Sopenharmony_cipub enum Delimiter {
6567ac06127Sopenharmony_ci    /// `( ... )`
6577ac06127Sopenharmony_ci    Parenthesis,
6587ac06127Sopenharmony_ci    /// `{ ... }`
6597ac06127Sopenharmony_ci    Brace,
6607ac06127Sopenharmony_ci    /// `[ ... ]`
6617ac06127Sopenharmony_ci    Bracket,
6627ac06127Sopenharmony_ci    /// `Ø ... Ø`
6637ac06127Sopenharmony_ci    ///
6647ac06127Sopenharmony_ci    /// An implicit delimiter, that may, for example, appear around tokens
6657ac06127Sopenharmony_ci    /// coming from a "macro variable" `$var`. It is important to preserve
6667ac06127Sopenharmony_ci    /// operator priorities in cases like `$var * 3` where `$var` is `1 + 2`.
6677ac06127Sopenharmony_ci    /// Implicit delimiters may not survive roundtrip of a token stream through
6687ac06127Sopenharmony_ci    /// a string.
6697ac06127Sopenharmony_ci    None,
6707ac06127Sopenharmony_ci}
6717ac06127Sopenharmony_ci
6727ac06127Sopenharmony_ciimpl Group {
6737ac06127Sopenharmony_ci    fn _new(inner: imp::Group) -> Self {
6747ac06127Sopenharmony_ci        Group { inner }
6757ac06127Sopenharmony_ci    }
6767ac06127Sopenharmony_ci
6777ac06127Sopenharmony_ci    fn _new_fallback(inner: fallback::Group) -> Self {
6787ac06127Sopenharmony_ci        Group {
6797ac06127Sopenharmony_ci            inner: inner.into(),
6807ac06127Sopenharmony_ci        }
6817ac06127Sopenharmony_ci    }
6827ac06127Sopenharmony_ci
6837ac06127Sopenharmony_ci    /// Creates a new `Group` with the given delimiter and token stream.
6847ac06127Sopenharmony_ci    ///
6857ac06127Sopenharmony_ci    /// This constructor will set the span for this group to
6867ac06127Sopenharmony_ci    /// `Span::call_site()`. To change the span you can use the `set_span`
6877ac06127Sopenharmony_ci    /// method below.
6887ac06127Sopenharmony_ci    pub fn new(delimiter: Delimiter, stream: TokenStream) -> Self {
6897ac06127Sopenharmony_ci        Group {
6907ac06127Sopenharmony_ci            inner: imp::Group::new(delimiter, stream.inner),
6917ac06127Sopenharmony_ci        }
6927ac06127Sopenharmony_ci    }
6937ac06127Sopenharmony_ci
6947ac06127Sopenharmony_ci    /// Returns the punctuation used as the delimiter for this group: a set of
6957ac06127Sopenharmony_ci    /// parentheses, square brackets, or curly braces.
6967ac06127Sopenharmony_ci    pub fn delimiter(&self) -> Delimiter {
6977ac06127Sopenharmony_ci        self.inner.delimiter()
6987ac06127Sopenharmony_ci    }
6997ac06127Sopenharmony_ci
7007ac06127Sopenharmony_ci    /// Returns the `TokenStream` of tokens that are delimited in this `Group`.
7017ac06127Sopenharmony_ci    ///
7027ac06127Sopenharmony_ci    /// Note that the returned token stream does not include the delimiter
7037ac06127Sopenharmony_ci    /// returned above.
7047ac06127Sopenharmony_ci    pub fn stream(&self) -> TokenStream {
7057ac06127Sopenharmony_ci        TokenStream::_new(self.inner.stream())
7067ac06127Sopenharmony_ci    }
7077ac06127Sopenharmony_ci
7087ac06127Sopenharmony_ci    /// Returns the span for the delimiters of this token stream, spanning the
7097ac06127Sopenharmony_ci    /// entire `Group`.
7107ac06127Sopenharmony_ci    ///
7117ac06127Sopenharmony_ci    /// ```text
7127ac06127Sopenharmony_ci    /// pub fn span(&self) -> Span {
7137ac06127Sopenharmony_ci    ///            ^^^^^^^
7147ac06127Sopenharmony_ci    /// ```
7157ac06127Sopenharmony_ci    pub fn span(&self) -> Span {
7167ac06127Sopenharmony_ci        Span::_new(self.inner.span())
7177ac06127Sopenharmony_ci    }
7187ac06127Sopenharmony_ci
7197ac06127Sopenharmony_ci    /// Returns the span pointing to the opening delimiter of this group.
7207ac06127Sopenharmony_ci    ///
7217ac06127Sopenharmony_ci    /// ```text
7227ac06127Sopenharmony_ci    /// pub fn span_open(&self) -> Span {
7237ac06127Sopenharmony_ci    ///                 ^
7247ac06127Sopenharmony_ci    /// ```
7257ac06127Sopenharmony_ci    pub fn span_open(&self) -> Span {
7267ac06127Sopenharmony_ci        Span::_new(self.inner.span_open())
7277ac06127Sopenharmony_ci    }
7287ac06127Sopenharmony_ci
7297ac06127Sopenharmony_ci    /// Returns the span pointing to the closing delimiter of this group.
7307ac06127Sopenharmony_ci    ///
7317ac06127Sopenharmony_ci    /// ```text
7327ac06127Sopenharmony_ci    /// pub fn span_close(&self) -> Span {
7337ac06127Sopenharmony_ci    ///                        ^
7347ac06127Sopenharmony_ci    /// ```
7357ac06127Sopenharmony_ci    pub fn span_close(&self) -> Span {
7367ac06127Sopenharmony_ci        Span::_new(self.inner.span_close())
7377ac06127Sopenharmony_ci    }
7387ac06127Sopenharmony_ci
7397ac06127Sopenharmony_ci    /// Returns an object that holds this group's `span_open()` and
7407ac06127Sopenharmony_ci    /// `span_close()` together (in a more compact representation than holding
7417ac06127Sopenharmony_ci    /// those 2 spans individually).
7427ac06127Sopenharmony_ci    pub fn delim_span(&self) -> DelimSpan {
7437ac06127Sopenharmony_ci        DelimSpan::new(&self.inner)
7447ac06127Sopenharmony_ci    }
7457ac06127Sopenharmony_ci
7467ac06127Sopenharmony_ci    /// Configures the span for this `Group`'s delimiters, but not its internal
7477ac06127Sopenharmony_ci    /// tokens.
7487ac06127Sopenharmony_ci    ///
7497ac06127Sopenharmony_ci    /// This method will **not** set the span of all the internal tokens spanned
7507ac06127Sopenharmony_ci    /// by this group, but rather it will only set the span of the delimiter
7517ac06127Sopenharmony_ci    /// tokens at the level of the `Group`.
7527ac06127Sopenharmony_ci    pub fn set_span(&mut self, span: Span) {
7537ac06127Sopenharmony_ci        self.inner.set_span(span.inner);
7547ac06127Sopenharmony_ci    }
7557ac06127Sopenharmony_ci}
7567ac06127Sopenharmony_ci
7577ac06127Sopenharmony_ci/// Prints the group as a string that should be losslessly convertible back
7587ac06127Sopenharmony_ci/// into the same group (modulo spans), except for possibly `TokenTree::Group`s
7597ac06127Sopenharmony_ci/// with `Delimiter::None` delimiters.
7607ac06127Sopenharmony_ciimpl Display for Group {
7617ac06127Sopenharmony_ci    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
7627ac06127Sopenharmony_ci        Display::fmt(&self.inner, formatter)
7637ac06127Sopenharmony_ci    }
7647ac06127Sopenharmony_ci}
7657ac06127Sopenharmony_ci
7667ac06127Sopenharmony_ciimpl Debug for Group {
7677ac06127Sopenharmony_ci    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
7687ac06127Sopenharmony_ci        Debug::fmt(&self.inner, formatter)
7697ac06127Sopenharmony_ci    }
7707ac06127Sopenharmony_ci}
7717ac06127Sopenharmony_ci
7727ac06127Sopenharmony_ci/// A `Punct` is a single punctuation character like `+`, `-` or `#`.
7737ac06127Sopenharmony_ci///
7747ac06127Sopenharmony_ci/// Multicharacter operators like `+=` are represented as two instances of
7757ac06127Sopenharmony_ci/// `Punct` with different forms of `Spacing` returned.
7767ac06127Sopenharmony_ci#[derive(Clone)]
7777ac06127Sopenharmony_cipub struct Punct {
7787ac06127Sopenharmony_ci    ch: char,
7797ac06127Sopenharmony_ci    spacing: Spacing,
7807ac06127Sopenharmony_ci    span: Span,
7817ac06127Sopenharmony_ci}
7827ac06127Sopenharmony_ci
7837ac06127Sopenharmony_ci/// Whether a `Punct` is followed immediately by another `Punct` or followed by
7847ac06127Sopenharmony_ci/// another token or whitespace.
7857ac06127Sopenharmony_ci#[derive(Copy, Clone, Debug, Eq, PartialEq)]
7867ac06127Sopenharmony_cipub enum Spacing {
7877ac06127Sopenharmony_ci    /// E.g. `+` is `Alone` in `+ =`, `+ident` or `+()`.
7887ac06127Sopenharmony_ci    Alone,
7897ac06127Sopenharmony_ci    /// E.g. `+` is `Joint` in `+=` or `'` is `Joint` in `'#`.
7907ac06127Sopenharmony_ci    ///
7917ac06127Sopenharmony_ci    /// Additionally, single quote `'` can join with identifiers to form
7927ac06127Sopenharmony_ci    /// lifetimes `'ident`.
7937ac06127Sopenharmony_ci    Joint,
7947ac06127Sopenharmony_ci}
7957ac06127Sopenharmony_ci
7967ac06127Sopenharmony_ciimpl Punct {
7977ac06127Sopenharmony_ci    /// Creates a new `Punct` from the given character and spacing.
7987ac06127Sopenharmony_ci    ///
7997ac06127Sopenharmony_ci    /// The `ch` argument must be a valid punctuation character permitted by the
8007ac06127Sopenharmony_ci    /// language, otherwise the function will panic.
8017ac06127Sopenharmony_ci    ///
8027ac06127Sopenharmony_ci    /// The returned `Punct` will have the default span of `Span::call_site()`
8037ac06127Sopenharmony_ci    /// which can be further configured with the `set_span` method below.
8047ac06127Sopenharmony_ci    pub fn new(ch: char, spacing: Spacing) -> Self {
8057ac06127Sopenharmony_ci        Punct {
8067ac06127Sopenharmony_ci            ch,
8077ac06127Sopenharmony_ci            spacing,
8087ac06127Sopenharmony_ci            span: Span::call_site(),
8097ac06127Sopenharmony_ci        }
8107ac06127Sopenharmony_ci    }
8117ac06127Sopenharmony_ci
8127ac06127Sopenharmony_ci    /// Returns the value of this punctuation character as `char`.
8137ac06127Sopenharmony_ci    pub fn as_char(&self) -> char {
8147ac06127Sopenharmony_ci        self.ch
8157ac06127Sopenharmony_ci    }
8167ac06127Sopenharmony_ci
8177ac06127Sopenharmony_ci    /// Returns the spacing of this punctuation character, indicating whether
8187ac06127Sopenharmony_ci    /// it's immediately followed by another `Punct` in the token stream, so
8197ac06127Sopenharmony_ci    /// they can potentially be combined into a multicharacter operator
8207ac06127Sopenharmony_ci    /// (`Joint`), or it's followed by some other token or whitespace (`Alone`)
8217ac06127Sopenharmony_ci    /// so the operator has certainly ended.
8227ac06127Sopenharmony_ci    pub fn spacing(&self) -> Spacing {
8237ac06127Sopenharmony_ci        self.spacing
8247ac06127Sopenharmony_ci    }
8257ac06127Sopenharmony_ci
8267ac06127Sopenharmony_ci    /// Returns the span for this punctuation character.
8277ac06127Sopenharmony_ci    pub fn span(&self) -> Span {
8287ac06127Sopenharmony_ci        self.span
8297ac06127Sopenharmony_ci    }
8307ac06127Sopenharmony_ci
8317ac06127Sopenharmony_ci    /// Configure the span for this punctuation character.
8327ac06127Sopenharmony_ci    pub fn set_span(&mut self, span: Span) {
8337ac06127Sopenharmony_ci        self.span = span;
8347ac06127Sopenharmony_ci    }
8357ac06127Sopenharmony_ci}
8367ac06127Sopenharmony_ci
8377ac06127Sopenharmony_ci/// Prints the punctuation character as a string that should be losslessly
8387ac06127Sopenharmony_ci/// convertible back into the same character.
8397ac06127Sopenharmony_ciimpl Display for Punct {
8407ac06127Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8417ac06127Sopenharmony_ci        Display::fmt(&self.ch, f)
8427ac06127Sopenharmony_ci    }
8437ac06127Sopenharmony_ci}
8447ac06127Sopenharmony_ci
8457ac06127Sopenharmony_ciimpl Debug for Punct {
8467ac06127Sopenharmony_ci    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
8477ac06127Sopenharmony_ci        let mut debug = fmt.debug_struct("Punct");
8487ac06127Sopenharmony_ci        debug.field("char", &self.ch);
8497ac06127Sopenharmony_ci        debug.field("spacing", &self.spacing);
8507ac06127Sopenharmony_ci        imp::debug_span_field_if_nontrivial(&mut debug, self.span.inner);
8517ac06127Sopenharmony_ci        debug.finish()
8527ac06127Sopenharmony_ci    }
8537ac06127Sopenharmony_ci}
8547ac06127Sopenharmony_ci
8557ac06127Sopenharmony_ci/// A word of Rust code, which may be a keyword or legal variable name.
8567ac06127Sopenharmony_ci///
8577ac06127Sopenharmony_ci/// An identifier consists of at least one Unicode code point, the first of
8587ac06127Sopenharmony_ci/// which has the XID_Start property and the rest of which have the XID_Continue
8597ac06127Sopenharmony_ci/// property.
8607ac06127Sopenharmony_ci///
8617ac06127Sopenharmony_ci/// - The empty string is not an identifier. Use `Option<Ident>`.
8627ac06127Sopenharmony_ci/// - A lifetime is not an identifier. Use `syn::Lifetime` instead.
8637ac06127Sopenharmony_ci///
8647ac06127Sopenharmony_ci/// An identifier constructed with `Ident::new` is permitted to be a Rust
8657ac06127Sopenharmony_ci/// keyword, though parsing one through its [`Parse`] implementation rejects
8667ac06127Sopenharmony_ci/// Rust keywords. Use `input.call(Ident::parse_any)` when parsing to match the
8677ac06127Sopenharmony_ci/// behaviour of `Ident::new`.
8687ac06127Sopenharmony_ci///
8697ac06127Sopenharmony_ci/// [`Parse`]: https://docs.rs/syn/2.0/syn/parse/trait.Parse.html
8707ac06127Sopenharmony_ci///
8717ac06127Sopenharmony_ci/// # Examples
8727ac06127Sopenharmony_ci///
8737ac06127Sopenharmony_ci/// A new ident can be created from a string using the `Ident::new` function.
8747ac06127Sopenharmony_ci/// A span must be provided explicitly which governs the name resolution
8757ac06127Sopenharmony_ci/// behavior of the resulting identifier.
8767ac06127Sopenharmony_ci///
8777ac06127Sopenharmony_ci/// ```
8787ac06127Sopenharmony_ci/// use proc_macro2::{Ident, Span};
8797ac06127Sopenharmony_ci///
8807ac06127Sopenharmony_ci/// fn main() {
8817ac06127Sopenharmony_ci///     let call_ident = Ident::new("calligraphy", Span::call_site());
8827ac06127Sopenharmony_ci///
8837ac06127Sopenharmony_ci///     println!("{}", call_ident);
8847ac06127Sopenharmony_ci/// }
8857ac06127Sopenharmony_ci/// ```
8867ac06127Sopenharmony_ci///
8877ac06127Sopenharmony_ci/// An ident can be interpolated into a token stream using the `quote!` macro.
8887ac06127Sopenharmony_ci///
8897ac06127Sopenharmony_ci/// ```
8907ac06127Sopenharmony_ci/// use proc_macro2::{Ident, Span};
8917ac06127Sopenharmony_ci/// use quote::quote;
8927ac06127Sopenharmony_ci///
8937ac06127Sopenharmony_ci/// fn main() {
8947ac06127Sopenharmony_ci///     let ident = Ident::new("demo", Span::call_site());
8957ac06127Sopenharmony_ci///
8967ac06127Sopenharmony_ci///     // Create a variable binding whose name is this ident.
8977ac06127Sopenharmony_ci///     let expanded = quote! { let #ident = 10; };
8987ac06127Sopenharmony_ci///
8997ac06127Sopenharmony_ci///     // Create a variable binding with a slightly different name.
9007ac06127Sopenharmony_ci///     let temp_ident = Ident::new(&format!("new_{}", ident), Span::call_site());
9017ac06127Sopenharmony_ci///     let expanded = quote! { let #temp_ident = 10; };
9027ac06127Sopenharmony_ci/// }
9037ac06127Sopenharmony_ci/// ```
9047ac06127Sopenharmony_ci///
9057ac06127Sopenharmony_ci/// A string representation of the ident is available through the `to_string()`
9067ac06127Sopenharmony_ci/// method.
9077ac06127Sopenharmony_ci///
9087ac06127Sopenharmony_ci/// ```
9097ac06127Sopenharmony_ci/// # use proc_macro2::{Ident, Span};
9107ac06127Sopenharmony_ci/// #
9117ac06127Sopenharmony_ci/// # let ident = Ident::new("another_identifier", Span::call_site());
9127ac06127Sopenharmony_ci/// #
9137ac06127Sopenharmony_ci/// // Examine the ident as a string.
9147ac06127Sopenharmony_ci/// let ident_string = ident.to_string();
9157ac06127Sopenharmony_ci/// if ident_string.len() > 60 {
9167ac06127Sopenharmony_ci///     println!("Very long identifier: {}", ident_string)
9177ac06127Sopenharmony_ci/// }
9187ac06127Sopenharmony_ci/// ```
9197ac06127Sopenharmony_ci#[derive(Clone)]
9207ac06127Sopenharmony_cipub struct Ident {
9217ac06127Sopenharmony_ci    inner: imp::Ident,
9227ac06127Sopenharmony_ci    _marker: Marker,
9237ac06127Sopenharmony_ci}
9247ac06127Sopenharmony_ci
9257ac06127Sopenharmony_ciimpl Ident {
9267ac06127Sopenharmony_ci    fn _new(inner: imp::Ident) -> Self {
9277ac06127Sopenharmony_ci        Ident {
9287ac06127Sopenharmony_ci            inner,
9297ac06127Sopenharmony_ci            _marker: Marker,
9307ac06127Sopenharmony_ci        }
9317ac06127Sopenharmony_ci    }
9327ac06127Sopenharmony_ci
9337ac06127Sopenharmony_ci    /// Creates a new `Ident` with the given `string` as well as the specified
9347ac06127Sopenharmony_ci    /// `span`.
9357ac06127Sopenharmony_ci    ///
9367ac06127Sopenharmony_ci    /// The `string` argument must be a valid identifier permitted by the
9377ac06127Sopenharmony_ci    /// language, otherwise the function will panic.
9387ac06127Sopenharmony_ci    ///
9397ac06127Sopenharmony_ci    /// Note that `span`, currently in rustc, configures the hygiene information
9407ac06127Sopenharmony_ci    /// for this identifier.
9417ac06127Sopenharmony_ci    ///
9427ac06127Sopenharmony_ci    /// As of this time `Span::call_site()` explicitly opts-in to "call-site"
9437ac06127Sopenharmony_ci    /// hygiene meaning that identifiers created with this span will be resolved
9447ac06127Sopenharmony_ci    /// as if they were written directly at the location of the macro call, and
9457ac06127Sopenharmony_ci    /// other code at the macro call site will be able to refer to them as well.
9467ac06127Sopenharmony_ci    ///
9477ac06127Sopenharmony_ci    /// Later spans like `Span::def_site()` will allow to opt-in to
9487ac06127Sopenharmony_ci    /// "definition-site" hygiene meaning that identifiers created with this
9497ac06127Sopenharmony_ci    /// span will be resolved at the location of the macro definition and other
9507ac06127Sopenharmony_ci    /// code at the macro call site will not be able to refer to them.
9517ac06127Sopenharmony_ci    ///
9527ac06127Sopenharmony_ci    /// Due to the current importance of hygiene this constructor, unlike other
9537ac06127Sopenharmony_ci    /// tokens, requires a `Span` to be specified at construction.
9547ac06127Sopenharmony_ci    ///
9557ac06127Sopenharmony_ci    /// # Panics
9567ac06127Sopenharmony_ci    ///
9577ac06127Sopenharmony_ci    /// Panics if the input string is neither a keyword nor a legal variable
9587ac06127Sopenharmony_ci    /// name. If you are not sure whether the string contains an identifier and
9597ac06127Sopenharmony_ci    /// need to handle an error case, use
9607ac06127Sopenharmony_ci    /// <a href="https://docs.rs/syn/2.0/syn/fn.parse_str.html"><code
9617ac06127Sopenharmony_ci    ///   style="padding-right:0;">syn::parse_str</code></a><code
9627ac06127Sopenharmony_ci    ///   style="padding-left:0;">::&lt;Ident&gt;</code>
9637ac06127Sopenharmony_ci    /// rather than `Ident::new`.
9647ac06127Sopenharmony_ci    #[track_caller]
9657ac06127Sopenharmony_ci    pub fn new(string: &str, span: Span) -> Self {
9667ac06127Sopenharmony_ci        Ident::_new(imp::Ident::new_checked(string, span.inner))
9677ac06127Sopenharmony_ci    }
9687ac06127Sopenharmony_ci
9697ac06127Sopenharmony_ci    /// Same as `Ident::new`, but creates a raw identifier (`r#ident`). The
9707ac06127Sopenharmony_ci    /// `string` argument must be a valid identifier permitted by the language
9717ac06127Sopenharmony_ci    /// (including keywords, e.g. `fn`). Keywords which are usable in path
9727ac06127Sopenharmony_ci    /// segments (e.g. `self`, `super`) are not supported, and will cause a
9737ac06127Sopenharmony_ci    /// panic.
9747ac06127Sopenharmony_ci    #[track_caller]
9757ac06127Sopenharmony_ci    pub fn new_raw(string: &str, span: Span) -> Self {
9767ac06127Sopenharmony_ci        Ident::_new(imp::Ident::new_raw_checked(string, span.inner))
9777ac06127Sopenharmony_ci    }
9787ac06127Sopenharmony_ci
9797ac06127Sopenharmony_ci    /// Returns the span of this `Ident`.
9807ac06127Sopenharmony_ci    pub fn span(&self) -> Span {
9817ac06127Sopenharmony_ci        Span::_new(self.inner.span())
9827ac06127Sopenharmony_ci    }
9837ac06127Sopenharmony_ci
9847ac06127Sopenharmony_ci    /// Configures the span of this `Ident`, possibly changing its hygiene
9857ac06127Sopenharmony_ci    /// context.
9867ac06127Sopenharmony_ci    pub fn set_span(&mut self, span: Span) {
9877ac06127Sopenharmony_ci        self.inner.set_span(span.inner);
9887ac06127Sopenharmony_ci    }
9897ac06127Sopenharmony_ci}
9907ac06127Sopenharmony_ci
9917ac06127Sopenharmony_ciimpl PartialEq for Ident {
9927ac06127Sopenharmony_ci    fn eq(&self, other: &Ident) -> bool {
9937ac06127Sopenharmony_ci        self.inner == other.inner
9947ac06127Sopenharmony_ci    }
9957ac06127Sopenharmony_ci}
9967ac06127Sopenharmony_ci
9977ac06127Sopenharmony_ciimpl<T> PartialEq<T> for Ident
9987ac06127Sopenharmony_ciwhere
9997ac06127Sopenharmony_ci    T: ?Sized + AsRef<str>,
10007ac06127Sopenharmony_ci{
10017ac06127Sopenharmony_ci    fn eq(&self, other: &T) -> bool {
10027ac06127Sopenharmony_ci        self.inner == other
10037ac06127Sopenharmony_ci    }
10047ac06127Sopenharmony_ci}
10057ac06127Sopenharmony_ci
10067ac06127Sopenharmony_ciimpl Eq for Ident {}
10077ac06127Sopenharmony_ci
10087ac06127Sopenharmony_ciimpl PartialOrd for Ident {
10097ac06127Sopenharmony_ci    fn partial_cmp(&self, other: &Ident) -> Option<Ordering> {
10107ac06127Sopenharmony_ci        Some(self.cmp(other))
10117ac06127Sopenharmony_ci    }
10127ac06127Sopenharmony_ci}
10137ac06127Sopenharmony_ci
10147ac06127Sopenharmony_ciimpl Ord for Ident {
10157ac06127Sopenharmony_ci    fn cmp(&self, other: &Ident) -> Ordering {
10167ac06127Sopenharmony_ci        self.to_string().cmp(&other.to_string())
10177ac06127Sopenharmony_ci    }
10187ac06127Sopenharmony_ci}
10197ac06127Sopenharmony_ci
10207ac06127Sopenharmony_ciimpl Hash for Ident {
10217ac06127Sopenharmony_ci    fn hash<H: Hasher>(&self, hasher: &mut H) {
10227ac06127Sopenharmony_ci        self.to_string().hash(hasher);
10237ac06127Sopenharmony_ci    }
10247ac06127Sopenharmony_ci}
10257ac06127Sopenharmony_ci
10267ac06127Sopenharmony_ci/// Prints the identifier as a string that should be losslessly convertible back
10277ac06127Sopenharmony_ci/// into the same identifier.
10287ac06127Sopenharmony_ciimpl Display for Ident {
10297ac06127Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10307ac06127Sopenharmony_ci        Display::fmt(&self.inner, f)
10317ac06127Sopenharmony_ci    }
10327ac06127Sopenharmony_ci}
10337ac06127Sopenharmony_ci
10347ac06127Sopenharmony_ciimpl Debug for Ident {
10357ac06127Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10367ac06127Sopenharmony_ci        Debug::fmt(&self.inner, f)
10377ac06127Sopenharmony_ci    }
10387ac06127Sopenharmony_ci}
10397ac06127Sopenharmony_ci
10407ac06127Sopenharmony_ci/// A literal string (`"hello"`), byte string (`b"hello"`), character (`'a'`),
10417ac06127Sopenharmony_ci/// byte character (`b'a'`), an integer or floating point number with or without
10427ac06127Sopenharmony_ci/// a suffix (`1`, `1u8`, `2.3`, `2.3f32`).
10437ac06127Sopenharmony_ci///
10447ac06127Sopenharmony_ci/// Boolean literals like `true` and `false` do not belong here, they are
10457ac06127Sopenharmony_ci/// `Ident`s.
10467ac06127Sopenharmony_ci#[derive(Clone)]
10477ac06127Sopenharmony_cipub struct Literal {
10487ac06127Sopenharmony_ci    inner: imp::Literal,
10497ac06127Sopenharmony_ci    _marker: Marker,
10507ac06127Sopenharmony_ci}
10517ac06127Sopenharmony_ci
10527ac06127Sopenharmony_cimacro_rules! suffixed_int_literals {
10537ac06127Sopenharmony_ci    ($($name:ident => $kind:ident,)*) => ($(
10547ac06127Sopenharmony_ci        /// Creates a new suffixed integer literal with the specified value.
10557ac06127Sopenharmony_ci        ///
10567ac06127Sopenharmony_ci        /// This function will create an integer like `1u32` where the integer
10577ac06127Sopenharmony_ci        /// value specified is the first part of the token and the integral is
10587ac06127Sopenharmony_ci        /// also suffixed at the end. Literals created from negative numbers may
10597ac06127Sopenharmony_ci        /// not survive roundtrips through `TokenStream` or strings and may be
10607ac06127Sopenharmony_ci        /// broken into two tokens (`-` and positive literal).
10617ac06127Sopenharmony_ci        ///
10627ac06127Sopenharmony_ci        /// Literals created through this method have the `Span::call_site()`
10637ac06127Sopenharmony_ci        /// span by default, which can be configured with the `set_span` method
10647ac06127Sopenharmony_ci        /// below.
10657ac06127Sopenharmony_ci        pub fn $name(n: $kind) -> Literal {
10667ac06127Sopenharmony_ci            Literal::_new(imp::Literal::$name(n))
10677ac06127Sopenharmony_ci        }
10687ac06127Sopenharmony_ci    )*)
10697ac06127Sopenharmony_ci}
10707ac06127Sopenharmony_ci
10717ac06127Sopenharmony_cimacro_rules! unsuffixed_int_literals {
10727ac06127Sopenharmony_ci    ($($name:ident => $kind:ident,)*) => ($(
10737ac06127Sopenharmony_ci        /// Creates a new unsuffixed integer literal with the specified value.
10747ac06127Sopenharmony_ci        ///
10757ac06127Sopenharmony_ci        /// This function will create an integer like `1` where the integer
10767ac06127Sopenharmony_ci        /// value specified is the first part of the token. No suffix is
10777ac06127Sopenharmony_ci        /// specified on this token, meaning that invocations like
10787ac06127Sopenharmony_ci        /// `Literal::i8_unsuffixed(1)` are equivalent to
10797ac06127Sopenharmony_ci        /// `Literal::u32_unsuffixed(1)`. Literals created from negative numbers
10807ac06127Sopenharmony_ci        /// may not survive roundtrips through `TokenStream` or strings and may
10817ac06127Sopenharmony_ci        /// be broken into two tokens (`-` and positive literal).
10827ac06127Sopenharmony_ci        ///
10837ac06127Sopenharmony_ci        /// Literals created through this method have the `Span::call_site()`
10847ac06127Sopenharmony_ci        /// span by default, which can be configured with the `set_span` method
10857ac06127Sopenharmony_ci        /// below.
10867ac06127Sopenharmony_ci        pub fn $name(n: $kind) -> Literal {
10877ac06127Sopenharmony_ci            Literal::_new(imp::Literal::$name(n))
10887ac06127Sopenharmony_ci        }
10897ac06127Sopenharmony_ci    )*)
10907ac06127Sopenharmony_ci}
10917ac06127Sopenharmony_ci
10927ac06127Sopenharmony_ciimpl Literal {
10937ac06127Sopenharmony_ci    fn _new(inner: imp::Literal) -> Self {
10947ac06127Sopenharmony_ci        Literal {
10957ac06127Sopenharmony_ci            inner,
10967ac06127Sopenharmony_ci            _marker: Marker,
10977ac06127Sopenharmony_ci        }
10987ac06127Sopenharmony_ci    }
10997ac06127Sopenharmony_ci
11007ac06127Sopenharmony_ci    fn _new_fallback(inner: fallback::Literal) -> Self {
11017ac06127Sopenharmony_ci        Literal {
11027ac06127Sopenharmony_ci            inner: inner.into(),
11037ac06127Sopenharmony_ci            _marker: Marker,
11047ac06127Sopenharmony_ci        }
11057ac06127Sopenharmony_ci    }
11067ac06127Sopenharmony_ci
11077ac06127Sopenharmony_ci    suffixed_int_literals! {
11087ac06127Sopenharmony_ci        u8_suffixed => u8,
11097ac06127Sopenharmony_ci        u16_suffixed => u16,
11107ac06127Sopenharmony_ci        u32_suffixed => u32,
11117ac06127Sopenharmony_ci        u64_suffixed => u64,
11127ac06127Sopenharmony_ci        u128_suffixed => u128,
11137ac06127Sopenharmony_ci        usize_suffixed => usize,
11147ac06127Sopenharmony_ci        i8_suffixed => i8,
11157ac06127Sopenharmony_ci        i16_suffixed => i16,
11167ac06127Sopenharmony_ci        i32_suffixed => i32,
11177ac06127Sopenharmony_ci        i64_suffixed => i64,
11187ac06127Sopenharmony_ci        i128_suffixed => i128,
11197ac06127Sopenharmony_ci        isize_suffixed => isize,
11207ac06127Sopenharmony_ci    }
11217ac06127Sopenharmony_ci
11227ac06127Sopenharmony_ci    unsuffixed_int_literals! {
11237ac06127Sopenharmony_ci        u8_unsuffixed => u8,
11247ac06127Sopenharmony_ci        u16_unsuffixed => u16,
11257ac06127Sopenharmony_ci        u32_unsuffixed => u32,
11267ac06127Sopenharmony_ci        u64_unsuffixed => u64,
11277ac06127Sopenharmony_ci        u128_unsuffixed => u128,
11287ac06127Sopenharmony_ci        usize_unsuffixed => usize,
11297ac06127Sopenharmony_ci        i8_unsuffixed => i8,
11307ac06127Sopenharmony_ci        i16_unsuffixed => i16,
11317ac06127Sopenharmony_ci        i32_unsuffixed => i32,
11327ac06127Sopenharmony_ci        i64_unsuffixed => i64,
11337ac06127Sopenharmony_ci        i128_unsuffixed => i128,
11347ac06127Sopenharmony_ci        isize_unsuffixed => isize,
11357ac06127Sopenharmony_ci    }
11367ac06127Sopenharmony_ci
11377ac06127Sopenharmony_ci    /// Creates a new unsuffixed floating-point literal.
11387ac06127Sopenharmony_ci    ///
11397ac06127Sopenharmony_ci    /// This constructor is similar to those like `Literal::i8_unsuffixed` where
11407ac06127Sopenharmony_ci    /// the float's value is emitted directly into the token but no suffix is
11417ac06127Sopenharmony_ci    /// used, so it may be inferred to be a `f64` later in the compiler.
11427ac06127Sopenharmony_ci    /// Literals created from negative numbers may not survive round-trips
11437ac06127Sopenharmony_ci    /// through `TokenStream` or strings and may be broken into two tokens (`-`
11447ac06127Sopenharmony_ci    /// and positive literal).
11457ac06127Sopenharmony_ci    ///
11467ac06127Sopenharmony_ci    /// # Panics
11477ac06127Sopenharmony_ci    ///
11487ac06127Sopenharmony_ci    /// This function requires that the specified float is finite, for example
11497ac06127Sopenharmony_ci    /// if it is infinity or NaN this function will panic.
11507ac06127Sopenharmony_ci    pub fn f64_unsuffixed(f: f64) -> Literal {
11517ac06127Sopenharmony_ci        assert!(f.is_finite());
11527ac06127Sopenharmony_ci        Literal::_new(imp::Literal::f64_unsuffixed(f))
11537ac06127Sopenharmony_ci    }
11547ac06127Sopenharmony_ci
11557ac06127Sopenharmony_ci    /// Creates a new suffixed floating-point literal.
11567ac06127Sopenharmony_ci    ///
11577ac06127Sopenharmony_ci    /// This constructor will create a literal like `1.0f64` where the value
11587ac06127Sopenharmony_ci    /// specified is the preceding part of the token and `f64` is the suffix of
11597ac06127Sopenharmony_ci    /// the token. This token will always be inferred to be an `f64` in the
11607ac06127Sopenharmony_ci    /// compiler. Literals created from negative numbers may not survive
11617ac06127Sopenharmony_ci    /// round-trips through `TokenStream` or strings and may be broken into two
11627ac06127Sopenharmony_ci    /// tokens (`-` and positive literal).
11637ac06127Sopenharmony_ci    ///
11647ac06127Sopenharmony_ci    /// # Panics
11657ac06127Sopenharmony_ci    ///
11667ac06127Sopenharmony_ci    /// This function requires that the specified float is finite, for example
11677ac06127Sopenharmony_ci    /// if it is infinity or NaN this function will panic.
11687ac06127Sopenharmony_ci    pub fn f64_suffixed(f: f64) -> Literal {
11697ac06127Sopenharmony_ci        assert!(f.is_finite());
11707ac06127Sopenharmony_ci        Literal::_new(imp::Literal::f64_suffixed(f))
11717ac06127Sopenharmony_ci    }
11727ac06127Sopenharmony_ci
11737ac06127Sopenharmony_ci    /// Creates a new unsuffixed floating-point literal.
11747ac06127Sopenharmony_ci    ///
11757ac06127Sopenharmony_ci    /// This constructor is similar to those like `Literal::i8_unsuffixed` where
11767ac06127Sopenharmony_ci    /// the float's value is emitted directly into the token but no suffix is
11777ac06127Sopenharmony_ci    /// used, so it may be inferred to be a `f64` later in the compiler.
11787ac06127Sopenharmony_ci    /// Literals created from negative numbers may not survive round-trips
11797ac06127Sopenharmony_ci    /// through `TokenStream` or strings and may be broken into two tokens (`-`
11807ac06127Sopenharmony_ci    /// and positive literal).
11817ac06127Sopenharmony_ci    ///
11827ac06127Sopenharmony_ci    /// # Panics
11837ac06127Sopenharmony_ci    ///
11847ac06127Sopenharmony_ci    /// This function requires that the specified float is finite, for example
11857ac06127Sopenharmony_ci    /// if it is infinity or NaN this function will panic.
11867ac06127Sopenharmony_ci    pub fn f32_unsuffixed(f: f32) -> Literal {
11877ac06127Sopenharmony_ci        assert!(f.is_finite());
11887ac06127Sopenharmony_ci        Literal::_new(imp::Literal::f32_unsuffixed(f))
11897ac06127Sopenharmony_ci    }
11907ac06127Sopenharmony_ci
11917ac06127Sopenharmony_ci    /// Creates a new suffixed floating-point literal.
11927ac06127Sopenharmony_ci    ///
11937ac06127Sopenharmony_ci    /// This constructor will create a literal like `1.0f32` where the value
11947ac06127Sopenharmony_ci    /// specified is the preceding part of the token and `f32` is the suffix of
11957ac06127Sopenharmony_ci    /// the token. This token will always be inferred to be an `f32` in the
11967ac06127Sopenharmony_ci    /// compiler. Literals created from negative numbers may not survive
11977ac06127Sopenharmony_ci    /// round-trips through `TokenStream` or strings and may be broken into two
11987ac06127Sopenharmony_ci    /// tokens (`-` and positive literal).
11997ac06127Sopenharmony_ci    ///
12007ac06127Sopenharmony_ci    /// # Panics
12017ac06127Sopenharmony_ci    ///
12027ac06127Sopenharmony_ci    /// This function requires that the specified float is finite, for example
12037ac06127Sopenharmony_ci    /// if it is infinity or NaN this function will panic.
12047ac06127Sopenharmony_ci    pub fn f32_suffixed(f: f32) -> Literal {
12057ac06127Sopenharmony_ci        assert!(f.is_finite());
12067ac06127Sopenharmony_ci        Literal::_new(imp::Literal::f32_suffixed(f))
12077ac06127Sopenharmony_ci    }
12087ac06127Sopenharmony_ci
12097ac06127Sopenharmony_ci    /// String literal.
12107ac06127Sopenharmony_ci    pub fn string(string: &str) -> Literal {
12117ac06127Sopenharmony_ci        Literal::_new(imp::Literal::string(string))
12127ac06127Sopenharmony_ci    }
12137ac06127Sopenharmony_ci
12147ac06127Sopenharmony_ci    /// Character literal.
12157ac06127Sopenharmony_ci    pub fn character(ch: char) -> Literal {
12167ac06127Sopenharmony_ci        Literal::_new(imp::Literal::character(ch))
12177ac06127Sopenharmony_ci    }
12187ac06127Sopenharmony_ci
12197ac06127Sopenharmony_ci    /// Byte string literal.
12207ac06127Sopenharmony_ci    pub fn byte_string(s: &[u8]) -> Literal {
12217ac06127Sopenharmony_ci        Literal::_new(imp::Literal::byte_string(s))
12227ac06127Sopenharmony_ci    }
12237ac06127Sopenharmony_ci
12247ac06127Sopenharmony_ci    /// Returns the span encompassing this literal.
12257ac06127Sopenharmony_ci    pub fn span(&self) -> Span {
12267ac06127Sopenharmony_ci        Span::_new(self.inner.span())
12277ac06127Sopenharmony_ci    }
12287ac06127Sopenharmony_ci
12297ac06127Sopenharmony_ci    /// Configures the span associated for this literal.
12307ac06127Sopenharmony_ci    pub fn set_span(&mut self, span: Span) {
12317ac06127Sopenharmony_ci        self.inner.set_span(span.inner);
12327ac06127Sopenharmony_ci    }
12337ac06127Sopenharmony_ci
12347ac06127Sopenharmony_ci    /// Returns a `Span` that is a subset of `self.span()` containing only
12357ac06127Sopenharmony_ci    /// the source bytes in range `range`. Returns `None` if the would-be
12367ac06127Sopenharmony_ci    /// trimmed span is outside the bounds of `self`.
12377ac06127Sopenharmony_ci    ///
12387ac06127Sopenharmony_ci    /// Warning: the underlying [`proc_macro::Literal::subspan`] method is
12397ac06127Sopenharmony_ci    /// nightly-only. When called from within a procedural macro not using a
12407ac06127Sopenharmony_ci    /// nightly compiler, this method will always return `None`.
12417ac06127Sopenharmony_ci    ///
12427ac06127Sopenharmony_ci    /// [`proc_macro::Literal::subspan`]: https://doc.rust-lang.org/proc_macro/struct.Literal.html#method.subspan
12437ac06127Sopenharmony_ci    pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> {
12447ac06127Sopenharmony_ci        self.inner.subspan(range).map(Span::_new)
12457ac06127Sopenharmony_ci    }
12467ac06127Sopenharmony_ci
12477ac06127Sopenharmony_ci    // Intended for the `quote!` macro to use when constructing a proc-macro2
12487ac06127Sopenharmony_ci    // token out of a macro_rules $:literal token, which is already known to be
12497ac06127Sopenharmony_ci    // a valid literal. This avoids reparsing/validating the literal's string
12507ac06127Sopenharmony_ci    // representation. This is not public API other than for quote.
12517ac06127Sopenharmony_ci    #[doc(hidden)]
12527ac06127Sopenharmony_ci    pub unsafe fn from_str_unchecked(repr: &str) -> Self {
12537ac06127Sopenharmony_ci        Literal::_new(unsafe { imp::Literal::from_str_unchecked(repr) })
12547ac06127Sopenharmony_ci    }
12557ac06127Sopenharmony_ci}
12567ac06127Sopenharmony_ci
12577ac06127Sopenharmony_ciimpl FromStr for Literal {
12587ac06127Sopenharmony_ci    type Err = LexError;
12597ac06127Sopenharmony_ci
12607ac06127Sopenharmony_ci    fn from_str(repr: &str) -> Result<Self, LexError> {
12617ac06127Sopenharmony_ci        repr.parse().map(Literal::_new).map_err(|inner| LexError {
12627ac06127Sopenharmony_ci            inner,
12637ac06127Sopenharmony_ci            _marker: Marker,
12647ac06127Sopenharmony_ci        })
12657ac06127Sopenharmony_ci    }
12667ac06127Sopenharmony_ci}
12677ac06127Sopenharmony_ci
12687ac06127Sopenharmony_ciimpl Debug for Literal {
12697ac06127Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
12707ac06127Sopenharmony_ci        Debug::fmt(&self.inner, f)
12717ac06127Sopenharmony_ci    }
12727ac06127Sopenharmony_ci}
12737ac06127Sopenharmony_ci
12747ac06127Sopenharmony_ciimpl Display for Literal {
12757ac06127Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
12767ac06127Sopenharmony_ci        Display::fmt(&self.inner, f)
12777ac06127Sopenharmony_ci    }
12787ac06127Sopenharmony_ci}
12797ac06127Sopenharmony_ci
12807ac06127Sopenharmony_ci/// Public implementation details for the `TokenStream` type, such as iterators.
12817ac06127Sopenharmony_cipub mod token_stream {
12827ac06127Sopenharmony_ci    use crate::marker::Marker;
12837ac06127Sopenharmony_ci    use crate::{imp, TokenTree};
12847ac06127Sopenharmony_ci    use core::fmt::{self, Debug};
12857ac06127Sopenharmony_ci
12867ac06127Sopenharmony_ci    pub use crate::TokenStream;
12877ac06127Sopenharmony_ci
12887ac06127Sopenharmony_ci    /// An iterator over `TokenStream`'s `TokenTree`s.
12897ac06127Sopenharmony_ci    ///
12907ac06127Sopenharmony_ci    /// The iteration is "shallow", e.g. the iterator doesn't recurse into
12917ac06127Sopenharmony_ci    /// delimited groups, and returns whole groups as token trees.
12927ac06127Sopenharmony_ci    #[derive(Clone)]
12937ac06127Sopenharmony_ci    pub struct IntoIter {
12947ac06127Sopenharmony_ci        inner: imp::TokenTreeIter,
12957ac06127Sopenharmony_ci        _marker: Marker,
12967ac06127Sopenharmony_ci    }
12977ac06127Sopenharmony_ci
12987ac06127Sopenharmony_ci    impl Iterator for IntoIter {
12997ac06127Sopenharmony_ci        type Item = TokenTree;
13007ac06127Sopenharmony_ci
13017ac06127Sopenharmony_ci        fn next(&mut self) -> Option<TokenTree> {
13027ac06127Sopenharmony_ci            self.inner.next()
13037ac06127Sopenharmony_ci        }
13047ac06127Sopenharmony_ci
13057ac06127Sopenharmony_ci        fn size_hint(&self) -> (usize, Option<usize>) {
13067ac06127Sopenharmony_ci            self.inner.size_hint()
13077ac06127Sopenharmony_ci        }
13087ac06127Sopenharmony_ci    }
13097ac06127Sopenharmony_ci
13107ac06127Sopenharmony_ci    impl Debug for IntoIter {
13117ac06127Sopenharmony_ci        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
13127ac06127Sopenharmony_ci            f.write_str("TokenStream ")?;
13137ac06127Sopenharmony_ci            f.debug_list().entries(self.clone()).finish()
13147ac06127Sopenharmony_ci        }
13157ac06127Sopenharmony_ci    }
13167ac06127Sopenharmony_ci
13177ac06127Sopenharmony_ci    impl IntoIterator for TokenStream {
13187ac06127Sopenharmony_ci        type Item = TokenTree;
13197ac06127Sopenharmony_ci        type IntoIter = IntoIter;
13207ac06127Sopenharmony_ci
13217ac06127Sopenharmony_ci        fn into_iter(self) -> IntoIter {
13227ac06127Sopenharmony_ci            IntoIter {
13237ac06127Sopenharmony_ci                inner: self.inner.into_iter(),
13247ac06127Sopenharmony_ci                _marker: Marker,
13257ac06127Sopenharmony_ci            }
13267ac06127Sopenharmony_ci        }
13277ac06127Sopenharmony_ci    }
13287ac06127Sopenharmony_ci}
1329