Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
dc3d878
Add support for macro expansion inside trait items
Jun 11, 2016
01a0877
Remove inherent method `attrs()` on AST nodes. `attrs()` is now a met…
jseyfried May 24, 2016
febe6a4
Refactor away field `ctxt` of `ast::Mac_`
jseyfried May 24, 2016
5bf7970
Change `fold_tt` and `fold_tts` to take token trees by value (instead…
jseyfried Jun 3, 2016
683e480
Refactor away `WithAttrs` trait
jseyfried Jun 10, 2016
0644aba
Remove the type parameter from `syntax::visit::Visitor`
jseyfried Jun 12, 2016
b5dbe01
Refactor away `ast::Decl` and refactor `ast::Stmt`
jseyfried Jun 17, 2016
962d5c1
Fix fallout
jseyfried Jun 17, 2016
f0b21c2
Rename `ast::ExprKind::Again` -> `ast::ExprKind::Continue`
jseyfried Jun 17, 2016
5033eca
Generalize and abstract `ThinAttributes`
jseyfried Jun 18, 2016
f903c97
Merge PatKind::QPath into PatKind::Path in AST
petrochenkov Jun 11, 2016
d59accf
Refactored tokentrees into their own files in preparation for tokenst…
Jun 20, 2016
f6fe5b6
Cleanup comments
jseyfried Jun 23, 2016
6ae3502
Move errors from libsyntax to its own crate
Jun 21, 2016
2829fbc
Address comments and fix travis warning
Jun 21, 2016
51deb4f
Address more travis errors
Jun 22, 2016
f2fe204
Consolidate codemap tests and fix more errors for travis
Jun 22, 2016
bad4869
Fix touchy test to work with old and new error format
Jun 22, 2016
b68e079
Actually consolidate the CodeMap tests
Jun 23, 2016
2b8bab0
Move test helper functions to consolidated codemap testing
Jun 23, 2016
d4e79de
Add missing refernce to RenderedLine
Jun 23, 2016
b7da35a
Remove field `expr` of `ast::Block`
jseyfried Jun 23, 2016
a48a4f5
Avoid wasting node ids
jseyfried Jun 23, 2016
94479ad
Add regression test
jseyfried Jun 23, 2016
80f1c78
make old school mode a bit more configurable
Jun 23, 2016
bdd3f8f
Updating test I missed for old school refactor
Jun 23, 2016
ea7ba12
Add missing 'extern crate' for test
Jun 23, 2016
060a84d
Refactor away duplicate method `ecx.block_all()`
jseyfried Jun 23, 2016
f960f9e
Fix up rpass tests missing imports
Jun 24, 2016
89da728
Fix rfail test missing import
Jun 24, 2016
8cad251
Add `ecx.stmt_semi()` and fix issues with the pretty-printer
jseyfried Jun 24, 2016
f5259ab
Reexport syntax_pos in codemap and fix some cfail tests
Jun 24, 2016
9f2a507
Fix codemap tests to not double import
Jun 24, 2016
bc14006
reexport errors from syntax. fix failing cfail test
Jun 24, 2016
f0310e0
Rollup merge of #34213 - josephDunne:trait_item_macros, r=jseyfried
jseyfried Jun 25, 2016
4e2e31c
Rollup merge of #34368 - petrochenkov:astqpath, r=Manishearth
jseyfried Jun 25, 2016
d3ae56d
Rollup merge of #34403 - jonathandturner:move_liberror, r=alexcrichton
jseyfried Jun 25, 2016
82a15a6
Rollup merge of #34385 - cgswords:tstream, r=nrc
jseyfried Jun 25, 2016
33ea1e3
Rollup merge of #33943 - jseyfried:libsyntax_cleanup, r=nrc
jseyfried Jun 26, 2016
8748cd9
Rollup merge of #34316 - jseyfried:refactor_ast_stmt, r=eddyb
jseyfried Jun 26, 2016
8eddf02
Rollup merge of #34339 - jseyfried:thin_vec, r=petrochenkov,Manishearth
jseyfried Jun 26, 2016
9bb3ea0
Rollup merge of #34436 - jseyfried:no_block_expr, r=eddyb
jseyfried Jun 26, 2016
542ba8c
Fix `Cargo.toml`s
jseyfried Jun 27, 2016
360dcae
Update `src/rustc/Cargo.lock`
jseyfried Jun 27, 2016
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactored tokentrees into their own files in preparation for tokenst…
…reams. Modified tests to point to the new file now.
  • Loading branch information
cgswords authored and cgswords committed Jun 21, 2016
commit d59accfb065843d12db9180a4f504664e3d23ef1
3 changes: 2 additions & 1 deletion src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ use util::nodemap::{NodeMap, FnvHashSet};

use syntax::codemap::{self, mk_sp, respan, Span, Spanned, ExpnId};
use syntax::abi::Abi;
use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, TokenTree, AsmDialect};
use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, AsmDialect};
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem};
use syntax::attr::{ThinAttributes, ThinAttributesExt};
use syntax::parse::token::{keywords, InternedString};
use syntax::ptr::P;
use syntax::tokenstream::TokenTree;

