2016-10-03 09:49:39 -07:00
|
|
|
//! A support library for macro authors when defining new macros.
|
2016-08-04 12:20:01 -07:00
|
|
|
//!
|
2016-10-03 09:49:39 -07:00
|
|
|
//! This library, provided by the standard distribution, provides the types
|
2018-05-13 18:35:57 +03:00
|
|
|
//! consumed in the interfaces of procedurally defined macro definitions such as
|
2018-08-19 15:30:23 +02:00
|
|
|
//! function-like macros `#[proc_macro]`, macro attributes `#[proc_macro_attribute]` and
|
2018-05-13 18:35:57 +03:00
|
|
|
//! custom derive attributes`#[proc_macro_derive]`.
|
2016-08-04 12:20:01 -07:00
|
|
|
//!
|
2019-01-23 03:55:37 +00:00
|
|
|
//! See [the book] for more.
|
|
|
|
//!
|
|
|
|
//! [the book]: ../book/ch19-06-macros.html#procedural-macros-for-generating-code-from-attributes
|
2016-08-04 12:20:01 -07:00
|
|
|
|
2017-01-01 16:14:35 -08:00
|
|
|
#![stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2016-10-03 09:49:39 -07:00
|
|
|
#![deny(missing_docs)]
|
2019-12-22 17:42:04 -05:00
|
|
|
#![doc(
|
|
|
|
html_root_url = "https://doc.rust-lang.org/nightly/",
|
|
|
|
html_playground_url = "https://play.rust-lang.org/",
|
|
|
|
issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
|
|
|
|
test(no_crate_inject, attr(deny(warnings))),
|
|
|
|
test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
|
|
|
|
)]
|
2019-02-10 16:13:30 +09:00
|
|
|
#![feature(nll)]
|
2016-10-03 09:49:39 -07:00
|
|
|
#![feature(staged_api)]
|
2019-08-20 02:26:40 +03:00
|
|
|
#![feature(allow_internal_unstable)]
|
|
|
|
#![feature(decl_macro)]
|
2018-03-16 01:09:22 +02:00
|
|
|
#![feature(extern_types)]
|
|
|
|
#![feature(in_band_lifetimes)]
|
2020-04-22 15:45:35 -04:00
|
|
|
#![feature(negative_impls)]
|
2018-03-02 02:42:22 +01:00
|
|
|
#![feature(optin_builtin_traits)]
|
2020-05-31 18:09:25 -07:00
|
|
|
#![feature(restricted_std)]
|
2019-08-20 02:26:40 +03:00
|
|
|
#![feature(rustc_attrs)]
|
2020-04-22 15:45:35 -04:00
|
|
|
#![feature(min_specialization)]
|
2019-12-22 17:42:04 -05:00
|
|
|
#![recursion_limit = "256"]
|
2018-03-03 06:22:19 +01:00
|
|
|
|
2018-03-16 01:09:22 +02:00
|
|
|
#[unstable(feature = "proc_macro_internals", issue = "27812")]
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub mod bridge;
|
|
|
|
|
2017-08-28 02:56:43 -07:00
|
|
|
mod diagnostic;
|
|
|
|
|
2018-09-13 08:16:54 +00:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2018-09-19 19:22:21 -07:00
|
|
|
pub use diagnostic::{Diagnostic, Level, MultiSpan};
|
2016-08-04 12:20:01 -07:00
|
|
|
|
2020-05-21 17:05:13 -07:00
|
|
|
use std::cmp::Ordering;
|
2018-11-20 21:12:30 -08:00
|
|
|
use std::ops::{Bound, RangeBounds};
|
2018-03-16 17:04:14 +02:00
|
|
|
use std::path::PathBuf;
|
2016-10-03 09:49:39 -07:00
|
|
|
use std::str::FromStr;
|
2020-02-06 12:25:18 -06:00
|
|
|
use std::{error, fmt, iter, mem};
|
2016-10-03 09:49:39 -07:00
|
|
|
|
2020-04-21 12:28:11 -07:00
|
|
|
/// Determines whether proc_macro has been made accessible to the currently
|
|
|
|
/// running program.
|
|
|
|
///
|
|
|
|
/// The proc_macro crate is only intended for use inside the implementation of
|
|
|
|
/// procedural macros. All the functions in this crate panic if invoked from
|
|
|
|
/// outside of a procedural macro, such as from a build script or unit test or
|
|
|
|
/// ordinary Rust binary.
|
|
|
|
///
|
|
|
|
/// With consideration for Rust libraries that are designed to support both
|
|
|
|
/// macro and non-macro use cases, `proc_macro::is_available()` provides a
|
|
|
|
/// non-panicking way to detect whether the infrastructure required to use the
|
|
|
|
/// API of proc_macro is presently available. Returns true if invoked from
|
|
|
|
/// inside of a procedural macro, false if invoked from any other binary.
|
2020-04-22 11:07:07 -07:00
|
|
|
#[unstable(feature = "proc_macro_is_available", issue = "71436")]
|
2020-04-21 12:28:11 -07:00
|
|
|
pub fn is_available() -> bool {
|
|
|
|
bridge::Bridge::is_available()
|
|
|
|
}
|
|
|
|
|
2016-10-03 09:49:39 -07:00
|
|
|
/// The main type provided by this crate, representing an abstract stream of
|
2018-05-05 20:12:37 +03:00
|
|
|
/// tokens, or, more specifically, a sequence of token trees.
|
|
|
|
/// The type provide interfaces for iterating over those token trees and, conversely,
|
|
|
|
/// collecting a number of token trees into one stream.
|
2016-10-03 09:49:39 -07:00
|
|
|
///
|
2018-05-05 20:12:37 +03:00
|
|
|
/// This is both the input and output of `#[proc_macro]`, `#[proc_macro_attribute]`
|
|
|
|
/// and `#[proc_macro_derive]` definitions.
|
2017-01-01 16:14:35 -08:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
#[derive(Clone)]
|
2018-03-16 01:09:22 +02:00
|
|
|
pub struct TokenStream(bridge::client::TokenStream);
|
2016-10-03 09:49:39 -07:00
|
|
|
|
2018-05-13 18:46:38 +03:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2018-05-04 14:11:22 -07:00
|
|
|
impl !Send for TokenStream {}
|
2018-05-13 18:46:38 +03:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2018-05-04 14:11:22 -07:00
|
|
|
impl !Sync for TokenStream {}
|
|
|
|
|
2016-10-03 09:49:39 -07:00
|
|
|
/// Error returned from `TokenStream::from_str`.
|
2017-01-01 16:14:35 -08:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2017-06-05 01:41:33 +00:00
|
|
|
#[derive(Debug)]
|
2016-10-03 09:49:39 -07:00
|
|
|
pub struct LexError {
|
|
|
|
_inner: (),
|
|
|
|
}
|
|
|
|
|
2020-02-08 09:55:49 -06:00
|
|
|
#[stable(feature = "proc_macro_lexerror_impls", since = "1.44.0")]
|
2020-02-06 12:25:18 -06:00
|
|
|
impl fmt::Display for LexError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-02-08 10:51:54 -06:00
|
|
|
f.write_str("cannot parse string into token stream")
|
2020-02-06 12:25:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 09:55:49 -06:00
|
|
|
#[stable(feature = "proc_macro_lexerror_impls", since = "1.44.0")]
|
2020-02-06 12:25:18 -06:00
|
|
|
impl error::Error for LexError {}
|
|
|
|
|
2018-05-13 18:46:38 +03:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2018-05-04 14:11:22 -07:00
|
|
|
impl !Send for LexError {}
|
2018-05-13 18:46:38 +03:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2018-05-04 14:11:22 -07:00
|
|
|
impl !Sync for LexError {}
|
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
impl TokenStream {
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Returns an empty `TokenStream` containing no token trees.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-25 19:44:07 -07:00
|
|
|
pub fn new() -> TokenStream {
|
2018-03-16 01:09:22 +02:00
|
|
|
TokenStream(bridge::client::TokenStream::new())
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if this `TokenStream` is empty.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.0.is_empty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Attempts to break the string into tokens and parse those tokens into a token stream.
|
|
|
|
/// May fail for a number of reasons, for example, if the string contains unbalanced delimiters
|
|
|
|
/// or characters not existing in the language.
|
2018-06-24 20:02:24 +03:00
|
|
|
/// All tokens in the parsed stream get `Span::call_site()` spans.
|
2018-05-05 20:12:37 +03:00
|
|
|
///
|
2019-02-09 21:23:30 +00:00
|
|
|
/// NOTE: some errors may cause panics instead of returning `LexError`. We reserve the right to
|
2018-05-13 18:35:57 +03:00
|
|
|
/// change these errors into `LexError`s later.
|
2017-03-17 23:41:09 +00:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
|
|
|
impl FromStr for TokenStream {
|
|
|
|
type Err = LexError;
|
|
|
|
|
|
|
|
fn from_str(src: &str) -> Result<TokenStream, LexError> {
|
2018-03-16 01:09:22 +02:00
|
|
|
Ok(TokenStream(bridge::client::TokenStream::from_str(src)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
|
2018-03-16 01:09:22 +02:00
|
|
|
// based on it (the reverse of the usual relationship between the two).
|
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
|
|
|
impl ToString for TokenStream {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
self.0.to_string()
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-13 18:35:57 +03:00
|
|
|
/// Prints the token stream as a string that is supposed to be losslessly convertible back
|
2018-05-05 20:12:37 +03:00
|
|
|
/// into the same token stream (modulo spans), except for possibly `TokenTree::Group`s
|
2018-05-13 18:35:57 +03:00
|
|
|
/// with `Delimiter::None` delimiters and negative numeric literals.
|
2017-03-17 23:41:09 +00:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
|
|
|
impl fmt::Display for TokenStream {
|
2019-02-04 03:55:40 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-16 01:09:22 +02:00
|
|
|
f.write_str(&self.to_string())
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Prints token in a form convenient for debugging.
|
2018-04-02 08:19:32 -07:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
|
|
|
impl fmt::Debug for TokenStream {
|
2019-02-04 03:55:40 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-04-06 15:20:57 -07:00
|
|
|
f.write_str("TokenStream ")?;
|
|
|
|
f.debug_list().entries(self.clone()).finish()
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
}
|
2017-06-05 01:41:33 +00:00
|
|
|
|
2020-05-15 09:12:41 -07:00
|
|
|
#[stable(feature = "proc_macro_token_stream_default", since = "1.45.0")]
|
|
|
|
impl Default for TokenStream {
|
|
|
|
fn default() -> Self {
|
|
|
|
TokenStream::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 10:42:16 -07:00
|
|
|
#[unstable(feature = "proc_macro_quote", issue = "54722")]
|
2018-04-22 03:05:02 +03:00
|
|
|
pub use quote::{quote, quote_span};
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Creates a token stream containing a single token tree.
|
2019-12-22 17:42:04 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 23:41:09 +00:00
|
|
|
impl From<TokenTree> for TokenStream {
|
|
|
|
fn from(tree: TokenTree) -> TokenStream {
|
2018-03-16 01:09:22 +02:00
|
|
|
TokenStream(bridge::client::TokenStream::from_token_tree(match tree {
|
|
|
|
TokenTree::Group(tt) => bridge::TokenTree::Group(tt.0),
|
|
|
|
TokenTree::Punct(tt) => bridge::TokenTree::Punct(tt.0),
|
|
|
|
TokenTree::Ident(tt) => bridge::TokenTree::Ident(tt.0),
|
2019-12-22 17:42:04 -05:00
|
|
|
TokenTree::Literal(tt) => bridge::TokenTree::Literal(tt.0),
|
2018-03-16 01:09:22 +02:00
|
|
|
}))
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Collects a number of token trees into a single stream.
|
2019-12-22 17:42:04 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
impl iter::FromIterator<TokenTree> for TokenStream {
|
|
|
|
fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
|
2018-04-06 07:44:21 -07:00
|
|
|
trees.into_iter().map(TokenStream::from).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// A "flattening" operation on token streams, collects token trees
|
|
|
|
/// from multiple token streams into a single stream.
|
2018-05-13 18:46:38 +03:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2018-04-06 07:44:21 -07:00
|
|
|
impl iter::FromIterator<TokenStream> for TokenStream {
|
|
|
|
fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
|
2018-03-16 01:09:22 +02:00
|
|
|
let mut builder = bridge::client::TokenStreamBuilder::new();
|
2019-04-05 14:51:07 -07:00
|
|
|
streams.into_iter().for_each(|stream| builder.push(stream.0));
|
2017-03-17 23:41:09 +00:00
|
|
|
TokenStream(builder.build())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 12:45:48 -07:00
|
|
|
#[stable(feature = "token_stream_extend", since = "1.30.0")]
|
|
|
|
impl Extend<TokenTree> for TokenStream {
|
|
|
|
fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, trees: I) {
|
|
|
|
self.extend(trees.into_iter().map(TokenStream::from));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "token_stream_extend", since = "1.30.0")]
|
|
|
|
impl Extend<TokenStream> for TokenStream {
|
|
|
|
fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
|
2018-03-16 01:09:22 +02:00
|
|
|
// FIXME(eddyb) Use an optimized implementation if/when possible.
|
|
|
|
*self = iter::once(mem::replace(self, Self::new())).chain(streams).collect();
|
2018-08-12 12:45:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Public implementation details for the `TokenStream` type, such as iterators.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub mod token_stream {
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::{bridge, Group, Ident, Literal, Punct, TokenStream, TokenTree};
|
2017-03-17 23:41:09 +00:00
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// An iterator over `TokenStream`'s `TokenTree`s.
|
2018-11-27 02:59:49 +00:00
|
|
|
/// The iteration is "shallow", e.g., the iterator doesn't recurse into delimited groups,
|
2018-05-05 20:12:37 +03:00
|
|
|
/// and returns whole groups as token trees.
|
2018-04-02 08:19:32 -07:00
|
|
|
#[derive(Clone)]
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-03-16 01:09:22 +02:00
|
|
|
pub struct IntoIter(bridge::client::TokenStreamIter);
|
2017-03-17 23:41:09 +00:00
|
|
|
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
impl Iterator for IntoIter {
|
|
|
|
type Item = TokenTree;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<TokenTree> {
|
2018-03-16 01:09:22 +02:00
|
|
|
self.0.next().map(|tree| match tree {
|
|
|
|
bridge::TokenTree::Group(tt) => TokenTree::Group(Group(tt)),
|
|
|
|
bridge::TokenTree::Punct(tt) => TokenTree::Punct(Punct(tt)),
|
|
|
|
bridge::TokenTree::Ident(tt) => TokenTree::Ident(Ident(tt)),
|
|
|
|
bridge::TokenTree::Literal(tt) => TokenTree::Literal(Literal(tt)),
|
|
|
|
})
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
impl IntoIterator for TokenStream {
|
|
|
|
type Item = TokenTree;
|
|
|
|
type IntoIter = IntoIter;
|
|
|
|
|
|
|
|
fn into_iter(self) -> IntoIter {
|
2018-03-16 01:09:22 +02:00
|
|
|
IntoIter(self.0.into_iter())
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
/// `quote!(..)` accepts arbitrary tokens and expands into a `TokenStream` describing the input.
|
|
|
|
/// For example, `quote!(a + b)` will produce a expression, that, when evaluated, constructs
|
2018-05-13 18:35:57 +03:00
|
|
|
/// the `TokenStream` `[Ident("a"), Punct('+', Alone), Ident("b")]`.
|
2018-04-02 08:19:32 -07:00
|
|
|
///
|
|
|
|
/// Unquoting is done with `$`, and works by taking the single next ident as the unquoted term.
|
|
|
|
/// To quote `$` itself, use `$$`.
|
2018-10-01 10:42:16 -07:00
|
|
|
#[unstable(feature = "proc_macro_quote", issue = "54722")]
|
2019-08-20 02:26:40 +03:00
|
|
|
#[allow_internal_unstable(proc_macro_def_site)]
|
2019-09-25 08:42:46 -04:00
|
|
|
#[rustc_builtin_macro]
|
2019-12-22 17:42:04 -05:00
|
|
|
pub macro quote($($t:tt)*) {
|
|
|
|
/* compiler built-in */
|
|
|
|
}
|
2017-03-17 23:41:09 +00:00
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
#[unstable(feature = "proc_macro_internals", issue = "27812")]
|
|
|
|
#[doc(hidden)]
|
|
|
|
mod quote;
|
2017-03-17 23:41:09 +00:00
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
/// A region of source code, along with macro expansion information.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-06 15:20:57 -07:00
|
|
|
#[derive(Copy, Clone)]
|
2018-03-16 01:09:22 +02:00
|
|
|
pub struct Span(bridge::client::Span);
|
2018-04-02 08:19:32 -07:00
|
|
|
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-04 14:11:22 -07:00
|
|
|
impl !Send for Span {}
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-04 14:11:22 -07:00
|
|
|
impl !Sync for Span {}
|
|
|
|
|
2017-08-28 02:56:43 -07:00
|
|
|
macro_rules! diagnostic_method {
|
2020-04-22 16:16:43 -04:00
|
|
|
($name:ident, $level:expr) => {
|
2019-02-09 21:23:30 +00:00
|
|
|
/// Creates a new `Diagnostic` with the given `message` at the span
|
2017-08-28 02:56:43 -07:00
|
|
|
/// `self`.
|
2018-09-13 08:16:54 +00:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2017-08-28 02:56:43 -07:00
|
|
|
pub fn $name<T: Into<String>>(self, message: T) -> Diagnostic {
|
|
|
|
Diagnostic::spanned(self, $level, message)
|
|
|
|
}
|
2020-04-22 16:16:43 -04:00
|
|
|
};
|
2017-08-28 02:56:43 -07:00
|
|
|
}
|
|
|
|
|
2017-03-17 23:41:09 +00:00
|
|
|
impl Span {
|
2018-04-02 08:19:32 -07:00
|
|
|
/// A span that resolves at the macro definition site.
|
2018-10-01 10:47:18 -07:00
|
|
|
#[unstable(feature = "proc_macro_def_site", issue = "54724")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn def_site() -> Span {
|
2018-03-16 01:09:22 +02:00
|
|
|
Span(bridge::client::Span::def_site())
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
|
2017-03-17 23:41:09 +00:00
|
|
|
/// The span of the invocation of the current procedural macro.
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Identifiers created with this span will be resolved as if they were written
|
|
|
|
/// directly at the macro call location (call-site hygiene) and other code
|
|
|
|
/// at the macro call site will be able to refer to them as well.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 23:41:09 +00:00
|
|
|
pub fn call_site() -> Span {
|
2018-03-16 01:09:22 +02:00
|
|
|
Span(bridge::client::Span::call_site())
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
2017-08-28 02:56:43 -07:00
|
|
|
|
2019-09-22 18:38:02 +03:00
|
|
|
/// A span that represents `macro_rules` hygiene, and sometimes resolves at the macro
|
|
|
|
/// definition site (local variables, labels, `$crate`) and sometimes at the macro
|
|
|
|
/// call site (everything else).
|
|
|
|
/// The span location is taken from the call-site.
|
2020-02-01 00:18:14 +03:00
|
|
|
#[stable(feature = "proc_macro_mixed_site", since = "1.45.0")]
|
2019-09-22 18:38:02 +03:00
|
|
|
pub fn mixed_site() -> Span {
|
|
|
|
Span(bridge::client::Span::mixed_site())
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:05:08 -07:00
|
|
|
/// The original source file into which this span points.
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 18:05:08 -07:00
|
|
|
pub fn source_file(&self) -> SourceFile {
|
2018-03-16 01:09:22 +02:00
|
|
|
SourceFile(self.0.source_file())
|
2017-08-01 18:05:08 -07:00
|
|
|
}
|
|
|
|
|
2017-12-31 18:30:13 -08:00
|
|
|
/// The `Span` for the tokens in the previous macro expansion from which
|
|
|
|
/// `self` was generated from, if any.
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-12-31 18:30:13 -08:00
|
|
|
pub fn parent(&self) -> Option<Span> {
|
2018-04-26 18:28:34 -04:00
|
|
|
self.0.parent().map(Span)
|
2017-12-31 18:30:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The span for the origin source code that `self` was generated from. If
|
|
|
|
/// this `Span` wasn't generated from other macro expansions then the return
|
|
|
|
/// value is the same as `*self`.
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-12-31 18:30:13 -08:00
|
|
|
pub fn source(&self) -> Span {
|
2018-03-16 01:09:22 +02:00
|
|
|
Span(self.0.source())
|
2017-12-31 18:30:13 -08:00
|
|
|
}
|
|
|
|
|
2019-02-09 21:23:30 +00:00
|
|
|
/// Gets the starting line/column in the source file for this span.
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 18:05:08 -07:00
|
|
|
pub fn start(&self) -> LineColumn {
|
2018-03-16 01:09:22 +02:00
|
|
|
self.0.start()
|
2017-08-01 18:05:08 -07:00
|
|
|
}
|
|
|
|
|
2019-02-09 21:23:30 +00:00
|
|
|
/// Gets the ending line/column in the source file for this span.
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 18:05:08 -07:00
|
|
|
pub fn end(&self) -> LineColumn {
|
2018-03-16 01:09:22 +02:00
|
|
|
self.0.end()
|
2017-08-01 18:05:08 -07:00
|
|
|
}
|
|
|
|
|
2019-02-09 21:23:30 +00:00
|
|
|
/// Creates a new span encompassing `self` and `other`.
|
2017-08-01 18:05:08 -07:00
|
|
|
///
|
|
|
|
/// Returns `None` if `self` and `other` are from different files.
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 18:05:08 -07:00
|
|
|
pub fn join(&self, other: Span) -> Option<Span> {
|
2018-03-16 01:09:22 +02:00
|
|
|
self.0.join(other.0).map(Span)
|
2017-08-01 18:05:08 -07:00
|
|
|
}
|
|
|
|
|
2018-01-02 23:29:11 -08:00
|
|
|
/// Creates a new span with the same line/column information as `self` but
|
|
|
|
/// that resolves symbols as though it were at `other`.
|
2020-04-26 12:08:42 +02:00
|
|
|
#[stable(feature = "proc_macro_span_resolved_at", since = "1.45.0")]
|
2018-01-02 23:29:11 -08:00
|
|
|
pub fn resolved_at(&self, other: Span) -> Span {
|
2018-03-16 01:09:22 +02:00
|
|
|
Span(self.0.resolved_at(other.0))
|
2018-01-02 23:29:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new span with the same name resolution behavior as `self` but
|
|
|
|
/// with the line/column information of `other`.
|
2020-04-26 12:08:42 +02:00
|
|
|
#[stable(feature = "proc_macro_span_located_at", since = "1.45.0")]
|
2018-01-02 23:29:11 -08:00
|
|
|
pub fn located_at(&self, other: Span) -> Span {
|
|
|
|
other.resolved_at(*self)
|
|
|
|
}
|
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
/// Compares to spans to see if they're equal.
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn eq(&self, other: &Span) -> bool {
|
|
|
|
self.0 == other.0
|
|
|
|
}
|
|
|
|
|
2018-11-08 10:07:02 +01:00
|
|
|
/// Returns the source text behind a span. This preserves the original source
|
|
|
|
/// code, including spaces and comments. It only returns a result if the span
|
|
|
|
/// corresponds to real source code.
|
|
|
|
///
|
|
|
|
/// Note: The observable result of a macro should only rely on the tokens and
|
|
|
|
/// not on this source text. The result of this function is a best effort to
|
|
|
|
/// be used for diagnostics only.
|
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
|
|
|
pub fn source_text(&self) -> Option<String> {
|
|
|
|
self.0.source_text()
|
|
|
|
}
|
|
|
|
|
2017-08-28 02:56:43 -07:00
|
|
|
diagnostic_method!(error, Level::Error);
|
|
|
|
diagnostic_method!(warning, Level::Warning);
|
|
|
|
diagnostic_method!(note, Level::Note);
|
|
|
|
diagnostic_method!(help, Level::Help);
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Prints a span in a form convenient for debugging.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-06 15:20:57 -07:00
|
|
|
impl fmt::Debug for Span {
|
2019-02-04 03:55:40 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-16 01:09:22 +02:00
|
|
|
self.0.fmt(f)
|
2018-04-06 15:20:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:05:08 -07:00
|
|
|
/// A line-column pair representing the start or end of a `Span`.
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 18:05:08 -07:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub struct LineColumn {
|
|
|
|
/// The 1-indexed line in the source file on which the span starts or ends (inclusive).
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-12-12 12:14:54 -05:00
|
|
|
pub line: usize,
|
2017-08-01 18:05:08 -07:00
|
|
|
/// The 0-indexed column (in UTF-8 characters) in the source file on which
|
|
|
|
/// the span starts or ends (inclusive).
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2019-12-22 17:42:04 -05:00
|
|
|
pub column: usize,
|
2017-08-01 18:05:08 -07:00
|
|
|
}
|
|
|
|
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2018-05-04 14:11:22 -07:00
|
|
|
impl !Send for LineColumn {}
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2018-05-04 14:11:22 -07:00
|
|
|
impl !Sync for LineColumn {}
|
|
|
|
|
2020-05-21 17:05:13 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
|
|
|
impl Ord for LineColumn {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
self.line.cmp(&other.line).then(self.column.cmp(&other.column))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
|
|
|
impl PartialOrd for LineColumn {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:05:08 -07:00
|
|
|
/// The source file of a given `Span`.
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 18:05:08 -07:00
|
|
|
#[derive(Clone)]
|
2018-03-16 01:09:22 +02:00
|
|
|
pub struct SourceFile(bridge::client::SourceFile);
|
2018-03-02 02:42:22 +01:00
|
|
|
|
2017-08-01 18:05:08 -07:00
|
|
|
impl SourceFile {
|
2019-02-09 21:23:30 +00:00
|
|
|
/// Gets the path to this source file.
|
2017-08-01 18:05:08 -07:00
|
|
|
///
|
|
|
|
/// ### Note
|
|
|
|
/// If the code span associated with this `SourceFile` was generated by an external macro, this
|
2018-08-18 12:14:31 +02:00
|
|
|
/// macro, this may not be an actual path on the filesystem. Use [`is_real`] to check.
|
2017-08-01 18:05:08 -07:00
|
|
|
///
|
2018-02-18 15:05:24 -08:00
|
|
|
/// Also note that even if `is_real` returns `true`, if `--remap-path-prefix` was passed on
|
2017-08-01 18:05:08 -07:00
|
|
|
/// the command line, the path as given may not actually be valid.
|
|
|
|
///
|
|
|
|
/// [`is_real`]: #method.is_real
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2018-03-16 17:04:14 +02:00
|
|
|
pub fn path(&self) -> PathBuf {
|
2018-03-16 01:09:22 +02:00
|
|
|
PathBuf::from(self.0.path())
|
2017-08-01 18:05:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if this source file is a real source file, and not generated by an external
|
|
|
|
/// macro's expansion.
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 18:05:08 -07:00
|
|
|
pub fn is_real(&self) -> bool {
|
|
|
|
// This is a hack until intercrate spans are implemented and we can have real source files
|
|
|
|
// for spans generated in external macros.
|
|
|
|
// https://github.com/rust-lang/rust/pull/43604#issuecomment-333334368
|
2018-03-16 01:09:22 +02:00
|
|
|
self.0.is_real()
|
2017-08-01 18:05:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 18:05:08 -07:00
|
|
|
impl fmt::Debug for SourceFile {
|
2019-02-04 03:55:40 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-08-01 18:05:08 -07:00
|
|
|
f.debug_struct("SourceFile")
|
2018-03-16 17:04:14 +02:00
|
|
|
.field("path", &self.path())
|
2017-08-01 18:05:08 -07:00
|
|
|
.field("is_real", &self.is_real())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 18:05:08 -07:00
|
|
|
impl PartialEq for SourceFile {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
2018-03-16 01:09:22 +02:00
|
|
|
self.0.eq(&other.0)
|
2017-08-01 18:05:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 18:05:08 -07:00
|
|
|
impl Eq for SourceFile {}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
/// A single token or a delimited sequence of token trees (e.g., `[1, (), ..]`).
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-06 15:20:57 -07:00
|
|
|
#[derive(Clone)]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub enum TokenTree {
|
2018-05-05 20:12:37 +03:00
|
|
|
/// A token stream surrounded by bracket delimiters.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2019-12-22 17:42:04 -05:00
|
|
|
Group(#[stable(feature = "proc_macro_lib2", since = "1.29.0")] Group),
|
2018-05-14 00:01:56 +03:00
|
|
|
/// An identifier.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2019-12-22 17:42:04 -05:00
|
|
|
Ident(#[stable(feature = "proc_macro_lib2", since = "1.29.0")] Ident),
|
2018-05-05 20:12:37 +03:00
|
|
|
/// A single punctuation character (`+`, `,`, `$`, etc.).
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2019-12-22 17:42:04 -05:00
|
|
|
Punct(#[stable(feature = "proc_macro_lib2", since = "1.29.0")] Punct),
|
2018-04-02 08:19:32 -07:00
|
|
|
/// A literal character (`'a'`), string (`"hello"`), number (`2.3`), etc.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2019-12-22 17:42:04 -05:00
|
|
|
Literal(#[stable(feature = "proc_macro_lib2", since = "1.29.0")] Literal),
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-04 14:11:22 -07:00
|
|
|
impl !Send for TokenTree {}
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-04 14:11:22 -07:00
|
|
|
impl !Sync for TokenTree {}
|
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
impl TokenTree {
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Returns the span of this tree, delegating to the `span` method of
|
|
|
|
/// the contained token or a delimited stream.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
match *self {
|
|
|
|
TokenTree::Group(ref t) => t.span(),
|
2018-05-05 21:09:41 +03:00
|
|
|
TokenTree::Ident(ref t) => t.span(),
|
|
|
|
TokenTree::Punct(ref t) => t.span(),
|
2018-04-02 08:19:32 -07:00
|
|
|
TokenTree::Literal(ref t) => t.span(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configures the span for *only this token*.
|
|
|
|
///
|
|
|
|
/// Note that if this token is a `Group` then this method will not configure
|
|
|
|
/// the span of each of the internal tokens, this will simply delegate to
|
|
|
|
/// the `set_span` method of each variant.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn set_span(&mut self, span: Span) {
|
|
|
|
match *self {
|
|
|
|
TokenTree::Group(ref mut t) => t.set_span(span),
|
2018-05-05 21:09:41 +03:00
|
|
|
TokenTree::Ident(ref mut t) => t.set_span(span),
|
|
|
|
TokenTree::Punct(ref mut t) => t.set_span(span),
|
2018-04-02 08:19:32 -07:00
|
|
|
TokenTree::Literal(ref mut t) => t.set_span(span),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 13:05:20 -05:00
|
|
|
/// Prints token tree in a form convenient for debugging.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-06 15:20:57 -07:00
|
|
|
impl fmt::Debug for TokenTree {
|
2019-02-04 03:55:40 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-04-06 15:20:57 -07:00
|
|
|
// Each of these has the name in the struct type in the derived debug,
|
|
|
|
// so don't bother with an extra layer of indirection
|
|
|
|
match *self {
|
|
|
|
TokenTree::Group(ref tt) => tt.fmt(f),
|
2018-05-05 21:09:41 +03:00
|
|
|
TokenTree::Ident(ref tt) => tt.fmt(f),
|
|
|
|
TokenTree::Punct(ref tt) => tt.fmt(f),
|
2018-04-06 15:20:57 -07:00
|
|
|
TokenTree::Literal(ref tt) => tt.fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
impl From<Group> for TokenTree {
|
|
|
|
fn from(g: Group) -> TokenTree {
|
|
|
|
TokenTree::Group(g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 21:09:41 +03:00
|
|
|
impl From<Ident> for TokenTree {
|
|
|
|
fn from(g: Ident) -> TokenTree {
|
|
|
|
TokenTree::Ident(g)
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 21:09:41 +03:00
|
|
|
impl From<Punct> for TokenTree {
|
|
|
|
fn from(g: Punct) -> TokenTree {
|
|
|
|
TokenTree::Punct(g)
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
impl From<Literal> for TokenTree {
|
|
|
|
fn from(g: Literal) -> TokenTree {
|
|
|
|
TokenTree::Literal(g)
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
|
2018-03-16 01:09:22 +02:00
|
|
|
// based on it (the reverse of the usual relationship between the two).
|
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
|
|
|
impl ToString for TokenTree {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
match *self {
|
|
|
|
TokenTree::Group(ref t) => t.to_string(),
|
|
|
|
TokenTree::Ident(ref t) => t.to_string(),
|
|
|
|
TokenTree::Punct(ref t) => t.to_string(),
|
|
|
|
TokenTree::Literal(ref t) => t.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-13 18:35:57 +03:00
|
|
|
/// Prints the token tree as a string that is supposed to be losslessly convertible back
|
2018-05-05 20:12:37 +03:00
|
|
|
/// into the same token tree (modulo spans), except for possibly `TokenTree::Group`s
|
2018-05-13 18:35:57 +03:00
|
|
|
/// with `Delimiter::None` delimiters and negative numeric literals.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 23:41:09 +00:00
|
|
|
impl fmt::Display for TokenTree {
|
2019-02-04 03:55:40 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-16 01:09:22 +02:00
|
|
|
f.write_str(&self.to_string())
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// A delimited token stream.
|
2018-04-02 08:19:32 -07:00
|
|
|
///
|
2018-05-05 20:12:37 +03:00
|
|
|
/// A `Group` internally contains a `TokenStream` which is surrounded by `Delimiter`s.
|
2018-07-20 00:09:05 +03:00
|
|
|
#[derive(Clone)]
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-03-16 01:09:22 +02:00
|
|
|
pub struct Group(bridge::client::Group);
|
2017-03-17 23:41:09 +00:00
|
|
|
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-04 14:11:22 -07:00
|
|
|
impl !Send for Group {}
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-04 14:11:22 -07:00
|
|
|
impl !Sync for Group {}
|
|
|
|
|
2017-03-17 23:41:09 +00:00
|
|
|
/// Describes how a sequence of token trees is delimited.
|
2017-08-20 23:20:34 +02:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 23:41:09 +00:00
|
|
|
pub enum Delimiter {
|
|
|
|
/// `( ... )`
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 23:41:09 +00:00
|
|
|
Parenthesis,
|
|
|
|
/// `{ ... }`
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-08-21 17:17:27 +02:00
|
|
|
Brace,
|
|
|
|
/// `[ ... ]`
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 23:41:09 +00:00
|
|
|
Bracket,
|
2018-05-05 20:12:37 +03:00
|
|
|
/// `Ø ... Ø`
|
|
|
|
/// An implicit delimiter, that may, for example, appear around tokens coming from a
|
|
|
|
/// "macro variable" `$var`. It is important to preserve operator priorities in cases like
|
|
|
|
/// `$var * 3` where `$var` is `1 + 2`.
|
|
|
|
/// Implicit delimiters may not survive roundtrip of a token stream through a string.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 23:41:09 +00:00
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
impl Group {
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Creates a new `Group` with the given delimiter and token stream.
|
2018-04-02 08:19:32 -07:00
|
|
|
///
|
|
|
|
/// This constructor will set the span for this group to
|
|
|
|
/// `Span::call_site()`. To change the span you can use the `set_span`
|
|
|
|
/// method below.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
|
2018-03-16 01:09:22 +02:00
|
|
|
Group(bridge::client::Group::new(delimiter, stream.0))
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
2017-03-17 23:41:09 +00:00
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
/// Returns the delimiter of this `Group`
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn delimiter(&self) -> Delimiter {
|
2018-03-16 01:09:22 +02:00
|
|
|
self.0.delimiter()
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
/// Returns the `TokenStream` of tokens that are delimited in this `Group`.
|
|
|
|
///
|
|
|
|
/// Note that the returned token stream does not include the delimiter
|
|
|
|
/// returned above.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn stream(&self) -> TokenStream {
|
2018-03-16 01:09:22 +02:00
|
|
|
TokenStream(self.0.stream())
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the span for the delimiters of this token stream, spanning the
|
|
|
|
/// entire `Group`.
|
2018-09-02 06:59:02 -07:00
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// pub fn span(&self) -> Span {
|
|
|
|
/// ^^^^^^^
|
|
|
|
/// ```
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn span(&self) -> Span {
|
2018-03-16 01:09:22 +02:00
|
|
|
Span(self.0.span())
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
|
2018-09-08 18:07:02 -07:00
|
|
|
/// Returns the span pointing to the opening delimiter of this group.
|
2018-09-02 06:59:02 -07:00
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// pub fn span_open(&self) -> Span {
|
|
|
|
/// ^
|
|
|
|
/// ```
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2018-09-02 06:59:02 -07:00
|
|
|
pub fn span_open(&self) -> Span {
|
2018-03-16 01:09:22 +02:00
|
|
|
Span(self.0.span_open())
|
2018-09-02 06:59:02 -07:00
|
|
|
}
|
|
|
|
|
2018-09-08 18:07:02 -07:00
|
|
|
/// Returns the span pointing to the closing delimiter of this group.
|
2018-09-02 06:59:02 -07:00
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// pub fn span_close(&self) -> Span {
|
|
|
|
/// ^
|
|
|
|
/// ```
|
2018-10-01 10:53:03 -07:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2018-09-02 06:59:02 -07:00
|
|
|
pub fn span_close(&self) -> Span {
|
2018-03-16 01:09:22 +02:00
|
|
|
Span(self.0.span_close())
|
2018-09-02 06:59:02 -07:00
|
|
|
}
|
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
/// Configures the span for this `Group`'s delimiters, but not its internal
|
|
|
|
/// tokens.
|
|
|
|
///
|
|
|
|
/// This method will **not** set the span of all the internal tokens spanned
|
|
|
|
/// by this group, but rather it will only set the span of the delimiter
|
|
|
|
/// tokens at the level of the `Group`.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn set_span(&mut self, span: Span) {
|
2018-03-16 01:09:22 +02:00
|
|
|
self.0.set_span(span.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
|
2018-03-16 01:09:22 +02:00
|
|
|
// based on it (the reverse of the usual relationship between the two).
|
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
|
|
|
impl ToString for Group {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
TokenStream::from(TokenTree::from(self.clone())).to_string()
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Prints the group as a string that should be losslessly convertible back
|
|
|
|
/// into the same group (modulo spans), except for possibly `TokenTree::Group`s
|
|
|
|
/// with `Delimiter::None` delimiters.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
impl fmt::Display for Group {
|
2019-02-04 03:55:40 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-16 01:09:22 +02:00
|
|
|
f.write_str(&self.to_string())
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-20 00:09:05 +03:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
impl fmt::Debug for Group {
|
2019-02-04 03:55:40 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-07-20 00:09:05 +03:00
|
|
|
f.debug_struct("Group")
|
|
|
|
.field("delimiter", &self.delimiter())
|
|
|
|
.field("stream", &self.stream())
|
|
|
|
.field("span", &self.span())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 21:09:41 +03:00
|
|
|
/// An `Punct` is an single punctuation character like `+`, `-` or `#`.
|
2018-04-02 08:19:32 -07:00
|
|
|
///
|
2018-11-12 13:05:20 -05:00
|
|
|
/// Multi-character operators like `+=` are represented as two instances of `Punct` with different
|
2018-04-02 08:19:32 -07:00
|
|
|
/// forms of `Spacing` returned.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-07-20 00:09:05 +03:00
|
|
|
#[derive(Clone)]
|
2018-03-16 01:09:22 +02:00
|
|
|
pub struct Punct(bridge::client::Punct);
|
2018-04-02 08:19:32 -07:00
|
|
|
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 21:09:41 +03:00
|
|
|
impl !Send for Punct {}
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 21:09:41 +03:00
|
|
|
impl !Sync for Punct {}
|
2018-05-04 14:11:22 -07:00
|
|
|
|
2018-05-05 21:09:41 +03:00
|
|
|
/// Whether an `Punct` is followed immediately by another `Punct` or
|
2018-05-05 20:12:37 +03:00
|
|
|
/// followed by another token or whitespace.
|
2017-08-20 23:20:34 +02:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-06-05 01:41:33 +00:00
|
|
|
pub enum Spacing {
|
2018-11-27 02:59:49 +00:00
|
|
|
/// e.g., `+` is `Alone` in `+ =`, `+ident` or `+()`.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 23:41:09 +00:00
|
|
|
Alone,
|
2018-11-27 02:59:49 +00:00
|
|
|
/// e.g., `+` is `Joint` in `+=` or `'#`.
|
2018-05-14 00:01:56 +03:00
|
|
|
/// Additionally, single quote `'` can join with identifiers to form lifetimes `'ident`.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 23:41:09 +00:00
|
|
|
Joint,
|
|
|
|
}
|
|
|
|
|
2018-05-05 21:09:41 +03:00
|
|
|
impl Punct {
|
|
|
|
/// Creates a new `Punct` from the given character and spacing.
|
2018-05-05 22:45:59 +03:00
|
|
|
/// The `ch` argument must be a valid punctuation character permitted by the language,
|
|
|
|
/// otherwise the function will panic.
|
2018-04-02 08:19:32 -07:00
|
|
|
///
|
2018-05-05 21:09:41 +03:00
|
|
|
/// The returned `Punct` will have the default span of `Span::call_site()`
|
2018-04-02 08:19:32 -07:00
|
|
|
/// which can be further configured with the `set_span` method below.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 21:09:41 +03:00
|
|
|
pub fn new(ch: char, spacing: Spacing) -> Punct {
|
2018-03-16 01:09:22 +02:00
|
|
|
Punct(bridge::client::Punct::new(ch, spacing))
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Returns the value of this punctuation character as `char`.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 21:09:41 +03:00
|
|
|
pub fn as_char(&self) -> char {
|
2018-03-16 01:09:22 +02:00
|
|
|
self.0.as_char()
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Returns the spacing of this punctuation character, indicating whether it's immediately
|
2018-05-05 21:09:41 +03:00
|
|
|
/// followed by another `Punct` in the token stream, so they can potentially be combined into
|
2018-11-12 13:05:20 -05:00
|
|
|
/// a multi-character operator (`Joint`), or it's followed by some other token or whitespace
|
2018-05-05 20:12:37 +03:00
|
|
|
/// (`Alone`) so the operator has certainly ended.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn spacing(&self) -> Spacing {
|
2018-03-16 01:09:22 +02:00
|
|
|
self.0.spacing()
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Returns the span for this punctuation character.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn span(&self) -> Span {
|
2018-03-16 01:09:22 +02:00
|
|
|
Span(self.0.span())
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Configure the span for this punctuation character.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn set_span(&mut self, span: Span) {
|
2018-03-16 01:09:22 +02:00
|
|
|
self.0 = self.0.with_span(span.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
|
2018-03-16 01:09:22 +02:00
|
|
|
// based on it (the reverse of the usual relationship between the two).
|
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
|
|
|
impl ToString for Punct {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
TokenStream::from(TokenTree::from(self.clone())).to_string()
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Prints the punctuation character as a string that should be losslessly convertible
|
|
|
|
/// back into the same character.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 21:09:41 +03:00
|
|
|
impl fmt::Display for Punct {
|
2019-02-04 03:55:40 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-16 01:09:22 +02:00
|
|
|
f.write_str(&self.to_string())
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
}
|
2017-03-17 23:41:09 +00:00
|
|
|
|
2018-07-20 00:09:05 +03:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
impl fmt::Debug for Punct {
|
2019-02-04 03:55:40 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-07-20 00:09:05 +03:00
|
|
|
f.debug_struct("Punct")
|
|
|
|
.field("ch", &self.as_char())
|
|
|
|
.field("spacing", &self.spacing())
|
|
|
|
.field("span", &self.span())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 00:01:56 +03:00
|
|
|
/// An identifier (`ident`).
|
2018-07-20 00:09:05 +03:00
|
|
|
#[derive(Clone)]
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-03-16 01:09:22 +02:00
|
|
|
pub struct Ident(bridge::client::Ident);
|
2018-05-04 14:11:22 -07:00
|
|
|
|
2018-05-05 21:09:41 +03:00
|
|
|
impl Ident {
|
|
|
|
/// Creates a new `Ident` with the given `string` as well as the specified
|
2018-04-02 08:19:32 -07:00
|
|
|
/// `span`.
|
2018-05-14 00:01:56 +03:00
|
|
|
/// The `string` argument must be a valid identifier permitted by the
|
2020-08-02 23:53:41 -04:00
|
|
|
/// language (including keywords, e.g. `self` or `fn`). Otherwise, the function will panic.
|
2018-04-02 08:19:32 -07:00
|
|
|
///
|
|
|
|
/// Note that `span`, currently in rustc, configures the hygiene information
|
2018-05-05 20:12:37 +03:00
|
|
|
/// for this identifier.
|
|
|
|
///
|
|
|
|
/// As of this time `Span::call_site()` explicitly opts-in to "call-site" hygiene
|
|
|
|
/// meaning that identifiers created with this span will be resolved as if they were written
|
|
|
|
/// directly at the location of the macro call, and other code at the macro call site will be
|
|
|
|
/// able to refer to them as well.
|
|
|
|
///
|
|
|
|
/// Later spans like `Span::def_site()` will allow to opt-in to "definition-site" hygiene
|
|
|
|
/// meaning that identifiers created with this span will be resolved at the location of the
|
|
|
|
/// macro definition and other code at the macro call site will not be able to refer to them.
|
2018-04-02 08:19:32 -07:00
|
|
|
///
|
|
|
|
/// Due to the current importance of hygiene this constructor, unlike other
|
|
|
|
/// tokens, requires a `Span` to be specified at construction.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 21:09:41 +03:00
|
|
|
pub fn new(string: &str, span: Span) -> Ident {
|
2018-03-16 01:09:22 +02:00
|
|
|
Ident(bridge::client::Ident::new(string, span.0, false))
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
|
2018-05-05 22:39:05 +03:00
|
|
|
/// Same as `Ident::new`, but creates a raw identifier (`r#ident`).
|
2020-08-02 23:53:41 -04:00
|
|
|
/// The `string` argument be a valid identifier permitted by the language
|
|
|
|
/// (including keywords, e.g. `fn`). Keywords which are usable in path segments
|
|
|
|
/// (e.g. `self`, `super`) are not supported, and will cause a panic.
|
|
|
|
#[stable(feature = "proc_macro_raw_ident", since = "1.47.0")]
|
2018-05-05 22:39:05 +03:00
|
|
|
pub fn new_raw(string: &str, span: Span) -> Ident {
|
2018-03-16 01:09:22 +02:00
|
|
|
Ident(bridge::client::Ident::new(string, span.0, true))
|
2018-05-05 22:39:05 +03:00
|
|
|
}
|
|
|
|
|
2018-05-05 21:09:41 +03:00
|
|
|
/// Returns the span of this `Ident`, encompassing the entire string returned
|
2018-04-02 08:19:32 -07:00
|
|
|
/// by `as_str`.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn span(&self) -> Span {
|
2018-03-16 01:09:22 +02:00
|
|
|
Span(self.0.span())
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
|
2018-05-05 21:09:41 +03:00
|
|
|
/// Configures the span of this `Ident`, possibly changing its hygiene context.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn set_span(&mut self, span: Span) {
|
2018-03-16 01:09:22 +02:00
|
|
|
self.0 = self.0.with_span(span.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
|
2018-03-16 01:09:22 +02:00
|
|
|
// based on it (the reverse of the usual relationship between the two).
|
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
|
|
|
impl ToString for Ident {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
TokenStream::from(TokenTree::from(self.clone())).to_string()
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Prints the identifier as a string that should be losslessly convertible
|
|
|
|
/// back into the same identifier.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 21:09:41 +03:00
|
|
|
impl fmt::Display for Ident {
|
2019-02-04 03:55:40 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-16 01:09:22 +02:00
|
|
|
f.write_str(&self.to_string())
|
2018-07-20 00:09:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
impl fmt::Debug for Ident {
|
2019-02-04 03:55:40 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-07-20 00:09:05 +03:00
|
|
|
f.debug_struct("Ident")
|
|
|
|
.field("ident", &self.to_string())
|
|
|
|
.field("span", &self.span())
|
|
|
|
.finish()
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// A literal string (`"hello"`), byte string (`b"hello"`),
|
|
|
|
/// character (`'a'`), byte character (`b'a'`), an integer or floating point number
|
|
|
|
/// with or without a suffix (`1`, `1u8`, `2.3`, `2.3f32`).
|
2018-05-05 21:09:41 +03:00
|
|
|
/// Boolean literals like `true` and `false` do not belong here, they are `Ident`s.
|
2018-03-16 01:09:22 +02:00
|
|
|
#[derive(Clone)]
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-03-16 01:09:22 +02:00
|
|
|
pub struct Literal(bridge::client::Literal);
|
2018-05-04 14:11:22 -07:00
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
macro_rules! suffixed_int_literals {
|
|
|
|
($($name:ident => $kind:ident,)*) => ($(
|
|
|
|
/// Creates a new suffixed integer literal with the specified value.
|
|
|
|
///
|
|
|
|
/// This function will create an integer like `1u32` where the integer
|
|
|
|
/// value specified is the first part of the token and the integral is
|
|
|
|
/// also suffixed at the end.
|
2018-11-12 13:05:20 -05:00
|
|
|
/// Literals created from negative numbers may not survive round-trips through
|
2018-05-13 18:35:57 +03:00
|
|
|
/// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
|
2018-04-02 08:19:32 -07:00
|
|
|
///
|
|
|
|
/// Literals created through this method have the `Span::call_site()`
|
|
|
|
/// span by default, which can be configured with the `set_span` method
|
|
|
|
/// below.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn $name(n: $kind) -> Literal {
|
2018-03-16 01:09:22 +02:00
|
|
|
Literal(bridge::client::Literal::typed_integer(&n.to_string(), stringify!($kind)))
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
)*)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! unsuffixed_int_literals {
|
|
|
|
($($name:ident => $kind:ident,)*) => ($(
|
|
|
|
/// Creates a new unsuffixed integer literal with the specified value.
|
|
|
|
///
|
|
|
|
/// This function will create an integer like `1` where the integer
|
|
|
|
/// value specified is the first part of the token. No suffix is
|
|
|
|
/// specified on this token, meaning that invocations like
|
|
|
|
/// `Literal::i8_unsuffixed(1)` are equivalent to
|
|
|
|
/// `Literal::u32_unsuffixed(1)`.
|
2018-05-13 18:35:57 +03:00
|
|
|
/// Literals created from negative numbers may not survive rountrips through
|
|
|
|
/// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
|
2018-04-02 08:19:32 -07:00
|
|
|
///
|
|
|
|
/// Literals created through this method have the `Span::call_site()`
|
|
|
|
/// span by default, which can be configured with the `set_span` method
|
|
|
|
/// below.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn $name(n: $kind) -> Literal {
|
2018-03-16 01:09:22 +02:00
|
|
|
Literal(bridge::client::Literal::integer(&n.to_string()))
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
2018-04-02 08:19:32 -07:00
|
|
|
)*)
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Literal {
|
2018-04-02 08:19:32 -07:00
|
|
|
suffixed_int_literals! {
|
|
|
|
u8_suffixed => u8,
|
|
|
|
u16_suffixed => u16,
|
|
|
|
u32_suffixed => u32,
|
|
|
|
u64_suffixed => u64,
|
|
|
|
u128_suffixed => u128,
|
|
|
|
usize_suffixed => usize,
|
|
|
|
i8_suffixed => i8,
|
|
|
|
i16_suffixed => i16,
|
|
|
|
i32_suffixed => i32,
|
|
|
|
i64_suffixed => i64,
|
|
|
|
i128_suffixed => i128,
|
|
|
|
isize_suffixed => isize,
|
2017-06-05 01:41:33 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
unsuffixed_int_literals! {
|
|
|
|
u8_unsuffixed => u8,
|
|
|
|
u16_unsuffixed => u16,
|
|
|
|
u32_unsuffixed => u32,
|
|
|
|
u64_unsuffixed => u64,
|
|
|
|
u128_unsuffixed => u128,
|
|
|
|
usize_unsuffixed => usize,
|
|
|
|
i8_unsuffixed => i8,
|
|
|
|
i16_unsuffixed => i16,
|
|
|
|
i32_unsuffixed => i32,
|
|
|
|
i64_unsuffixed => i64,
|
|
|
|
i128_unsuffixed => i128,
|
|
|
|
isize_unsuffixed => isize,
|
2017-06-05 01:41:33 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
/// Creates a new unsuffixed floating-point literal.
|
|
|
|
///
|
|
|
|
/// This constructor is similar to those like `Literal::i8_unsuffixed` where
|
|
|
|
/// the float's value is emitted directly into the token but no suffix is
|
|
|
|
/// used, so it may be inferred to be a `f64` later in the compiler.
|
2018-05-13 18:35:57 +03:00
|
|
|
/// Literals created from negative numbers may not survive rountrips through
|
|
|
|
/// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
|
2018-04-02 08:19:32 -07:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This function requires that the specified float is finite, for
|
|
|
|
/// example if it is infinity or NaN this function will panic.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn f32_unsuffixed(n: f32) -> Literal {
|
2017-06-05 01:41:33 +00:00
|
|
|
if !n.is_finite() {
|
|
|
|
panic!("Invalid float literal {}", n);
|
|
|
|
}
|
2018-03-16 01:09:22 +02:00
|
|
|
Literal(bridge::client::Literal::float(&n.to_string()))
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
/// Creates a new suffixed floating-point literal.
|
|
|
|
///
|
2018-11-12 13:05:20 -05:00
|
|
|
/// This constructor will create a literal like `1.0f32` where the value
|
2018-04-02 08:19:32 -07:00
|
|
|
/// specified is the preceding part of the token and `f32` is the suffix of
|
|
|
|
/// the token. This token will always be inferred to be an `f32` in the
|
|
|
|
/// compiler.
|
2018-05-13 18:35:57 +03:00
|
|
|
/// Literals created from negative numbers may not survive rountrips through
|
|
|
|
/// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
|
2018-04-02 08:19:32 -07:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This function requires that the specified float is finite, for
|
|
|
|
/// example if it is infinity or NaN this function will panic.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn f32_suffixed(n: f32) -> Literal {
|
2017-06-05 01:41:33 +00:00
|
|
|
if !n.is_finite() {
|
2018-04-02 08:19:32 -07:00
|
|
|
panic!("Invalid float literal {}", n);
|
|
|
|
}
|
2018-03-16 01:09:22 +02:00
|
|
|
Literal(bridge::client::Literal::f32(&n.to_string()))
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
/// Creates a new unsuffixed floating-point literal.
|
|
|
|
///
|
|
|
|
/// This constructor is similar to those like `Literal::i8_unsuffixed` where
|
|
|
|
/// the float's value is emitted directly into the token but no suffix is
|
|
|
|
/// used, so it may be inferred to be a `f64` later in the compiler.
|
2018-05-13 18:35:57 +03:00
|
|
|
/// Literals created from negative numbers may not survive rountrips through
|
|
|
|
/// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
|
2018-04-02 08:19:32 -07:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This function requires that the specified float is finite, for
|
|
|
|
/// example if it is infinity or NaN this function will panic.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn f64_unsuffixed(n: f64) -> Literal {
|
2017-06-05 01:41:33 +00:00
|
|
|
if !n.is_finite() {
|
2018-04-02 08:19:32 -07:00
|
|
|
panic!("Invalid float literal {}", n);
|
|
|
|
}
|
2018-03-16 01:09:22 +02:00
|
|
|
Literal(bridge::client::Literal::float(&n.to_string()))
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new suffixed floating-point literal.
|
|
|
|
///
|
2018-11-12 13:05:20 -05:00
|
|
|
/// This constructor will create a literal like `1.0f64` where the value
|
2018-04-02 08:19:32 -07:00
|
|
|
/// specified is the preceding part of the token and `f64` is the suffix of
|
|
|
|
/// the token. This token will always be inferred to be an `f64` in the
|
|
|
|
/// compiler.
|
2018-05-13 18:35:57 +03:00
|
|
|
/// Literals created from negative numbers may not survive rountrips through
|
|
|
|
/// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
|
2018-04-02 08:19:32 -07:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This function requires that the specified float is finite, for
|
|
|
|
/// example if it is infinity or NaN this function will panic.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn f64_suffixed(n: f64) -> Literal {
|
|
|
|
if !n.is_finite() {
|
|
|
|
panic!("Invalid float literal {}", n);
|
|
|
|
}
|
2018-03-16 01:09:22 +02:00
|
|
|
Literal(bridge::client::Literal::f64(&n.to_string()))
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// String literal.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 23:41:09 +00:00
|
|
|
pub fn string(string: &str) -> Literal {
|
2018-03-16 01:09:22 +02:00
|
|
|
Literal(bridge::client::Literal::string(string))
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Character literal.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 23:41:09 +00:00
|
|
|
pub fn character(ch: char) -> Literal {
|
2018-03-16 01:09:22 +02:00
|
|
|
Literal(bridge::client::Literal::character(ch))
|
2017-06-05 01:41:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Byte string literal.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-06-05 01:41:33 +00:00
|
|
|
pub fn byte_string(bytes: &[u8]) -> Literal {
|
2018-03-16 01:09:22 +02:00
|
|
|
Literal(bridge::client::Literal::byte_string(bytes))
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 08:19:32 -07:00
|
|
|
/// Returns the span encompassing this literal.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn span(&self) -> Span {
|
2018-03-16 01:09:22 +02:00
|
|
|
Span(self.0.span())
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Configures the span associated for this literal.
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
pub fn set_span(&mut self, span: Span) {
|
2018-03-16 01:09:22 +02:00
|
|
|
self.0.set_span(span.0);
|
2018-04-02 08:19:32 -07:00
|
|
|
}
|
2018-11-20 21:12:30 -08:00
|
|
|
|
|
|
|
/// Returns a `Span` that is a subset of `self.span()` containing only the
|
|
|
|
/// source bytes in range `range`. Returns `None` if the would-be trimmed
|
|
|
|
/// span is outside the bounds of `self`.
|
|
|
|
// FIXME(SergioBenitez): check that the byte range starts and ends at a
|
|
|
|
// UTF-8 boundary of the source. otherwise, it's likely that a panic will
|
|
|
|
// occur elsewhere when the source text is printed.
|
|
|
|
// FIXME(SergioBenitez): there is no way for the user to know what
|
|
|
|
// `self.span()` actually maps to, so this method can currently only be
|
|
|
|
// called blindly. For example, `to_string()` for the character 'c' returns
|
|
|
|
// "'\u{63}'"; there is no way for the user to know whether the source text
|
|
|
|
// was 'c' or whether it was '\u{63}'.
|
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
|
|
|
pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> {
|
2018-03-16 01:09:22 +02:00
|
|
|
// HACK(eddyb) something akin to `Option::cloned`, but for `Bound<&T>`.
|
|
|
|
fn cloned_bound<T: Clone>(bound: Bound<&T>) -> Bound<T> {
|
|
|
|
match bound {
|
|
|
|
Bound::Included(x) => Bound::Included(x.clone()),
|
|
|
|
Bound::Excluded(x) => Bound::Excluded(x.clone()),
|
|
|
|
Bound::Unbounded => Bound::Unbounded,
|
|
|
|
}
|
2018-11-20 21:12:30 -08:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
self.0.subspan(cloned_bound(range.start_bound()), cloned_bound(range.end_bound())).map(Span)
|
2018-03-16 01:09:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
|
2018-03-16 01:09:22 +02:00
|
|
|
// based on it (the reverse of the usual relationship between the two).
|
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
|
|
|
impl ToString for Literal {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
TokenStream::from(TokenTree::from(self.clone())).to_string()
|
2018-11-20 21:12:30 -08:00
|
|
|
}
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 20:12:37 +03:00
|
|
|
/// Prints the literal as a string that should be losslessly convertible
|
|
|
|
/// back into the same literal (except for possible rounding for floating point literals).
|
2018-07-03 15:36:31 -07:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 08:19:32 -07:00
|
|
|
impl fmt::Display for Literal {
|
2019-02-04 03:55:40 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-16 01:09:22 +02:00
|
|
|
f.write_str(&self.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
impl fmt::Debug for Literal {
|
2019-02-04 03:55:40 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-16 01:09:22 +02:00
|
|
|
self.0.fmt(f)
|
2017-03-17 23:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-22 21:08:01 +03:00
|
|
|
|
|
|
|
/// Tracked access to environment variables.
|
|
|
|
#[unstable(feature = "proc_macro_tracked_env", issue = "74690")]
|
|
|
|
pub mod tracked_env {
|
|
|
|
use std::env::{self, VarError};
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
|
|
|
|
/// Retrieve an environment variable and add it to build dependency info.
|
|
|
|
/// Build system executing the compiler will know that the variable was accessed during
|
|
|
|
/// compilation, and will be able to rerun the build when the value of that variable changes.
|
|
|
|
/// Besides the dependency tracking this function should be equivalent to `env::var` from the
|
|
|
|
/// standard library, except that the argument must be UTF-8.
|
|
|
|
#[unstable(feature = "proc_macro_tracked_env", issue = "74690")]
|
|
|
|
pub fn var<K: AsRef<OsStr> + AsRef<str>>(key: K) -> Result<String, VarError> {
|
|
|
|
let key: &str = key.as_ref();
|
|
|
|
let value = env::var(key);
|
|
|
|
crate::bridge::client::FreeFunctions::track_env_var(key, value.as_deref().ok());
|
|
|
|
value
|
|
|
|
}
|
|
|
|
}
|