1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use ast::Ident;
use ext::base::ExtCtxt;
use ext::expand::Marker;
use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal};
use ext::tt::quoted;
use fold::noop_fold_tt;
use parse::token::{self, Token, NtTT};
use syntax_pos::{Span, DUMMY_SP};
use tokenstream::{TokenStream, TokenTree, Delimited};
use util::small_vector::SmallVector;
use std::rc::Rc;
use std::mem;
use std::ops::Add;
use std::collections::HashMap;
enum Frame {
Delimited {
forest: Rc<quoted::Delimited>,
idx: usize,
span: Span,
},
Sequence {
forest: Rc<quoted::SequenceRepetition>,
idx: usize,
sep: Option<Token>,
},
}
impl Frame {
fn new(tts: Vec<quoted::TokenTree>) -> Frame {
let forest = Rc::new(quoted::Delimited { delim: token::NoDelim, tts: tts });
Frame::Delimited { forest: forest, idx: 0, span: DUMMY_SP }
}
}
impl Iterator for Frame {
type Item = quoted::TokenTree;
fn next(&mut self) -> Option<quoted::TokenTree> {
match *self {
Frame::Delimited { ref forest, ref mut idx, .. } => {
*idx += 1;
forest.tts.get(*idx - 1).cloned()
}
Frame::Sequence { ref forest, ref mut idx, .. } => {
*idx += 1;
forest.tts.get(*idx - 1).cloned()
}
}
}
}
pub fn transcribe(cx: &ExtCtxt,
interp: Option<HashMap<Ident, Rc<NamedMatch>>>,
src: Vec<quoted::TokenTree>)
-> TokenStream {
let mut stack = SmallVector::one(Frame::new(src));
let interpolations = interp.unwrap_or_else(HashMap::new);
let mut repeats = Vec::new();
let mut result: Vec<TokenStream> = Vec::new();
let mut result_stack = Vec::new();
loop {
let tree = if let Some(tree) = stack.last_mut().unwrap().next() {
tree
} else {
if let Frame::Sequence { ref mut idx, ref sep, .. } = *stack.last_mut().unwrap() {
let (ref mut repeat_idx, repeat_len) = *repeats.last_mut().unwrap();
*repeat_idx += 1;
if *repeat_idx < repeat_len {
*idx = 0;
if let Some(sep) = sep.clone() {
let prev_span = match result.last() {
Some(stream) => stream.trees().next().unwrap().span(),
None => DUMMY_SP,
};
result.push(TokenTree::Token(prev_span, sep).into());
}
continue
}
}
match stack.pop().unwrap() {
Frame::Sequence { .. } => {
repeats.pop();
}
Frame::Delimited { forest, span, .. } => {
if result_stack.is_empty() {
return TokenStream::concat(result);
}
let tree = TokenTree::Delimited(span, Delimited {
delim: forest.delim,
tts: TokenStream::concat(result).into(),
});
result = result_stack.pop().unwrap();
result.push(tree.into());
}
}
continue
};
match tree {
quoted::TokenTree::Sequence(sp, seq) => {
match lockstep_iter_size("ed::TokenTree::Sequence(sp, seq.clone()),
&interpolations,
&repeats) {
LockstepIterSize::Unconstrained => {
cx.span_fatal(sp,
"attempted to repeat an expression \
containing no syntax \
variables matched as repeating at this depth");
}
LockstepIterSize::Contradiction(ref msg) => {
cx.span_fatal(sp, &msg[..]);
}
LockstepIterSize::Constraint(len, _) => {
if len == 0 {
if seq.op == quoted::KleeneOp::OneOrMore {
cx.span_fatal(sp, "this must repeat at least once");
}
} else {
repeats.push((0, len));
stack.push(Frame::Sequence {
idx: 0,
sep: seq.separator.clone(),
forest: seq,
});
}
}
}
}
quoted::TokenTree::MetaVar(mut sp, ident) => {
if let Some(cur_matched) = lookup_cur_matched(ident, &interpolations, &repeats) {
if let MatchedNonterminal(ref nt) = *cur_matched {
if let NtTT(ref tt) = **nt {
result.push(tt.clone().into());
} else {
sp = sp.with_ctxt(sp.ctxt().apply_mark(cx.current_expansion.mark));
let token = TokenTree::Token(sp, Token::interpolated((**nt).clone()));
result.push(token.into());
}
} else {
cx.span_fatal(sp,
&format!("variable '{}' is still repeating at this depth", ident));
}
} else {
let ident =
Ident { ctxt: ident.ctxt.apply_mark(cx.current_expansion.mark), ..ident };
sp = sp.with_ctxt(sp.ctxt().apply_mark(cx.current_expansion.mark));
result.push(TokenTree::Token(sp, token::Dollar).into());
result.push(TokenTree::Token(sp, token::Ident(ident)).into());
}
}
quoted::TokenTree::Delimited(mut span, delimited) => {
span = span.with_ctxt(span.ctxt().apply_mark(cx.current_expansion.mark));
stack.push(Frame::Delimited { forest: delimited, idx: 0, span: span });
result_stack.push(mem::replace(&mut result, Vec::new()));
}
quoted::TokenTree::Token(sp, tok) => {
let mut marker = Marker(cx.current_expansion.mark);
result.push(noop_fold_tt(TokenTree::Token(sp, tok), &mut marker).into())
}
quoted::TokenTree::MetaVarDecl(..) => panic!("unexpected `TokenTree::MetaVarDecl"),
}
}
}
fn lookup_cur_matched(ident: Ident,
interpolations: &HashMap<Ident, Rc<NamedMatch>>,
repeats: &[(usize, usize)])
-> Option<Rc<NamedMatch>> {
interpolations.get(&ident).map(|matched| {
let mut matched = matched.clone();
for &(idx, _) in repeats {
let m = matched.clone();
match *m {
MatchedNonterminal(_) => break,
MatchedSeq(ref ads, _) => matched = Rc::new(ads[idx].clone()),
}
}
matched
})
}
#[derive(Clone)]
enum LockstepIterSize {
Unconstrained,
Constraint(usize, Ident),
Contradiction(String),
}
impl Add for LockstepIterSize {
type Output = LockstepIterSize;
fn add(self, other: LockstepIterSize) -> LockstepIterSize {
match self {
LockstepIterSize::Unconstrained => other,
LockstepIterSize::Contradiction(_) => self,
LockstepIterSize::Constraint(l_len, ref l_id) => match other {
LockstepIterSize::Unconstrained => self.clone(),
LockstepIterSize::Contradiction(_) => other,
LockstepIterSize::Constraint(r_len, _) if l_len == r_len => self.clone(),
LockstepIterSize::Constraint(r_len, r_id) => {
let msg = format!("inconsistent lockstep iteration: \
'{}' has {} items, but '{}' has {}",
l_id, l_len, r_id, r_len);
LockstepIterSize::Contradiction(msg)
}
},
}
}
}
fn lockstep_iter_size(tree: "ed::TokenTree,
interpolations: &HashMap<Ident, Rc<NamedMatch>>,
repeats: &[(usize, usize)])
-> LockstepIterSize {
use self::quoted::TokenTree;
match *tree {
TokenTree::Delimited(_, ref delimed) => {
delimed.tts.iter().fold(LockstepIterSize::Unconstrained, |size, tt| {
size + lockstep_iter_size(tt, interpolations, repeats)
})
},
TokenTree::Sequence(_, ref seq) => {
seq.tts.iter().fold(LockstepIterSize::Unconstrained, |size, tt| {
size + lockstep_iter_size(tt, interpolations, repeats)
})
},
TokenTree::MetaVar(_, name) | TokenTree::MetaVarDecl(_, name, _) =>
match lookup_cur_matched(name, interpolations, repeats) {
Some(matched) => match *matched {
MatchedNonterminal(_) => LockstepIterSize::Unconstrained,
MatchedSeq(ref ads, _) => LockstepIterSize::Constraint(ads.len(), name),
},
_ => LockstepIterSize::Unconstrained
},
TokenTree::Token(..) => LockstepIterSize::Unconstrained,
}
}