2016-10-03 11:49:39 -05:00
|
|
|
//! A support library for macro authors when defining new macros.
|
2016-08-04 14:20:01 -05:00
|
|
|
//!
|
2016-10-03 11:49:39 -05:00
|
|
|
//! This library, provided by the standard distribution, provides the types
|
2018-05-13 10:35:57 -05:00
|
|
|
//! consumed in the interfaces of procedurally defined macro definitions such as
|
2018-08-19 08:30:23 -05:00
|
|
|
//! function-like macros `#[proc_macro]`, macro attributes `#[proc_macro_attribute]` and
|
2018-05-13 10:35:57 -05:00
|
|
|
//! custom derive attributes`#[proc_macro_derive]`.
|
2016-08-04 14:20:01 -05:00
|
|
|
//!
|
2019-01-22 21:55:37 -06:00
|
|
|
//! See [the book] for more.
|
|
|
|
//!
|
|
|
|
//! [the book]: ../book/ch19-06-macros.html#procedural-macros-for-generating-code-from-attributes
|
2016-08-04 14:20:01 -05:00
|
|
|
|
2017-01-01 18:14:35 -06:00
|
|
|
#![stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2016-10-03 11:49:39 -05:00
|
|
|
#![deny(missing_docs)]
|
2019-02-05 07:37:15 -06:00
|
|
|
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
|
2017-03-11 20:54:43 -06:00
|
|
|
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))))]
|
2016-08-04 14:20:01 -05:00
|
|
|
|
2019-02-10 01:13:30 -06:00
|
|
|
#![feature(nll)]
|
2016-10-03 11:49:39 -05:00
|
|
|
#![feature(staged_api)]
|
2018-03-15 18:09:22 -05:00
|
|
|
#![feature(const_fn)]
|
|
|
|
#![feature(extern_types)]
|
|
|
|
#![feature(in_band_lifetimes)]
|
2018-03-01 19:42:22 -06:00
|
|
|
#![feature(optin_builtin_traits)]
|
2019-06-30 13:38:06 -05:00
|
|
|
#![feature(mem_take)]
|
2018-08-15 10:56:54 -05:00
|
|
|
#![feature(non_exhaustive)]
|
2018-03-15 18:09:22 -05:00
|
|
|
#![feature(specialization)]
|
2016-08-04 14:20:01 -05:00
|
|
|
|
2018-03-02 23:22:19 -06:00
|
|
|
#![recursion_limit="256"]
|
|
|
|
|
2018-03-15 18:09:22 -05:00
|
|
|
#[unstable(feature = "proc_macro_internals", issue = "27812")]
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub mod bridge;
|
|
|
|
|
2017-08-28 04:56:43 -05:00
|
|
|
mod diagnostic;
|
|
|
|
|
2018-09-13 03:16:54 -05:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2018-09-19 21:22:21 -05:00
|
|
|
pub use diagnostic::{Diagnostic, Level, MultiSpan};
|
2016-08-04 14:20:01 -05:00
|
|
|
|
2018-03-15 18:09:22 -05:00
|
|
|
use std::{fmt, iter, mem};
|
2018-11-20 23:12:30 -06:00
|
|
|
use std::ops::{Bound, RangeBounds};
|
2018-03-16 10:04:14 -05:00
|
|
|
use std::path::PathBuf;
|
2016-10-03 11:49:39 -05:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
/// The main type provided by this crate, representing an abstract stream of
|
2018-05-05 12:12:37 -05: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 11:49:39 -05:00
|
|
|
///
|
2018-05-05 12:12:37 -05:00
|
|
|
/// This is both the input and output of `#[proc_macro]`, `#[proc_macro_attribute]`
|
|
|
|
/// and `#[proc_macro_derive]` definitions.
|
2017-01-01 18:14:35 -06:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
#[derive(Clone)]
|
2018-03-15 18:09:22 -05:00
|
|
|
pub struct TokenStream(bridge::client::TokenStream);
|
2016-10-03 11:49:39 -05:00
|
|
|
|
2018-05-13 10:46:38 -05:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2018-05-04 16:11:22 -05:00
|
|
|
impl !Send for TokenStream {}
|
2018-05-13 10:46:38 -05:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2018-05-04 16:11:22 -05:00
|
|
|
impl !Sync for TokenStream {}
|
|
|
|
|
2016-10-03 11:49:39 -05:00
|
|
|
/// Error returned from `TokenStream::from_str`.
|
2017-01-01 18:14:35 -06:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2017-06-04 20:41:33 -05:00
|
|
|
#[derive(Debug)]
|
2016-10-03 11:49:39 -05:00
|
|
|
pub struct LexError {
|
|
|
|
_inner: (),
|
|
|
|
}
|
|
|
|
|
2018-05-13 10:46:38 -05:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2018-05-04 16:11:22 -05:00
|
|
|
impl !Send for LexError {}
|
2018-05-13 10:46:38 -05:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2018-05-04 16:11:22 -05:00
|
|
|
impl !Sync for LexError {}
|
|
|
|
|
2018-04-02 10:19:32 -05:00
|
|
|
impl TokenStream {
|
2018-05-05 12:12:37 -05:00
|
|
|
/// Returns an empty `TokenStream` containing no token trees.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-25 21:44:07 -05:00
|
|
|
pub fn new() -> TokenStream {
|
2018-03-15 18:09:22 -05:00
|
|
|
TokenStream(bridge::client::TokenStream::new())
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if this `TokenStream` is empty.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.0.is_empty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05: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 12:02:24 -05:00
|
|
|
/// All tokens in the parsed stream get `Span::call_site()` spans.
|
2018-05-05 12:12:37 -05:00
|
|
|
///
|
2019-02-09 15:23:30 -06:00
|
|
|
/// NOTE: some errors may cause panics instead of returning `LexError`. We reserve the right to
|
2018-05-13 10:35:57 -05:00
|
|
|
/// change these errors into `LexError`s later.
|
2017-03-17 18:41:09 -05: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-15 18:09:22 -05:00
|
|
|
Ok(TokenStream(bridge::client::TokenStream::from_str(src)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 20:59:49 -06:00
|
|
|
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
|
2018-03-15 18:09:22 -05: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 18:41:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-13 10:35:57 -05:00
|
|
|
/// Prints the token stream as a string that is supposed to be losslessly convertible back
|
2018-05-05 12:12:37 -05:00
|
|
|
/// into the same token stream (modulo spans), except for possibly `TokenTree::Group`s
|
2018-05-13 10:35:57 -05:00
|
|
|
/// with `Delimiter::None` delimiters and negative numeric literals.
|
2017-03-17 18:41:09 -05:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
|
|
|
impl fmt::Display for TokenStream {
|
2019-02-03 12:55:40 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-15 18:09:22 -05:00
|
|
|
f.write_str(&self.to_string())
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05:00
|
|
|
/// Prints token in a form convenient for debugging.
|
2018-04-02 10:19:32 -05:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
|
|
|
impl fmt::Debug for TokenStream {
|
2019-02-03 12:55:40 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-04-06 17:20:57 -05:00
|
|
|
f.write_str("TokenStream ")?;
|
|
|
|
f.debug_list().entries(self.clone()).finish()
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
}
|
2017-06-04 20:41:33 -05:00
|
|
|
|
2018-10-01 12:42:16 -05:00
|
|
|
#[unstable(feature = "proc_macro_quote", issue = "54722")]
|
2018-04-21 19:05:02 -05:00
|
|
|
pub use quote::{quote, quote_span};
|
|
|
|
|
2018-05-05 12:12:37 -05:00
|
|
|
/// Creates a token stream containing a single token tree.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 18:41:09 -05:00
|
|
|
impl From<TokenTree> for TokenStream {
|
|
|
|
fn from(tree: TokenTree) -> TokenStream {
|
2018-03-15 18:09:22 -05: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),
|
|
|
|
TokenTree::Literal(tt) => bridge::TokenTree::Literal(tt.0)
|
|
|
|
}))
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05:00
|
|
|
/// Collects a number of token trees into a single stream.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
impl iter::FromIterator<TokenTree> for TokenStream {
|
|
|
|
fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
|
2018-04-06 09:44:21 -05:00
|
|
|
trees.into_iter().map(TokenStream::from).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05:00
|
|
|
/// A "flattening" operation on token streams, collects token trees
|
|
|
|
/// from multiple token streams into a single stream.
|
2018-05-13 10:46:38 -05:00
|
|
|
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
|
2018-04-06 09:44:21 -05:00
|
|
|
impl iter::FromIterator<TokenStream> for TokenStream {
|
|
|
|
fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
|
2018-03-15 18:09:22 -05:00
|
|
|
let mut builder = bridge::client::TokenStreamBuilder::new();
|
2019-04-05 16:51:07 -05:00
|
|
|
streams.into_iter().for_each(|stream| builder.push(stream.0));
|
2017-03-17 18:41:09 -05:00
|
|
|
TokenStream(builder.build())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 14:45:48 -05: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-15 18:09:22 -05:00
|
|
|
// FIXME(eddyb) Use an optimized implementation if/when possible.
|
|
|
|
*self = iter::once(mem::replace(self, Self::new())).chain(streams).collect();
|
2018-08-12 14:45:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05:00
|
|
|
/// Public implementation details for the `TokenStream` type, such as iterators.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub mod token_stream {
|
2019-02-03 12:55:40 -06:00
|
|
|
use crate::{bridge, Group, Ident, Literal, Punct, TokenTree, TokenStream};
|
2017-03-17 18:41:09 -05:00
|
|
|
|
2018-05-05 12:12:37 -05:00
|
|
|
/// An iterator over `TokenStream`'s `TokenTree`s.
|
2018-11-26 20:59:49 -06:00
|
|
|
/// The iteration is "shallow", e.g., the iterator doesn't recurse into delimited groups,
|
2018-05-05 12:12:37 -05:00
|
|
|
/// and returns whole groups as token trees.
|
2018-04-02 10:19:32 -05:00
|
|
|
#[derive(Clone)]
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-03-15 18:09:22 -05:00
|
|
|
pub struct IntoIter(bridge::client::TokenStreamIter);
|
2017-03-17 18:41:09 -05:00
|
|
|
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
impl Iterator for IntoIter {
|
|
|
|
type Item = TokenTree;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<TokenTree> {
|
2018-03-15 18:09:22 -05: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 10:19:32 -05:00
|
|
|
}
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
impl IntoIterator for TokenStream {
|
|
|
|
type Item = TokenTree;
|
|
|
|
type IntoIter = IntoIter;
|
|
|
|
|
|
|
|
fn into_iter(self) -> IntoIter {
|
2018-03-15 18:09:22 -05:00
|
|
|
IntoIter(self.0.into_iter())
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 10:19:32 -05: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 10:35:57 -05:00
|
|
|
/// the `TokenStream` `[Ident("a"), Punct('+', Alone), Ident("b")]`.
|
2018-04-02 10:19:32 -05:00
|
|
|
///
|
|
|
|
/// Unquoting is done with `$`, and works by taking the single next ident as the unquoted term.
|
|
|
|
/// To quote `$` itself, use `$$`.
|
2018-07-14 14:06:50 -05:00
|
|
|
///
|
2018-04-21 19:05:02 -05:00
|
|
|
/// This is a dummy macro, the actual implementation is in `quote::quote`.`
|
2018-10-01 12:42:16 -05:00
|
|
|
#[unstable(feature = "proc_macro_quote", issue = "54722")]
|
2018-04-02 10:19:32 -05:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! quote { () => {} }
|
2017-03-17 18:41:09 -05:00
|
|
|
|
2018-04-02 10:19:32 -05:00
|
|
|
#[unstable(feature = "proc_macro_internals", issue = "27812")]
|
|
|
|
#[doc(hidden)]
|
|
|
|
mod quote;
|
2017-03-17 18:41:09 -05:00
|
|
|
|
2018-04-02 10:19:32 -05:00
|
|
|
/// A region of source code, along with macro expansion information.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-06 17:20:57 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2018-03-15 18:09:22 -05:00
|
|
|
pub struct Span(bridge::client::Span);
|
2018-04-02 10:19:32 -05:00
|
|
|
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-04 16:11:22 -05:00
|
|
|
impl !Send for Span {}
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-04 16:11:22 -05:00
|
|
|
impl !Sync for Span {}
|
|
|
|
|
2017-08-28 04:56:43 -05:00
|
|
|
macro_rules! diagnostic_method {
|
|
|
|
($name:ident, $level:expr) => (
|
2019-02-09 15:23:30 -06:00
|
|
|
/// Creates a new `Diagnostic` with the given `message` at the span
|
2017-08-28 04:56:43 -05:00
|
|
|
/// `self`.
|
2018-09-13 03:16:54 -05:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2017-08-28 04:56:43 -05:00
|
|
|
pub fn $name<T: Into<String>>(self, message: T) -> Diagnostic {
|
|
|
|
Diagnostic::spanned(self, $level, message)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-03-17 18:41:09 -05:00
|
|
|
impl Span {
|
2018-04-02 10:19:32 -05:00
|
|
|
/// A span that resolves at the macro definition site.
|
2018-10-01 12:47:18 -05:00
|
|
|
#[unstable(feature = "proc_macro_def_site", issue = "54724")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn def_site() -> Span {
|
2018-03-15 18:09:22 -05:00
|
|
|
Span(bridge::client::Span::def_site())
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
|
2017-03-17 18:41:09 -05:00
|
|
|
/// The span of the invocation of the current procedural macro.
|
2018-05-05 12:12:37 -05: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 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 18:41:09 -05:00
|
|
|
pub fn call_site() -> Span {
|
2018-03-15 18:09:22 -05:00
|
|
|
Span(bridge::client::Span::call_site())
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
2017-08-28 04:56:43 -05:00
|
|
|
|
2017-08-01 20:05:08 -05:00
|
|
|
/// The original source file into which this span points.
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 20:05:08 -05:00
|
|
|
pub fn source_file(&self) -> SourceFile {
|
2018-03-15 18:09:22 -05:00
|
|
|
SourceFile(self.0.source_file())
|
2017-08-01 20:05:08 -05:00
|
|
|
}
|
|
|
|
|
2017-12-31 20:30:13 -06:00
|
|
|
/// The `Span` for the tokens in the previous macro expansion from which
|
|
|
|
/// `self` was generated from, if any.
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-12-31 20:30:13 -06:00
|
|
|
pub fn parent(&self) -> Option<Span> {
|
2018-04-26 17:28:34 -05:00
|
|
|
self.0.parent().map(Span)
|
2017-12-31 20:30:13 -06: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 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-12-31 20:30:13 -06:00
|
|
|
pub fn source(&self) -> Span {
|
2018-03-15 18:09:22 -05:00
|
|
|
Span(self.0.source())
|
2017-12-31 20:30:13 -06:00
|
|
|
}
|
|
|
|
|
2019-02-09 15:23:30 -06:00
|
|
|
/// Gets the starting line/column in the source file for this span.
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 20:05:08 -05:00
|
|
|
pub fn start(&self) -> LineColumn {
|
2018-03-15 18:09:22 -05:00
|
|
|
self.0.start()
|
2017-08-01 20:05:08 -05:00
|
|
|
}
|
|
|
|
|
2019-02-09 15:23:30 -06:00
|
|
|
/// Gets the ending line/column in the source file for this span.
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 20:05:08 -05:00
|
|
|
pub fn end(&self) -> LineColumn {
|
2018-03-15 18:09:22 -05:00
|
|
|
self.0.end()
|
2017-08-01 20:05:08 -05:00
|
|
|
}
|
|
|
|
|
2019-02-09 15:23:30 -06:00
|
|
|
/// Creates a new span encompassing `self` and `other`.
|
2017-08-01 20:05:08 -05:00
|
|
|
///
|
|
|
|
/// Returns `None` if `self` and `other` are from different files.
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 20:05:08 -05:00
|
|
|
pub fn join(&self, other: Span) -> Option<Span> {
|
2018-03-15 18:09:22 -05:00
|
|
|
self.0.join(other.0).map(Span)
|
2017-08-01 20:05:08 -05:00
|
|
|
}
|
|
|
|
|
2018-01-03 01:29:11 -06:00
|
|
|
/// Creates a new span with the same line/column information as `self` but
|
|
|
|
/// that resolves symbols as though it were at `other`.
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2018-01-03 01:29:11 -06:00
|
|
|
pub fn resolved_at(&self, other: Span) -> Span {
|
2018-03-15 18:09:22 -05:00
|
|
|
Span(self.0.resolved_at(other.0))
|
2018-01-03 01:29:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new span with the same name resolution behavior as `self` but
|
|
|
|
/// with the line/column information of `other`.
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2018-01-03 01:29:11 -06:00
|
|
|
pub fn located_at(&self, other: Span) -> Span {
|
|
|
|
other.resolved_at(*self)
|
|
|
|
}
|
|
|
|
|
2018-04-02 10:19:32 -05:00
|
|
|
/// Compares to spans to see if they're equal.
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn eq(&self, other: &Span) -> bool {
|
|
|
|
self.0 == other.0
|
|
|
|
}
|
|
|
|
|
2018-11-08 03:07:02 -06: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 04:56:43 -05:00
|
|
|
diagnostic_method!(error, Level::Error);
|
|
|
|
diagnostic_method!(warning, Level::Warning);
|
|
|
|
diagnostic_method!(note, Level::Note);
|
|
|
|
diagnostic_method!(help, Level::Help);
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05:00
|
|
|
/// Prints a span in a form convenient for debugging.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-06 17:20:57 -05:00
|
|
|
impl fmt::Debug for Span {
|
2019-02-03 12:55:40 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-15 18:09:22 -05:00
|
|
|
self.0.fmt(f)
|
2018-04-06 17:20:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 20:05:08 -05:00
|
|
|
/// A line-column pair representing the start or end of a `Span`.
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 20:05:08 -05: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 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-12-12 11:14:54 -06:00
|
|
|
pub line: usize,
|
2017-08-01 20:05:08 -05:00
|
|
|
/// The 0-indexed column (in UTF-8 characters) in the source file on which
|
|
|
|
/// the span starts or ends (inclusive).
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-12-12 11:14:54 -06:00
|
|
|
pub column: usize
|
2017-08-01 20:05:08 -05:00
|
|
|
}
|
|
|
|
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2018-05-04 16:11:22 -05:00
|
|
|
impl !Send for LineColumn {}
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2018-05-04 16:11:22 -05:00
|
|
|
impl !Sync for LineColumn {}
|
|
|
|
|
2017-08-01 20:05:08 -05:00
|
|
|
/// The source file of a given `Span`.
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 20:05:08 -05:00
|
|
|
#[derive(Clone)]
|
2018-03-15 18:09:22 -05:00
|
|
|
pub struct SourceFile(bridge::client::SourceFile);
|
2018-03-01 19:42:22 -06:00
|
|
|
|
2017-08-01 20:05:08 -05:00
|
|
|
impl SourceFile {
|
2019-02-09 15:23:30 -06:00
|
|
|
/// Gets the path to this source file.
|
2017-08-01 20:05:08 -05:00
|
|
|
///
|
|
|
|
/// ### Note
|
|
|
|
/// If the code span associated with this `SourceFile` was generated by an external macro, this
|
2018-08-18 05:14:31 -05:00
|
|
|
/// macro, this may not be an actual path on the filesystem. Use [`is_real`] to check.
|
2017-08-01 20:05:08 -05:00
|
|
|
///
|
2018-02-18 17:05:24 -06:00
|
|
|
/// Also note that even if `is_real` returns `true`, if `--remap-path-prefix` was passed on
|
2017-08-01 20:05:08 -05:00
|
|
|
/// the command line, the path as given may not actually be valid.
|
|
|
|
///
|
|
|
|
/// [`is_real`]: #method.is_real
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2018-03-16 10:04:14 -05:00
|
|
|
pub fn path(&self) -> PathBuf {
|
2018-03-15 18:09:22 -05:00
|
|
|
PathBuf::from(self.0.path())
|
2017-08-01 20:05:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if this source file is a real source file, and not generated by an external
|
|
|
|
/// macro's expansion.
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 20:05:08 -05: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-15 18:09:22 -05:00
|
|
|
self.0.is_real()
|
2017-08-01 20:05:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 20:05:08 -05:00
|
|
|
impl fmt::Debug for SourceFile {
|
2019-02-03 12:55:40 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-08-01 20:05:08 -05:00
|
|
|
f.debug_struct("SourceFile")
|
2018-03-16 10:04:14 -05:00
|
|
|
.field("path", &self.path())
|
2017-08-01 20:05:08 -05:00
|
|
|
.field("is_real", &self.is_real())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 20:05:08 -05:00
|
|
|
impl PartialEq for SourceFile {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
2018-03-15 18:09:22 -05:00
|
|
|
self.0.eq(&other.0)
|
2017-08-01 20:05:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2017-08-01 20:05:08 -05:00
|
|
|
impl Eq for SourceFile {}
|
|
|
|
|
2018-11-26 20:59:49 -06:00
|
|
|
/// A single token or a delimited sequence of token trees (e.g., `[1, (), ..]`).
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-06 17:20:57 -05:00
|
|
|
#[derive(Clone)]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub enum TokenTree {
|
2018-05-05 12:12:37 -05:00
|
|
|
/// A token stream surrounded by bracket delimiters.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
Group(
|
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
Group
|
|
|
|
),
|
2018-05-13 16:01:56 -05:00
|
|
|
/// An identifier.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
Ident(
|
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
Ident
|
|
|
|
),
|
2018-05-05 12:12:37 -05:00
|
|
|
/// A single punctuation character (`+`, `,`, `$`, etc.).
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
Punct(
|
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
Punct
|
|
|
|
),
|
2018-04-02 10:19:32 -05:00
|
|
|
/// A literal character (`'a'`), string (`"hello"`), number (`2.3`), etc.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
Literal(
|
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
Literal
|
|
|
|
),
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-04 16:11:22 -05:00
|
|
|
impl !Send for TokenTree {}
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-04 16:11:22 -05:00
|
|
|
impl !Sync for TokenTree {}
|
|
|
|
|
2018-04-02 10:19:32 -05:00
|
|
|
impl TokenTree {
|
2018-05-05 12:12:37 -05:00
|
|
|
/// Returns the span of this tree, delegating to the `span` method of
|
|
|
|
/// the contained token or a delimited stream.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
match *self {
|
|
|
|
TokenTree::Group(ref t) => t.span(),
|
2018-05-05 13:09:41 -05:00
|
|
|
TokenTree::Ident(ref t) => t.span(),
|
|
|
|
TokenTree::Punct(ref t) => t.span(),
|
2018-04-02 10:19:32 -05: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 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn set_span(&mut self, span: Span) {
|
|
|
|
match *self {
|
|
|
|
TokenTree::Group(ref mut t) => t.set_span(span),
|
2018-05-05 13:09:41 -05:00
|
|
|
TokenTree::Ident(ref mut t) => t.set_span(span),
|
|
|
|
TokenTree::Punct(ref mut t) => t.set_span(span),
|
2018-04-02 10:19:32 -05:00
|
|
|
TokenTree::Literal(ref mut t) => t.set_span(span),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 12:05:20 -06:00
|
|
|
/// Prints token tree in a form convenient for debugging.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-06 17:20:57 -05:00
|
|
|
impl fmt::Debug for TokenTree {
|
2019-02-03 12:55:40 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-04-06 17:20:57 -05: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 13:09:41 -05:00
|
|
|
TokenTree::Ident(ref tt) => tt.fmt(f),
|
|
|
|
TokenTree::Punct(ref tt) => tt.fmt(f),
|
2018-04-06 17:20:57 -05:00
|
|
|
TokenTree::Literal(ref tt) => tt.fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
impl From<Group> for TokenTree {
|
|
|
|
fn from(g: Group) -> TokenTree {
|
|
|
|
TokenTree::Group(g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 13:09:41 -05:00
|
|
|
impl From<Ident> for TokenTree {
|
|
|
|
fn from(g: Ident) -> TokenTree {
|
|
|
|
TokenTree::Ident(g)
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 13:09:41 -05:00
|
|
|
impl From<Punct> for TokenTree {
|
|
|
|
fn from(g: Punct) -> TokenTree {
|
|
|
|
TokenTree::Punct(g)
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
impl From<Literal> for TokenTree {
|
|
|
|
fn from(g: Literal) -> TokenTree {
|
|
|
|
TokenTree::Literal(g)
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 20:59:49 -06:00
|
|
|
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
|
2018-03-15 18:09:22 -05: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 10:35:57 -05:00
|
|
|
/// Prints the token tree as a string that is supposed to be losslessly convertible back
|
2018-05-05 12:12:37 -05:00
|
|
|
/// into the same token tree (modulo spans), except for possibly `TokenTree::Group`s
|
2018-05-13 10:35:57 -05:00
|
|
|
/// with `Delimiter::None` delimiters and negative numeric literals.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 18:41:09 -05:00
|
|
|
impl fmt::Display for TokenTree {
|
2019-02-03 12:55:40 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-15 18:09:22 -05:00
|
|
|
f.write_str(&self.to_string())
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05:00
|
|
|
/// A delimited token stream.
|
2018-04-02 10:19:32 -05:00
|
|
|
///
|
2018-05-05 12:12:37 -05:00
|
|
|
/// A `Group` internally contains a `TokenStream` which is surrounded by `Delimiter`s.
|
2018-07-19 16:09:05 -05:00
|
|
|
#[derive(Clone)]
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-03-15 18:09:22 -05:00
|
|
|
pub struct Group(bridge::client::Group);
|
2017-03-17 18:41:09 -05:00
|
|
|
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-04 16:11:22 -05:00
|
|
|
impl !Send for Group {}
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-04 16:11:22 -05:00
|
|
|
impl !Sync for Group {}
|
|
|
|
|
2017-03-17 18:41:09 -05:00
|
|
|
/// Describes how a sequence of token trees is delimited.
|
2017-08-20 16:20:34 -05:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 18:41:09 -05:00
|
|
|
pub enum Delimiter {
|
|
|
|
/// `( ... )`
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 18:41:09 -05:00
|
|
|
Parenthesis,
|
|
|
|
/// `{ ... }`
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-08-21 10:17:27 -05:00
|
|
|
Brace,
|
|
|
|
/// `[ ... ]`
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 18:41:09 -05:00
|
|
|
Bracket,
|
2018-05-05 12:12:37 -05: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 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 18:41:09 -05:00
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2018-04-02 10:19:32 -05:00
|
|
|
impl Group {
|
2018-05-05 12:12:37 -05:00
|
|
|
/// Creates a new `Group` with the given delimiter and token stream.
|
2018-04-02 10:19:32 -05: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 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
|
2018-03-15 18:09:22 -05:00
|
|
|
Group(bridge::client::Group::new(delimiter, stream.0))
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
2017-03-17 18:41:09 -05:00
|
|
|
|
2018-04-02 10:19:32 -05:00
|
|
|
/// Returns the delimiter of this `Group`
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn delimiter(&self) -> Delimiter {
|
2018-03-15 18:09:22 -05:00
|
|
|
self.0.delimiter()
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
2018-04-02 10:19:32 -05: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 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn stream(&self) -> TokenStream {
|
2018-03-15 18:09:22 -05:00
|
|
|
TokenStream(self.0.stream())
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the span for the delimiters of this token stream, spanning the
|
|
|
|
/// entire `Group`.
|
2018-09-02 08:59:02 -05:00
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// pub fn span(&self) -> Span {
|
|
|
|
/// ^^^^^^^
|
|
|
|
/// ```
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn span(&self) -> Span {
|
2018-03-15 18:09:22 -05:00
|
|
|
Span(self.0.span())
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
|
2018-09-08 20:07:02 -05:00
|
|
|
/// Returns the span pointing to the opening delimiter of this group.
|
2018-09-02 08:59:02 -05:00
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// pub fn span_open(&self) -> Span {
|
|
|
|
/// ^
|
|
|
|
/// ```
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2018-09-02 08:59:02 -05:00
|
|
|
pub fn span_open(&self) -> Span {
|
2018-03-15 18:09:22 -05:00
|
|
|
Span(self.0.span_open())
|
2018-09-02 08:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-09-08 20:07:02 -05:00
|
|
|
/// Returns the span pointing to the closing delimiter of this group.
|
2018-09-02 08:59:02 -05:00
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// pub fn span_close(&self) -> Span {
|
|
|
|
/// ^
|
|
|
|
/// ```
|
2018-10-01 12:53:03 -05:00
|
|
|
#[unstable(feature = "proc_macro_span", issue = "54725")]
|
2018-09-02 08:59:02 -05:00
|
|
|
pub fn span_close(&self) -> Span {
|
2018-03-15 18:09:22 -05:00
|
|
|
Span(self.0.span_close())
|
2018-09-02 08:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-04-02 10:19:32 -05: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 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn set_span(&mut self, span: Span) {
|
2018-03-15 18:09:22 -05:00
|
|
|
self.0.set_span(span.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 20:59:49 -06:00
|
|
|
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
|
2018-03-15 18:09:22 -05: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 18:41:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05: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 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
impl fmt::Display for Group {
|
2019-02-03 12:55:40 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-15 18:09:22 -05:00
|
|
|
f.write_str(&self.to_string())
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:09:05 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
impl fmt::Debug for Group {
|
2019-02-03 12:55:40 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-07-19 16:09:05 -05:00
|
|
|
f.debug_struct("Group")
|
|
|
|
.field("delimiter", &self.delimiter())
|
|
|
|
.field("stream", &self.stream())
|
|
|
|
.field("span", &self.span())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 13:09:41 -05:00
|
|
|
/// An `Punct` is an single punctuation character like `+`, `-` or `#`.
|
2018-04-02 10:19:32 -05:00
|
|
|
///
|
2018-11-12 12:05:20 -06:00
|
|
|
/// Multi-character operators like `+=` are represented as two instances of `Punct` with different
|
2018-04-02 10:19:32 -05:00
|
|
|
/// forms of `Spacing` returned.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-07-19 16:09:05 -05:00
|
|
|
#[derive(Clone)]
|
2018-03-15 18:09:22 -05:00
|
|
|
pub struct Punct(bridge::client::Punct);
|
2018-04-02 10:19:32 -05:00
|
|
|
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 13:09:41 -05:00
|
|
|
impl !Send for Punct {}
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 13:09:41 -05:00
|
|
|
impl !Sync for Punct {}
|
2018-05-04 16:11:22 -05:00
|
|
|
|
2018-05-05 13:09:41 -05:00
|
|
|
/// Whether an `Punct` is followed immediately by another `Punct` or
|
2018-05-05 12:12:37 -05:00
|
|
|
/// followed by another token or whitespace.
|
2017-08-20 16:20:34 -05:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-06-04 20:41:33 -05:00
|
|
|
pub enum Spacing {
|
2018-11-26 20:59:49 -06:00
|
|
|
/// e.g., `+` is `Alone` in `+ =`, `+ident` or `+()`.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 18:41:09 -05:00
|
|
|
Alone,
|
2018-11-26 20:59:49 -06:00
|
|
|
/// e.g., `+` is `Joint` in `+=` or `'#`.
|
2018-05-13 16:01:56 -05:00
|
|
|
/// Additionally, single quote `'` can join with identifiers to form lifetimes `'ident`.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 18:41:09 -05:00
|
|
|
Joint,
|
|
|
|
}
|
|
|
|
|
2018-05-05 13:09:41 -05:00
|
|
|
impl Punct {
|
|
|
|
/// Creates a new `Punct` from the given character and spacing.
|
2018-05-05 14:45:59 -05:00
|
|
|
/// The `ch` argument must be a valid punctuation character permitted by the language,
|
|
|
|
/// otherwise the function will panic.
|
2018-04-02 10:19:32 -05:00
|
|
|
///
|
2018-05-05 13:09:41 -05:00
|
|
|
/// The returned `Punct` will have the default span of `Span::call_site()`
|
2018-04-02 10:19:32 -05:00
|
|
|
/// which can be further configured with the `set_span` method below.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 13:09:41 -05:00
|
|
|
pub fn new(ch: char, spacing: Spacing) -> Punct {
|
2018-03-15 18:09:22 -05:00
|
|
|
Punct(bridge::client::Punct::new(ch, spacing))
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05:00
|
|
|
/// Returns the value of this punctuation character as `char`.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 13:09:41 -05:00
|
|
|
pub fn as_char(&self) -> char {
|
2018-03-15 18:09:22 -05:00
|
|
|
self.0.as_char()
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05:00
|
|
|
/// Returns the spacing of this punctuation character, indicating whether it's immediately
|
2018-05-05 13:09:41 -05:00
|
|
|
/// followed by another `Punct` in the token stream, so they can potentially be combined into
|
2018-11-12 12:05:20 -06:00
|
|
|
/// a multi-character operator (`Joint`), or it's followed by some other token or whitespace
|
2018-05-05 12:12:37 -05:00
|
|
|
/// (`Alone`) so the operator has certainly ended.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn spacing(&self) -> Spacing {
|
2018-03-15 18:09:22 -05:00
|
|
|
self.0.spacing()
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05:00
|
|
|
/// Returns the span for this punctuation character.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn span(&self) -> Span {
|
2018-03-15 18:09:22 -05:00
|
|
|
Span(self.0.span())
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05:00
|
|
|
/// Configure the span for this punctuation character.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn set_span(&mut self, span: Span) {
|
2018-03-15 18:09:22 -05:00
|
|
|
self.0 = self.0.with_span(span.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 20:59:49 -06:00
|
|
|
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
|
2018-03-15 18:09:22 -05: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 10:19:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05:00
|
|
|
/// Prints the punctuation character as a string that should be losslessly convertible
|
|
|
|
/// back into the same character.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 13:09:41 -05:00
|
|
|
impl fmt::Display for Punct {
|
2019-02-03 12:55:40 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-15 18:09:22 -05:00
|
|
|
f.write_str(&self.to_string())
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
}
|
2017-03-17 18:41:09 -05:00
|
|
|
|
2018-07-19 16:09:05 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
impl fmt::Debug for Punct {
|
2019-02-03 12:55:40 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-07-19 16:09:05 -05:00
|
|
|
f.debug_struct("Punct")
|
|
|
|
.field("ch", &self.as_char())
|
|
|
|
.field("spacing", &self.spacing())
|
|
|
|
.field("span", &self.span())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-13 16:01:56 -05:00
|
|
|
/// An identifier (`ident`).
|
2018-07-19 16:09:05 -05:00
|
|
|
#[derive(Clone)]
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-03-15 18:09:22 -05:00
|
|
|
pub struct Ident(bridge::client::Ident);
|
2018-05-04 16:11:22 -05:00
|
|
|
|
2018-05-05 13:09:41 -05:00
|
|
|
impl Ident {
|
|
|
|
/// Creates a new `Ident` with the given `string` as well as the specified
|
2018-04-02 10:19:32 -05:00
|
|
|
/// `span`.
|
2018-05-13 16:01:56 -05:00
|
|
|
/// The `string` argument must be a valid identifier permitted by the
|
2018-05-05 14:45:59 -05:00
|
|
|
/// language, otherwise the function will panic.
|
2018-04-02 10:19:32 -05:00
|
|
|
///
|
|
|
|
/// Note that `span`, currently in rustc, configures the hygiene information
|
2018-05-05 12:12:37 -05: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 10:19:32 -05:00
|
|
|
///
|
|
|
|
/// Due to the current importance of hygiene this constructor, unlike other
|
|
|
|
/// tokens, requires a `Span` to be specified at construction.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 13:09:41 -05:00
|
|
|
pub fn new(string: &str, span: Span) -> Ident {
|
2018-03-15 18:09:22 -05:00
|
|
|
Ident(bridge::client::Ident::new(string, span.0, false))
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
|
2018-05-05 14:39:05 -05:00
|
|
|
/// Same as `Ident::new`, but creates a raw identifier (`r#ident`).
|
2018-10-01 12:44:19 -05:00
|
|
|
#[unstable(feature = "proc_macro_raw_ident", issue = "54723")]
|
2018-05-05 14:39:05 -05:00
|
|
|
pub fn new_raw(string: &str, span: Span) -> Ident {
|
2018-03-15 18:09:22 -05:00
|
|
|
Ident(bridge::client::Ident::new(string, span.0, true))
|
2018-05-05 14:39:05 -05:00
|
|
|
}
|
|
|
|
|
2018-05-05 13:09:41 -05:00
|
|
|
/// Returns the span of this `Ident`, encompassing the entire string returned
|
2018-04-02 10:19:32 -05:00
|
|
|
/// by `as_str`.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn span(&self) -> Span {
|
2018-03-15 18:09:22 -05:00
|
|
|
Span(self.0.span())
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
|
2018-05-05 13:09:41 -05:00
|
|
|
/// Configures the span of this `Ident`, possibly changing its hygiene context.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn set_span(&mut self, span: Span) {
|
2018-03-15 18:09:22 -05:00
|
|
|
self.0 = self.0.with_span(span.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 20:59:49 -06:00
|
|
|
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
|
2018-03-15 18:09:22 -05: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 10:19:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05:00
|
|
|
/// Prints the identifier as a string that should be losslessly convertible
|
|
|
|
/// back into the same identifier.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-05-05 13:09:41 -05:00
|
|
|
impl fmt::Display for Ident {
|
2019-02-03 12:55:40 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-15 18:09:22 -05:00
|
|
|
f.write_str(&self.to_string())
|
2018-07-19 16:09:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
impl fmt::Debug for Ident {
|
2019-02-03 12:55:40 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-07-19 16:09:05 -05:00
|
|
|
f.debug_struct("Ident")
|
|
|
|
.field("ident", &self.to_string())
|
|
|
|
.field("span", &self.span())
|
|
|
|
.finish()
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05: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 13:09:41 -05:00
|
|
|
/// Boolean literals like `true` and `false` do not belong here, they are `Ident`s.
|
2018-03-15 18:09:22 -05:00
|
|
|
#[derive(Clone)]
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-03-15 18:09:22 -05:00
|
|
|
pub struct Literal(bridge::client::Literal);
|
2018-05-04 16:11:22 -05:00
|
|
|
|
2018-04-02 10:19:32 -05: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 12:05:20 -06:00
|
|
|
/// Literals created from negative numbers may not survive round-trips through
|
2018-05-13 10:35:57 -05:00
|
|
|
/// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
|
2018-04-02 10:19:32 -05: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 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn $name(n: $kind) -> Literal {
|
2018-03-15 18:09:22 -05:00
|
|
|
Literal(bridge::client::Literal::typed_integer(&n.to_string(), stringify!($kind)))
|
2018-04-02 10:19:32 -05: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 10:35:57 -05: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 10:19:32 -05: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 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn $name(n: $kind) -> Literal {
|
2018-03-15 18:09:22 -05:00
|
|
|
Literal(bridge::client::Literal::integer(&n.to_string()))
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
2018-04-02 10:19:32 -05:00
|
|
|
)*)
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Literal {
|
2018-04-02 10:19:32 -05: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-04 20:41:33 -05:00
|
|
|
}
|
|
|
|
|
2018-04-02 10:19:32 -05: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-04 20:41:33 -05:00
|
|
|
}
|
|
|
|
|
2018-04-02 10:19:32 -05: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 10:35:57 -05: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 10:19:32 -05: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 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn f32_unsuffixed(n: f32) -> Literal {
|
2017-06-04 20:41:33 -05:00
|
|
|
if !n.is_finite() {
|
|
|
|
panic!("Invalid float literal {}", n);
|
|
|
|
}
|
2018-03-15 18:09:22 -05:00
|
|
|
Literal(bridge::client::Literal::float(&n.to_string()))
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
2018-04-02 10:19:32 -05:00
|
|
|
/// Creates a new suffixed floating-point literal.
|
|
|
|
///
|
2018-11-12 12:05:20 -06:00
|
|
|
/// This constructor will create a literal like `1.0f32` where the value
|
2018-04-02 10:19:32 -05: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 10:35:57 -05: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 10:19:32 -05: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 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn f32_suffixed(n: f32) -> Literal {
|
2017-06-04 20:41:33 -05:00
|
|
|
if !n.is_finite() {
|
2018-04-02 10:19:32 -05:00
|
|
|
panic!("Invalid float literal {}", n);
|
|
|
|
}
|
2018-03-15 18:09:22 -05:00
|
|
|
Literal(bridge::client::Literal::f32(&n.to_string()))
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
2018-04-02 10:19:32 -05: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 10:35:57 -05: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 10:19:32 -05: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 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn f64_unsuffixed(n: f64) -> Literal {
|
2017-06-04 20:41:33 -05:00
|
|
|
if !n.is_finite() {
|
2018-04-02 10:19:32 -05:00
|
|
|
panic!("Invalid float literal {}", n);
|
|
|
|
}
|
2018-03-15 18:09:22 -05:00
|
|
|
Literal(bridge::client::Literal::float(&n.to_string()))
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new suffixed floating-point literal.
|
|
|
|
///
|
2018-11-12 12:05:20 -06:00
|
|
|
/// This constructor will create a literal like `1.0f64` where the value
|
2018-04-02 10:19:32 -05: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 10:35:57 -05: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 10:19:32 -05: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 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn f64_suffixed(n: f64) -> Literal {
|
|
|
|
if !n.is_finite() {
|
|
|
|
panic!("Invalid float literal {}", n);
|
|
|
|
}
|
2018-03-15 18:09:22 -05:00
|
|
|
Literal(bridge::client::Literal::f64(&n.to_string()))
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// String literal.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 18:41:09 -05:00
|
|
|
pub fn string(string: &str) -> Literal {
|
2018-03-15 18:09:22 -05:00
|
|
|
Literal(bridge::client::Literal::string(string))
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Character literal.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-03-17 18:41:09 -05:00
|
|
|
pub fn character(ch: char) -> Literal {
|
2018-03-15 18:09:22 -05:00
|
|
|
Literal(bridge::client::Literal::character(ch))
|
2017-06-04 20:41:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Byte string literal.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2017-06-04 20:41:33 -05:00
|
|
|
pub fn byte_string(bytes: &[u8]) -> Literal {
|
2018-03-15 18:09:22 -05:00
|
|
|
Literal(bridge::client::Literal::byte_string(bytes))
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
2018-04-02 10:19:32 -05:00
|
|
|
/// Returns the span encompassing this literal.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn span(&self) -> Span {
|
2018-03-15 18:09:22 -05:00
|
|
|
Span(self.0.span())
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Configures the span associated for this literal.
|
2018-07-03 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
pub fn set_span(&mut self, span: Span) {
|
2018-03-15 18:09:22 -05:00
|
|
|
self.0.set_span(span.0);
|
2018-04-02 10:19:32 -05:00
|
|
|
}
|
2018-11-20 23:12:30 -06: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-15 18:09:22 -05: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 23:12:30 -06:00
|
|
|
}
|
|
|
|
|
2018-03-15 18:09:22 -05:00
|
|
|
self.0.subspan(
|
|
|
|
cloned_bound(range.start_bound()),
|
|
|
|
cloned_bound(range.end_bound()),
|
|
|
|
).map(Span)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 20:59:49 -06:00
|
|
|
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
|
2018-03-15 18:09:22 -05: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 23:12:30 -06:00
|
|
|
}
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:37 -05: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 17:36:31 -05:00
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
2018-04-02 10:19:32 -05:00
|
|
|
impl fmt::Display for Literal {
|
2019-02-03 12:55:40 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-15 18:09:22 -05:00
|
|
|
f.write_str(&self.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
|
|
|
|
impl fmt::Debug for Literal {
|
2019-02-03 12:55:40 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-15 18:09:22 -05:00
|
|
|
// FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
|
|
|
|
self.0.fmt(f)
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
}
|