Skip to content

Commit

Permalink
Integrate TokenStream.
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Mar 3, 2017
1 parent 8cd0c08 commit f6eaaf3
Show file tree
Hide file tree
Showing 27 changed files with 276 additions and 319 deletions.
7 changes: 3 additions & 4 deletions src/libproc_macro/lib.rs
Expand Up @@ -101,7 +101,7 @@ pub mod __internal {

pub fn token_stream_parse_items(stream: TokenStream) -> Result<Vec<P<ast::Item>>, LexError> {
with_parse_sess(move |sess| {
let mut parser = parse::new_parser_from_ts(sess, stream.inner);
let mut parser = parse::stream_to_parser(sess, stream.inner);
let mut items = Vec::new();

while let Some(item) = try!(parser.parse_item().map_err(super::parse_to_lex_err)) {
Expand Down Expand Up @@ -177,9 +177,8 @@ impl FromStr for TokenStream {
__internal::with_parse_sess(|sess| {
let src = src.to_string();
let name = "<proc-macro source code>".to_string();
let tts = parse::parse_tts_from_source_str(name, src, sess);

Ok(__internal::token_stream_wrap(tts.into_iter().collect()))
let stream = parse::parse_stream_from_source_str(name, src, sess);
Ok(__internal::token_stream_wrap(stream))
})
}
}
Expand Down
21 changes: 5 additions & 16 deletions src/libproc_macro_plugin/qquote.rs
Expand Up @@ -18,7 +18,6 @@ use syntax::tokenstream::{self, Delimited, TokenTree, TokenStream};
use syntax_pos::DUMMY_SP;

use std::iter;
use std::rc::Rc;

pub fn qquote<'cx>(stream: TokenStream) -> TokenStream {
stream.quote()
Expand Down Expand Up @@ -50,10 +49,7 @@ macro_rules! quote_tree {
}

fn delimit(delim: token::DelimToken, stream: TokenStream) -> TokenStream {
TokenTree::Delimited(DUMMY_SP, Rc::new(Delimited {
delim: delim,
tts: stream.into_trees().collect(),
})).into()
TokenTree::Delimited(DUMMY_SP, Delimited { delim: delim, tts: stream.into() }).into()
}

macro_rules! quote {
Expand Down Expand Up @@ -102,13 +98,6 @@ impl Quote for TokenStream {
}
}

impl Quote for Vec<TokenTree> {
fn quote(&self) -> TokenStream {
let stream = self.iter().cloned().collect::<TokenStream>();
quote!((quote stream).into_trees().collect::<::std::vec::Vec<_> >())
}
}

impl Quote for TokenTree {
fn quote(&self) -> TokenStream {
match *self {
Expand All @@ -124,12 +113,12 @@ impl Quote for TokenTree {
}
}

impl Quote for Rc<Delimited> {
impl Quote for Delimited {
fn quote(&self) -> TokenStream {
quote!(::std::rc::Rc::new(::syntax::tokenstream::Delimited {
quote!(::syntax::tokenstream::Delimited {
delim: (quote self.delim),
tts: (quote self.tts),
}))
tts: (quote self.stream()).into(),
})
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/mod.rs
Expand Up @@ -40,7 +40,7 @@ use syntax::ast::{Ident, Name, NodeId, DUMMY_NODE_ID, AsmDialect};
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem};
use syntax::ptr::P;
use syntax::symbol::{Symbol, keywords};
use syntax::tokenstream::TokenTree;
use syntax::tokenstream::TokenStream;
use syntax::util::ThinVec;

use std::collections::BTreeMap;
Expand Down Expand Up @@ -466,7 +466,7 @@ pub struct MacroDef {
pub attrs: HirVec<Attribute>,
pub id: NodeId,
pub span: Span,
pub body: HirVec<TokenTree>,
pub body: TokenStream,
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
Expand Down
16 changes: 5 additions & 11 deletions src/librustc_incremental/calculate_svh/svh_visitor.rs
Expand Up @@ -866,8 +866,8 @@ impl<'a, 'hash, 'tcx> visit::Visitor<'tcx> for StrictVersionHashVisitor<'a, 'has
debug!("visit_macro_def: st={:?}", self.st);
SawMacroDef.hash(self.st);
hash_attrs!(self, &macro_def.attrs);
for tt in &macro_def.body {
self.hash_token_tree(tt);
for tt in macro_def.body.trees() {
self.hash_token_tree(&tt);
}
visit::walk_macro_def(self, macro_def)
}
Expand Down Expand Up @@ -1033,15 +1033,9 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
}
tokenstream::TokenTree::Delimited(span, ref delimited) => {
hash_span!(self, span);
let tokenstream::Delimited {
ref delim,
ref tts,
} = **delimited;

delim.hash(self.st);
tts.len().hash(self.st);
for sub_tt in tts {
self.hash_token_tree(sub_tt);
delimited.delim.hash(self.st);
for sub_tt in delimited.stream().trees() {
self.hash_token_tree(&sub_tt);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_metadata/cstore_impl.rs
Expand Up @@ -34,7 +34,7 @@ use std::rc::Rc;

use syntax::ast;
use syntax::attr;
use syntax::parse::filemap_to_tts;
use syntax::parse::filemap_to_stream;
use syntax::symbol::Symbol;
use syntax_pos::{mk_sp, Span};
use rustc::hir::svh::Svh;
Expand Down Expand Up @@ -397,7 +397,7 @@ impl CrateStore for cstore::CStore {

let filemap = sess.parse_sess.codemap().new_filemap(source_name, None, def.body);
let local_span = mk_sp(filemap.start_pos, filemap.end_pos);
let body = filemap_to_tts(&sess.parse_sess, filemap);
let body = filemap_to_stream(&sess.parse_sess, filemap);

// Mark the attrs as used
let attrs = data.get_item_attrs(id.index);
Expand All @@ -415,7 +415,7 @@ impl CrateStore for cstore::CStore {
id: ast::DUMMY_NODE_ID,
span: local_span,
attrs: attrs,
body: body,
body: body.into(),
})
}

Expand Down
3 changes: 2 additions & 1 deletion src/librustc_metadata/encoder.rs
Expand Up @@ -853,9 +853,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {

/// Serialize the text of exported macros
fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef) -> Entry<'tcx> {
use syntax::print::pprust;
Entry {
kind: EntryKind::MacroDef(self.lazy(&MacroDef {
body: ::syntax::print::pprust::tts_to_string(&macro_def.body)
body: pprust::tts_to_string(&macro_def.body.trees().collect::<Vec<_>>()),
})),
visibility: self.lazy(&ty::Visibility::Public),
span: self.lazy(&macro_def.span),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -516,7 +516,7 @@ impl<'a> Resolver<'a> {
expansion: Cell::new(LegacyScope::Empty),
});
self.invocations.insert(mark, invocation);
macro_rules.body = mark_tts(&macro_rules.body, mark);
macro_rules.body = mark_tts(macro_rules.stream(), mark).into();
let ext = Rc::new(macro_rules::compile(&self.session.parse_sess, &macro_rules));
self.macro_map.insert(def_id, ext.clone());
ext
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_resolve/macros.rs
Expand Up @@ -545,7 +545,7 @@ impl<'a> Resolver<'a> {

pub fn define_macro(&mut self, item: &ast::Item, legacy_scope: &mut LegacyScope<'a>) {
let tts = match item.node {
ast::ItemKind::Mac(ref mac) => &mac.node.tts,
ast::ItemKind::Mac(ref mac) => mac.node.stream(),
_ => unreachable!(),
};

Expand All @@ -562,7 +562,7 @@ impl<'a> Resolver<'a> {
attrs: item.attrs.clone(),
id: ast::DUMMY_NODE_ID,
span: item.span,
body: mark_tts(tts, mark),
body: mark_tts(tts, mark).into(),
};

*legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(LegacyBinding {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_save_analysis/span_utils.rs
Expand Up @@ -284,7 +284,7 @@ impl<'a> SpanUtils<'a> {
pub fn signature_string_for_span(&self, span: Span) -> String {
let mut toks = self.retokenise_span(span);
toks.real_token();
let mut toks = toks.parse_all_token_trees().unwrap().into_iter();
let mut toks = toks.parse_all_token_trees().unwrap().trees();
let mut prev = toks.next().unwrap();

let first_span = prev.span();
Expand Down
6 changes: 4 additions & 2 deletions src/librustdoc/visit_ast.rs
Expand Up @@ -211,7 +211,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
};

// FIXME(jseyfried) merge with `self.visit_macro()`
let matchers = def.body.chunks(4).map(|arm| arm[0].span()).collect();
let tts = def.stream().trees().collect::<Vec<_>>();
let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect();
om.macros.push(Macro {
def_id: def_id,
attrs: def.attrs.clone().into(),
Expand Down Expand Up @@ -520,8 +521,9 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {

// convert each exported_macro into a doc item
fn visit_local_macro(&self, def: &hir::MacroDef) -> Macro {
let tts = def.body.trees().collect::<Vec<_>>();
// Extract the spans of all matchers. They represent the "interface" of the macro.
let matchers = def.body.chunks(4).map(|arm| arm[0].span()).collect();
let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect();

Macro {
def_id: self.cx.tcx.hir.local_def_id(def.id),
Expand Down
18 changes: 15 additions & 3 deletions src/libsyntax/ast.rs
Expand Up @@ -24,7 +24,7 @@ use ext::hygiene::SyntaxContext;
use print::pprust;
use ptr::P;
use symbol::{Symbol, keywords};
use tokenstream::{TokenTree};
use tokenstream::{ThinTokenStream, TokenStream};

use std::collections::HashSet;
use std::fmt;
Expand Down Expand Up @@ -1033,7 +1033,13 @@ pub type Mac = Spanned<Mac_>;
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct Mac_ {
pub path: Path,
pub tts: Vec<TokenTree>,
pub tts: ThinTokenStream,
}

impl Mac_ {
pub fn stream(&self) -> TokenStream {
self.tts.clone().into()
}
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
Expand Down Expand Up @@ -1915,7 +1921,13 @@ pub struct MacroDef {
pub attrs: Vec<Attribute>,
pub id: NodeId,
pub span: Span,
pub body: Vec<TokenTree>,
pub body: ThinTokenStream,
}

impl MacroDef {
pub fn stream(&self) -> TokenStream {
self.body.clone().into()
}
}

#[cfg(test)]
Expand Down
20 changes: 6 additions & 14 deletions src/libsyntax/ext/base.rs
Expand Up @@ -188,10 +188,7 @@ impl<F> AttrProcMacro for F

/// Represents a thing that maps token trees to Macro Results
pub trait TTMacroExpander {
fn expand<'cx>(&self,
ecx: &'cx mut ExtCtxt,
span: Span,
token_tree: &[tokenstream::TokenTree])
fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, span: Span, input: TokenStream)
-> Box<MacResult+'cx>;
}

Expand All @@ -200,15 +197,11 @@ pub type MacroExpanderFn =
-> Box<MacResult+'cx>;

impl<F> TTMacroExpander for F
where F : for<'cx> Fn(&'cx mut ExtCtxt, Span, &[tokenstream::TokenTree])
-> Box<MacResult+'cx>
where F: for<'cx> Fn(&'cx mut ExtCtxt, Span, &[tokenstream::TokenTree]) -> Box<MacResult+'cx>
{
fn expand<'cx>(&self,
ecx: &'cx mut ExtCtxt,
span: Span,
token_tree: &[tokenstream::TokenTree])
fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, span: Span, input: TokenStream)
-> Box<MacResult+'cx> {
(*self)(ecx, span, token_tree)
(*self)(ecx, span, &input.trees().collect::<Vec<_>>())
}
}

Expand Down Expand Up @@ -654,9 +647,8 @@ impl<'a> ExtCtxt<'a> {
expand::MacroExpander::new(self, true)
}

pub fn new_parser_from_tts(&self, tts: &[tokenstream::TokenTree])
-> parser::Parser<'a> {
parse::tts_to_parser(self.parse_sess, tts.to_vec())
pub fn new_parser_from_tts(&self, tts: &[tokenstream::TokenTree]) -> parser::Parser<'a> {
parse::stream_to_parser(self.parse_sess, tts.iter().cloned().collect())
}
pub fn codemap(&self) -> &'a CodeMap { self.parse_sess.codemap() }
pub fn parse_sess(&self) -> &'a parse::ParseSess { self.parse_sess }
Expand Down

0 comments on commit f6eaaf3

Please sign in to comment.