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
|
|
|
|
//!
|
|
|
|
//! TokenStreams represent syntactic objects before they are converted into ASTs.
|
|
|
|
//! 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-01-28 08:00:43 -06:00
|
|
|
//! TokenStreams are persistent data structures constructed as ropes with reference
|
2016-07-19 17:50:34 -05:00
|
|
|
//! counted-children. In general, this means that calling an operation on a TokenStream
|
|
|
|
//! (such as `slice`) produces an entirely new TokenStream from the borrowed reference to
|
|
|
|
//! the original. This essentially coerces TokenStreams into 'views' of their subparts,
|
|
|
|
//! and a borrowed TokenStream is sufficient to build an owned TokenStream without taking
|
|
|
|
//! ownership of the original.
|
2016-06-20 10:49:33 -05:00
|
|
|
|
2016-06-29 13:55:10 -05:00
|
|
|
use ast::{self, AttrStyle, LitKind};
|
2017-01-22 22:58:15 -06:00
|
|
|
use syntax_pos::{BytePos, Span, DUMMY_SP};
|
2017-01-17 21:27:09 -06:00
|
|
|
use codemap::Spanned;
|
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};
|
2016-06-20 10:49:33 -05:00
|
|
|
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
|
2016-12-06 18:28:51 -06:00
|
|
|
use parse::{self, Directory};
|
2017-01-29 02:38:44 -06:00
|
|
|
use parse::token::{self, Token, Lit};
|
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};
|
2016-11-16 04:52:37 -06:00
|
|
|
use symbol::Symbol;
|
2017-01-17 21:27:09 -06:00
|
|
|
use util::RcSlice;
|
2016-06-29 13:55:10 -05:00
|
|
|
|
2017-02-18 00:18:29 -06:00
|
|
|
use std::{fmt, iter, mem};
|
2016-07-04 05:25:50 -05:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
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
|
|
|
|
pub tts: Vec<TokenTree>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
let open_span = match span {
|
|
|
|
DUMMY_SP => DUMMY_SP,
|
2017-01-27 05:00:10 -06:00
|
|
|
_ => 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 {
|
|
|
|
let close_span = match span {
|
|
|
|
DUMMY_SP => DUMMY_SP,
|
2017-01-27 05:00:10 -06:00
|
|
|
_ => 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.
|
|
|
|
pub fn subtrees(&self) -> &[TokenTree] {
|
|
|
|
&self.tts
|
|
|
|
}
|
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
|
|
|
|
/// macro_parser::matched_nonterminals into the `SubstNt`s it finds.
|
|
|
|
///
|
|
|
|
/// 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
|
2016-07-04 05:25:50 -05:00
|
|
|
Delimited(Span, Rc<Delimited>),
|
2016-06-20 10:49:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenTree {
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
match *self {
|
|
|
|
TokenTree::Token(_, token::DocComment(name)) => {
|
|
|
|
match doc_comment_style(&name.as_str()) {
|
|
|
|
AttrStyle::Outer => 2,
|
2016-06-29 13:55:10 -05:00
|
|
|
AttrStyle::Inner => 3,
|
2016-06-20 10:49:33 -05:00
|
|
|
}
|
|
|
|
}
|
2017-01-12 22:49:20 -06:00
|
|
|
TokenTree::Delimited(_, ref delimed) => match delimed.delim {
|
|
|
|
token::NoDelim => delimed.tts.len(),
|
|
|
|
_ => delimed.tts.len() + 2,
|
|
|
|
},
|
2016-06-29 13:55:10 -05:00
|
|
|
TokenTree::Token(..) => 0,
|
2016-06-20 10:49:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_tt(&self, index: usize) -> TokenTree {
|
|
|
|
match (self, index) {
|
2016-06-29 13:55:10 -05:00
|
|
|
(&TokenTree::Token(sp, token::DocComment(_)), 0) => TokenTree::Token(sp, token::Pound),
|
2016-06-20 10:49:33 -05:00
|
|
|
(&TokenTree::Token(sp, token::DocComment(name)), 1)
|
2016-06-29 13:55:10 -05:00
|
|
|
if doc_comment_style(&name.as_str()) == AttrStyle::Inner => {
|
2016-06-20 10:49:33 -05:00
|
|
|
TokenTree::Token(sp, token::Not)
|
|
|
|
}
|
|
|
|
(&TokenTree::Token(sp, token::DocComment(name)), _) => {
|
|
|
|
let stripped = strip_doc_comment_decoration(&name.as_str());
|
|
|
|
|
|
|
|
// Searches for the occurrences of `"#*` and returns the minimum number of `#`s
|
|
|
|
// required to wrap the text.
|
2016-06-29 13:55:10 -05:00
|
|
|
let num_of_hashes = stripped.chars()
|
|
|
|
.scan(0, |cnt, x| {
|
|
|
|
*cnt = if x == '"' {
|
|
|
|
1
|
|
|
|
} else if *cnt != 0 && x == '#' {
|
|
|
|
*cnt + 1
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
Some(*cnt)
|
|
|
|
})
|
|
|
|
.max()
|
|
|
|
.unwrap_or(0);
|
2016-06-20 10:49:33 -05:00
|
|
|
|
2016-07-04 05:25:50 -05:00
|
|
|
TokenTree::Delimited(sp, Rc::new(Delimited {
|
2016-06-20 10:49:33 -05:00
|
|
|
delim: token::Bracket,
|
2016-11-16 02:21:52 -06:00
|
|
|
tts: vec![TokenTree::Token(sp, token::Ident(ast::Ident::from_str("doc"))),
|
2016-06-20 10:49:33 -05:00
|
|
|
TokenTree::Token(sp, token::Eq),
|
|
|
|
TokenTree::Token(sp, token::Literal(
|
2016-11-16 02:21:52 -06:00
|
|
|
token::StrRaw(Symbol::intern(&stripped), num_of_hashes), None))],
|
2016-07-04 05:25:50 -05:00
|
|
|
}))
|
2016-06-20 10:49:33 -05:00
|
|
|
}
|
2017-01-12 22:49:20 -06:00
|
|
|
(&TokenTree::Delimited(_, ref delimed), _) if delimed.delim == token::NoDelim => {
|
|
|
|
delimed.tts[index].clone()
|
|
|
|
}
|
2017-01-22 22:58:15 -06:00
|
|
|
(&TokenTree::Delimited(span, ref delimed), _) => {
|
2016-06-20 10:49:33 -05:00
|
|
|
if index == 0 {
|
2017-01-22 22:58:15 -06:00
|
|
|
return delimed.open_tt(span);
|
2016-06-20 10:49:33 -05:00
|
|
|
}
|
|
|
|
if index == delimed.tts.len() + 1 {
|
2017-01-22 22:58:15 -06:00
|
|
|
return delimed.close_tt(span);
|
2016-06-20 10:49:33 -05:00
|
|
|
}
|
|
|
|
delimed.tts[index - 1].clone()
|
|
|
|
}
|
2016-06-29 13:55:10 -05:00
|
|
|
_ => panic!("Cannot expand a token tree"),
|
2016-06-20 10:49:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Use this token tree as a matcher to parse given tts.
|
2017-01-29 02:38:44 -06:00
|
|
|
pub fn parse(cx: &base::ExtCtxt, mtch: &[quoted::TokenTree], tts: &[TokenTree])
|
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-01-12 22:49:20 -06:00
|
|
|
macro_parser::parse(cx.parse_sess(), tts.iter().cloned().collect(), 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)) => {
|
|
|
|
(*dl).delim == (*dl2).delim && dl.tts.len() == dl2.tts.len() &&
|
|
|
|
{
|
|
|
|
for (tt1, tt2) in dl.tts.iter().zip(dl2.tts.iter()) {
|
|
|
|
if !tt1.eq_unspanned(tt2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(_, _) => 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates if the token is an identifier.
|
|
|
|
pub fn is_ident(&self) -> bool {
|
|
|
|
self.maybe_ident().is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an identifier.
|
|
|
|
pub fn maybe_ident(&self) -> Option<ast::Ident> {
|
|
|
|
match *self {
|
|
|
|
TokenTree::Token(_, Token::Ident(t)) => Some(t.clone()),
|
|
|
|
TokenTree::Delimited(_, ref dl) => {
|
|
|
|
let tts = dl.subtrees();
|
|
|
|
if tts.len() != 1 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
tts[0].maybe_ident()
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a Token literal.
|
|
|
|
pub fn maybe_lit(&self) -> Option<token::Lit> {
|
|
|
|
match *self {
|
|
|
|
TokenTree::Token(_, Token::Literal(l, _)) => Some(l.clone()),
|
|
|
|
TokenTree::Delimited(_, ref dl) => {
|
|
|
|
let tts = dl.subtrees();
|
|
|
|
if tts.len() != 1 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
tts[0].maybe_lit()
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an AST string literal.
|
|
|
|
pub fn maybe_str(&self) -> Option<ast::Lit> {
|
|
|
|
match *self {
|
|
|
|
TokenTree::Token(sp, Token::Literal(Lit::Str_(s), _)) => {
|
2016-11-16 04:52:37 -06:00
|
|
|
let l = LitKind::Str(Symbol::intern(&parse::str_lit(&s.as_str())),
|
2016-06-29 13:55:10 -05:00
|
|
|
ast::StrStyle::Cooked);
|
|
|
|
Some(Spanned {
|
|
|
|
node: l,
|
|
|
|
span: sp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
TokenTree::Token(sp, Token::Literal(Lit::StrRaw(s, n), _)) => {
|
2016-11-16 04:52:37 -06:00
|
|
|
let l = LitKind::Str(Symbol::intern(&parse::raw_str_lit(&s.as_str())),
|
2016-06-29 13:55:10 -05:00
|
|
|
ast::StrStyle::Raw(n));
|
|
|
|
Some(Spanned {
|
|
|
|
node: l,
|
|
|
|
span: sp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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-01-17 21:27:09 -06:00
|
|
|
impl fmt::Display for TokenStream {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2017-02-18 00:18:29 -06:00
|
|
|
f.write_str(&pprust::tts_to_string(&self.trees().collect::<Vec<_>>()))
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2016-07-19 17:50:34 -05:00
|
|
|
use util::parser_testing::string_to_tts;
|
2017-01-17 21:27:09 -06:00
|
|
|
|
|
|
|
fn string_to_ts(string: &str) -> TokenStream {
|
|
|
|
string_to_tts(string.to_owned()).into_iter().collect()
|
|
|
|
}
|
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),
|
|
|
|
expn_id: NO_EXPANSION,
|
|
|
|
}
|
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");
|
|
|
|
let eq_res = TokenStream::concat([test_fst, test_snd].iter().cloned());
|
|
|
|
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_from_to_bijection() {
|
|
|
|
let test_start = string_to_tts("foo::bar(baz)".to_string());
|
2017-01-17 21:27:09 -06:00
|
|
|
let ts = test_start.iter().cloned().collect::<TokenStream>();
|
2017-02-18 00:18:29 -06:00
|
|
|
let test_end: Vec<TokenTree> = ts.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_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);
|
|
|
|
}
|
|
|
|
}
|