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
|
|
|
//!
|
2017-05-12 13:05:39 -05:00
|
|
|
//! `TokenStreams` are persistent data structures constructed as ropes with reference
|
|
|
|
//! 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-02-06 11:33:01 -06:00
|
|
|
use crate::ext::base;
|
|
|
|
use crate::ext::tt::{macro_parser, quoted};
|
|
|
|
use crate::parse::Directory;
|
2019-06-04 12:42:43 -05:00
|
|
|
use crate::parse::token::{self, DelimToken, Token, TokenKind};
|
2019-02-06 11:33:01 -06:00
|
|
|
use crate::print::pprust;
|
|
|
|
|
2019-07-15 17:04:05 -05:00
|
|
|
use syntax_pos::{BytePos, ExpnId, Span, DUMMY_SP};
|
2019-02-06 11:33:01 -06:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2019-05-19 05:59:44 -05:00
|
|
|
use rustc_data_structures::static_assert_size;
|
2018-12-11 16:01:39 -06:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2017-01-17 21:27:09 -06:00
|
|
|
use serialize::{Decoder, Decodable, Encoder, Encodable};
|
2019-03-27 20:27:26 -05:00
|
|
|
use smallvec::{SmallVec, smallvec};
|
2016-06-29 13:55:10 -05:00
|
|
|
|
2018-05-18 01:19:35 -05:00
|
|
|
use std::borrow::Cow;
|
2017-02-18 00:18:29 -06:00
|
|
|
use std::{fmt, 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.
|
2018-06-26 16:57:27 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, RustcEncodable, RustcDecodable)]
|
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,
|
|
|
|
{}
|
|
|
|
|
2016-06-20 10:49:33 -05:00
|
|
|
impl TokenTree {
|
|
|
|
/// Use this token tree as a matcher to parse given tts.
|
2019-02-06 11:33:01 -06:00
|
|
|
pub fn parse(cx: &base::ExtCtxt<'_>, mtch: &[quoted::TokenTree], tts: TokenStream)
|
2016-06-20 10:49:33 -05:00
|
|
|
-> macro_parser::NamedParseResult {
|
|
|
|
// `None` is because we're not interpolating
|
2016-12-06 18:28:51 -06:00
|
|
|
let directory = Directory {
|
2018-05-18 01:19:35 -05:00
|
|
|
path: Cow::from(cx.current_expansion.module.directory.as_path()),
|
2016-12-06 18:28:51 -06:00
|
|
|
ownership: cx.current_expansion.directory_ownership,
|
|
|
|
};
|
2017-05-17 17:37:24 -05:00
|
|
|
macro_parser::parse(cx.parse_sess(), tts, mtch, Some(directory), true)
|
2016-06-20 10:49:33 -05:00
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
|
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-17 17:06:26 -06:00
|
|
|
// See comments in `Nonterminal::to_tokenstream` for why we care about
|
2018-04-18 21:36:48 -05:00
|
|
|
// *probably* equal here rather than actual equality
|
|
|
|
//
|
|
|
|
// This is otherwise the same as `eq_unspanned`, only recursing with a
|
|
|
|
// different method.
|
|
|
|
pub fn probably_equal_for_proc_macro(&self, other: &TokenTree) -> bool {
|
|
|
|
match (self, other) {
|
2019-06-04 12:42:43 -05:00
|
|
|
(TokenTree::Token(token), TokenTree::Token(token2)) => {
|
|
|
|
token.probably_equal_for_proc_macro(token2)
|
2018-04-18 21:36:48 -05:00
|
|
|
}
|
2019-06-04 12:42:43 -05:00
|
|
|
(TokenTree::Delimited(_, delim, tts), TokenTree::Delimited(_, delim2, tts2)) => {
|
2019-01-08 23:53:14 -06:00
|
|
|
delim == delim2 && tts.probably_equal_for_proc_macro(&tts2)
|
2018-04-18 21:36:48 -05:00
|
|
|
}
|
2019-06-04 12:42:43 -05:00
|
|
|
_ => false,
|
2018-04-18 21:36:48 -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 {
|
2019-01-08 22:20:56 -06:00
|
|
|
TokenStream::new(vec![(self, 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.
|
|
|
|
pub fn open_tt(span: Span, delim: DelimToken) -> TokenTree {
|
|
|
|
let open_span = if span.is_dummy() {
|
|
|
|
span
|
|
|
|
} else {
|
|
|
|
span.with_hi(span.lo() + BytePos(delim.len() as u32))
|
|
|
|
};
|
2019-06-05 05:25:26 -05:00
|
|
|
TokenTree::token(token::OpenDelim(delim), open_span)
|
2018-11-29 17:02:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the closing delimiter as a token tree.
|
|
|
|
pub fn close_tt(span: Span, delim: DelimToken) -> TokenTree {
|
|
|
|
let close_span = if span.is_dummy() {
|
|
|
|
span
|
|
|
|
} else {
|
|
|
|
span.with_lo(span.hi() - BytePos(delim.len() as u32))
|
|
|
|
};
|
2019-06-05 05:25:26 -05:00
|
|
|
TokenTree::token(token::CloseDelim(delim), close_span)
|
2018-11-29 17:02:04 -06:00
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2017-01-17 21:27:09 -06:00
|
|
|
/// # Token Streams
|
2016-06-29 13:55:10 -05:00
|
|
|
///
|
2017-01-17 21:27:09 -06:00
|
|
|
/// A `TokenStream` is an abstract sequence of tokens, organized into `TokenTree`s.
|
|
|
|
/// 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.
|
2019-01-10 17:36:54 -06:00
|
|
|
///
|
|
|
|
/// The use of `Option` is an optimization that avoids the need for an
|
|
|
|
/// allocation when the stream is empty. However, it is not guaranteed that an
|
|
|
|
/// empty stream is represented with `None`; it may be represented as a `Some`
|
|
|
|
/// around an empty `Vec`.
|
2017-01-17 21:27:09 -06:00
|
|
|
#[derive(Clone, Debug)]
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-04 22:20:55 -06:00
|
|
|
pub struct TokenStream(pub Option<Lrc<Vec<TreeAndJoint>>>);
|
2016-07-19 17:50:34 -05:00
|
|
|
|
2018-12-18 21:53:52 -06:00
|
|
|
pub type TreeAndJoint = (TokenTree, IsJoint);
|
|
|
|
|
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-05-19 05:59:44 -05:00
|
|
|
static_assert_size!(TokenStream, 8);
|
2018-11-29 17:02:04 -06:00
|
|
|
|
2018-12-19 16:50:14 -06:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
pub enum IsJoint {
|
|
|
|
Joint,
|
|
|
|
NonJoint
|
|
|
|
}
|
|
|
|
|
2019-02-06 11:33:01 -06:00
|
|
|
use IsJoint::*;
|
2018-12-19 16:50:14 -06:00
|
|
|
|
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.
|
|
|
|
pub(crate) 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-01-10 17:36:54 -06:00
|
|
|
if let Some(ref stream) = self.0 {
|
2018-08-08 00:28:09 -05:00
|
|
|
let mut suggestion = None;
|
2018-12-11 16:40:22 -06:00
|
|
|
let mut iter = stream.iter().enumerate().peekable();
|
2018-08-08 00:28:09 -05:00
|
|
|
while let Some((pos, ts)) = iter.next() {
|
|
|
|
if let Some((_, next)) = iter.peek() {
|
2018-12-11 16:55:43 -06:00
|
|
|
let sp = match (&ts, &next) {
|
2019-06-04 12:42:43 -05:00
|
|
|
(_, (TokenTree::Token(Token { kind: token::Comma, .. }), _)) => continue,
|
2019-06-05 06:17:56 -05:00
|
|
|
((TokenTree::Token(token_left), NonJoint),
|
|
|
|
(TokenTree::Token(token_right), _))
|
2019-04-24 18:45:29 -05:00
|
|
|
if ((token_left.is_ident() && !token_left.is_reserved_ident())
|
|
|
|
|| token_left.is_lit()) &&
|
|
|
|
((token_right.is_ident() && !token_right.is_reserved_ident())
|
2019-06-04 12:42:43 -05:00
|
|
|
|| token_right.is_lit()) => token_left.span,
|
2018-12-18 21:53:52 -06:00
|
|
|
((TokenTree::Delimited(sp, ..), NonJoint), _) => sp.entire(),
|
2018-09-08 20:07:02 -05:00
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
let sp = sp.shrink_to_hi();
|
2019-06-05 05:25:26 -05:00
|
|
|
let comma = (TokenTree::token(token::Comma, sp), NonJoint);
|
2018-09-08 20:07:02 -05:00
|
|
|
suggestion = Some((pos, comma, sp));
|
2018-08-08 00:28:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some((pos, comma, sp)) = suggestion {
|
2018-12-11 16:40:22 -06:00
|
|
|
let mut new_stream = vec![];
|
|
|
|
let parts = stream.split_at(pos + 1);
|
|
|
|
new_stream.extend_from_slice(parts.0);
|
|
|
|
new_stream.push(comma);
|
|
|
|
new_stream.extend_from_slice(parts.1);
|
2018-12-11 17:01:08 -06:00
|
|
|
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 {
|
2019-01-08 22:20:56 -06:00
|
|
|
TokenStream::new(vec![(tree, NonJoint)])
|
2018-12-18 21:53:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<TokenTree> for TreeAndJoint {
|
|
|
|
fn from(tree: TokenTree) -> TreeAndJoint {
|
|
|
|
(tree, NonJoint)
|
2016-07-19 17:50:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:27:09 -06:00
|
|
|
impl<T: Into<TokenStream>> iter::FromIterator<T> for TokenStream {
|
|
|
|
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
|
2019-03-27 20:27:26 -05:00
|
|
|
TokenStream::from_streams(iter.into_iter().map(Into::into).collect::<SmallVec<_>>())
|
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 {
|
2017-07-19 23:54:01 -05:00
|
|
|
pub fn len(&self) -> usize {
|
2019-01-10 17:36:54 -06:00
|
|
|
if let Some(ref slice) = self.0 {
|
2017-07-19 23:54:01 -05:00
|
|
|
slice.len()
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:27:09 -06:00
|
|
|
pub fn empty() -> TokenStream {
|
2019-01-10 17:36:54 -06:00
|
|
|
TokenStream(None)
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
2019-01-10 17:36:54 -06:00
|
|
|
match self.0 {
|
|
|
|
None => true,
|
|
|
|
Some(ref stream) => stream.is_empty(),
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
2016-07-19 17:50:34 -05:00
|
|
|
}
|
|
|
|
|
2019-03-27 20:27:26 -05:00
|
|
|
pub(crate) fn from_streams(mut streams: SmallVec<[TokenStream; 2]>) -> TokenStream {
|
2017-02-18 06:45:32 -06:00
|
|
|
match streams.len() {
|
|
|
|
0 => TokenStream::empty(),
|
2017-03-17 18:23:12 -05:00
|
|
|
1 => streams.pop().unwrap(),
|
2018-12-18 21:53:52 -06:00
|
|
|
_ => {
|
2019-01-30 08:12:41 -06:00
|
|
|
// rust-lang/rust#57735: pre-allocate vector to avoid
|
|
|
|
// quadratic blow-up due to on-the-fly reallocations.
|
|
|
|
let tree_count = streams.iter()
|
|
|
|
.map(|ts| match &ts.0 { None => 0, Some(s) => s.len() })
|
|
|
|
.sum();
|
|
|
|
let mut vec = Vec::with_capacity(tree_count);
|
|
|
|
|
2018-12-18 21:53:52 -06:00
|
|
|
for stream in streams {
|
2019-01-10 17:36:54 -06:00
|
|
|
match stream.0 {
|
|
|
|
None => {},
|
|
|
|
Some(stream2) => vec.extend(stream2.iter().cloned()),
|
2018-12-18 21:53:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TokenStream::new(vec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 22:20:56 -06:00
|
|
|
pub fn new(streams: Vec<TreeAndJoint>) -> TokenStream {
|
2018-12-18 21:53:52 -06:00
|
|
|
match streams.len() {
|
2019-01-10 17:36:54 -06:00
|
|
|
0 => TokenStream(None),
|
|
|
|
_ => TokenStream(Some(Lrc::new(streams))),
|
2017-02-18 06:45:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 21:53:52 -06:00
|
|
|
pub fn append_to_tree_and_joint_vec(self, vec: &mut Vec<TreeAndJoint>) {
|
2019-01-10 17:36:54 -06:00
|
|
|
if let Some(stream) = self.0 {
|
|
|
|
vec.extend(stream.iter().cloned());
|
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
|
|
|
}
|
|
|
|
|
2016-07-19 17:50:34 -05:00
|
|
|
/// Compares two TokenStreams, checking equality without regarding span information.
|
|
|
|
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
|
|
|
|
2019-02-17 17:06:26 -06:00
|
|
|
// See comments in `Nonterminal::to_tokenstream` for why we care about
|
2018-04-18 21:36:48 -05:00
|
|
|
// *probably* equal here rather than actual equality
|
|
|
|
//
|
|
|
|
// This is otherwise the same as `eq_unspanned`, only recursing with a
|
|
|
|
// different method.
|
|
|
|
pub fn probably_equal_for_proc_macro(&self, other: &TokenStream) -> bool {
|
2018-11-17 01:37:23 -06:00
|
|
|
// When checking for `probably_eq`, we ignore certain tokens that aren't
|
|
|
|
// preserved in the AST. Because they are not preserved, the pretty
|
|
|
|
// printer arbitrarily adds or removes them when printing as token
|
|
|
|
// streams, making a comparison between a token stream generated from an
|
|
|
|
// AST and a token stream which was parsed into an AST more reliable.
|
|
|
|
fn semantic_tree(tree: &TokenTree) -> bool {
|
2019-06-04 12:42:43 -05:00
|
|
|
if let TokenTree::Token(token) = tree {
|
|
|
|
if let
|
|
|
|
// The pretty printer tends to add trailing commas to
|
|
|
|
// everything, and in particular, after struct fields.
|
|
|
|
| token::Comma
|
|
|
|
// The pretty printer emits `NoDelim` as whitespace.
|
|
|
|
| token::OpenDelim(DelimToken::NoDelim)
|
|
|
|
| token::CloseDelim(DelimToken::NoDelim)
|
|
|
|
// The pretty printer collapses many semicolons into one.
|
|
|
|
| token::Semi
|
|
|
|
// The pretty printer collapses whitespace arbitrarily and can
|
|
|
|
// introduce whitespace from `NoDelim`.
|
|
|
|
| token::Whitespace
|
|
|
|
// The pretty printer can turn `$crate` into `::crate_name`
|
|
|
|
| token::ModSep = token.kind {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-17 01:37:23 -06:00
|
|
|
}
|
2019-06-04 12:42:43 -05:00
|
|
|
true
|
2018-11-17 01:37:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut t1 = self.trees().filter(semantic_tree);
|
|
|
|
let mut t2 = other.trees().filter(semantic_tree);
|
2018-04-18 21:36:48 -05:00
|
|
|
for (t1, t2) in t1.by_ref().zip(t2.by_ref()) {
|
|
|
|
if !t1.probably_equal_for_proc_macro(&t2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t1.next().is_none() && t2.next().is_none()
|
|
|
|
}
|
|
|
|
|
2017-07-21 18:44:23 -05:00
|
|
|
pub fn map_enumerated<F: FnMut(usize, TokenTree) -> TokenTree>(self, mut f: F) -> TokenStream {
|
2019-01-10 17:36:54 -06:00
|
|
|
TokenStream(self.0.map(|stream| {
|
|
|
|
Lrc::new(
|
2018-12-18 21:53:52 -06:00
|
|
|
stream
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, (tree, is_joint))| (f(i, tree.clone()), *is_joint))
|
2019-01-10 17:36:54 -06:00
|
|
|
.collect())
|
|
|
|
}))
|
2017-07-19 23:54:01 -05:00
|
|
|
}
|
|
|
|
|
2017-03-17 18:41:09 -05:00
|
|
|
pub fn map<F: FnMut(TokenTree) -> TokenTree>(self, mut f: F) -> TokenStream {
|
2019-01-10 17:36:54 -06:00
|
|
|
TokenStream(self.0.map(|stream| {
|
|
|
|
Lrc::new(
|
2018-12-18 21:53:52 -06:00
|
|
|
stream
|
|
|
|
.iter()
|
|
|
|
.map(|(tree, is_joint)| (f(tree.clone()), *is_joint))
|
2019-01-10 17:36:54 -06:00
|
|
|
.collect())
|
|
|
|
}))
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
2019-01-10 17:36:54 -06:00
|
|
|
fn first_tree_and_joint(&self) -> Option<TreeAndJoint> {
|
|
|
|
self.0.as_ref().map(|stream| {
|
|
|
|
stream.first().unwrap().clone()
|
|
|
|
})
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn last_tree_if_joint(&self) -> Option<TokenTree> {
|
2019-01-10 17:36:54 -06:00
|
|
|
match self.0 {
|
|
|
|
None => None,
|
|
|
|
Some(ref stream) => {
|
2018-12-18 21:53:52 -06:00
|
|
|
if let (tree, Joint) = stream.last().unwrap() {
|
|
|
|
Some(tree.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
let stream = stream.into();
|
|
|
|
let last_tree_if_joint = self.0.last().and_then(TokenStream::last_tree_if_joint);
|
2019-06-04 12:42:43 -05:00
|
|
|
if let Some(TokenTree::Token(last_token)) = last_tree_if_joint {
|
|
|
|
if let Some((TokenTree::Token(token), is_joint)) = stream.first_tree_and_joint() {
|
2019-06-08 11:45:12 -05:00
|
|
|
if let Some(glued_tok) = last_token.glue(token) {
|
2017-03-17 18:41:09 -05:00
|
|
|
let last_stream = self.0.pop().unwrap();
|
|
|
|
self.push_all_but_last_tree(&last_stream);
|
2019-06-08 11:45:12 -05:00
|
|
|
let glued_tt = TokenTree::Token(glued_tok);
|
2019-01-08 22:20:56 -06:00
|
|
|
let glued_tokenstream = TokenStream::new(vec![(glued_tt, is_joint)]);
|
2018-01-10 19:20:04 -06:00
|
|
|
self.0.push(glued_tokenstream);
|
2017-03-17 18:41:09 -05:00
|
|
|
self.push_all_but_first_tree(&stream);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn push_all_but_last_tree(&mut self, stream: &TokenStream) {
|
2019-01-10 17:36:54 -06:00
|
|
|
if let Some(ref streams) = stream.0 {
|
2017-03-17 18:41:09 -05:00
|
|
|
let len = streams.len();
|
|
|
|
match len {
|
|
|
|
1 => {}
|
2019-01-10 17:36:54 -06:00
|
|
|
_ => self.0.push(TokenStream(Some(Lrc::new(streams[0 .. len - 1].to_vec())))),
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn push_all_but_first_tree(&mut self, stream: &TokenStream) {
|
2019-01-10 17:36:54 -06:00
|
|
|
if let Some(ref streams) = stream.0 {
|
2017-03-17 18:41:09 -05:00
|
|
|
let len = streams.len();
|
|
|
|
match len {
|
|
|
|
1 => {}
|
2019-01-10 17:36:54 -06:00
|
|
|
_ => self.0.push(TokenStream(Some(Lrc::new(streams[1 .. len].to_vec())))),
|
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> {
|
2018-12-18 21:53:52 -06:00
|
|
|
self.next_with_joint().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
|
|
|
}
|
|
|
|
|
2018-12-18 21:53:52 -06:00
|
|
|
pub fn next_with_joint(&mut self) -> Option<TreeAndJoint> {
|
2019-01-10 17:36:54 -06:00
|
|
|
match self.stream.0 {
|
|
|
|
None => None,
|
|
|
|
Some(ref stream) => {
|
2018-12-18 21:53:52 -06:00
|
|
|
if self.index < stream.len() {
|
|
|
|
self.index += 1;
|
|
|
|
Some(stream[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-01-10 17:36:54 -06:00
|
|
|
let stream = mem::replace(&mut self.stream, TokenStream(None));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn look_ahead(&self, n: usize) -> Option<TokenTree> {
|
2019-01-10 17:36:54 -06:00
|
|
|
match self.stream.0 {
|
|
|
|
None => None,
|
|
|
|
Some(ref stream) => stream[self.index ..].get(n).map(|(tree, _)| tree.clone()),
|
2017-02-19 23:44:06 -06:00
|
|
|
}
|
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2017-01-17 21:27:09 -06:00
|
|
|
impl fmt::Display for TokenStream {
|
2019-02-06 11:33:01 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-03-03 03:23:59 -06:00
|
|
|
f.write_str(&pprust::tokens_to_string(self.clone()))
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:27:09 -06:00
|
|
|
impl Encodable for TokenStream {
|
|
|
|
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), E::Error> {
|
2017-02-18 00:18:29 -06:00
|
|
|
self.trees().collect::<Vec<_>>().encode(encoder)
|
2017-01-17 21:27:09 -06:00
|
|
|
}
|
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
|
2017-01-17 21:27:09 -06:00
|
|
|
impl Decodable for TokenStream {
|
|
|
|
fn decode<D: Decoder>(decoder: &mut D) -> Result<TokenStream, D::Error> {
|
|
|
|
Vec::<TokenTree>::decode(decoder).map(|vec| vec.into_iter().collect())
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-08 20:07:02 -05:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
|
|
|
|
pub struct DelimSpan {
|
|
|
|
pub open: Span,
|
|
|
|
pub close: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DelimSpan {
|
|
|
|
pub fn from_single(sp: Span) -> Self {
|
|
|
|
DelimSpan {
|
|
|
|
open: sp,
|
|
|
|
close: sp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2019-07-15 17:42:58 -05:00
|
|
|
pub fn apply_mark(self, expn_id: ExpnId) -> Self {
|
2018-09-08 20:07:02 -05:00
|
|
|
DelimSpan {
|
2019-07-15 17:42:58 -05:00
|
|
|
open: self.open.apply_mark(expn_id),
|
|
|
|
close: self.close.apply_mark(expn_id),
|
2018-09-08 20:07:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:50:34 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-06-05 06:17:56 -05:00
|
|
|
use crate::ast::Name;
|
2019-04-05 17:15:49 -05:00
|
|
|
use crate::with_default_globals;
|
2019-02-06 11:33:01 -06:00
|
|
|
use crate::util::parser_testing::string_to_stream;
|
2017-01-17 21:27:09 -06:00
|
|
|
use syntax_pos::{Span, BytePos, NO_EXPANSION};
|
|
|
|
|
|
|
|
fn string_to_ts(string: &str) -> TokenStream {
|
2017-03-01 19:29:40 -06:00
|
|
|
string_to_stream(string.to_owned())
|
2017-01-17 21:27:09 -06:00
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
|
2016-07-19 17:50:34 -05:00
|
|
|
fn sp(a: u32, b: u32) -> Span {
|
2017-07-31 15:04:34 -05:00
|
|
|
Span::new(BytePos(a), BytePos(b), NO_EXPANSION)
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2016-07-19 17:50:34 -05:00
|
|
|
#[test]
|
|
|
|
fn test_concat() {
|
2019-04-05 17:15:49 -05:00
|
|
|
with_default_globals(|| {
|
2018-03-06 19:44:10 -06:00
|
|
|
let test_res = string_to_ts("foo::bar::baz");
|
|
|
|
let test_fst = string_to_ts("foo::bar");
|
|
|
|
let test_snd = string_to_ts("::baz");
|
2019-03-27 20:27:26 -05:00
|
|
|
let eq_res = TokenStream::from_streams(smallvec![test_fst, test_snd]);
|
2018-03-06 19:44:10 -06:00
|
|
|
assert_eq!(test_res.trees().count(), 5);
|
|
|
|
assert_eq!(eq_res.trees().count(), 5);
|
|
|
|
assert_eq!(test_res.eq_unspanned(&eq_res), true);
|
|
|
|
})
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2016-07-19 17:50:34 -05:00
|
|
|
#[test]
|
|
|
|
fn test_to_from_bijection() {
|
2019-04-05 17:15:49 -05:00
|
|
|
with_default_globals(|| {
|
2018-03-06 19:44:10 -06:00
|
|
|
let test_start = string_to_ts("foo::bar(baz)");
|
|
|
|
let test_end = test_start.trees().collect();
|
|
|
|
assert_eq!(test_start, test_end)
|
|
|
|
})
|
2016-07-19 17:50:34 -05:00
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
|
2016-07-19 17:50:34 -05:00
|
|
|
#[test]
|
|
|
|
fn test_eq_0() {
|
2019-04-05 17:15:49 -05:00
|
|
|
with_default_globals(|| {
|
2018-03-06 19:44:10 -06:00
|
|
|
let test_res = string_to_ts("foo");
|
|
|
|
let test_eqs = string_to_ts("foo");
|
|
|
|
assert_eq!(test_res, test_eqs)
|
|
|
|
})
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2016-07-19 17:50:34 -05:00
|
|
|
#[test]
|
|
|
|
fn test_eq_1() {
|
2019-04-05 17:15:49 -05:00
|
|
|
with_default_globals(|| {
|
2018-03-06 19:44:10 -06:00
|
|
|
let test_res = string_to_ts("::bar::baz");
|
|
|
|
let test_eqs = string_to_ts("::bar::baz");
|
|
|
|
assert_eq!(test_res, test_eqs)
|
|
|
|
})
|
2016-07-19 17:50:34 -05:00
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
|
2016-07-19 17:50:34 -05:00
|
|
|
#[test]
|
|
|
|
fn test_eq_3() {
|
2019-04-05 17:15:49 -05:00
|
|
|
with_default_globals(|| {
|
2018-03-06 19:44:10 -06:00
|
|
|
let test_res = string_to_ts("");
|
|
|
|
let test_eqs = string_to_ts("");
|
|
|
|
assert_eq!(test_res, test_eqs)
|
|
|
|
})
|
2016-07-19 17:50:34 -05:00
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
|
2016-07-19 17:50:34 -05:00
|
|
|
#[test]
|
|
|
|
fn test_diseq_0() {
|
2019-04-05 17:15:49 -05:00
|
|
|
with_default_globals(|| {
|
2018-03-06 19:44:10 -06:00
|
|
|
let test_res = string_to_ts("::bar::baz");
|
|
|
|
let test_eqs = string_to_ts("bar::baz");
|
|
|
|
assert_eq!(test_res == test_eqs, false)
|
|
|
|
})
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2016-07-19 17:50:34 -05:00
|
|
|
#[test]
|
|
|
|
fn test_diseq_1() {
|
2019-04-05 17:15:49 -05:00
|
|
|
with_default_globals(|| {
|
2018-03-06 19:44:10 -06:00
|
|
|
let test_res = string_to_ts("(bar,baz)");
|
|
|
|
let test_eqs = string_to_ts("bar,baz");
|
|
|
|
assert_eq!(test_res == test_eqs, false)
|
|
|
|
})
|
2016-07-19 17:50:34 -05:00
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_empty() {
|
2019-04-05 17:15:49 -05:00
|
|
|
with_default_globals(|| {
|
2018-03-06 19:44:10 -06:00
|
|
|
let test0: TokenStream = Vec::<TokenTree>::new().into_iter().collect();
|
|
|
|
let test1: TokenStream =
|
2019-06-05 05:25:26 -05:00
|
|
|
TokenTree::token(token::Ident(Name::intern("a"), false), sp(0, 1)).into();
|
2018-03-06 19:44:10 -06:00
|
|
|
let test2 = string_to_ts("foo(bar::baz)");
|
|
|
|
|
|
|
|
assert_eq!(test0.is_empty(), true);
|
|
|
|
assert_eq!(test1.is_empty(), false);
|
|
|
|
assert_eq!(test2.is_empty(), false);
|
|
|
|
})
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
2018-01-10 19:20:04 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_dotdotdot() {
|
2019-04-05 17:15:49 -05:00
|
|
|
with_default_globals(|| {
|
|
|
|
let mut builder = TokenStreamBuilder::new();
|
2019-06-05 05:25:26 -05:00
|
|
|
builder.push(TokenTree::token(token::Dot, sp(0, 1)).joint());
|
|
|
|
builder.push(TokenTree::token(token::Dot, sp(1, 2)).joint());
|
|
|
|
builder.push(TokenTree::token(token::Dot, sp(2, 3)));
|
2019-04-05 17:15:49 -05:00
|
|
|
let stream = builder.build();
|
|
|
|
assert!(stream.eq_unspanned(&string_to_ts("...")));
|
|
|
|
assert_eq!(stream.trees().count(), 1);
|
|
|
|
})
|
2018-01-10 19:20:04 -06:00
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|