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