Skip to content

Commit e2b3fec

Browse files
committed
Avoid recontructing the Parser in macro_parser.rs.
1 parent b60bcba commit e2b3fec

File tree

2 files changed

+16
-42
lines changed

2 files changed

+16
-42
lines changed

src/libsyntax/ext/tt/macro_parser.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -278,17 +278,16 @@ pub fn token_name_eq(t1 : &Token, t2 : &Token) -> bool {
278278
}
279279
}
280280

281-
pub fn parse(sess: &ParseSess, mut rdr: TtReader, ms: &[TokenTree]) -> NamedParseResult {
282-
let mut cur_eis = SmallVector::one(initial_matcher_pos(ms.to_owned(),
283-
None,
284-
rdr.peek().sp.lo));
281+
pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseResult {
282+
let mut parser = Parser::new(sess, Box::new(rdr));
283+
let mut cur_eis = SmallVector::one(initial_matcher_pos(ms.to_owned(), None, parser.span.lo));
285284

286285
loop {
287286
let mut bb_eis = Vec::new(); // black-box parsed by parser.rs
288287
let mut next_eis = Vec::new(); // or proceed normally
289288
let mut eof_eis = Vec::new();
290289

291-
let TokenAndSpan { tok, sp } = rdr.peek();
290+
let (sp, tok) = (parser.span, parser.token.clone());
292291

293292
/* we append new items to this while we go */
294293
loop {
@@ -473,23 +472,19 @@ pub fn parse(sess: &ParseSess, mut rdr: TtReader, ms: &[TokenTree]) -> NamedPars
473472
while !next_eis.is_empty() {
474473
cur_eis.push(next_eis.pop().unwrap());
475474
}
476-
rdr.next_token();
475+
parser.bump();
477476
} else /* bb_eis.len() == 1 */ {
478-
rdr.next_tok = {
479-
let mut rust_parser = Parser::new(sess, Box::new(&mut rdr));
480-
let mut ei = bb_eis.pop().unwrap();
481-
if let TokenTree::Token(span, MatchNt(_, ident)) = ei.top_elts.get_tt(ei.idx) {
482-
let match_cur = ei.match_cur;
483-
(&mut ei.matches[match_cur]).push(Rc::new(MatchedNonterminal(
484-
Rc::new(parse_nt(&mut rust_parser, span, &ident.name.as_str())))));
485-
ei.idx += 1;
486-
ei.match_cur += 1;
487-
} else {
488-
unreachable!()
489-
}
490-
cur_eis.push(ei);
491-
Some(TokenAndSpan { tok: rust_parser.token, sp: rust_parser.span })
492-
};
477+
let mut ei = bb_eis.pop().unwrap();
478+
if let TokenTree::Token(span, MatchNt(_, ident)) = ei.top_elts.get_tt(ei.idx) {
479+
let match_cur = ei.match_cur;
480+
(&mut ei.matches[match_cur]).push(Rc::new(MatchedNonterminal(
481+
Rc::new(parse_nt(&mut parser, span, &ident.name.as_str())))));
482+
ei.idx += 1;
483+
ei.match_cur += 1;
484+
} else {
485+
unreachable!()
486+
}
487+
cur_eis.push(ei);
493488
}
494489
}
495490

src/libsyntax/parse/lexer/mod.rs

-21
Original file line numberDiff line numberDiff line change
@@ -178,27 +178,6 @@ impl<'a> Reader for TtReader<'a> {
178178
}
179179
}
180180

181-
impl<'a, 'b> Reader for &'b mut TtReader<'a> {
182-
fn is_eof(&self) -> bool {
183-
(**self).is_eof()
184-
}
185-
fn try_next_token(&mut self) -> Result<TokenAndSpan, ()> {
186-
(**self).try_next_token()
187-
}
188-
fn fatal(&self, m: &str) -> FatalError {
189-
(**self).fatal(m)
190-
}
191-
fn err(&self, m: &str) {
192-
(**self).err(m)
193-
}
194-
fn emit_fatal_errors(&mut self) {
195-
(**self).emit_fatal_errors()
196-
}
197-
fn peek(&self) -> TokenAndSpan {
198-
(**self).peek()
199-
}
200-
}
201-
202181
impl<'a> StringReader<'a> {
203182
/// For comments.rs, which hackily pokes into next_pos and ch
204183
pub fn new_raw<'b>(span_diagnostic: &'b Handler,

0 commit comments

Comments
 (0)