use std::collections::BTreeMap;
use std::fmt;
Expand Down
192 changes: 1 addition & 191 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ use attr::{ThinAttributes, HasAttrs};
use codemap::{mk_sp, respan, Span, Spanned, DUMMY_SP, ExpnId};
use abi::Abi;
use errors;
use ext::base;
use ext::tt::macro_parser;
use parse::token::{self, keywords, InternedString};
use parse::lexer;
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
use print::pprust;
use ptr::P;
use tokenstream::{TokenTree};

use std::fmt;
use std::rc::Rc;
Expand Down Expand Up @@ -1097,193 +1094,6 @@ pub enum CaptureBy {
Ref,
}

/// 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 span covering the opening delimiter
pub open_span: Span,
/// The delimited sequence of token trees
pub tts: Vec<TokenTree>,
/// The span covering the closing delimiter
pub close_span: Span,
}

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.
pub fn open_tt(&self) -> TokenTree {
TokenTree::Token(self.open_span, self.open_token())
}

/// Returns the closing delimiter as a token tree.
pub fn close_tt(&self) -> TokenTree {
TokenTree::Token(self.close_span, self.close_token())
}
}

/// A sequence of token trees
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct SequenceRepetition {
/// The sequence of token trees
pub tts: Vec<TokenTree>,
/// The optional separator
pub separator: Option<token::Token>,
/// Whether the sequence can be repeated zero (*), or one or more times (+)
pub op: KleeneOp,
/// The number of `MatchNt`s that appear in the sequence (and subsequences)
pub num_captures: usize,
}

/// A Kleene-style [repetition operator](http://en.wikipedia.org/wiki/Kleene_star)
/// for token sequences.
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
pub enum KleeneOp {
ZeroOrMore,
OneOrMore,
}

/// 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.
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum TokenTree {
/// A single token
Token(Span, token::Token),
/// A delimited sequence of token trees
Delimited(Span, Rc<Delimited>),

// This only makes sense in MBE macros.

/// A kleene-style repetition sequence with a span
// FIXME(eddyb) #12938 Use DST.
Sequence(Span, Rc<SequenceRepetition>),
}

