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
|
|
|
|
2017-01-22 22:58:15 -06:00
|
|
|
use syntax_pos::{BytePos, 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;
|
|
|
|
use parse::token::{self, Token};
|
2016-08-28 23:16:43 -05:00
|
|
|
use print::pprust;
|
2017-01-17 21:27:09 -06:00
|
|
|
use serialize::{Decoder, Decodable, Encoder, Encodable};
|
|
|
|
use util::RcSlice;
|
2016-06-29 13:55:10 -05:00
|
|
|
|
2017-02-18 00:18:29 -06:00
|
|
|
use std::{fmt, iter, mem};
|
2017-02-20 23:05:59 -06:00
|
|
|
use std::hash::{self, Hash};
|
2016-07-04 05:25:50 -05:00
|
|
|
|
2016-06-20 10:49:33 -05:00
|
|
|
/// A delimited sequence of token trees
|
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub struct Delimited {
|
|
|
|
/// The type of delimiter
|
|
|
|
pub delim: token::DelimToken,
|
|
|
|
/// The delimited sequence of token trees
|
2017-02-20 23:05:59 -06:00
|
|
|
pub tts: ThinTokenStream,
|
2016-06-20 10:49:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Delimited {
|
|
|
|
/// Returns the opening delimiter as a token.
|
|
|
|
pub fn open_token(&self) -> token::Token {
|
|
|
|
token::OpenDelim(self.delim)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the closing delimiter as a token.
|
|
|
|
pub fn close_token(&self) -> token::Token {
|
|
|
|
token::CloseDelim(self.delim)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the opening delimiter as a token tree.
|
2017-01-22 22:58:15 -06:00
|
|
|
pub fn open_tt(&self, span: Span) -> TokenTree {
|
2017-03-16 23:04:41 -05:00
|
|
|
let open_span = if span == DUMMY_SP {
|
|
|
|
DUMMY_SP
|
|
|
|
} else {
|
|
|
|
Span { hi: span.lo + BytePos(self.delim.len() as u32), ..span }
|
2017-01-22 22:58:15 -06:00
|
|
|
};
|
|
|
|
TokenTree::Token(open_span, self.open_token())
|
2016-06-20 10:49:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the closing delimiter as a token tree.
|
2017-01-22 22:58:15 -06:00
|
|
|
pub fn close_tt(&self, span: Span) -> TokenTree {
|
2017-03-16 23:04:41 -05:00
|
|
|
let close_span = if span == DUMMY_SP {
|
|
|
|
DUMMY_SP
|
|
|
|
} else {
|
|
|
|
Span { lo: span.hi - BytePos(self.delim.len() as u32), ..span }
|
2017-01-22 22:58:15 -06:00
|
|
|
};
|
|
|
|
TokenTree::Token(close_span, self.close_token())
|
2016-06-20 10:49:33 -05:00
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
|
|
|
|
/// Returns the token trees inside the delimiters.
|
2017-02-20 23:05:59 -06:00
|
|
|
pub fn stream(&self) -> TokenStream {
|
|
|
|
self.tts.clone().into()
|
2016-06-29 13:55:10 -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.
|
2016-06-29 13:55:10 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
|
2016-06-20 10:49:33 -05:00
|
|
|
pub enum TokenTree {
|
|
|
|
/// A single token
|
|
|
|
Token(Span, token::Token),
|
|
|
|
/// A delimited sequence of token trees
|
2017-02-20 23:05:59 -06:00
|
|
|
Delimited(Span, Delimited),
|
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 {
|
|
|
|
path: cx.current_expansion.module.directory.clone(),
|
|
|
|
ownership: cx.current_expansion.directory_ownership,
|
|
|
|
};
|
2017-02-20 23:05:59 -06:00
|
|
|
macro_parser::parse(cx.parse_sess(), tts, mtch, Some(directory))
|
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,
|
|
|
|
(&TokenTree::Delimited(_, ref dl), &TokenTree::Delimited(_, ref dl2)) => {
|
2017-02-20 23:05:59 -06:00
|
|
|
dl.delim == dl2.delim &&
|
|
|
|
dl.stream().trees().zip(dl2.stream().trees()).all(|(tt, tt2)| tt.eq_unspanned(&tt2))
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
(_, _) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve the TokenTree's span.
|
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
match *self {
|
2017-01-29 02:38:44 -06:00
|
|
|
TokenTree::Token(sp, _) | TokenTree::Delimited(sp, _) => sp,
|
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-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)]
|
2016-06-29 13:55:10 -05:00
|
|
|
pub struct TokenStream {
|
2017-01-17 21:27:09 -06:00
|
|
|
kind: TokenStreamKind,
|
2016-07-19 17:50:34 -05:00
|
|
|
}
|
|
|
|
|
2017-01-17 21:27:09 -06:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
enum TokenStreamKind {
|
|
|
|
Empty,
|
|
|
|
Tree(TokenTree),
|
|
|
|
Stream(RcSlice<TokenStream>),
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2017-01-17 21:27:09 -06:00
|
|
|
impl From<TokenTree> for TokenStream {
|
|
|
|
fn from(tt: TokenTree) -> TokenStream {
|
|
|
|
TokenStream { kind: TokenStreamKind::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 {
|
2017-02-18 06:45:32 -06:00
|
|
|
TokenStream::concat(iter.into_iter().map(Into::into).collect::<Vec<_>>())
|
2016-06-29 13:55:10 -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-01-17 21:27:09 -06:00
|
|
|
pub fn empty() -> TokenStream {
|
|
|
|
TokenStream { kind: TokenStreamKind::Empty }
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
2017-01-17 21:27:09 -06:00
|
|
|
match self.kind {
|
|
|
|
TokenStreamKind::Empty => true,
|
|
|
|
_ => false,
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
2016-07-19 17:50:34 -05:00
|
|
|
}
|
|
|
|
|
2017-02-18 06:45:32 -06:00
|
|
|
pub fn concat(mut streams: Vec<TokenStream>) -> TokenStream {
|
|
|
|
match streams.len() {
|
|
|
|
0 => TokenStream::empty(),
|
|
|
|
1 => TokenStream::from(streams.pop().unwrap()),
|
|
|
|
_ => TokenStream::concat_rc_slice(RcSlice::new(streams)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn concat_rc_slice(streams: RcSlice<TokenStream>) -> TokenStream {
|
|
|
|
TokenStream { kind: TokenStreamKind::Stream(streams) }
|
2016-06-29 13:55:10 -05: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 {
|
2017-01-17 21:27:09 -06:00
|
|
|
for (t1, t2) in self.trees().zip(other.trees()) {
|
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
|
|
|
}
|
|
|
|
}
|
2016-07-19 17:50:34 -05:00
|
|
|
true
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-18 06:45:32 -06:00
|
|
|
pub struct Cursor(CursorKind);
|
|
|
|
|
|
|
|
enum CursorKind {
|
|
|
|
Empty,
|
|
|
|
Tree(TokenTree, bool /* consumed? */),
|
|
|
|
Stream(StreamCursor),
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StreamCursor {
|
|
|
|
stream: RcSlice<TokenStream>,
|
|
|
|
index: usize,
|
|
|
|
stack: Vec<(RcSlice<TokenStream>, usize)>,
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
2017-02-18 00:18:29 -06:00
|
|
|
impl Iterator for Cursor {
|
|
|
|
type Item = TokenTree;
|
2016-06-29 13:55:10 -05:00
|
|
|
|
2017-02-18 00:18:29 -06:00
|
|
|
fn next(&mut self) -> Option<TokenTree> {
|
2017-02-18 06:45:32 -06:00
|
|
|
let cursor = match self.0 {
|
|
|
|
CursorKind::Stream(ref mut cursor) => cursor,
|
|
|
|
CursorKind::Tree(ref tree, ref mut consumed @ false) => {
|
|
|
|
*consumed = true;
|
|
|
|
return Some(tree.clone());
|
|
|
|
}
|
|
|
|
_ => return None,
|
|
|
|
};
|
2016-06-29 13:55:10 -05:00
|
|
|
|
2017-02-18 06:45:32 -06:00
|
|
|
loop {
|
|
|
|
if cursor.index < cursor.stream.len() {
|
|
|
|
match cursor.stream[cursor.index].kind.clone() {
|
|
|
|
TokenStreamKind::Tree(tree) => {
|
|
|
|
cursor.index += 1;
|
|
|
|
return Some(tree);
|
|
|
|
}
|
|
|
|
TokenStreamKind::Stream(stream) => {
|
|
|
|
cursor.stack.push((mem::replace(&mut cursor.stream, stream),
|
|
|
|
mem::replace(&mut cursor.index, 0) + 1));
|
|
|
|
}
|
|
|
|
TokenStreamKind::Empty => {
|
|
|
|
cursor.index += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if let Some((stream, index)) = cursor.stack.pop() {
|
|
|
|
cursor.stream = stream;
|
|
|
|
cursor.index = index;
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
2017-01-17 21:27:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
|
2017-02-18 00:18:29 -06:00
|
|
|
impl Cursor {
|
|
|
|
fn new(stream: TokenStream) -> Self {
|
2017-02-18 06:45:32 -06:00
|
|
|
Cursor(match stream.kind {
|
|
|
|
TokenStreamKind::Empty => CursorKind::Empty,
|
|
|
|
TokenStreamKind::Tree(tree) => CursorKind::Tree(tree, false),
|
|
|
|
TokenStreamKind::Stream(stream) => {
|
|
|
|
CursorKind::Stream(StreamCursor { stream: stream, index: 0, stack: Vec::new() })
|
|
|
|
}
|
|
|
|
})
|
2016-06-29 13:55:10 -05:00
|
|
|
}
|
2017-02-19 23:44:06 -06:00
|
|
|
|
|
|
|
pub fn original_stream(self) -> TokenStream {
|
|
|
|
match self.0 {
|
|
|
|
CursorKind::Empty => TokenStream::empty(),
|
|
|
|
CursorKind::Tree(tree, _) => tree.into(),
|
|
|
|
CursorKind::Stream(cursor) => TokenStream::concat_rc_slice({
|
|
|
|
cursor.stack.get(0).cloned().map(|(stream, _)| stream).unwrap_or(cursor.stream)
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn look_ahead(&self, n: usize) -> Option<TokenTree> {
|
|
|
|
fn look_ahead(streams: &[TokenStream], mut n: usize) -> Result<TokenTree, usize> {
|
|
|
|
for stream in streams {
|
|
|
|
n = match stream.kind {
|
|
|
|
TokenStreamKind::Tree(ref tree) if n == 0 => return Ok(tree.clone()),
|
|
|
|
TokenStreamKind::Tree(..) => n - 1,
|
|
|
|
TokenStreamKind::Stream(ref stream) => match look_ahead(stream, n) {
|
|
|
|
Ok(tree) => return Ok(tree),
|
|
|
|
Err(n) => n,
|
|
|
|
},
|
|
|
|
_ => n,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.0 {
|
|
|
|
CursorKind::Empty | CursorKind::Tree(_, true) => Err(n),
|
|
|
|
CursorKind::Tree(ref tree, false) => look_ahead(&[tree.clone().into()], n),
|
|
|
|
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)]
|
|
|
|
pub struct ThinTokenStream(Option<RcSlice<TokenStream>>);
|
|
|
|
|
|
|
|
impl From<TokenStream> for ThinTokenStream {
|
|
|
|
fn from(stream: TokenStream) -> ThinTokenStream {
|
|
|
|
ThinTokenStream(match stream.kind {
|
|
|
|
TokenStreamKind::Empty => None,
|
|
|
|
TokenStreamKind::Tree(tree) => Some(RcSlice::new(vec![tree.into()])),
|
|
|
|
TokenStreamKind::Stream(stream) => Some(stream),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ThinTokenStream> for TokenStream {
|
|
|
|
fn from(stream: ThinTokenStream) -> TokenStream {
|
|
|
|
stream.0.map(TokenStream::concat_rc_slice).unwrap_or_else(TokenStream::empty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 Hash for TokenStream {
|
|
|
|
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
|
|
|
for tree in self.trees() {
|
|
|
|
tree.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Hash for ThinTokenStream {
|
|
|
|
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
|
|
|
TokenStream::from(self.clone()).hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 13:55:10 -05:00
|
|
|
|
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;
|
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 {
|
|
|
|
Span {
|
|
|
|
lo: BytePos(a),
|
|
|
|
hi: BytePos(b),
|
2017-03-16 23:04:41 -05:00
|
|
|
ctxt: NO_EXPANSION,
|
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_concat() {
|
2017-01-17 21:27:09 -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");
|
2017-03-01 19:29:40 -06:00
|
|
|
let eq_res = TokenStream::concat(vec![test_fst, test_snd]);
|
2017-01-17 21:27:09 -06:00
|
|
|
assert_eq!(test_res.trees().count(), 5);
|
|
|
|
assert_eq!(eq_res.trees().count(), 5);
|
2016-07-19 17:50:34 -05:00
|
|
|
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() {
|
2017-01-17 21:27:09 -06:00
|
|
|
let test_start = string_to_ts("foo::bar(baz)");
|
2017-02-18 00:18:29 -06:00
|
|
|
let test_end = test_start.trees().collect();
|
2016-07-19 17:50:34 -05:00
|
|
|
assert_eq!(test_start, test_end)
|
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
|
2016-07-19 17:50:34 -05:00
|
|
|
#[test]
|
|
|
|
fn test_eq_0() {
|
2017-01-17 21:27:09 -06:00
|
|
|
let test_res = string_to_ts("foo");
|
|
|
|
let test_eqs = string_to_ts("foo");
|
2016-07-19 17:50:34 -05:00
|
|
|
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() {
|
2017-01-17 21:27:09 -06:00
|
|
|
let test_res = string_to_ts("::bar::baz");
|
|
|
|
let test_eqs = string_to_ts("::bar::baz");
|
2016-07-19 17:50:34 -05:00
|
|
|
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_3() {
|
2017-01-17 21:27:09 -06:00
|
|
|
let test_res = string_to_ts("");
|
|
|
|
let test_eqs = string_to_ts("");
|
2016-07-19 17:50:34 -05:00
|
|
|
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_diseq_0() {
|
2017-01-17 21:27:09 -06:00
|
|
|
let test_res = string_to_ts("::bar::baz");
|
|
|
|
let test_eqs = string_to_ts("bar::baz");
|
2016-07-19 17:50:34 -05:00
|
|
|
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() {
|
2017-01-17 21:27:09 -06:00
|
|
|
let test_res = string_to_ts("(bar,baz)");
|
|
|
|
let test_eqs = string_to_ts("bar,baz");
|
2016-07-19 17:50:34 -05:00
|
|
|
assert_eq!(test_res == test_eqs, false)
|
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_empty() {
|
2017-01-17 21:27:09 -06:00
|
|
|
let test0: TokenStream = Vec::<TokenTree>::new().into_iter().collect();
|
|
|
|
let test1: TokenStream =
|
|
|
|
TokenTree::Token(sp(0, 1), Token::Ident(Ident::from_str("a"))).into();
|
|
|
|
let test2 = string_to_ts("foo(bar::baz)");
|
2016-06-29 13:55:10 -05:00
|
|
|
|
|
|
|
assert_eq!(test0.is_empty(), true);
|
|
|
|
assert_eq!(test1.is_empty(), false);
|
|
|
|
assert_eq!(test2.is_empty(), false);
|
|
|
|
}
|
|
|
|
}
|