aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorandroid-build-team Robot <android-build-team-robot@google.com>2021-06-17 19:04:18 +0000
committerandroid-build-team Robot <android-build-team-robot@google.com>2021-06-17 19:04:18 +0000
commit0e34cbb3d5f10430aeab6273ca7f72305927beaf (patch)
treedfd15a994d01260db5255146bfd1a9a0284aa94e /src
parenta78d493bdbb465c11c6ff31b08fa4213b1cba79c (diff)
parent5bba24088af19dfc9b313ed9bd8ae15c16a35b60 (diff)
downloadquote-d9c09d7d37021de0f90912ab7a82cc7a7bec52ed.tar.gz
Change-Id: If71805668bef85d961968a323869895bdd3ca4fc
Diffstat (limited to 'src')
-rw-r--r--src/format.rs8
-rw-r--r--src/ident_fragment.rs20
-rw-r--r--src/lib.rs501
-rw-r--r--src/runtime.rs191
4 files changed, 541 insertions, 179 deletions
diff --git a/src/format.rs b/src/format.rs
index 13c8811..745cb5d 100644
--- a/src/format.rs
+++ b/src/format.rs
@@ -128,7 +128,7 @@ macro_rules! format_ident {
macro_rules! format_ident_impl {
// Final state
([$span:expr, $($fmt:tt)*]) => {
- $crate::__rt::mk_ident(&format!($($fmt)*), $span)
+ $crate::__private::mk_ident(&format!($($fmt)*), $span)
};
// Span argument
@@ -137,7 +137,7 @@ macro_rules! format_ident_impl {
};
([$old:expr, $($fmt:tt)*] span = $span:expr, $($rest:tt)*) => {
$crate::format_ident_impl!([
- ::std::option::Option::Some::<$crate::__rt::Span>($span),
+ ::std::option::Option::Some::<$crate::__private::Span>($span),
$($fmt)*
] $($rest)*)
};
@@ -147,7 +147,7 @@ macro_rules! format_ident_impl {
$crate::format_ident_impl!([$span, $($fmt)*] $name = $arg,)
};
([$span:expr, $($fmt:tt)*] $name:ident = $arg:expr, $($rest:tt)*) => {
- match $crate::__rt::IdentFragmentAdapter(&$arg) {
+ match $crate::__private::IdentFragmentAdapter(&$arg) {
arg => $crate::format_ident_impl!([$span.or(arg.span()), $($fmt)*, $name = arg] $($rest)*),
}
};
@@ -157,7 +157,7 @@ macro_rules! format_ident_impl {
$crate::format_ident_impl!([$span, $($fmt)*] $arg,)
};
([$span:expr, $($fmt:tt)*] $arg:expr, $($rest:tt)*) => {
- match $crate::__rt::IdentFragmentAdapter(&$arg) {
+ match $crate::__private::IdentFragmentAdapter(&$arg) {
arg => $crate::format_ident_impl!([$span.or(arg.span()), $($fmt)*, arg] $($rest)*),
}
};
diff --git a/src/ident_fragment.rs b/src/ident_fragment.rs
index 09ead65..e7472fe 100644
--- a/src/ident_fragment.rs
+++ b/src/ident_fragment.rs
@@ -1,4 +1,5 @@
use proc_macro2::{Ident, Span};
+use std::borrow::Cow;
use std::fmt;
/// Specialized formatting trait used by `format_ident!`.
@@ -19,7 +20,7 @@ pub trait IdentFragment {
}
}
-impl<'a, T: IdentFragment + ?Sized> IdentFragment for &'a T {
+impl<T: IdentFragment + ?Sized> IdentFragment for &T {
fn span(&self) -> Option<Span> {
<T as IdentFragment>::span(*self)
}
@@ -29,7 +30,7 @@ impl<'a, T: IdentFragment + ?Sized> IdentFragment for &'a T {
}
}
-impl<'a, T: IdentFragment + ?Sized> IdentFragment for &'a mut T {
+impl<T: IdentFragment + ?Sized> IdentFragment for &mut T {
fn span(&self) -> Option<Span> {
<T as IdentFragment>::span(*self)
}
@@ -54,6 +55,19 @@ impl IdentFragment for Ident {
}
}
+impl<T> IdentFragment for Cow<'_, T>
+where
+ T: IdentFragment + ToOwned + ?Sized,
+{
+ fn span(&self) -> Option<Span> {
+ T::span(self)
+ }
+
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ T::fmt(self, f)
+ }
+}
+
// Limited set of types which this is implemented for, as we want to avoid types
// which will often include non-identifier characters in their `Display` impl.
macro_rules! ident_fragment_display {
@@ -68,5 +82,5 @@ macro_rules! ident_fragment_display {
}
}
-ident_fragment_display!(bool, str, String);
+ident_fragment_display!(bool, str, String, char);
ident_fragment_display!(u8, u16, u32, u64, u128, usize);
diff --git a/src/lib.rs b/src/lib.rs
index 3341a16..356e43a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,11 @@
+//! [![github]](https://github.com/dtolnay/quote)&ensp;[![crates-io]](https://crates.io/crates/quote)&ensp;[![docs-rs]](https://docs.rs/quote)
+//!
+//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
+//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
+//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K
+//!
+//! <br>
+//!
//! This crate provides the [`quote!`] macro for turning Rust syntax tree data
//! structures into tokens of source code.
//!
@@ -73,7 +81,13 @@
//! ```
// Quote types in rustdoc of other crates get linked to here.
-#![doc(html_root_url = "https://docs.rs/quote/1.0.2")]
+#![doc(html_root_url = "https://docs.rs/quote/1.0.9")]
+#![allow(
+ clippy::doc_markdown,
+ clippy::missing_errors_doc,
+ clippy::missing_panics_doc,
+ clippy::module_name_repetitions
+)]
#[cfg(all(
not(all(target_arch = "wasm32", target_os = "unknown")),
@@ -89,7 +103,7 @@ mod to_tokens;
// Not public API.
#[doc(hidden)]
#[path = "runtime.rs"]
-pub mod __rt;
+pub mod __private;
pub use crate::ext::TokenStreamExt;
pub use crate::ident_fragment::IdentFragment;
@@ -462,9 +476,14 @@ pub mod spanned;
/// ```
#[macro_export]
macro_rules! quote {
- ($($tt:tt)*) => {
- $crate::quote_spanned!($crate::__rt::Span::call_site()=> $($tt)*)
+ () => {
+ $crate::__private::TokenStream::new()
};
+ ($($tt:tt)*) => {{
+ let mut _s = $crate::__private::TokenStream::new();
+ $crate::quote_each_token!(_s $($tt)*);
+ _s
+ }};
}
/// Same as `quote!`, but applies a given span to all tokens originating within
@@ -550,7 +569,7 @@ macro_rules! quote {
/// ```
///
/// If the assertion fails, the user will see an error like the following. The
-/// input span of their type is hightlighted in the error.
+/// input span of their type is highlighted in the error.
///
/// ```text
/// error[E0277]: the trait bound `*const (): std::marker::Sync` is not satisfied
@@ -565,10 +584,14 @@ macro_rules! quote {
/// placed appropriately by the compiler.
#[macro_export]
macro_rules! quote_spanned {
+ ($span:expr=>) => {{
+ let _: $crate::__private::Span = $span;
+ $crate::__private::TokenStream::new()
+ }};
($span:expr=> $($tt:tt)*) => {{
- let mut _s = $crate::__rt::TokenStream::new();
- let _span: $crate::__rt::Span = $span;
- $crate::quote_each_token!(_s _span $($tt)*);
+ let mut _s = $crate::__private::TokenStream::new();
+ let _span: $crate::__private::Span = $span;
+ $crate::quote_each_token_spanned!(_s _span $($tt)*);
_s
}};
}
@@ -638,7 +661,7 @@ macro_rules! quote_bind_into_iter {
macro_rules! quote_bind_next_or_break {
($var:ident) => {
let $var = match $var.next() {
- Some(_x) => $crate::__rt::RepInterp(_x),
+ Some(_x) => $crate::__private::RepInterp(_x),
None => break,
};
};
@@ -647,8 +670,24 @@ macro_rules! quote_bind_next_or_break {
#[macro_export]
#[doc(hidden)]
macro_rules! quote_each_token {
+ ($tokens:ident $($tts:tt)*) => {
+ $crate::quote_tokens_with_context!($tokens
+ (@ @ @ @ @ @ $($tts)*)
+ (@ @ @ @ @ $($tts)* @)
+ (@ @ @ @ $($tts)* @ @)
+ (@ @ @ $(($tts))* @ @ @)
+ (@ @ $($tts)* @ @ @ @)
+ (@ $($tts)* @ @ @ @ @)
+ ($($tts)* @ @ @ @ @ @)
+ );
+ };
+}
+
+#[macro_export]
+#[doc(hidden)]
+macro_rules! quote_each_token_spanned {
($tokens:ident $span:ident $($tts:tt)*) => {
- $crate::quote_tokens_with_context!($tokens $span
+ $crate::quote_tokens_with_context_spanned!($tokens $span
(@ @ @ @ @ @ $($tts)*)
(@ @ @ @ @ $($tts)* @)
(@ @ @ @ $($tts)* @ @)
@@ -663,13 +702,27 @@ macro_rules! quote_each_token {
#[macro_export]
#[doc(hidden)]
macro_rules! quote_tokens_with_context {
+ ($tokens:ident
+ ($($b3:tt)*) ($($b2:tt)*) ($($b1:tt)*)
+ ($($curr:tt)*)
+ ($($a1:tt)*) ($($a2:tt)*) ($($a3:tt)*)
+ ) => {
+ $(
+ $crate::quote_token_with_context!($tokens $b3 $b2 $b1 $curr $a1 $a2 $a3);
+ )*
+ };
+}
+
+#[macro_export]
+#[doc(hidden)]
+macro_rules! quote_tokens_with_context_spanned {
($tokens:ident $span:ident
($($b3:tt)*) ($($b2:tt)*) ($($b1:tt)*)
($($curr:tt)*)
($($a1:tt)*) ($($a2:tt)*) ($($a3:tt)*)
) => {
$(
- $crate::quote_token_with_context!($tokens $span $b3 $b2 $b1 $curr $a1 $a2 $a3);
+ $crate::quote_token_with_context_spanned!($tokens $span $b3 $b2 $b1 $curr $a1 $a2 $a3);
)*
};
}
@@ -677,13 +730,69 @@ macro_rules! quote_tokens_with_context {
#[macro_export]
#[doc(hidden)]
macro_rules! quote_token_with_context {
+ ($tokens:ident $b3:tt $b2:tt $b1:tt @ $a1:tt $a2:tt $a3:tt) => {};
+
+ ($tokens:ident $b3:tt $b2:tt $b1:tt (#) ( $($inner:tt)* ) * $a3:tt) => {{
+ use $crate::__private::ext::*;
+ let has_iter = $crate::__private::ThereIsNoIteratorInRepetition;
+ $crate::pounded_var_names!(quote_bind_into_iter!(has_iter) () $($inner)*);
+ let _: $crate::__private::HasIterator = has_iter;
+ // This is `while true` instead of `loop` because if there are no
+ // iterators used inside of this repetition then the body would not
+ // contain any `break`, so the compiler would emit unreachable code
+ // warnings on anything below the loop. We use has_iter to detect and
+ // fail to compile when there are no iterators, so here we just work
+ // around the unneeded extra warning.
+ while true {
+ $crate::pounded_var_names!(quote_bind_next_or_break!() () $($inner)*);
+ $crate::quote_each_token!($tokens $($inner)*);
+ }
+ }};
+ ($tokens:ident $b3:tt $b2:tt # (( $($inner:tt)* )) * $a2:tt $a3:tt) => {};
+ ($tokens:ident $b3:tt # ( $($inner:tt)* ) (*) $a1:tt $a2:tt $a3:tt) => {};
+
+ ($tokens:ident $b3:tt $b2:tt $b1:tt (#) ( $($inner:tt)* ) $sep:tt *) => {{
+ use $crate::__private::ext::*;
+ let mut _i = 0usize;
+ let has_iter = $crate::__private::ThereIsNoIteratorInRepetition;
+ $crate::pounded_var_names!(quote_bind_into_iter!(has_iter) () $($inner)*);
+ let _: $crate::__private::HasIterator = has_iter;
+ while true {
+ $crate::pounded_var_names!(quote_bind_next_or_break!() () $($inner)*);
+ if _i > 0 {
+ $crate::quote_token!($tokens $sep);
+ }
+ _i += 1;
+ $crate::quote_each_token!($tokens $($inner)*);
+ }
+ }};
+ ($tokens:ident $b3:tt $b2:tt # (( $($inner:tt)* )) $sep:tt * $a3:tt) => {};
+ ($tokens:ident $b3:tt # ( $($inner:tt)* ) ($sep:tt) * $a2:tt $a3:tt) => {};
+ ($tokens:ident # ( $($inner:tt)* ) * (*) $a1:tt $a2:tt $a3:tt) => {
+ // https://github.com/dtolnay/quote/issues/130
+ $crate::quote_token!($tokens *);
+ };
+ ($tokens:ident # ( $($inner:tt)* ) $sep:tt (*) $a1:tt $a2:tt $a3:tt) => {};
+
+ ($tokens:ident $b3:tt $b2:tt $b1:tt (#) $var:ident $a2:tt $a3:tt) => {
+ $crate::ToTokens::to_tokens(&$var, &mut $tokens);
+ };
+ ($tokens:ident $b3:tt $b2:tt # ($var:ident) $a1:tt $a2:tt $a3:tt) => {};
+ ($tokens:ident $b3:tt $b2:tt $b1:tt ($curr:tt) $a1:tt $a2:tt $a3:tt) => {
+ $crate::quote_token!($tokens $curr);
+ };
+}
+
+#[macro_export]
+#[doc(hidden)]
+macro_rules! quote_token_with_context_spanned {
($tokens:ident $span:ident $b3:tt $b2:tt $b1:tt @ $a1:tt $a2:tt $a3:tt) => {};
($tokens:ident $span:ident $b3:tt $b2:tt $b1:tt (#) ( $($inner:tt)* ) * $a3:tt) => {{
- use $crate::__rt::ext::*;
- let has_iter = $crate::__rt::ThereIsNoIteratorInRepetition;
+ use $crate::__private::ext::*;
+ let has_iter = $crate::__private::ThereIsNoIteratorInRepetition;
$crate::pounded_var_names!(quote_bind_into_iter!(has_iter) () $($inner)*);
- let _: $crate::__rt::HasIterator = has_iter;
+ let _: $crate::__private::HasIterator = has_iter;
// This is `while true` instead of `loop` because if there are no
// iterators used inside of this repetition then the body would not
// contain any `break`, so the compiler would emit unreachable code
@@ -692,32 +801,32 @@ macro_rules! quote_token_with_context {
// around the unneeded extra warning.
while true {
$crate::pounded_var_names!(quote_bind_next_or_break!() () $($inner)*);
- $crate::quote_each_token!($tokens $span $($inner)*);
+ $crate::quote_each_token_spanned!($tokens $span $($inner)*);
}
}};
($tokens:ident $span:ident $b3:tt $b2:tt # (( $($inner:tt)* )) * $a2:tt $a3:tt) => {};
($tokens:ident $span:ident $b3:tt # ( $($inner:tt)* ) (*) $a1:tt $a2:tt $a3:tt) => {};
($tokens:ident $span:ident $b3:tt $b2:tt $b1:tt (#) ( $($inner:tt)* ) $sep:tt *) => {{
- use $crate::__rt::ext::*;
+ use $crate::__private::ext::*;
let mut _i = 0usize;
- let has_iter = $crate::__rt::ThereIsNoIteratorInRepetition;
+ let has_iter = $crate::__private::ThereIsNoIteratorInRepetition;
$crate::pounded_var_names!(quote_bind_into_iter!(has_iter) () $($inner)*);
- let _: $crate::__rt::HasIterator = has_iter;
+ let _: $crate::__private::HasIterator = has_iter;
while true {
$crate::pounded_var_names!(quote_bind_next_or_break!() () $($inner)*);
if _i > 0 {
- $crate::quote_token!($tokens $span $sep);
+ $crate::quote_token_spanned!($tokens $span $sep);
}
_i += 1;
- $crate::quote_each_token!($tokens $span $($inner)*);
+ $crate::quote_each_token_spanned!($tokens $span $($inner)*);
}
}};
($tokens:ident $span:ident $b3:tt $b2:tt # (( $($inner:tt)* )) $sep:tt * $a3:tt) => {};
($tokens:ident $span:ident $b3:tt # ( $($inner:tt)* ) ($sep:tt) * $a2:tt $a3:tt) => {};
($tokens:ident $span:ident # ( $($inner:tt)* ) * (*) $a1:tt $a2:tt $a3:tt) => {
// https://github.com/dtolnay/quote/issues/130
- $crate::quote_token!($tokens $span *);
+ $crate::quote_token_spanned!($tokens $span *);
};
($tokens:ident $span:ident # ( $($inner:tt)* ) $sep:tt (*) $a1:tt $a2:tt $a3:tt) => {};
@@ -726,223 +835,433 @@ macro_rules! quote_token_with_context {
};
($tokens:ident $span:ident $b3:tt $b2:tt # ($var:ident) $a1:tt $a2:tt $a3:tt) => {};
($tokens:ident $span:ident $b3:tt $b2:tt $b1:tt ($curr:tt) $a1:tt $a2:tt $a3:tt) => {
- $crate::quote_token!($tokens $span $curr);
+ $crate::quote_token_spanned!($tokens $span $curr);
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! quote_token {
+ ($tokens:ident ( $($inner:tt)* )) => {
+ $crate::__private::push_group(
+ &mut $tokens,
+ $crate::__private::Delimiter::Parenthesis,
+ $crate::quote!($($inner)*),
+ );
+ };
+
+ ($tokens:ident [ $($inner:tt)* ]) => {
+ $crate::__private::push_group(
+ &mut $tokens,
+ $crate::__private::Delimiter::Bracket,
+ $crate::quote!($($inner)*),
+ );
+ };
+
+ ($tokens:ident { $($inner:tt)* }) => {
+ $crate::__private::push_group(
+ &mut $tokens,
+ $crate::__private::Delimiter::Brace,
+ $crate::quote!($($inner)*),
+ );
+ };
+
+ ($tokens:ident +) => {
+ $crate::__private::push_add(&mut $tokens);
+ };
+
+ ($tokens:ident +=) => {
+ $crate::__private::push_add_eq(&mut $tokens);
+ };
+
+ ($tokens:ident &) => {
+ $crate::__private::push_and(&mut $tokens);
+ };
+
+ ($tokens:ident &&) => {
+ $crate::__private::push_and_and(&mut $tokens);
+ };
+
+ ($tokens:ident &=) => {
+ $crate::__private::push_and_eq(&mut $tokens);
+ };
+
+ ($tokens:ident @) => {
+ $crate::__private::push_at(&mut $tokens);
+ };
+
+ ($tokens:ident !) => {
+ $crate::__private::push_bang(&mut $tokens);
+ };
+
+ ($tokens:ident ^) => {
+ $crate::__private::push_caret(&mut $tokens);
+ };
+
+ ($tokens:ident ^=) => {
+ $crate::__private::push_caret_eq(&mut $tokens);
+ };
+
+ ($tokens:ident :) => {
+ $crate::__private::push_colon(&mut $tokens);
+ };
+
+ ($tokens:ident ::) => {
+ $crate::__private::push_colon2(&mut $tokens);
+ };
+
+ ($tokens:ident ,) => {
+ $crate::__private::push_comma(&mut $tokens);
+ };
+
+ ($tokens:ident /) => {
+ $crate::__private::push_div(&mut $tokens);
+ };
+
+ ($tokens:ident /=) => {
+ $crate::__private::push_div_eq(&mut $tokens);
+ };
+
+ ($tokens:ident .) => {
+ $crate::__private::push_dot(&mut $tokens);
+ };
+
+ ($tokens:ident ..) => {
+ $crate::__private::push_dot2(&mut $tokens);
+ };
+
+ ($tokens:ident ...) => {
+ $crate::__private::push_dot3(&mut $tokens);
+ };
+
+ ($tokens:ident ..=) => {
+ $crate::__private::push_dot_dot_eq(&mut $tokens);
+ };
+
+ ($tokens:ident =) => {
+ $crate::__private::push_eq(&mut $tokens);
+ };
+
+ ($tokens:ident ==) => {
+ $crate::__private::push_eq_eq(&mut $tokens);
+ };
+
+ ($tokens:ident >=) => {
+ $crate::__private::push_ge(&mut $tokens);
+ };
+
+ ($tokens:ident >) => {
+ $crate::__private::push_gt(&mut $tokens);
+ };
+
+ ($tokens:ident <=) => {
+ $crate::__private::push_le(&mut $tokens);
+ };
+
+ ($tokens:ident <) => {
+ $crate::__private::push_lt(&mut $tokens);
+ };
+
+ ($tokens:ident *=) => {
+ $crate::__private::push_mul_eq(&mut $tokens);
+ };
+
+ ($tokens:ident !=) => {
+ $crate::__private::push_ne(&mut $tokens);
+ };
+
+ ($tokens:ident |) => {
+ $crate::__private::push_or(&mut $tokens);
+ };
+
+ ($tokens:ident |=) => {
+ $crate::__private::push_or_eq(&mut $tokens);
+ };
+
+ ($tokens:ident ||) => {
+ $crate::__private::push_or_or(&mut $tokens);
+ };
+
+ ($tokens:ident #) => {
+ $crate::__private::push_pound(&mut $tokens);
+ };
+
+ ($tokens:ident ?) => {
+ $crate::__private::push_question(&mut $tokens);
+ };
+
+ ($tokens:ident ->) => {
+ $crate::__private::push_rarrow(&mut $tokens);
+ };
+
+ ($tokens:ident <-) => {
+ $crate::__private::push_larrow(&mut $tokens);
+ };
+
+ ($tokens:ident %) => {
+ $crate::__private::push_rem(&mut $tokens);
+ };
+
+ ($tokens:ident %=) => {
+ $crate::__private::push_rem_eq(&mut $tokens);
+ };
+
+ ($tokens:ident =>) => {
+ $crate::__private::push_fat_arrow(&mut $tokens);
+ };
+
+ ($tokens:ident ;) => {
+ $crate::__private::push_semi(&mut $tokens);
+ };
+
+ ($tokens:ident <<) => {
+ $crate::__private::push_shl(&mut $tokens);
+ };
+
+ ($tokens:ident <<=) => {
+ $crate::__private::push_shl_eq(&mut $tokens);
+ };
+
+ ($tokens:ident >>) => {
+ $crate::__private::push_shr(&mut $tokens);
+ };
+
+ ($tokens:ident >>=) => {
+ $crate::__private::push_shr_eq(&mut $tokens);
+ };
+
+ ($tokens:ident *) => {
+ $crate::__private::push_star(&mut $tokens);
+ };
+
+ ($tokens:ident -) => {
+ $crate::__private::push_sub(&mut $tokens);
+ };
+
+ ($tokens:ident -=) => {
+ $crate::__private::push_sub_eq(&mut $tokens);
+ };
+
+ ($tokens:ident $ident:ident) => {
+ $crate::__private::push_ident(&mut $tokens, stringify!($ident));
+ };
+
+ ($tokens:ident $other:tt) => {
+ $crate::__private::parse(&mut $tokens, stringify!($other));
+ };
+}
+
+#[macro_export]
+#[doc(hidden)]
+macro_rules! quote_token_spanned {
($tokens:ident $span:ident ( $($inner:tt)* )) => {
- $tokens.extend({
- let mut g = $crate::__rt::Group::new(
- $crate::__rt::Delimiter::Parenthesis,
- $crate::quote_spanned!($span=> $($inner)*),
- );
- g.set_span($span);
- Some($crate::__rt::TokenTree::from(g))
- });
+ $crate::__private::push_group_spanned(
+ &mut $tokens,
+ $span,
+ $crate::__private::Delimiter::Parenthesis,
+ $crate::quote_spanned!($span=> $($inner)*),
+ );
};
($tokens:ident $span:ident [ $($inner:tt)* ]) => {
- $tokens.extend({
- let mut g = $crate::__rt::Group::new(
- $crate::__rt::Delimiter::Bracket,
- $crate::quote_spanned!($span=> $($inner)*),
- );
- g.set_span($span);
- Some($crate::__rt::TokenTree::from(g))
- });
+ $crate::__private::push_group_spanned(
+ &mut $tokens,
+ $span,
+ $crate::__private::Delimiter::Bracket,
+ $crate::quote_spanned!($span=> $($inner)*),
+ );
};
($tokens:ident $span:ident { $($inner:tt)* }) => {
- $tokens.extend({
- let mut g = $crate::__rt::Group::new(
- $crate::__rt::Delimiter::Brace,
- $crate::quote_spanned!($span=> $($inner)*),
- );
- g.set_span($span);
- Some($crate::__rt::TokenTree::from(g))
- });
+ $crate::__private::push_group_spanned(
+ &mut $tokens,
+ $span,
+ $crate::__private::Delimiter::Brace,
+ $crate::quote_spanned!($span=> $($inner)*),
+ );
};
($tokens:ident $span:ident +) => {
- $crate::__rt::push_add(&mut $tokens, $span);
+ $crate::__private::push_add_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident +=) => {
- $crate::__rt::push_add_eq(&mut $tokens, $span);
+ $crate::__private::push_add_eq_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident &) => {
- $crate::__rt::push_and(&mut $tokens, $span);
+ $crate::__private::push_and_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident &&) => {
- $crate::__rt::push_and_and(&mut $tokens, $span);
+ $crate::__private::push_and_and_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident &=) => {
- $crate::__rt::push_and_eq(&mut $tokens, $span);
+ $crate::__private::push_and_eq_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident @) => {
- $crate::__rt::push_at(&mut $tokens, $span);
+ $crate::__private::push_at_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident !) => {
- $crate::__rt::push_bang(&mut $tokens, $span);
+ $crate::__private::push_bang_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident ^) => {
- $crate::__rt::push_caret(&mut $tokens, $span);
+ $crate::__private::push_caret_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident ^=) => {
- $crate::__rt::push_caret_eq(&mut $tokens, $span);
+ $crate::__private::push_caret_eq_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident :) => {
- $crate::__rt::push_colon(&mut $tokens, $span);
+ $crate::__private::push_colon_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident ::) => {
- $crate::__rt::push_colon2(&mut $tokens, $span);
+ $crate::__private::push_colon2_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident ,) => {
- $crate::__rt::push_comma(&mut $tokens, $span);
+ $crate::__private::push_comma_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident /) => {
- $crate::__rt::push_div(&mut $tokens, $span);
+ $crate::__private::push_div_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident /=) => {
- $crate::__rt::push_div_eq(&mut $tokens, $span);
+ $crate::__private::push_div_eq_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident .) => {
- $crate::__rt::push_dot(&mut $tokens, $span);
+ $crate::__private::push_dot_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident ..) => {
- $crate::__rt::push_dot2(&mut $tokens, $span);
+ $crate::__private::push_dot2_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident ...) => {
- $crate::__rt::push_dot3(&mut $tokens, $span);
+ $crate::__private::push_dot3_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident ..=) => {
- $crate::__rt::push_dot_dot_eq(&mut $tokens, $span);
+ $crate::__private::push_dot_dot_eq_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident =) => {
- $crate::__rt::push_eq(&mut $tokens, $span);
+ $crate::__private::push_eq_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident ==) => {
- $crate::__rt::push_eq_eq(&mut $tokens, $span);
+ $crate::__private::push_eq_eq_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident >=) => {
- $crate::__rt::push_ge(&mut $tokens, $span);
+ $crate::__private::push_ge_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident >) => {
- $crate::__rt::push_gt(&mut $tokens, $span);
+ $crate::__private::push_gt_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident <=) => {
- $crate::__rt::push_le(&mut $tokens, $span);
+ $crate::__private::push_le_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident <) => {
- $crate::__rt::push_lt(&mut $tokens, $span);
+ $crate::__private::push_lt_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident *=) => {
- $crate::__rt::push_mul_eq(&mut $tokens, $span);
+ $crate::__private::push_mul_eq_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident !=) => {
- $crate::__rt::push_ne(&mut $tokens, $span);
+ $crate::__private::push_ne_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident |) => {
- $crate::__rt::push_or(&mut $tokens, $span);
+ $crate::__private::push_or_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident |=) => {
- $crate::__rt::push_or_eq(&mut $tokens, $span);
+ $crate::__private::push_or_eq_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident ||) => {
- $crate::__rt::push_or_or(&mut $tokens, $span);
+ $crate::__private::push_or_or_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident #) => {
- $crate::__rt::push_pound(&mut $tokens, $span);
+ $crate::__private::push_pound_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident ?) => {
- $crate::__rt::push_question(&mut $tokens, $span);
+ $crate::__private::push_question_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident ->) => {
- $crate::__rt::push_rarrow(&mut $tokens, $span);
+ $crate::__private::push_rarrow_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident <-) => {
- $crate::__rt::push_larrow(&mut $tokens, $span);
+ $crate::__private::push_larrow_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident %) => {
- $crate::__rt::push_rem(&mut $tokens, $span);
+ $crate::__private::push_rem_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident %=) => {
- $crate::__rt::push_rem_eq(&mut $tokens, $span);
+ $crate::__private::push_rem_eq_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident =>) => {
- $crate::__rt::push_fat_arrow(&mut $tokens, $span);
+ $crate::__private::push_fat_arrow_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident ;) => {
- $crate::__rt::push_semi(&mut $tokens, $span);
+ $crate::__private::push_semi_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident <<) => {
- $crate::__rt::push_shl(&mut $tokens, $span);
+ $crate::__private::push_shl_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident <<=) => {
- $crate::__rt::push_shl_eq(&mut $tokens, $span);
+ $crate::__private::push_shl_eq_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident >>) => {
- $crate::__rt::push_shr(&mut $tokens, $span);
+ $crate::__private::push_shr_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident >>=) => {
- $crate::__rt::push_shr_eq(&mut $tokens, $span);
+ $crate::__private::push_shr_eq_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident *) => {
- $crate::__rt::push_star(&mut $tokens, $span);
+ $crate::__private::push_star_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident -) => {
- $crate::__rt::push_sub(&mut $tokens, $span);
+ $crate::__private::push_sub_spanned(&mut $tokens, $span);
};
($tokens:ident $span:ident -=) => {
- $crate::__rt::push_sub_eq(&mut $tokens, $span);
+ $crate::__private::push_sub_eq_spanned(&mut $tokens, $span);
+ };
+
+ ($tokens:ident $span:ident $ident:ident) => {
+ $crate::__private::push_ident_spanned(&mut $tokens, $span, stringify!($ident));
};
($tokens:ident $span:ident $other:tt) => {
- $crate::__rt::parse(&mut $tokens, $span, stringify!($other));
+ $crate::__private::parse_spanned(&mut $tokens, $span, stringify!($other));
};
}
diff --git a/src/runtime.rs b/src/runtime.rs
index 4a1c14c..db3b6a9 100644
--- a/src/runtime.rs
+++ b/src/runtime.rs
@@ -180,44 +180,75 @@ impl<T: ToTokens> ToTokens for RepInterp<T> {
}
}
-fn is_ident_start(c: u8) -> bool {
- (b'a' <= c && c <= b'z') || (b'A' <= c && c <= b'Z') || c == b'_'
+pub fn push_group(tokens: &mut TokenStream, delimiter: Delimiter, inner: TokenStream) {
+ tokens.append(Group::new(delimiter, inner));
}
-fn is_ident_continue(c: u8) -> bool {
- (b'a' <= c && c <= b'z') || (b'A' <= c && c <= b'Z') || c == b'_' || (b'0' <= c && c <= b'9')
+pub fn push_group_spanned(
+ tokens: &mut TokenStream,
+ span: Span,
+ delimiter: Delimiter,
+ inner: TokenStream,
+) {
+ let mut g = Group::new(delimiter, inner);
+ g.set_span(span);
+ tokens.append(g);
}
-fn is_ident(token: &str) -> bool {
- let mut iter = token.bytes();
- let first_ok = iter.next().map(is_ident_start).unwrap_or(false);
+pub fn parse(tokens: &mut TokenStream, s: &str) {
+ let s: TokenStream = s.parse().expect("invalid token stream");
+ tokens.extend(s);
+}
- first_ok && iter.all(is_ident_continue)
+pub fn parse_spanned(tokens: &mut TokenStream, span: Span, s: &str) {
+ let s: TokenStream = s.parse().expect("invalid token stream");
+ tokens.extend(s.into_iter().map(|mut t| {
+ t.set_span(span);
+ t
+ }));
}
-pub fn parse(tokens: &mut TokenStream, span: Span, s: &str) {
- if is_ident(s) {
- // Fast path, since idents are the most common token.
- tokens.append(Ident::new(s, span));
+pub fn push_ident(tokens: &mut TokenStream, s: &str) {
+ // Optimization over `mk_ident`, as `s` is guaranteed to be a valid ident.
+ //
+ // FIXME: When `Ident::new_raw` becomes stable, this method should be
+ // updated to call it when available.
+ if s.starts_with("r#") {
+ parse(tokens, s);
} else {
- let s: TokenStream = s.parse().expect("invalid token stream");
- tokens.extend(s.into_iter().map(|mut t| {
- t.set_span(span);
- t
- }));
+ tokens.append(Ident::new(s, Span::call_site()));
+ }
+}
+
+pub fn push_ident_spanned(tokens: &mut TokenStream, span: Span, s: &str) {
+ // Optimization over `mk_ident`, as `s` is guaranteed to be a valid ident.
+ //
+ // FIXME: When `Ident::new_raw` becomes stable, this method should be
+ // updated to call it when available.
+ if s.starts_with("r#") {
+ parse_spanned(tokens, span, s);
+ } else {
+ tokens.append(Ident::new(s, span));
}
}
macro_rules! push_punct {
- ($name:ident $char1:tt) => {
- pub fn $name(tokens: &mut TokenStream, span: Span) {
+ ($name:ident $spanned:ident $char1:tt) => {
+ pub fn $name(tokens: &mut TokenStream) {
+ tokens.append(Punct::new($char1, Spacing::Alone));
+ }
+ pub fn $spanned(tokens: &mut TokenStream, span: Span) {
let mut punct = Punct::new($char1, Spacing::Alone);
punct.set_span(span);
tokens.append(punct);
}
};
- ($name:ident $char1:tt $char2:tt) => {
- pub fn $name(tokens: &mut TokenStream, span: Span) {
+ ($name:ident $spanned:ident $char1:tt $char2:tt) => {
+ pub fn $name(tokens: &mut TokenStream) {
+ tokens.append(Punct::new($char1, Spacing::Joint));
+ tokens.append(Punct::new($char2, Spacing::Alone));
+ }
+ pub fn $spanned(tokens: &mut TokenStream, span: Span) {
let mut punct = Punct::new($char1, Spacing::Joint);
punct.set_span(span);
tokens.append(punct);
@@ -226,8 +257,13 @@ macro_rules! push_punct {
tokens.append(punct);
}
};
- ($name:ident $char1:tt $char2:tt $char3:tt) => {
- pub fn $name(tokens: &mut TokenStream, span: Span) {
+ ($name:ident $spanned:ident $char1:tt $char2:tt $char3:tt) => {
+ pub fn $name(tokens: &mut TokenStream) {
+ tokens.append(Punct::new($char1, Spacing::Joint));
+ tokens.append(Punct::new($char2, Spacing::Joint));
+ tokens.append(Punct::new($char3, Spacing::Alone));
+ }
+ pub fn $spanned(tokens: &mut TokenStream, span: Span) {
let mut punct = Punct::new($char1, Spacing::Joint);
punct.set_span(span);
tokens.append(punct);
@@ -241,50 +277,50 @@ macro_rules! push_punct {
};
}
-push_punct!(push_add '+');
-push_punct!(push_add_eq '+' '=');
-push_punct!(push_and '&');
-push_punct!(push_and_and '&' '&');
-push_punct!(push_and_eq '&' '=');
-push_punct!(push_at '@');
-push_punct!(push_bang '!');
-push_punct!(push_caret '^');
-push_punct!(push_caret_eq '^' '=');
-push_punct!(push_colon ':');
-push_punct!(push_colon2 ':' ':');
-push_punct!(push_comma ',');
-push_punct!(push_div '/');
-push_punct!(push_div_eq '/' '=');
-push_punct!(push_dot '.');
-push_punct!(push_dot2 '.' '.');
-push_punct!(push_dot3 '.' '.' '.');
-push_punct!(push_dot_dot_eq '.' '.' '=');
-push_punct!(push_eq '=');
-push_punct!(push_eq_eq '=' '=');
-push_punct!(push_ge '>' '=');
-push_punct!(push_gt '>');
-push_punct!(push_le '<' '=');
-push_punct!(push_lt '<');
-push_punct!(push_mul_eq '*' '=');
-push_punct!(push_ne '!' '=');
-push_punct!(push_or '|');
-push_punct!(push_or_eq '|' '=');
-push_punct!(push_or_or '|' '|');
-push_punct!(push_pound '#');
-push_punct!(push_question '?');
-push_punct!(push_rarrow '-' '>');
-push_punct!(push_larrow '<' '-');
-push_punct!(push_rem '%');
-push_punct!(push_rem_eq '%' '=');
-push_punct!(push_fat_arrow '=' '>');
-push_punct!(push_semi ';');
-push_punct!(push_shl '<' '<');
-push_punct!(push_shl_eq '<' '<' '=');
-push_punct!(push_shr '>' '>');
-push_punct!(push_shr_eq '>' '>' '=');
-push_punct!(push_star '*');
-push_punct!(push_sub '-');
-push_punct!(push_sub_eq '-' '=');
+push_punct!(push_add push_add_spanned '+');
+push_punct!(push_add_eq push_add_eq_spanned '+' '=');
+push_punct!(push_and push_and_spanned '&');
+push_punct!(push_and_and push_and_and_spanned '&' '&');
+push_punct!(push_and_eq push_and_eq_spanned '&' '=');
+push_punct!(push_at push_at_spanned '@');
+push_punct!(push_bang push_bang_spanned '!');
+push_punct!(push_caret push_caret_spanned '^');
+push_punct!(push_caret_eq push_caret_eq_spanned '^' '=');
+push_punct!(push_colon push_colon_spanned ':');
+push_punct!(push_colon2 push_colon2_spanned ':' ':');
+push_punct!(push_comma push_comma_spanned ',');
+push_punct!(push_div push_div_spanned '/');
+push_punct!(push_div_eq push_div_eq_spanned '/' '=');
+push_punct!(push_dot push_dot_spanned '.');
+push_punct!(push_dot2 push_dot2_spanned '.' '.');
+push_punct!(push_dot3 push_dot3_spanned '.' '.' '.');
+push_punct!(push_dot_dot_eq push_dot_dot_eq_spanned '.' '.' '=');
+push_punct!(push_eq push_eq_spanned '=');
+push_punct!(push_eq_eq push_eq_eq_spanned '=' '=');
+push_punct!(push_ge push_ge_spanned '>' '=');
+push_punct!(push_gt push_gt_spanned '>');
+push_punct!(push_le push_le_spanned '<' '=');
+push_punct!(push_lt push_lt_spanned '<');
+push_punct!(push_mul_eq push_mul_eq_spanned '*' '=');
+push_punct!(push_ne push_ne_spanned '!' '=');
+push_punct!(push_or push_or_spanned '|');
+push_punct!(push_or_eq push_or_eq_spanned '|' '=');
+push_punct!(push_or_or push_or_or_spanned '|' '|');
+push_punct!(push_pound push_pound_spanned '#');
+push_punct!(push_question push_question_spanned '?');
+push_punct!(push_rarrow push_rarrow_spanned '-' '>');
+push_punct!(push_larrow push_larrow_spanned '<' '-');
+push_punct!(push_rem push_rem_spanned '%');
+push_punct!(push_rem_eq push_rem_eq_spanned '%' '=');
+push_punct!(push_fat_arrow push_fat_arrow_spanned '=' '>');
+push_punct!(push_semi push_semi_spanned ';');
+push_punct!(push_shl push_shl_spanned '<' '<');
+push_punct!(push_shl_eq push_shl_eq_spanned '<' '<' '=');
+push_punct!(push_shr push_shr_spanned '>' '>');
+push_punct!(push_shr_eq push_shr_eq_spanned '>' '>' '=');
+push_punct!(push_star push_star_spanned '*');
+push_punct!(push_sub push_sub_spanned '-');
+push_punct!(push_sub_eq push_sub_eq_spanned '-' '=');
// Helper method for constructing identifiers from the `format_ident!` macro,
// handling `r#` prefixes.
@@ -293,9 +329,6 @@ push_punct!(push_sub_eq '-' '=');
// although the input string was invalid, due to ignored characters such as
// whitespace and comments. Instead, we always create a non-raw identifier
// to validate that the string is OK, and only parse again if needed.
-//
-// The `is_ident` method defined above is insufficient for validation, as it
-// will reject non-ASCII identifiers.
pub fn mk_ident(id: &str, span: Option<Span>) -> Ident {
let span = span.unwrap_or_else(Span::call_site);
@@ -312,19 +345,15 @@ pub fn mk_ident(id: &str, span: Option<Span>) -> Ident {
//
// FIXME: When `Ident::new_raw` becomes stable, this method should be
// updated to call it when available.
- match id.parse::<TokenStream>() {
- Ok(ts) => {
- let mut iter = ts.into_iter();
- match (iter.next(), iter.next()) {
- (Some(TokenTree::Ident(mut id)), None) => {
- id.set_span(span);
- id
- }
- _ => unreachable!("valid raw ident fails to parse"),
- }
+ if let Ok(ts) = id.parse::<TokenStream>() {
+ let mut iter = ts.into_iter();
+ if let (Some(TokenTree::Ident(mut id)), None) = (iter.next(), iter.next()) {
+ id.set_span(span);
+ return id;
}
- Err(_) => unreachable!("valid raw ident fails to parse"),
}
+
+ panic!("not allowed as a raw identifier: `{}`", id);
}
// Adapts from `IdentFragment` to `fmt::Display` for use by the `format_ident!`