impl TokenTree {
pub fn len(&self) -> usize {
match *self {
TokenTree::Token(_, token::DocComment(name)) => {
match doc_comment_style(&name.as_str()) {
AttrStyle::Outer => 2,
AttrStyle::Inner => 3
}
}
TokenTree::Token(_, token::SpecialVarNt(..)) => 2,
TokenTree::Token(_, token::MatchNt(..)) => 3,
TokenTree::Delimited(_, ref delimed) => {
delimed.tts.len() + 2
}
TokenTree::Sequence(_, ref seq) => {
seq.tts.len()
}
TokenTree::Token(..) => 0
}
}

pub fn get_tt(&self, index: usize) -> TokenTree {
match (self, index) {
(&TokenTree::Token(sp, token::DocComment(_)), 0) => {
TokenTree::Token(sp, token::Pound)
}
(&TokenTree::Token(sp, token::DocComment(name)), 1)
if doc_comment_style(&name.as_str()) == AttrStyle::Inner => {
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.
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);

TokenTree::Delimited(sp, Rc::new(Delimited {
delim: token::Bracket,
open_span: sp,
tts: vec![TokenTree::Token(sp, token::Ident(token::str_to_ident("doc"))),
TokenTree::Token(sp, token::Eq),
TokenTree::Token(sp, token::Literal(
token::StrRaw(token::intern(&stripped), num_of_hashes), None))],
close_span: sp,
}))
}
(&TokenTree::Delimited(_, ref delimed), _) => {
if index == 0 {
return delimed.open_tt();
}
if index == delimed.tts.len() + 1 {
return delimed.close_tt();
}
delimed.tts[index - 1].clone()
}
(&TokenTree::Token(sp, token::SpecialVarNt(var)), _) => {
let v = [TokenTree::Token(sp, token::Dollar),
TokenTree::Token(sp, token::Ident(token::str_to_ident(var.as_str())))];
v[index].clone()
}
(&TokenTree::Token(sp, token::MatchNt(name, kind)), _) => {
let v = [TokenTree::Token(sp, token::SubstNt(name)),
TokenTree::Token(sp, token::Colon),
TokenTree::Token(sp, token::Ident(kind))];
v[index].clone()
}
(&TokenTree::Sequence(_, ref seq), _) => {
seq.tts[index].clone()
}
_ => panic!("Cannot expand a token tree")
}
}

/// Returns the `Span` corresponding to this token tree.
pub fn get_span(&self) -> Span {
match *self {
TokenTree::Token(span, _) => span,
TokenTree::Delimited(span, _) => span,
TokenTree::Sequence(span, _) => span,
}
}

/// Use this token tree as a matcher to parse given tts.
pub fn parse(cx: &base::ExtCtxt, mtch: &[TokenTree], tts: &[TokenTree])
-> macro_parser::NamedParseResult {
// `None` is because we're not interpolating
let arg_rdr = lexer::new_tt_reader_with_doc_flag(&cx.parse_sess().span_diagnostic,
None,
None,
tts.iter().cloned().collect(),
true);
macro_parser::parse(cx.parse_sess(), cx.cfg(), arg_rdr, mtch)
}
}

pub type Mac = Spanned<Mac_>;

/// Represents a macro invocation. The Path indicates which macro
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/diagnostics/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ use std::collections::BTreeMap;
use std::env;

use ast;
use ast::{Ident, Name, TokenTree};
use ast::{Ident, Name};
use codemap::Span;
use ext::base::{ExtCtxt, MacEager, MacResult};
use ext::build::AstBuilder;
use parse::token;
use ptr::P;
use tokenstream::{TokenTree};
use util::small_vector::SmallVector;

use diagnostics::metadata::output_metadata;
Expand Down
28 changes: 16 additions & 12 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use fold::Folder;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
use std::default::Default;
use tokenstream;


#[derive(Debug,Clone)]
Expand Down Expand Up @@ -163,20 +164,22 @@ pub trait TTMacroExpander {
fn expand<'cx>(&self,
ecx: &'cx mut ExtCtxt,
span: Span,
token_tree: &[ast::TokenTree])
token_tree: &[tokenstream::TokenTree])
-> Box<MacResult+'cx>;
}

pub type MacroExpanderFn =
for<'cx> fn(&'cx mut ExtCtxt, Span, &[ast::TokenTree]) -> Box<MacResult+'cx>;
for<'cx> fn(&'cx mut ExtCtxt, Span, &[tokenstream::TokenTree])
-> Box<MacResult+'cx>;

