2016-06-29 13:55:10 -05:00
|
|
|
//! # Token Streams
|
|
|
|
//!
|
2017-05-12 13:05:39 -05:00
|
|
|
//! `TokenStream`s represent syntactic objects before they are converted into ASTs.
|
2016-06-29 13:55:10 -05:00
|
|
|
//! A `TokenStream` is, roughly speaking, a sequence (eg stream) of `TokenTree`s,
|
2017-01-29 02:38:44 -06:00
|
|
|
//! which are themselves a single `Token` or a `Delimited` subsequence of tokens.
|
2016-06-29 13:55:10 -05:00
|
|
|
//!
|
2016-07-19 17:50:34 -05:00
|
|
|
//! ## Ownership
|
2019-02-08 07:53:55 -06:00
|
|
|
//!
|
2019-09-05 21:56:45 -05:00
|
|
|
//! `TokenStream`s are persistent data structures constructed as ropes with reference
|
2017-05-12 13:05:39 -05:00
|
|
|
//! counted-children. In general, this means that calling an operation on a `TokenStream`
|
|
|
|
//! (such as `slice`) produces an entirely new `TokenStream` from the borrowed reference to
|
|
|
|
//! the original. This essentially coerces `TokenStream`s into 'views' of their subparts,
|
|
|
|
//! and a borrowed `TokenStream` is sufficient to build an owned `TokenStream` without taking
|
2016-07-19 17:50:34 -05:00
|
|
|
//! ownership of the original.
|
2016-06-20 10:49:33 -05:00
|
|
|
|
2019-10-11 05:46:32 -05:00
|
|
|
use crate::token::{self, DelimToken, Token, TokenKind};
|
2019-02-06 11:33:01 -06:00
|
|
|
|
2019-11-10 11:24:37 -06:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
Rewrite `collect_tokens` implementations to use a flattened buffer
Instead of trying to collect tokens at each depth, we 'flatten' the
stream as we go allong, pushing open/close delimiters to our buffer
just like regular tokens. One capturing is complete, we reconstruct a
nested `TokenTree::Delimited` structure, producing a normal
`TokenStream`.
The reconstructed `TokenStream` is not created immediately - instead, it is
produced on-demand by a closure (wrapped in a new `LazyTokenStream` type). This
closure stores a clone of the original `TokenCursor`, plus a record of the
number of calls to `next()/next_desugared()`. This is sufficient to reconstruct
the tokenstream seen by the callback without storing any additional state. If
the tokenstream is never used (e.g. when a captured `macro_rules!` argument is
never passed to a proc macro), we never actually create a `TokenStream`.
This implementation has a number of advantages over the previous one:
* It is significantly simpler, with no edge cases around capturing the
start/end of a delimited group.
* It can be easily extended to allow replacing tokens an an arbitrary
'depth' by just using `Vec::splice` at the proper position. This is
important for PR #76130, which requires us to track information about
attributes along with tokens.
* The lazy approach to `TokenStream` construction allows us to easily
parse an AST struct, and then decide after the fact whether we need a
`TokenStream`. This will be useful when we start collecting tokens for
`Attribute` - we can discard the `LazyTokenStream` if the parsed
attribute doesn't need tokens (e.g. is a builtin attribute).
The performance impact seems to be neglibile (see
https://github.com/rust-lang/rust/pull/77250#issuecomment-703960604). There is a
small slowdown on a few benchmarks, but it only rises above 1% for incremental
builds, where it represents a larger fraction of the much smaller instruction
count. There a ~1% speedup on a few other incremental benchmarks - my guess is
that the speedups and slowdowns will usually cancel out in practice.
2020-09-26 20:56:29 -05:00
|
|
|
use rustc_data_structures::sync::{self, Lrc};
|
2019-12-22 16:42:04 -06:00
|
|
|
use rustc_macros::HashStable_Generic;
|
Rewrite `collect_tokens` implementations to use a flattened buffer
Instead of trying to collect tokens at each depth, we 'flatten' the
stream as we go allong, pushing open/close delimiters to our buffer
just like regular tokens. One capturing is complete, we reconstruct a
nested `TokenTree::Delimited` structure, producing a normal
`TokenStream`.
The reconstructed `TokenStream` is not created immediately - instead, it is
produced on-demand by a closure (wrapped in a new `LazyTokenStream` type). This
closure stores a clone of the original `TokenCursor`, plus a record of the
number of calls to `next()/next_desugared()`. This is sufficient to reconstruct
the tokenstream seen by the callback without storing any additional state. If
the tokenstream is never used (e.g. when a captured `macro_rules!` argument is
never passed to a proc macro), we never actually create a `TokenStream`.
This implementation has a number of advantages over the previous one:
* It is significantly simpler, with no edge cases around capturing the
start/end of a delimited group.
* It can be easily extended to allow replacing tokens an an arbitrary
'depth' by just using `Vec::splice` at the proper position. This is
important for PR #76130, which requires us to track information about
attributes along with tokens.
* The lazy approach to `TokenStream` construction allows us to easily
parse an AST struct, and then decide after the fact whether we need a
`TokenStream`. This will be useful when we start collecting tokens for
`Attribute` - we can discard the `LazyTokenStream` if the parsed
attribute doesn't need tokens (e.g. is a builtin attribute).
The performance impact seems to be neglibile (see
https://github.com/rust-lang/rust/pull/77250#issuecomment-703960604). There is a
small slowdown on a few benchmarks, but it only rises above 1% for incremental
builds, where it represents a larger fraction of the much smaller instruction
count. There a ~1% speedup on a few other incremental benchmarks - my guess is
that the speedups and slowdowns will usually cancel out in practice.
2020-09-26 20:56:29 -05:00
|
|
|
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
|
2019-12-31 11:15:40 -06:00
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2019-12-22 16:42:04 -06:00
|
|
|
use smallvec::{smallvec, SmallVec};
|
2016-06-29 13:55:10 -05:00
|
|
|
|
2019-10-08 18:45:40 -05:00
|
|
|
use std::{iter, mem};
|
2016-07-04 05:25:50 -05:00
|
|
|
|
2016-06-20 10:49:33 -05:00
|
|
|
/// When the main rust parser encounters a syntax-extension invocation, it
|
|
|
|
/// parses the arguments to the invocation as a token-tree. This is a very
|
|
|
|
/// loose structure, such that all sorts of different AST-fragments can
|
|
|
|
/// be passed to syntax extensions using a uniform type.
|
|
|
|
///
|
|
|
|
/// If the syntax extension is an MBE macro, it will attempt to match its
|
|
|
|
/// LHS token tree against the provided token tree, and if it finds a
|
|
|
|
/// match, will transcribe the RHS token tree, splicing in any captured
|
2017-05-12 13:05:39 -05:00
|
|
|
/// `macro_parser::matched_nonterminals` into the `SubstNt`s it finds.
|
2016-06-20 10:49:33 -05:00
|
|
|
///
|
|
|
|
/// The RHS of an MBE macro is the only place `SubstNt`s are substituted.
|
|
|
|
/// Nothing special happens to misnamed or misplaced `SubstNt`s.
|
2020-06-11 09:49:57 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Encodable, Decodable, HashStable_Generic)]
|
2016-06-20 10:49:33 -05:00
|
|
|
pub enum TokenTree {
|
|
|
|
/// A single token
|
2019-06-04 12:42:43 -05:00
|
|
|
Token(Token),
|
2016-06-20 10:49:33 -05:00
|
|
|
/// A delimited sequence of token trees
|
2019-01-08 23:53:14 -06:00
|
|
|
Delimited(DelimSpan, DelimToken, TokenStream),
|
2016-06-20 10:49:33 -05:00
|
|
|
}
|
|
|
|
|
2019-05-19 13:44:06 -05:00
|
|
|
// Ensure all fields of `TokenTree` is `Send` and `Sync`.
|
|
|
|
#[cfg(parallel_compiler)]
|
|
|
|
fn _dummy()
|
|
|
|
where
|
2019-06-04 12:42:43 -05:00
|
|
|
Token: Send + Sync,
|
2019-05-19 13:44:06 -05:00
|
|
|
DelimSpan: Send + Sync,
|
|
|
|
DelimToken: Send + Sync,
|
|
|
|
TokenStream: Send + Sync,
|
2019-12-22 16:42:04 -06:00
|
|
|
{
|
|
|
|
}
|
2019-05-19 13:44:06 -05:00
|
|
|
|
2016-06-20 10:49:33 -05:00
|
|
|
impl TokenTree {
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Checks if this TokenTree is equal to the other, regardless of span information.
|
2016-06-29 13:55:10 -05:00
|
|
|
pub fn eq_unspanned(&self, other: &TokenTree) -> bool {
|
|
|
|
match (self, other) {
|
2019-06-04 12:42:43 -05:00
|
|
|
(TokenTree::Token(token), TokenTree::Token(token2)) => token.kind == token2.kind,
|
|
|
|
(TokenTree::Delimited(_, delim, tts), TokenTree::Delimited(_, delim2, tts2)) => {
|
2019-01-08 23:53:14 -06:00
|
|
|
delim == delim2 && tts.eq_unspanned(&tts2)
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
2019-06-04 12:42:43 -05:00
|
|
|
_ => false,
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Retrieves the TokenTree's span.
|
2016-06-29 13:55:10 -05:00
|
|
|
pub fn span(&self) -> Span {
|
2019-06-04 12:42:43 -05:00
|
|
|
match self {
|
|
|
|
TokenTree::Token(token) => token.span,
|
2018-11-29 17:02:04 -06:00
|
|
|
TokenTree::Delimited(sp, ..) => sp.entire(),
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 12:05:20 -06:00
|
|
|
/// Modify the `TokenTree`'s span in-place.
|
2017-07-19 23:54:01 -05:00
|
|
|
pub fn set_span(&mut self, span: Span) {
|
2019-06-04 12:42:43 -05:00
|
|
|
match self {
|
|
|
|
TokenTree::Token(token) => token.span = span,
|
|
|
|
TokenTree::Delimited(dspan, ..) => *dspan = DelimSpan::from_single(span),
|
2017-07-19 23:54:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 18:41:09 -05:00
|
|
|
pub fn joint(self) -> TokenStream {
|
2020-09-03 10:21:53 -05:00
|
|
|
TokenStream::new(vec![(self, Spacing::Joint)])
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
2018-11-29 17:02:04 -06:00
|
|
|
|
2019-06-05 05:25:26 -05:00
|
|
|
pub fn token(kind: TokenKind, span: Span) -> TokenTree {
|
2019-06-05 01:39:34 -05:00
|
|
|
TokenTree::Token(Token::new(kind, span))
|
2019-06-04 12:42:43 -05:00
|
|
|
}
|
|
|
|
|
2018-11-29 17:02:04 -06:00
|
|
|
/// Returns the opening delimiter as a token tree.
|
2019-11-03 05:58:01 -06:00
|
|
|
pub fn open_tt(span: DelimSpan, delim: DelimToken) -> TokenTree {
|
|
|
|
TokenTree::token(token::OpenDelim(delim), span.open)
|
2018-11-29 17:02:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the closing delimiter as a token tree.
|
2019-11-03 05:58:01 -06:00
|
|
|
pub fn close_tt(span: DelimSpan, delim: DelimToken) -> TokenTree {
|
|
|
|
TokenTree::token(token::CloseDelim(delim), span.close)
|
2018-11-29 17:02:04 -06:00
|
|
|
}
|
2020-03-07 06:58:27 -06:00
|
|
|
|
|
|
|
pub fn uninterpolate(self) -> TokenTree {
|
|
|
|
match self {
|
|
|
|
TokenTree::Token(token) => TokenTree::Token(token.uninterpolate().into_owned()),
|
|
|
|
tt => tt,
|
|
|
|
}
|
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2019-11-10 11:24:37 -06:00
|
|
|
impl<CTX> HashStable<CTX> for TokenStream
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
CTX: crate::HashStableContext,
|
2019-11-10 11:24:37 -06:00
|
|
|
{
|
|
|
|
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
|
|
|
|
for sub_tt in self.trees() {
|
|
|
|
sub_tt.hash_stable(hcx, hasher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Rewrite `collect_tokens` implementations to use a flattened buffer
Instead of trying to collect tokens at each depth, we 'flatten' the
stream as we go allong, pushing open/close delimiters to our buffer
just like regular tokens. One capturing is complete, we reconstruct a
nested `TokenTree::Delimited` structure, producing a normal
`TokenStream`.
The reconstructed `TokenStream` is not created immediately - instead, it is
produced on-demand by a closure (wrapped in a new `LazyTokenStream` type). This
closure stores a clone of the original `TokenCursor`, plus a record of the
number of calls to `next()/next_desugared()`. This is sufficient to reconstruct
the tokenstream seen by the callback without storing any additional state. If
the tokenstream is never used (e.g. when a captured `macro_rules!` argument is
never passed to a proc macro), we never actually create a `TokenStream`.
This implementation has a number of advantages over the previous one:
* It is significantly simpler, with no edge cases around capturing the
start/end of a delimited group.
* It can be easily extended to allow replacing tokens an an arbitrary
'depth' by just using `Vec::splice` at the proper position. This is
important for PR #76130, which requires us to track information about
attributes along with tokens.
* The lazy approach to `TokenStream` construction allows us to easily
parse an AST struct, and then decide after the fact whether we need a
`TokenStream`. This will be useful when we start collecting tokens for
`Attribute` - we can discard the `LazyTokenStream` if the parsed
attribute doesn't need tokens (e.g. is a builtin attribute).
The performance impact seems to be neglibile (see
https://github.com/rust-lang/rust/pull/77250#issuecomment-703960604). There is a
small slowdown on a few benchmarks, but it only rises above 1% for incremental
builds, where it represents a larger fraction of the much smaller instruction
count. There a ~1% speedup on a few other incremental benchmarks - my guess is
that the speedups and slowdowns will usually cancel out in practice.
2020-09-26 20:56:29 -05:00
|
|
|
// A cloneable callback which produces a `TokenStream`. Each clone
|
|
|
|
// of this should produce the same `TokenStream`
|
|
|
|
pub trait CreateTokenStream: sync::Send + sync::Sync + FnOnce() -> TokenStream {
|
|
|
|
// Workaround for the fact that `Clone` is not object-safe
|
|
|
|
fn clone_it(&self) -> Box<dyn CreateTokenStream>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: 'static + Clone + sync::Send + sync::Sync + FnOnce() -> TokenStream> CreateTokenStream
|
|
|
|
for F
|
|
|
|
{
|
|
|
|
fn clone_it(&self) -> Box<dyn CreateTokenStream> {
|
|
|
|
Box::new(self.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for Box<dyn CreateTokenStream> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
let val: &(dyn CreateTokenStream) = &**self;
|
|
|
|
val.clone_it()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A lazy version of `TokenStream`, which may defer creation
|
|
|
|
/// of an actual `TokenStream` until it is needed.
|
|
|
|
pub type LazyTokenStream = Lrc<LazyTokenStreamInner>;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum LazyTokenStreamInner {
|
|
|
|
Lazy(Box<dyn CreateTokenStream>),
|
|
|
|
Ready(TokenStream),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for LazyTokenStreamInner {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
LazyTokenStreamInner::Lazy(..) => f.debug_struct("LazyTokenStream::Lazy").finish(),
|
|
|
|
LazyTokenStreamInner::Ready(..) => f.debug_struct("LazyTokenStream::Ready").finish(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LazyTokenStreamInner {
|
|
|
|
pub fn into_token_stream(&self) -> TokenStream {
|
|
|
|
match self {
|
|
|
|
// Note that we do not cache this. If this ever becomes a performance
|
|
|
|
// problem, we should investigate wrapping `LazyTokenStreamInner`
|
|
|
|
// in a lock
|
|
|
|
LazyTokenStreamInner::Lazy(cb) => (cb.clone())(),
|
|
|
|
LazyTokenStreamInner::Ready(stream) => stream.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: Encoder> Encodable<S> for LazyTokenStreamInner {
|
|
|
|
fn encode(&self, _s: &mut S) -> Result<(), S::Error> {
|
|
|
|
panic!("Attempted to encode LazyTokenStream");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<D: Decoder> Decodable<D> for LazyTokenStreamInner {
|
|
|
|
fn decode(_d: &mut D) -> Result<Self, D::Error> {
|
|
|
|
panic!("Attempted to decode LazyTokenStream");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<CTX> HashStable<CTX> for LazyTokenStreamInner {
|
|
|
|
fn hash_stable(&self, _hcx: &mut CTX, _hasher: &mut StableHasher) {
|
|
|
|
panic!("Attempted to compute stable hash for LazyTokenStream");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:27:09 -06:00
|
|
|
/// A `TokenStream` is an abstract sequence of tokens, organized into `TokenTree`s.
|
2019-09-05 21:56:45 -05:00
|
|
|
///
|
2017-01-17 21:27:09 -06:00
|
|
|
/// The goal is for procedural macros to work with `TokenStream`s and `TokenTree`s
|
|
|
|
/// instead of a representation of the abstract syntax tree.
|
2019-05-23 18:04:56 -05:00
|
|
|
/// Today's `TokenTree`s can still contain AST via `token::Interpolated` for back-compat.
|
2020-06-11 09:49:57 -05:00
|
|
|
#[derive(Clone, Debug, Default, Encodable, Decodable)]
|
2020-09-14 00:45:10 -05:00
|
|
|
pub struct TokenStream(pub(crate) Lrc<Vec<TreeAndSpacing>>);
|
2016-07-19 17:50:34 -05:00
|
|
|
|
2020-09-03 10:21:53 -05:00
|
|
|
pub type TreeAndSpacing = (TokenTree, Spacing);
|
2018-12-18 21:53:52 -06:00
|
|
|
|
2018-11-29 17:02:04 -06:00
|
|
|
// `TokenStream` is used a lot. Make sure it doesn't unintentionally get bigger.
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2019-11-11 13:18:35 -06:00
|
|
|
rustc_data_structures::static_assert_size!(TokenStream, 8);
|
2018-11-29 17:02:04 -06:00
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable)]
|
2020-09-03 10:21:53 -05:00
|
|
|
pub enum Spacing {
|
|
|
|
Alone,
|
2018-12-19 16:50:14 -06:00
|
|
|
Joint,
|
|
|
|
}
|
|
|
|
|
2018-07-15 01:50:08 -05:00
|
|
|
impl TokenStream {
|
|
|
|
/// Given a `TokenStream` with a `Stream` of only two arguments, return a new `TokenStream`
|
|
|
|
/// separating the two arguments with a comma for diagnostic suggestions.
|
2019-10-16 03:59:30 -05:00
|
|
|
pub fn add_comma(&self) -> Option<(TokenStream, Span)> {
|
2018-08-08 00:28:09 -05:00
|
|
|
// Used to suggest if a user writes `foo!(a b);`
|
2019-10-09 15:29:02 -05:00
|
|
|
let mut suggestion = None;
|
|
|
|
let mut iter = self.0.iter().enumerate().peekable();
|
|
|
|
while let Some((pos, ts)) = iter.next() {
|
|
|
|
if let Some((_, next)) = iter.peek() {
|
|
|
|
let sp = match (&ts, &next) {
|
|
|
|
(_, (TokenTree::Token(Token { kind: token::Comma, .. }), _)) => continue,
|
2019-12-22 16:42:04 -06:00
|
|
|
(
|
2020-09-03 10:21:53 -05:00
|
|
|
(TokenTree::Token(token_left), Spacing::Alone),
|
2019-12-22 16:42:04 -06:00
|
|
|
(TokenTree::Token(token_right), _),
|
|
|
|
) if ((token_left.is_ident() && !token_left.is_reserved_ident())
|
|
|
|
|| token_left.is_lit())
|
|
|
|
&& ((token_right.is_ident() && !token_right.is_reserved_ident())
|
|
|
|
|| token_right.is_lit()) =>
|
|
|
|
{
|
|
|
|
token_left.span
|
|
|
|
}
|
2020-09-03 10:21:53 -05:00
|
|
|
((TokenTree::Delimited(sp, ..), Spacing::Alone), _) => sp.entire(),
|
2019-10-09 15:29:02 -05:00
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
let sp = sp.shrink_to_hi();
|
2020-09-03 10:21:53 -05:00
|
|
|
let comma = (TokenTree::token(token::Comma, sp), Spacing::Alone);
|
2019-10-09 15:29:02 -05:00
|
|
|
suggestion = Some((pos, comma, sp));
|
2018-07-15 01:50:08 -05:00
|
|
|
}
|
|
|
|
}
|
2019-10-09 15:29:02 -05:00
|
|
|
if let Some((pos, comma, sp)) = suggestion {
|
|
|
|
let mut new_stream = vec![];
|
|
|
|
let parts = self.0.split_at(pos + 1);
|
|
|
|
new_stream.extend_from_slice(parts.0);
|
|
|
|
new_stream.push(comma);
|
|
|
|
new_stream.extend_from_slice(parts.1);
|
|
|
|
return Some((TokenStream::new(new_stream), sp));
|
|
|
|
}
|
2018-07-15 01:50:08 -05:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:27:09 -06:00
|
|
|
impl From<TokenTree> for TokenStream {
|
2018-12-18 21:53:52 -06:00
|
|
|
fn from(tree: TokenTree) -> TokenStream {
|
2020-09-03 10:21:53 -05:00
|
|
|
TokenStream::new(vec![(tree, Spacing::Alone)])
|
2018-12-18 21:53:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 10:21:53 -05:00
|
|
|
impl From<TokenTree> for TreeAndSpacing {
|
|
|
|
fn from(tree: TokenTree) -> TreeAndSpacing {
|
|
|
|
(tree, Spacing::Alone)
|
2016-07-19 17:50:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 18:37:21 -05:00
|
|
|
impl iter::FromIterator<TokenTree> for TokenStream {
|
|
|
|
fn from_iter<I: IntoIterator<Item = TokenTree>>(iter: I) -> Self {
|
2020-09-03 10:21:53 -05:00
|
|
|
TokenStream::new(iter.into_iter().map(Into::into).collect::<Vec<TreeAndSpacing>>())
|
2018-08-12 14:45:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:27:09 -06:00
|
|
|
impl Eq for TokenStream {}
|
|
|
|
|
2016-06-29 13:55:10 -05:00
|
|
|
impl PartialEq<TokenStream> for TokenStream {
|
|
|
|
fn eq(&self, other: &TokenStream) -> bool {
|
2017-01-17 21:27:09 -06:00
|
|
|
self.trees().eq(other.trees())
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenStream {
|
2020-09-03 10:21:53 -05:00
|
|
|
pub fn new(streams: Vec<TreeAndSpacing>) -> TokenStream {
|
2019-10-09 15:29:02 -05:00
|
|
|
TokenStream(Lrc::new(streams))
|
2017-07-19 23:54:01 -05:00
|
|
|
}
|
|
|
|
|
2019-10-09 15:29:02 -05:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.0.is_empty()
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2019-10-09 15:29:02 -05:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.0.len()
|
2016-07-19 17:50:34 -05:00
|
|
|
}
|
|
|
|
|
2019-12-01 06:55:32 -06:00
|
|
|
pub fn span(&self) -> Option<Span> {
|
|
|
|
match &**self.0 {
|
|
|
|
[] => None,
|
|
|
|
[(tt, _)] => Some(tt.span()),
|
|
|
|
[(tt_start, _), .., (tt_end, _)] => Some(tt_start.span().to(tt_end.span())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 03:26:10 -05:00
|
|
|
pub fn from_streams(mut streams: SmallVec<[TokenStream; 2]>) -> TokenStream {
|
2017-02-18 06:45:32 -06:00
|
|
|
match streams.len() {
|
2019-10-09 15:29:02 -05:00
|
|
|
0 => TokenStream::default(),
|
2017-03-17 18:23:12 -05:00
|
|
|
1 => streams.pop().unwrap(),
|
2018-12-18 21:53:52 -06:00
|
|
|
_ => {
|
2019-10-07 18:34:53 -05:00
|
|
|
// We are going to extend the first stream in `streams` with
|
|
|
|
// the elements from the subsequent streams. This requires
|
|
|
|
// using `make_mut()` on the first stream, and in practice this
|
|
|
|
// doesn't cause cloning 99.9% of the time.
|
|
|
|
//
|
|
|
|
// One very common use case is when `streams` has two elements,
|
|
|
|
// where the first stream has any number of elements within
|
|
|
|
// (often 1, but sometimes many more) and the second stream has
|
|
|
|
// a single element within.
|
|
|
|
|
|
|
|
// Determine how much the first stream will be extended.
|
|
|
|
// Needed to avoid quadratic blow up from on-the-fly
|
|
|
|
// reallocations (#57735).
|
2019-12-22 16:42:04 -06:00
|
|
|
let num_appends = streams.iter().skip(1).map(|ts| ts.len()).sum();
|
2019-01-30 08:12:41 -06:00
|
|
|
|
2019-10-07 18:34:53 -05:00
|
|
|
// Get the first stream. If it's `None`, create an empty
|
|
|
|
// stream.
|
2019-11-04 08:59:09 -06:00
|
|
|
let mut iter = streams.drain(..);
|
2019-10-09 15:29:02 -05:00
|
|
|
let mut first_stream_lrc = iter.next().unwrap().0;
|
2019-10-07 18:34:53 -05:00
|
|
|
|
|
|
|
// Append the elements to the first stream, after reserving
|
|
|
|
// space for them.
|
|
|
|
let first_vec_mut = Lrc::make_mut(&mut first_stream_lrc);
|
|
|
|
first_vec_mut.reserve(num_appends);
|
|
|
|
for stream in iter {
|
2019-10-09 15:29:02 -05:00
|
|
|
first_vec_mut.extend(stream.0.iter().cloned());
|
2018-12-18 21:53:52 -06:00
|
|
|
}
|
2019-10-07 18:34:53 -05:00
|
|
|
|
|
|
|
// Create the final `TokenStream`.
|
2019-10-09 15:29:02 -05:00
|
|
|
TokenStream(first_stream_lrc)
|
2018-12-18 21:53:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-18 00:18:29 -06:00
|
|
|
pub fn trees(&self) -> Cursor {
|
|
|
|
self.clone().into_trees()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn into_trees(self) -> Cursor {
|
2017-01-17 21:27:09 -06:00
|
|
|
Cursor::new(self)
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2019-09-05 21:56:45 -05:00
|
|
|
/// Compares two `TokenStream`s, checking equality without regarding span information.
|
2016-07-19 17:50:34 -05:00
|
|
|
pub fn eq_unspanned(&self, other: &TokenStream) -> bool {
|
2018-04-10 14:52:47 -05:00
|
|
|
let mut t1 = self.trees();
|
|
|
|
let mut t2 = other.trees();
|
|
|
|
for (t1, t2) in t1.by_ref().zip(t2.by_ref()) {
|
2017-02-18 00:18:29 -06:00
|
|
|
if !t1.eq_unspanned(&t2) {
|
2016-07-19 17:50:34 -05:00
|
|
|
return false;
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 14:52:47 -05:00
|
|
|
t1.next().is_none() && t2.next().is_none()
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
2017-03-17 18:41:09 -05:00
|
|
|
|
2020-09-14 00:45:10 -05:00
|
|
|
pub fn map_enumerated<F: FnMut(usize, &TokenTree) -> TokenTree>(self, mut f: F) -> TokenStream {
|
2019-10-09 15:29:02 -05:00
|
|
|
TokenStream(Lrc::new(
|
|
|
|
self.0
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2020-09-14 00:45:10 -05:00
|
|
|
.map(|(i, (tree, is_joint))| (f(i, tree), *is_joint))
|
2019-12-22 16:42:04 -06:00
|
|
|
.collect(),
|
2019-10-09 15:29:02 -05:00
|
|
|
))
|
2017-07-19 23:54:01 -05:00
|
|
|
}
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
2019-03-27 20:27:26 -05:00
|
|
|
// 99.5%+ of the time we have 1 or 2 elements in this vector.
|
2018-07-22 10:48:29 -05:00
|
|
|
#[derive(Clone)]
|
2019-03-27 20:27:26 -05:00
|
|
|
pub struct TokenStreamBuilder(SmallVec<[TokenStream; 2]>);
|
2017-03-17 18:41:09 -05:00
|
|
|
|
|
|
|
impl TokenStreamBuilder {
|
2017-06-04 20:41:33 -05:00
|
|
|
pub fn new() -> TokenStreamBuilder {
|
2019-03-27 20:27:26 -05:00
|
|
|
TokenStreamBuilder(SmallVec::new())
|
2017-06-04 20:41:33 -05:00
|
|
|
}
|
|
|
|
|
2017-03-17 18:41:09 -05:00
|
|
|
pub fn push<T: Into<TokenStream>>(&mut self, stream: T) {
|
2019-10-07 21:32:59 -05:00
|
|
|
let mut stream = stream.into();
|
|
|
|
|
|
|
|
// If `self` is not empty and the last tree within the last stream is a
|
|
|
|
// token tree marked with `Joint`...
|
2019-10-09 15:29:02 -05:00
|
|
|
if let Some(TokenStream(ref mut last_stream_lrc)) = self.0.last_mut() {
|
2020-09-03 10:21:53 -05:00
|
|
|
if let Some((TokenTree::Token(last_token), Spacing::Joint)) = last_stream_lrc.last() {
|
2019-10-07 21:32:59 -05:00
|
|
|
// ...and `stream` is not empty and the first tree within it is
|
|
|
|
// a token tree...
|
2019-10-09 15:29:02 -05:00
|
|
|
let TokenStream(ref mut stream_lrc) = stream;
|
2020-09-03 10:21:53 -05:00
|
|
|
if let Some((TokenTree::Token(token), spacing)) = stream_lrc.first() {
|
2019-10-09 15:29:02 -05:00
|
|
|
// ...and the two tokens can be glued together...
|
|
|
|
if let Some(glued_tok) = last_token.glue(&token) {
|
|
|
|
// ...then do so, by overwriting the last token
|
|
|
|
// tree in `self` and removing the first token tree
|
|
|
|
// from `stream`. This requires using `make_mut()`
|
|
|
|
// on the last stream in `self` and on `stream`,
|
|
|
|
// and in practice this doesn't cause cloning 99.9%
|
|
|
|
// of the time.
|
|
|
|
|
|
|
|
// Overwrite the last token tree with the merged
|
|
|
|
// token.
|
|
|
|
let last_vec_mut = Lrc::make_mut(last_stream_lrc);
|
2020-09-03 10:21:53 -05:00
|
|
|
*last_vec_mut.last_mut().unwrap() = (TokenTree::Token(glued_tok), *spacing);
|
2019-10-09 15:29:02 -05:00
|
|
|
|
|
|
|
// Remove the first token tree from `stream`. (This
|
|
|
|
// is almost always the only tree in `stream`.)
|
|
|
|
let stream_vec_mut = Lrc::make_mut(stream_lrc);
|
|
|
|
stream_vec_mut.remove(0);
|
|
|
|
|
|
|
|
// Don't push `stream` if it's empty -- that could
|
|
|
|
// block subsequent token gluing, by getting
|
|
|
|
// between two token trees that should be glued
|
|
|
|
// together.
|
|
|
|
if !stream.is_empty() {
|
|
|
|
self.0.push(stream);
|
2019-10-07 21:32:59 -05:00
|
|
|
}
|
2019-10-09 15:29:02 -05:00
|
|
|
return;
|
2019-10-07 21:32:59 -05:00
|
|
|
}
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.0.push(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build(self) -> TokenStream {
|
2018-12-18 21:53:52 -06:00
|
|
|
TokenStream::from_streams(self.0)
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2017-06-09 22:30:33 -05:00
|
|
|
#[derive(Clone)]
|
2018-12-18 21:53:52 -06:00
|
|
|
pub struct Cursor {
|
|
|
|
pub stream: TokenStream,
|
2017-02-18 06:45:32 -06:00
|
|
|
index: usize,
|
2017-01-17 21:27:09 -06:00
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
|
2017-03-17 18:23:12 -05:00
|
|
|
impl Iterator for Cursor {
|
|
|
|
type Item = TokenTree;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<TokenTree> {
|
2020-09-03 10:21:53 -05:00
|
|
|
self.next_with_spacing().map(|(tree, _)| tree)
|
2017-03-17 18:23:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-18 00:18:29 -06:00
|
|
|
impl Cursor {
|
|
|
|
fn new(stream: TokenStream) -> Self {
|
2018-12-18 21:53:52 -06:00
|
|
|
Cursor { stream, index: 0 }
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
2020-09-03 10:21:53 -05:00
|
|
|
pub fn next_with_spacing(&mut self) -> Option<TreeAndSpacing> {
|
2019-10-09 15:29:02 -05:00
|
|
|
if self.index < self.stream.len() {
|
|
|
|
self.index += 1;
|
|
|
|
Some(self.stream.0[self.index - 1].clone())
|
|
|
|
} else {
|
|
|
|
None
|
2017-03-28 20:55:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 21:53:52 -06:00
|
|
|
pub fn append(&mut self, new_stream: TokenStream) {
|
|
|
|
if new_stream.is_empty() {
|
|
|
|
return;
|
2017-02-19 23:44:06 -06:00
|
|
|
}
|
2018-12-18 21:53:52 -06:00
|
|
|
let index = self.index;
|
2019-10-09 15:29:02 -05:00
|
|
|
let stream = mem::take(&mut self.stream);
|
2019-03-27 20:27:26 -05:00
|
|
|
*self = TokenStream::from_streams(smallvec![stream, new_stream]).into_trees();
|
2018-12-18 21:53:52 -06:00
|
|
|
self.index = index;
|
2017-02-19 23:44:06 -06:00
|
|
|
}
|
|
|
|
|
2020-09-14 00:45:10 -05:00
|
|
|
pub fn look_ahead(&self, n: usize) -> Option<&TokenTree> {
|
|
|
|
self.stream.0[self.index..].get(n).map(|(tree, _)| tree)
|
2017-02-19 23:44:06 -06:00
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Encodable, Decodable, HashStable_Generic)]
|
2018-09-08 20:07:02 -05:00
|
|
|
pub struct DelimSpan {
|
|
|
|
pub open: Span,
|
|
|
|
pub close: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DelimSpan {
|
|
|
|
pub fn from_single(sp: Span) -> Self {
|
2019-12-22 16:42:04 -06:00
|
|
|
DelimSpan { open: sp, close: sp }
|
2018-09-08 20:07:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_pair(open: Span, close: Span) -> Self {
|
|
|
|
DelimSpan { open, close }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dummy() -> Self {
|
|
|
|
Self::from_single(DUMMY_SP)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn entire(self) -> Span {
|
|
|
|
self.open.with_hi(self.close.hi())
|
|
|
|
}
|
|
|
|
}
|