2016-06-20 10:49:33 -05:00
|
|
|
// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
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
|
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
|
|
|
|
2018-09-08 20:07:02 -05:00
|
|
|
use syntax_pos::{BytePos, Mark, Span, DUMMY_SP};
|
2016-06-20 10:49:33 -05:00
|
|
|
use ext::base;
|
2017-01-29 02:38:44 -06:00
|
|
|
use ext::tt::{macro_parser, quoted};
|
2017-02-20 23:05:59 -06:00
|
|
|
use parse::Directory;
|
2018-11-17 01:37:23 -06:00
|
|
|
use parse::token::{self, DelimToken, Token};
|
2016-08-28 23:16:43 -05:00
|
|
|
use print::pprust;
|
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};
|
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
|
|
|
|
Token(Span, token::Token),
|
|
|
|
/// A delimited sequence of token trees
|
2018-11-29 17:02:04 -06:00
|
|
|
Delimited(DelimSpan, DelimToken, ThinTokenStream),
|
2016-06-20 10:49:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenTree {
|
|
|
|
/// Use this token tree as a matcher to parse given tts.
|
2017-02-20 23:05:59 -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
|
|
|
|
|
|
|
/// Check if this TokenTree is equal to the other, regardless of span information.
|
|
|
|
pub fn eq_unspanned(&self, other: &TokenTree) -> bool {
|
|
|
|
match (self, other) {
|
|
|
|
(&TokenTree::Token(_, ref tk), &TokenTree::Token(_, ref tk2)) => tk == tk2,
|
2018-11-29 17:02:04 -06:00
|
|
|
(&TokenTree::Delimited(_, delim, ref tts),
|
|
|
|
&TokenTree::Delimited(_, delim2, ref tts2)) => {
|
|
|
|
delim == delim2 &&
|
|
|
|
tts.stream().eq_unspanned(&tts2.stream())
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
(_, _) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 21:36:48 -05:00
|
|
|
// See comments in `interpolated_to_tokenstream` for why we care about
|
|
|
|
// *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) {
|
|
|
|
(&TokenTree::Token(_, ref tk), &TokenTree::Token(_, ref tk2)) => {
|
|
|
|
tk.probably_equal_for_proc_macro(tk2)
|
|
|
|
}
|
2018-11-29 17:02:04 -06:00
|
|
|
(&TokenTree::Delimited(_, delim, ref tts),
|
|
|
|
&TokenTree::Delimited(_, delim2, ref tts2)) => {
|
|
|
|
delim == delim2 &&
|
|
|
|
tts.stream().probably_equal_for_proc_macro(&tts2.stream())
|
2018-04-18 21:36:48 -05:00
|
|
|
}
|
|
|
|
(_, _) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 13:55:10 -05:00
|
|
|
/// Retrieve the TokenTree's span.
|
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
match *self {
|
2018-09-08 20:07:02 -05:00
|
|
|
TokenTree::Token(sp, _) => sp,
|
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) {
|
|
|
|
match *self {
|
2018-09-08 20:07:02 -05:00
|
|
|
TokenTree::Token(ref mut sp, _) => *sp = span,
|
2018-11-29 17:02:04 -06:00
|
|
|
TokenTree::Delimited(ref mut sp, ..) => *sp = DelimSpan::from_single(span),
|
2017-07-19 23:54:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 13:55:10 -05:00
|
|
|
/// Indicates if the stream is a token that is equal to the provided token.
|
|
|
|
pub fn eq_token(&self, t: Token) -> bool {
|
|
|
|
match *self {
|
|
|
|
TokenTree::Token(_, ref tk) => *tk == t,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2017-03-17 18:41:09 -05:00
|
|
|
|
|
|
|
pub fn joint(self) -> TokenStream {
|
2018-12-11 16:55:43 -06:00
|
|
|
TokenStream::JointTree(self)
|
2017-03-17 18:41:09 -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))
|
|
|
|
};
|
|
|
|
TokenTree::Token(open_span, token::OpenDelim(delim))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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))
|
|
|
|
};
|
|
|
|
TokenTree::Token(close_span, token::CloseDelim(delim))
|
|
|
|
}
|
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.
|
|
|
|
/// Today's `TokenTree`s can still contain AST via `Token::Interpolated` for back-compat.
|
|
|
|
#[derive(Clone, Debug)]
|
2018-12-11 16:55:43 -06:00
|
|
|
pub enum TokenStream {
|
|
|
|
Empty,
|
|
|
|
Tree(TokenTree),
|
|
|
|
JointTree(TokenTree),
|
|
|
|
Stream(Lrc<Vec<TokenStream>>),
|
2016-07-19 17:50:34 -05: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")]
|
2018-12-11 16:01:39 -06:00
|
|
|
static_assert!(MEM_SIZE_OF_TOKEN_STREAM: mem::size_of::<TokenStream>() == 32);
|
2018-11-29 17:02:04 -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);`
|
2018-12-11 16:55:43 -06:00
|
|
|
if let TokenStream::Stream(ref stream) = self {
|
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) {
|
|
|
|
(TokenStream::Tree(TokenTree::Token(_, token::Token::Comma)), _) |
|
|
|
|
(_, TokenStream::Tree(TokenTree::Token(_, token::Token::Comma))) => {
|
2018-09-08 20:07:02 -05:00
|
|
|
continue;
|
2018-08-08 00:28:09 -05:00
|
|
|
}
|
2018-12-11 16:55:43 -06:00
|
|
|
(TokenStream::Tree(TokenTree::Token(sp, _)), _) => *sp,
|
|
|
|
(TokenStream::Tree(TokenTree::Delimited(sp, ..)), _) => sp.entire(),
|
2018-09-08 20:07:02 -05:00
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
let sp = sp.shrink_to_hi();
|
2018-12-11 16:55:43 -06:00
|
|
|
let comma = TokenStream::Tree(TokenTree::Token(sp, token::Comma));
|
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 {
|
|
|
|
fn from(tt: TokenTree) -> TokenStream {
|
2018-12-11 16:55:43 -06:00
|
|
|
TokenStream::Tree(tt)
|
2016-07-19 17:50:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 17:04:46 -05:00
|
|
|
impl From<Token> for TokenStream {
|
|
|
|
fn from(token: Token) -> TokenStream {
|
|
|
|
TokenTree::Token(DUMMY_SP, token).into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-12-11 17:01:08 -06:00
|
|
|
TokenStream::new(iter.into_iter().map(Into::into).collect::<Vec<_>>())
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 14:45:48 -05:00
|
|
|
impl Extend<TokenStream> for TokenStream {
|
|
|
|
fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, iter: I) {
|
|
|
|
let iter = iter.into_iter();
|
2018-12-11 16:55:43 -06:00
|
|
|
let this = mem::replace(self, TokenStream::Empty);
|
2018-08-12 14:45:48 -05:00
|
|
|
|
|
|
|
// Vector of token streams originally in self.
|
2018-12-11 16:55:43 -06:00
|
|
|
let tts: Vec<TokenStream> = match this {
|
|
|
|
TokenStream::Empty => {
|
2018-08-12 14:45:48 -05:00
|
|
|
let mut vec = Vec::new();
|
|
|
|
vec.reserve(iter.size_hint().0);
|
|
|
|
vec
|
|
|
|
}
|
2018-12-11 16:55:43 -06:00
|
|
|
TokenStream::Tree(_) | TokenStream::JointTree(_) => {
|
2018-08-12 14:45:48 -05:00
|
|
|
let mut vec = Vec::new();
|
|
|
|
vec.reserve(1 + iter.size_hint().0);
|
2018-12-11 16:55:43 -06:00
|
|
|
vec.push(this);
|
2018-08-12 14:45:48 -05:00
|
|
|
vec
|
|
|
|
}
|
2018-12-11 16:55:43 -06:00
|
|
|
TokenStream::Stream(rc_vec) => match Lrc::try_unwrap(rc_vec) {
|
2018-08-12 14:45:48 -05:00
|
|
|
Ok(mut vec) => {
|
|
|
|
// Extend in place using the existing capacity if possible.
|
|
|
|
// This is the fast path for libraries like `quote` that
|
|
|
|
// build a token stream.
|
|
|
|
vec.reserve(iter.size_hint().0);
|
|
|
|
vec
|
|
|
|
}
|
|
|
|
Err(rc_vec) => {
|
|
|
|
// Self is shared so we need to copy and extend that.
|
|
|
|
let mut vec = Vec::new();
|
|
|
|
vec.reserve(rc_vec.len() + iter.size_hint().0);
|
|
|
|
vec.extend_from_slice(&rc_vec);
|
|
|
|
vec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Perform the extend, joining tokens as needed along the way.
|
|
|
|
let mut builder = TokenStreamBuilder(tts);
|
|
|
|
for stream in iter {
|
|
|
|
builder.push(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the resulting token stream. If it contains more than one token,
|
|
|
|
// preserve capacity in the vector in anticipation of the caller
|
|
|
|
// performing additional calls to extend.
|
2018-12-11 17:01:08 -06:00
|
|
|
*self = TokenStream::new(builder.0);
|
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 {
|
2018-12-11 16:55:43 -06:00
|
|
|
if let TokenStream::Stream(ref slice) = self {
|
2017-07-19 23:54:01 -05:00
|
|
|
slice.len()
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:27:09 -06:00
|
|
|
pub fn empty() -> TokenStream {
|
2018-12-11 16:55:43 -06:00
|
|
|
TokenStream::Empty
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
2018-12-11 16:55:43 -06:00
|
|
|
match self {
|
|
|
|
TokenStream::Empty => true,
|
2017-01-17 21:27:09 -06:00
|
|
|
_ => false,
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
2016-07-19 17:50:34 -05:00
|
|
|
}
|
|
|
|
|
2018-12-11 17:01:08 -06:00
|
|
|
pub fn new(mut streams: Vec<TokenStream>) -> 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-11 17:01:08 -06:00
|
|
|
_ => TokenStream::Stream(Lrc::new(streams)),
|
2017-02-18 06:45:32 -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
|
|
|
|
2018-04-18 21:36:48 -05:00
|
|
|
// See comments in `interpolated_to_tokenstream` for why we care about
|
|
|
|
// *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 {
|
|
|
|
match tree {
|
|
|
|
// The pretty printer tends to add trailing commas to
|
|
|
|
// everything, and in particular, after struct fields.
|
|
|
|
| TokenTree::Token(_, Token::Comma)
|
|
|
|
// The pretty printer emits `NoDelim` as whitespace.
|
|
|
|
| TokenTree::Token(_, Token::OpenDelim(DelimToken::NoDelim))
|
|
|
|
| TokenTree::Token(_, Token::CloseDelim(DelimToken::NoDelim))
|
|
|
|
// The pretty printer collapses many semicolons into one.
|
|
|
|
| TokenTree::Token(_, Token::Semi)
|
|
|
|
// The pretty printer collapses whitespace arbitrarily and can
|
|
|
|
// introduce whitespace from `NoDelim`.
|
|
|
|
| TokenTree::Token(_, Token::Whitespace) => false,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-04 20:41:33 -05:00
|
|
|
/// Precondition: `self` consists of a single token tree.
|
|
|
|
/// Returns true if the token tree is a joint operation w.r.t. `proc_macro::TokenNode`.
|
2017-03-17 18:41:09 -05:00
|
|
|
pub fn as_tree(self) -> (TokenTree, bool /* joint? */) {
|
2018-12-11 16:55:43 -06:00
|
|
|
match self {
|
|
|
|
TokenStream::Tree(tree) => (tree, false),
|
|
|
|
TokenStream::JointTree(tree) => (tree, true),
|
2017-03-17 18:41:09 -05:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 18:44:23 -05:00
|
|
|
pub fn map_enumerated<F: FnMut(usize, TokenTree) -> TokenTree>(self, mut f: F) -> TokenStream {
|
2017-07-19 23:54:01 -05:00
|
|
|
let mut trees = self.into_trees();
|
|
|
|
let mut result = Vec::new();
|
|
|
|
let mut i = 0;
|
|
|
|
while let Some(stream) = trees.next_as_stream() {
|
2018-12-11 16:55:43 -06:00
|
|
|
result.push(match stream {
|
|
|
|
TokenStream::Tree(tree) => f(i, tree).into(),
|
|
|
|
TokenStream::JointTree(tree) => f(i, tree).joint(),
|
2017-07-19 23:54:01 -05:00
|
|
|
_ => unreachable!()
|
|
|
|
});
|
|
|
|
i += 1;
|
|
|
|
}
|
2018-12-11 17:01:08 -06:00
|
|
|
TokenStream::new(result)
|
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 {
|
|
|
|
let mut trees = self.into_trees();
|
|
|
|
let mut result = Vec::new();
|
|
|
|
while let Some(stream) = trees.next_as_stream() {
|
2018-12-11 16:55:43 -06:00
|
|
|
result.push(match stream {
|
|
|
|
TokenStream::Tree(tree) => f(tree).into(),
|
|
|
|
TokenStream::JointTree(tree) => f(tree).joint(),
|
2017-03-17 18:41:09 -05:00
|
|
|
_ => unreachable!()
|
|
|
|
});
|
|
|
|
}
|
2018-12-11 17:01:08 -06:00
|
|
|
TokenStream::new(result)
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
2018-01-10 19:20:04 -06:00
|
|
|
fn first_tree_and_joint(&self) -> Option<(TokenTree, bool)> {
|
2018-12-11 16:55:43 -06:00
|
|
|
match self {
|
|
|
|
TokenStream::Empty => None,
|
|
|
|
TokenStream::Tree(ref tree) => Some((tree.clone(), false)),
|
|
|
|
TokenStream::JointTree(ref tree) => Some((tree.clone(), true)),
|
|
|
|
TokenStream::Stream(ref stream) => stream.first().unwrap().first_tree_and_joint(),
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn last_tree_if_joint(&self) -> Option<TokenTree> {
|
2018-12-11 16:55:43 -06:00
|
|
|
match self {
|
|
|
|
TokenStream::Empty | TokenStream::Tree(..) => None,
|
|
|
|
TokenStream::JointTree(ref tree) => Some(tree.clone()),
|
|
|
|
TokenStream::Stream(ref stream) => stream.last().unwrap().last_tree_if_joint(),
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 10:48:29 -05:00
|
|
|
#[derive(Clone)]
|
2017-03-17 18:41:09 -05:00
|
|
|
pub struct TokenStreamBuilder(Vec<TokenStream>);
|
|
|
|
|
|
|
|
impl TokenStreamBuilder {
|
2017-06-04 20:41:33 -05:00
|
|
|
pub fn new() -> TokenStreamBuilder {
|
|
|
|
TokenStreamBuilder(Vec::new())
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
if let Some(TokenTree::Token(last_span, last_tok)) = last_tree_if_joint {
|
2018-01-10 19:20:04 -06:00
|
|
|
if let Some((TokenTree::Token(span, tok), is_joint)) = stream.first_tree_and_joint() {
|
2017-03-17 18:41:09 -05:00
|
|
|
if let Some(glued_tok) = last_tok.glue(tok) {
|
|
|
|
let last_stream = self.0.pop().unwrap();
|
|
|
|
self.push_all_but_last_tree(&last_stream);
|
|
|
|
let glued_span = last_span.to(span);
|
2018-01-10 19:20:04 -06:00
|
|
|
let glued_tt = TokenTree::Token(glued_span, glued_tok);
|
|
|
|
let glued_tokenstream = if is_joint {
|
|
|
|
glued_tt.joint()
|
|
|
|
} else {
|
|
|
|
glued_tt.into()
|
|
|
|
};
|
|
|
|
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 add<T: Into<TokenStream>>(mut self, stream: T) -> Self {
|
|
|
|
self.push(stream);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build(self) -> TokenStream {
|
2018-12-11 17:01:08 -06:00
|
|
|
TokenStream::new(self.0)
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn push_all_but_last_tree(&mut self, stream: &TokenStream) {
|
2018-12-11 16:55:43 -06:00
|
|
|
if let TokenStream::Stream(ref streams) = stream {
|
2017-03-17 18:41:09 -05:00
|
|
|
let len = streams.len();
|
|
|
|
match len {
|
|
|
|
1 => {}
|
|
|
|
2 => self.0.push(streams[0].clone().into()),
|
2018-12-11 17:01:08 -06:00
|
|
|
_ => self.0.push(TokenStream::new(streams[0 .. len - 1].to_vec())),
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
self.push_all_but_last_tree(&streams[len - 1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn push_all_but_first_tree(&mut self, stream: &TokenStream) {
|
2018-12-11 16:55:43 -06:00
|
|
|
if let TokenStream::Stream(ref streams) = stream {
|
2017-03-17 18:41:09 -05:00
|
|
|
let len = streams.len();
|
|
|
|
match len {
|
|
|
|
1 => {}
|
|
|
|
2 => self.0.push(streams[1].clone().into()),
|
2018-12-11 17:01:08 -06:00
|
|
|
_ => self.0.push(TokenStream::new(streams[1 .. len].to_vec())),
|
2017-03-17 18:41:09 -05:00
|
|
|
}
|
|
|
|
self.push_all_but_first_tree(&streams[0])
|
|
|
|
}
|
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2017-06-09 22:30:33 -05:00
|
|
|
#[derive(Clone)]
|
2017-02-18 06:45:32 -06:00
|
|
|
pub struct Cursor(CursorKind);
|
|
|
|
|
2017-06-09 22:30:33 -05:00
|
|
|
#[derive(Clone)]
|
2017-02-18 06:45:32 -06:00
|
|
|
enum CursorKind {
|
|
|
|
Empty,
|
|
|
|
Tree(TokenTree, bool /* consumed? */),
|
2017-03-17 18:41:09 -05:00
|
|
|
JointTree(TokenTree, bool /* consumed? */),
|
2017-02-18 06:45:32 -06:00
|
|
|
Stream(StreamCursor),
|
|
|
|
}
|
|
|
|
|
2017-06-09 22:30:33 -05:00
|
|
|
#[derive(Clone)]
|
2017-02-18 06:45:32 -06:00
|
|
|
struct StreamCursor {
|
2018-12-11 16:01:39 -06:00
|
|
|
stream: Lrc<Vec<TokenStream>>,
|
2017-02-18 06:45:32 -06:00
|
|
|
index: usize,
|
2018-12-11 16:01:39 -06:00
|
|
|
stack: Vec<(Lrc<Vec<TokenStream>>, usize)>,
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2017-03-17 18:23:12 -05:00
|
|
|
impl StreamCursor {
|
2018-12-11 16:01:39 -06:00
|
|
|
fn new(stream: Lrc<Vec<TokenStream>>) -> Self {
|
2017-03-28 20:55:01 -05:00
|
|
|
StreamCursor { stream: stream, index: 0, stack: Vec::new() }
|
|
|
|
}
|
|
|
|
|
2017-03-17 18:41:09 -05:00
|
|
|
fn next_as_stream(&mut self) -> Option<TokenStream> {
|
2017-02-18 06:45:32 -06:00
|
|
|
loop {
|
2017-03-17 18:23:12 -05:00
|
|
|
if self.index < self.stream.len() {
|
|
|
|
self.index += 1;
|
2017-03-17 18:41:09 -05:00
|
|
|
let next = self.stream[self.index - 1].clone();
|
2018-12-11 16:55:43 -06:00
|
|
|
match next {
|
|
|
|
TokenStream::Tree(..) | TokenStream::JointTree(..) => return Some(next),
|
|
|
|
TokenStream::Stream(stream) => self.insert(stream),
|
|
|
|
TokenStream::Empty => {}
|
2017-02-18 06:45:32 -06:00
|
|
|
}
|
2017-03-17 18:23:12 -05:00
|
|
|
} else if let Some((stream, index)) = self.stack.pop() {
|
|
|
|
self.stream = stream;
|
|
|
|
self.index = index;
|
2017-02-18 06:45:32 -06:00
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
2017-01-17 21:27:09 -06:00
|
|
|
}
|
|
|
|
}
|
2017-03-28 20:55:01 -05:00
|
|
|
|
2018-12-11 16:01:39 -06:00
|
|
|
fn insert(&mut self, stream: Lrc<Vec<TokenStream>>) {
|
2017-03-28 20:55:01 -05:00
|
|
|
self.stack.push((mem::replace(&mut self.stream, stream),
|
|
|
|
mem::replace(&mut self.index, 0)));
|
|
|
|
}
|
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-11 16:55:43 -06:00
|
|
|
self.next_as_stream().map(|stream| match stream {
|
|
|
|
TokenStream::Tree(tree) | TokenStream::JointTree(tree) => tree,
|
2017-03-17 18:41:09 -05:00
|
|
|
_ => unreachable!()
|
|
|
|
})
|
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-11 16:55:43 -06:00
|
|
|
Cursor(match stream {
|
|
|
|
TokenStream::Empty => CursorKind::Empty,
|
|
|
|
TokenStream::Tree(tree) => CursorKind::Tree(tree, false),
|
|
|
|
TokenStream::JointTree(tree) => CursorKind::JointTree(tree, false),
|
|
|
|
TokenStream::Stream(stream) => CursorKind::Stream(StreamCursor::new(stream)),
|
2017-02-18 06:45:32 -06:00
|
|
|
})
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
2017-02-19 23:44:06 -06:00
|
|
|
|
2017-03-17 18:41:09 -05:00
|
|
|
pub fn next_as_stream(&mut self) -> Option<TokenStream> {
|
|
|
|
let (stream, consumed) = match self.0 {
|
|
|
|
CursorKind::Tree(ref tree, ref mut consumed @ false) =>
|
|
|
|
(tree.clone().into(), consumed),
|
|
|
|
CursorKind::JointTree(ref tree, ref mut consumed @ false) =>
|
|
|
|
(tree.clone().joint(), consumed),
|
|
|
|
CursorKind::Stream(ref mut cursor) => return cursor.next_as_stream(),
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
*consumed = true;
|
|
|
|
Some(stream)
|
|
|
|
}
|
|
|
|
|
2017-03-28 20:55:01 -05:00
|
|
|
pub fn insert(&mut self, stream: TokenStream) {
|
|
|
|
match self.0 {
|
|
|
|
_ if stream.is_empty() => return,
|
|
|
|
CursorKind::Empty => *self = stream.trees(),
|
|
|
|
CursorKind::Tree(_, consumed) | CursorKind::JointTree(_, consumed) => {
|
2018-12-11 17:01:08 -06:00
|
|
|
*self = TokenStream::new(vec![self.original_stream(), stream]).trees();
|
2017-03-28 20:55:01 -05:00
|
|
|
if consumed {
|
|
|
|
self.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CursorKind::Stream(ref mut cursor) => {
|
|
|
|
cursor.insert(ThinTokenStream::from(stream).0.unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn original_stream(&self) -> TokenStream {
|
2017-02-19 23:44:06 -06:00
|
|
|
match self.0 {
|
|
|
|
CursorKind::Empty => TokenStream::empty(),
|
2017-03-28 20:55:01 -05:00
|
|
|
CursorKind::Tree(ref tree, _) => tree.clone().into(),
|
|
|
|
CursorKind::JointTree(ref tree, _) => tree.clone().joint(),
|
2018-12-11 17:01:08 -06:00
|
|
|
CursorKind::Stream(ref cursor) => TokenStream::Stream(
|
2017-03-28 20:55:01 -05:00
|
|
|
cursor.stack.get(0).cloned().map(|(stream, _)| stream)
|
2018-10-12 09:16:00 -05:00
|
|
|
.unwrap_or_else(|| cursor.stream.clone())
|
2018-12-11 17:01:08 -06:00
|
|
|
),
|
2017-02-19 23:44:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn look_ahead(&self, n: usize) -> Option<TokenTree> {
|
|
|
|
fn look_ahead(streams: &[TokenStream], mut n: usize) -> Result<TokenTree, usize> {
|
|
|
|
for stream in streams {
|
2018-12-11 16:55:43 -06:00
|
|
|
n = match stream {
|
|
|
|
TokenStream::Tree(ref tree) | TokenStream::JointTree(ref tree)
|
2017-03-17 18:41:09 -05:00
|
|
|
if n == 0 => return Ok(tree.clone()),
|
2018-12-11 16:55:43 -06:00
|
|
|
TokenStream::Tree(..) | TokenStream::JointTree(..) => n - 1,
|
|
|
|
TokenStream::Stream(ref stream) => match look_ahead(stream, n) {
|
2017-02-19 23:44:06 -06:00
|
|
|
Ok(tree) => return Ok(tree),
|
|
|
|
Err(n) => n,
|
|
|
|
},
|
|
|
|
_ => n,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Err(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.0 {
|
2017-03-17 18:41:09 -05:00
|
|
|
CursorKind::Empty |
|
|
|
|
CursorKind::Tree(_, true) |
|
|
|
|
CursorKind::JointTree(_, true) => Err(n),
|
|
|
|
CursorKind::Tree(ref tree, false) |
|
|
|
|
CursorKind::JointTree(ref tree, false) => look_ahead(&[tree.clone().into()], n),
|
2017-02-19 23:44:06 -06:00
|
|
|
CursorKind::Stream(ref cursor) => {
|
|
|
|
look_ahead(&cursor.stream[cursor.index ..], n).or_else(|mut n| {
|
|
|
|
for &(ref stream, index) in cursor.stack.iter().rev() {
|
|
|
|
n = match look_ahead(&stream[index..], n) {
|
|
|
|
Ok(tree) => return Ok(tree),
|
|
|
|
Err(n) => n,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(n)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}.ok()
|
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2017-02-20 23:05:59 -06:00
|
|
|
/// The `TokenStream` type is large enough to represent a single `TokenTree` without allocation.
|
|
|
|
/// `ThinTokenStream` is smaller, but needs to allocate to represent a single `TokenTree`.
|
|
|
|
/// We must use `ThinTokenStream` in `TokenTree::Delimited` to avoid infinite size due to recursion.
|
|
|
|
#[derive(Debug, Clone)]
|
2018-12-11 16:01:39 -06:00
|
|
|
pub struct ThinTokenStream(Option<Lrc<Vec<TokenStream>>>);
|
2017-02-20 23:05:59 -06:00
|
|
|
|
2018-11-29 17:02:04 -06:00
|
|
|
impl ThinTokenStream {
|
|
|
|
pub fn stream(&self) -> TokenStream {
|
|
|
|
self.clone().into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 23:05:59 -06:00
|
|
|
impl From<TokenStream> for ThinTokenStream {
|
|
|
|
fn from(stream: TokenStream) -> ThinTokenStream {
|
2018-12-11 16:55:43 -06:00
|
|
|
ThinTokenStream(match stream {
|
|
|
|
TokenStream::Empty => None,
|
|
|
|
TokenStream::Tree(tree) => Some(Lrc::new(vec![tree.into()])),
|
|
|
|
TokenStream::JointTree(tree) => Some(Lrc::new(vec![tree.joint()])),
|
|
|
|
TokenStream::Stream(stream) => Some(stream),
|
2017-02-20 23:05:59 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ThinTokenStream> for TokenStream {
|
|
|
|
fn from(stream: ThinTokenStream) -> TokenStream {
|
2018-12-11 17:01:08 -06:00
|
|
|
stream.0.map(TokenStream::Stream).unwrap_or_else(TokenStream::empty)
|
2017-02-20 23:05:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for ThinTokenStream {}
|
|
|
|
|
|
|
|
impl PartialEq<ThinTokenStream> for ThinTokenStream {
|
|
|
|
fn eq(&self, other: &ThinTokenStream) -> bool {
|
|
|
|
TokenStream::from(self.clone()) == TokenStream::from(other.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:27:09 -06:00
|
|
|
impl fmt::Display for TokenStream {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 23:05:59 -06:00
|
|
|
impl Encodable for ThinTokenStream {
|
|
|
|
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), E::Error> {
|
|
|
|
TokenStream::from(self.clone()).encode(encoder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for ThinTokenStream {
|
|
|
|
fn decode<D: Decoder>(decoder: &mut D) -> Result<ThinTokenStream, D::Error> {
|
|
|
|
TokenStream::decode(decoder).map(Into::into)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn apply_mark(self, mark: Mark) -> Self {
|
|
|
|
DelimSpan {
|
|
|
|
open: self.open.apply_mark(mark),
|
|
|
|
close: self.close.apply_mark(mark),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:50:34 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2016-11-17 08:04:36 -06:00
|
|
|
use syntax::ast::Ident;
|
2018-03-06 19:44:10 -06:00
|
|
|
use with_globals;
|
2017-01-17 21:27:09 -06:00
|
|
|
use syntax_pos::{Span, BytePos, NO_EXPANSION};
|
|
|
|
use parse::token::Token;
|
2017-03-01 19:29:40 -06:00
|
|
|
use util::parser_testing::string_to_stream;
|
2017-01-17 21:27:09 -06:00
|
|
|
|
|
|
|
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() {
|
2018-03-06 19:44:10 -06:00
|
|
|
with_globals(|| {
|
|
|
|
let test_res = string_to_ts("foo::bar::baz");
|
|
|
|
let test_fst = string_to_ts("foo::bar");
|
|
|
|
let test_snd = string_to_ts("::baz");
|
2018-12-11 17:01:08 -06:00
|
|
|
let eq_res = TokenStream::new(vec![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() {
|
2018-03-06 19:44:10 -06:00
|
|
|
with_globals(|| {
|
|
|
|
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() {
|
2018-03-06 19:44:10 -06:00
|
|
|
with_globals(|| {
|
|
|
|
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() {
|
2018-03-06 19:44:10 -06:00
|
|
|
with_globals(|| {
|
|
|
|
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() {
|
2018-03-06 19:44:10 -06:00
|
|
|
with_globals(|| {
|
|
|
|
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() {
|
2018-03-06 19:44:10 -06:00
|
|
|
with_globals(|| {
|
|
|
|
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() {
|
2018-03-06 19:44:10 -06:00
|
|
|
with_globals(|| {
|
|
|
|
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() {
|
2018-03-06 19:44:10 -06:00
|
|
|
with_globals(|| {
|
|
|
|
let test0: TokenStream = Vec::<TokenTree>::new().into_iter().collect();
|
|
|
|
let test1: TokenStream =
|
2018-03-09 23:56:40 -06:00
|
|
|
TokenTree::Token(sp(0, 1), Token::Ident(Ident::from_str("a"), false)).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() {
|
|
|
|
let mut builder = TokenStreamBuilder::new();
|
|
|
|
builder.push(TokenTree::Token(sp(0, 1), Token::Dot).joint());
|
|
|
|
builder.push(TokenTree::Token(sp(1, 2), Token::Dot).joint());
|
|
|
|
builder.push(TokenTree::Token(sp(2, 3), Token::Dot));
|
|
|
|
let stream = builder.build();
|
|
|
|
assert!(stream.eq_unspanned(&string_to_ts("...")));
|
|
|
|
assert_eq!(stream.trees().count(), 1);
|
|
|
|
}
|
|
|
|
|
2018-08-12 14:45:48 -05:00
|
|
|
#[test]
|
|
|
|
fn test_extend_empty() {
|
|
|
|
with_globals(|| {
|
|
|
|
// Append a token onto an empty token stream.
|
|
|
|
let mut stream = TokenStream::empty();
|
|
|
|
stream.extend(vec![string_to_ts("t")]);
|
|
|
|
|
|
|
|
let expected = string_to_ts("t");
|
|
|
|
assert!(stream.eq_unspanned(&expected));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_nothing() {
|
|
|
|
with_globals(|| {
|
|
|
|
// Append nothing onto a token stream containing one token.
|
|
|
|
let mut stream = string_to_ts("t");
|
|
|
|
stream.extend(vec![]);
|
|
|
|
|
|
|
|
let expected = string_to_ts("t");
|
|
|
|
assert!(stream.eq_unspanned(&expected));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_single() {
|
|
|
|
with_globals(|| {
|
|
|
|
// Append a token onto token stream containing a single token.
|
|
|
|
let mut stream = string_to_ts("t1");
|
|
|
|
stream.extend(vec![string_to_ts("t2")]);
|
|
|
|
|
|
|
|
let expected = string_to_ts("t1 t2");
|
|
|
|
assert!(stream.eq_unspanned(&expected));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_in_place() {
|
|
|
|
with_globals(|| {
|
|
|
|
// Append a token onto token stream containing a reference counted
|
|
|
|
// vec of tokens. The token stream has a reference count of 1 so
|
|
|
|
// this can happen in place.
|
|
|
|
let mut stream = string_to_ts("t1 t2");
|
|
|
|
stream.extend(vec![string_to_ts("t3")]);
|
|
|
|
|
|
|
|
let expected = string_to_ts("t1 t2 t3");
|
|
|
|
assert!(stream.eq_unspanned(&expected));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_copy() {
|
|
|
|
with_globals(|| {
|
|
|
|
// Append a token onto token stream containing a reference counted
|
|
|
|
// vec of tokens. The token stream is shared so the extend takes
|
|
|
|
// place on a copy.
|
|
|
|
let mut stream = string_to_ts("t1 t2");
|
|
|
|
let _incref = stream.clone();
|
|
|
|
stream.extend(vec![string_to_ts("t3")]);
|
|
|
|
|
|
|
|
let expected = string_to_ts("t1 t2 t3");
|
|
|
|
assert!(stream.eq_unspanned(&expected));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_no_join() {
|
|
|
|
with_globals(|| {
|
|
|
|
let first = TokenTree::Token(DUMMY_SP, Token::Dot);
|
|
|
|
let second = TokenTree::Token(DUMMY_SP, Token::Dot);
|
|
|
|
|
|
|
|
// Append a dot onto a token stream containing a dot, but do not
|
|
|
|
// join them.
|
|
|
|
let mut stream = TokenStream::from(first);
|
|
|
|
stream.extend(vec![TokenStream::from(second)]);
|
|
|
|
|
|
|
|
let expected = string_to_ts(". .");
|
|
|
|
assert!(stream.eq_unspanned(&expected));
|
|
|
|
|
|
|
|
let unexpected = string_to_ts("..");
|
|
|
|
assert!(!stream.eq_unspanned(&unexpected));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_join() {
|
|
|
|
with_globals(|| {
|
|
|
|
let first = TokenTree::Token(DUMMY_SP, Token::Dot).joint();
|
|
|
|
let second = TokenTree::Token(DUMMY_SP, Token::Dot);
|
|
|
|
|
|
|
|
// Append a dot onto a token stream containing a dot, forming a
|
|
|
|
// dotdot.
|
|
|
|
let mut stream = first;
|
|
|
|
stream.extend(vec![TokenStream::from(second)]);
|
|
|
|
|
|
|
|
let expected = string_to_ts("..");
|
|
|
|
assert!(stream.eq_unspanned(&expected));
|
|
|
|
|
|
|
|
let unexpected = string_to_ts(". .");
|
|
|
|
assert!(!stream.eq_unspanned(&unexpected));
|
|
|
|
});
|
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|