impl<F> TTMacroExpander for F
where F : for<'cx> Fn(&'cx mut ExtCtxt, Span, &[ast::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: &[ast::TokenTree])
token_tree: &[tokenstream::TokenTree])
-> Box<MacResult+'cx> {
(*self)(ecx, span, token_tree)
}
Expand All @@ -187,22 +190,23 @@ pub trait IdentMacroExpander {
cx: &'cx mut ExtCtxt,
sp: Span,
ident: ast::Ident,
token_tree: Vec<ast::TokenTree> )
token_tree: Vec<tokenstream::TokenTree> )
-> Box<MacResult+'cx>;
}

pub type IdentMacroExpanderFn =
for<'cx> fn(&'cx mut ExtCtxt, Span, ast::Ident, Vec<ast::TokenTree>) -> Box<MacResult+'cx>;
for<'cx> fn(&'cx mut ExtCtxt, Span, ast::Ident, Vec<tokenstream::TokenTree>)
-> Box<MacResult+'cx>;

impl<F> IdentMacroExpander for F
where F : for<'cx> Fn(&'cx mut ExtCtxt, Span, ast::Ident,
Vec<ast::TokenTree>) -> Box<MacResult+'cx>
Vec<tokenstream::TokenTree>) -> Box<MacResult+'cx>
{
fn expand<'cx>(&self,
cx: &'cx mut ExtCtxt,
sp: Span,
ident: ast::Ident,
token_tree: Vec<ast::TokenTree> )
token_tree: Vec<tokenstream::TokenTree> )
-> Box<MacResult+'cx>
{
(*self)(cx, sp, ident, token_tree)
Expand Down Expand Up @@ -607,7 +611,7 @@ impl<'a> ExtCtxt<'a> {
expand::MacroExpander::new(self)
}

pub fn new_parser_from_tts(&self, tts: &[ast::TokenTree])
pub fn new_parser_from_tts(&self, tts: &[tokenstream::TokenTree])
-> parser::Parser<'a> {
parse::tts_to_parser(self.parse_sess, tts.to_vec(), self.cfg())
}
Expand Down Expand Up @@ -806,7 +810,7 @@ pub fn expr_to_string(cx: &mut ExtCtxt, expr: P<ast::Expr>, err_msg: &str)
/// done as rarely as possible).
pub fn check_zero_tts(cx: &ExtCtxt,
sp: Span,
tts: &[ast::TokenTree],
tts: &[tokenstream::TokenTree],
name: &str) {
if !tts.is_empty() {
cx.span_err(sp, &format!("{} takes no arguments", name));
Expand All @@ -817,7 +821,7 @@ pub fn check_zero_tts(cx: &ExtCtxt,
/// is not a string literal, emit an error and return None.
pub fn get_single_str_from_tts(cx: &mut ExtCtxt,
sp: Span,
tts: &[ast::TokenTree],
tts: &[tokenstream::TokenTree],
name: &str)
-> Option<String> {
let mut p = cx.new_parser_from_tts(tts);
Expand All @@ -838,7 +842,7 @@ pub fn get_single_str_from_tts(cx: &mut ExtCtxt,
/// parsing error, emit a non-fatal error and return None.
pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
sp: Span,
tts: &[ast::TokenTree]) -> Option<Vec<P<ast::Expr>>> {
tts: &[tokenstream::TokenTree]) -> Option<Vec<P<ast::Expr>>> {
let mut p = cx.new_parser_from_tts(tts);
let mut es = Vec::new();
while p.token != token::Eof {
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use ast::{Block, Crate, DeclKind, PatKind};
use ast::{Local, Ident, Mac_, Name, SpannedIdent};
use ast::{MacStmtStyle, Mrk, Stmt, StmtKind, ItemKind};
use ast::TokenTree;
use ast;
use ext::mtwt;
use ext::build::AstBuilder;
Expand All @@ -27,6 +26,7 @@ use fold::*;
use util::move_map::MoveMap;
use parse::token::{fresh_mark, fresh_name, intern, keywords};
use ptr::P;
use tokenstream::TokenTree;
use util::small_vector::SmallVector;
use visit;
use visit::Visitor;
Expand Down
Loading