Skip to content

Commit 2471888

Browse files
committed
Avoid Token::{OpenDelim, CloseDelim}.
1 parent 8c4960b commit 2471888

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/libsyntax/ext/tt/transcribe.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use tokenstream::{self, TokenTree, Delimited, SequenceRepetition};
1717
use util::small_vector::SmallVector;
1818

1919
use std::rc::Rc;
20+
use std::mem;
2021
use std::ops::Add;
2122
use std::collections::HashMap;
2223

@@ -52,17 +53,9 @@ impl Iterator for Frame {
5253

5354
fn next(&mut self) -> Option<TokenTree> {
5455
match *self {
55-
Frame::Delimited { ref forest, ref mut idx, span } => {
56+
Frame::Delimited { ref forest, ref mut idx, .. } => {
5657
*idx += 1;
57-
if *idx == forest.delim.len() {
58-
Some(forest.open_tt(span))
59-
} else if let Some(tree) = forest.tts.get(*idx - forest.delim.len() - 1) {
60-
Some(tree.clone())
61-
} else if *idx == forest.tts.len() + 2 * forest.delim.len() {
62-
Some(forest.close_tt(span))
63-
} else {
64-
None
65-
}
58+
forest.tts.get(*idx - 1).cloned()
6659
}
6760
Frame::Sequence { ref forest, ref mut idx, .. } => {
6861
*idx += 1;
@@ -93,6 +86,7 @@ pub fn transcribe(sp_diag: &Handler,
9386
let mut repeat_idx = Vec::new();
9487
let mut repeat_len = Vec::new();
9588
let mut result = Vec::new();
89+
let mut result_stack = Vec::new();
9690

9791
loop {
9892
let tree = if let Some(tree) = stack.last_mut().unwrap().next() {
@@ -111,12 +105,23 @@ pub fn transcribe(sp_diag: &Handler,
111105
}
112106
}
113107

114-
if let Frame::Sequence { .. } = stack.pop().unwrap() {
115-
repeat_idx.pop();
116-
repeat_len.pop();
117-
}
118-
if stack.is_empty() {
119-
return result;
108+
match stack.pop().unwrap() {
109+
Frame::Sequence { .. } => {
110+
repeat_idx.pop();
111+
repeat_len.pop();
112+
}
113+
Frame::Delimited { forest, span, .. } => {
114+
if result_stack.is_empty() {
115+
return result;
116+
}
117+
let tree = TokenTree::Delimited(span, Rc::new(Delimited {
118+
delim: forest.delim,
119+
tts: result,
120+
}));
121+
result = result_stack.pop().unwrap();
122+
result.push(tree);
123+
}
124+
_ => {}
120125
}
121126
continue
122127
};
@@ -184,6 +189,7 @@ pub fn transcribe(sp_diag: &Handler,
184189
}
185190
TokenTree::Delimited(span, delimited) => {
186191
stack.push(Frame::Delimited { forest: delimited, idx: 0, span: span });
192+
result_stack.push(mem::replace(&mut result, Vec::new()));
187193
}
188194
TokenTree::Token(span, MatchNt(name, kind)) => {
189195
stack.push(Frame::MatchNt { name: name, kind: kind, idx: 0, span: span });

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2694,7 +2694,7 @@ impl<'a> Parser<'a> {
26942694
// yet.
26952695
match self.token {
26962696
token::OpenDelim(delim) => {
2697-
if self.quote_depth == 0 && self.tts.last().map(|&(_, i)| i == 1).unwrap_or(false) {
2697+
if self.quote_depth == 0 {
26982698
let tt = self.tts.pop().unwrap().0;
26992699
self.bump();
27002700
return Ok(tt);

0 commit comments

Comments
 (0)