1
Fork 0

Avoid interpolated token trees.

This commit is contained in:
Jeffrey Seyfried 2017-01-14 12:42:00 +00:00
parent 6a9248fc15
commit 57c0ed097c
3 changed files with 9 additions and 37 deletions

View file

@ -480,23 +480,8 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
match name { match name {
"tt" => { "tt" => {
p.quote_depth += 1; //but in theory, non-quoted tts might be useful p.quote_depth += 1; //but in theory, non-quoted tts might be useful
let mut tt = panictry!(p.parse_token_tree()); let tt = panictry!(p.parse_token_tree());
p.quote_depth -= 1; p.quote_depth -= 1;
while let TokenTree::Token(sp, token::Interpolated(nt)) = tt {
if let token::NtTT(..) = *nt {
match Rc::try_unwrap(nt) {
Ok(token::NtTT(sub_tt)) => tt = sub_tt,
Ok(_) => unreachable!(),
Err(nt_rc) => match *nt_rc {
token::NtTT(ref sub_tt) => tt = sub_tt.clone(),
_ => unreachable!(),
},
}
} else {
tt = TokenTree::Token(sp, token::Interpolated(nt.clone()));
break
}
}
return token::NtTT(tt); return token::NtTT(tt);
} }
_ => {} _ => {}

View file

@ -12,7 +12,7 @@ use self::LockstepIterSize::*;
use ast::Ident; use ast::Ident;
use errors::Handler; use errors::Handler;
use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal}; use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal};
use parse::token::{self, MatchNt, SubstNt, Token, NtIdent}; use parse::token::{self, MatchNt, SubstNt, Token, NtIdent, NtTT};
use syntax_pos::{Span, DUMMY_SP}; use syntax_pos::{Span, DUMMY_SP};
use tokenstream::{self, TokenTree}; use tokenstream::{self, TokenTree};
use util::small_vector::SmallVector; use util::small_vector::SmallVector;
@ -241,6 +241,7 @@ fn tt_next_token(r: &mut TtReader, prev_span: Span) -> Option<TokenTree> {
NtIdent(ref sn) => { NtIdent(ref sn) => {
return Some(TokenTree::Token(sn.span, token::Ident(sn.node))); return Some(TokenTree::Token(sn.span, token::Ident(sn.node)));
} }
NtTT(ref tt) => return Some(tt.clone()),
_ => { _ => {
// FIXME(pcwalton): Bad copy // FIXME(pcwalton): Bad copy
return Some(TokenTree::Token(sp, token::Interpolated(nt.clone()))); return Some(TokenTree::Token(sp, token::Interpolated(nt.clone())));

View file

@ -306,8 +306,8 @@ impl<'a> Parser<'a> {
} }
fn next_tok(&mut self) -> TokenAndSpan { fn next_tok(&mut self) -> TokenAndSpan {
'outer: loop { loop {
let mut tok = if let Some((tts, i)) = self.tts.pop() { let tok = if let Some((tts, i)) = self.tts.pop() {
let tt = tts.get_tt(i); let tt = tts.get_tt(i);
if i + 1 < tts.len() { if i + 1 < tts.len() {
self.tts.push((tts, i + 1)); self.tts.push((tts, i + 1));
@ -322,25 +322,11 @@ impl<'a> Parser<'a> {
TokenAndSpan { tok: token::Eof, sp: self.span } TokenAndSpan { tok: token::Eof, sp: self.span }
}; };
loop { match tok.tok {
let nt = match tok.tok {
token::Interpolated(ref nt) => nt.clone(),
token::DocComment(name) if self.desugar_doc_comments => { token::DocComment(name) if self.desugar_doc_comments => {
self.tts.push((TokenTree::Token(tok.sp, token::DocComment(name)), 0)); self.tts.push((TokenTree::Token(tok.sp, token::DocComment(name)), 0));
continue 'outer
} }
_ => return tok, _ => return tok,
};
match *nt {
token::NtTT(TokenTree::Token(sp, ref t)) => {
tok = TokenAndSpan { tok: t.clone(), sp: sp };
}
token::NtTT(ref tt) => {
self.tts.push((tt.clone(), 0));
continue 'outer
}
_ => return tok,
}
} }
} }
} }