Skip to content

Commit

Permalink
macros: Improve tt fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Jul 23, 2016
1 parent fd1d360 commit 41745f3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/libsyntax/ext/tt/transcribe.rs
Expand Up @@ -14,7 +14,7 @@ use syntax_pos::{Span, DUMMY_SP};
use errors::{Handler, DiagnosticBuilder};
use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal};
use parse::token::{DocComment, MatchNt, SubstNt};
use parse::token::{Token, NtIdent, SpecialMacroVar};
use parse::token::{Token, Interpolated, NtIdent, NtTT, SpecialMacroVar};
use parse::token;
use parse::lexer::TokenAndSpan;
use tokenstream::{self, TokenTree};
Expand Down Expand Up @@ -278,9 +278,9 @@ pub fn tt_next_token(r: &mut TtReader) -> TokenAndSpan {
}
// FIXME #2887: think about span stuff here
TokenTree::Token(sp, SubstNt(ident)) => {
r.stack.last_mut().unwrap().idx += 1;
match lookup_cur_matched(r, ident) {
None => {
r.stack.last_mut().unwrap().idx += 1;
r.cur_span = sp;
r.cur_tok = SubstNt(ident);
return ret_val;
Expand All @@ -292,14 +292,24 @@ pub fn tt_next_token(r: &mut TtReader) -> TokenAndSpan {
// (a) idents can be in lots of places, so it'd be a pain
// (b) we actually can, since it's a token.
MatchedNonterminal(NtIdent(ref sn)) => {
r.stack.last_mut().unwrap().idx += 1;
r.cur_span = sn.span;
r.cur_tok = token::Ident(sn.node);
return ret_val;
}
MatchedNonterminal(NtTT(ref tt)) => {
r.stack.push(TtFrame {
forest: TokenTree::Token(sp, Interpolated(NtTT(tt.clone()))),
idx: 0,
dotdotdoted: false,
sep: None,
});
}
MatchedNonterminal(ref other_whole_nt) => {
r.stack.last_mut().unwrap().idx += 1;
// FIXME(pcwalton): Bad copy.
r.cur_span = sp;
r.cur_tok = token::Interpolated((*other_whole_nt).clone());
r.cur_tok = Interpolated((*other_whole_nt).clone());
return ret_val;
}
MatchedSeq(..) => {
Expand Down
4 changes: 4 additions & 0 deletions src/libsyntax/tokenstream.rs
Expand Up @@ -135,6 +135,7 @@ impl TokenTree {
}
TokenTree::Token(_, token::SpecialVarNt(..)) => 2,
TokenTree::Token(_, token::MatchNt(..)) => 3,
TokenTree::Token(_, token::Interpolated(Nonterminal::NtTT(..))) => 1,
TokenTree::Delimited(_, ref delimed) => delimed.tts.len() + 2,
TokenTree::Sequence(_, ref seq) => seq.tts.len(),
TokenTree::Token(..) => 0,
Expand Down Expand Up @@ -197,6 +198,9 @@ impl TokenTree {
TokenTree::Token(sp, token::Ident(kind))];
v[index].clone()
}
(&TokenTree::Token(_, token::Interpolated(Nonterminal::NtTT(ref tt))), _) => {
tt.clone().unwrap()
}
(&TokenTree::Sequence(_, ref seq), _) => seq.tts[index].clone(),
_ => panic!("Cannot expand a token tree"),
}
Expand Down

0 comments on commit 41745f3

Please sign in to comment.