]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/tt/transcribe.rs
Merge pull request #20510 from tshepang/patch-6
[rust.git] / src / libsyntax / ext / tt / transcribe.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 use self::LockstepIterSize::*;
11
12 use ast;
13 use ast::{TokenTree, TtDelimited, TtToken, TtSequence, Ident};
14 use codemap::{Span, DUMMY_SP};
15 use diagnostic::SpanHandler;
16 use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal};
17 use parse::token::{Eof, DocComment, Interpolated, MatchNt, SubstNt};
18 use parse::token::{Token, NtIdent};
19 use parse::token;
20 use parse::lexer::TokenAndSpan;
21
22 use std::rc::Rc;
23 use std::ops::Add;
24 use std::collections::HashMap;
25
26 ///an unzipping of `TokenTree`s
27 #[derive(Clone)]
28 struct TtFrame {
29     forest: TokenTree,
30     idx: uint,
31     dotdotdoted: bool,
32     sep: Option<Token>,
33 }
34
35 #[derive(Clone)]
36 pub struct TtReader<'a> {
37     pub sp_diag: &'a SpanHandler,
38     /// the unzipped tree:
39     stack: Vec<TtFrame>,
40     /* for MBE-style macro transcription */
41     interpolations: HashMap<Ident, Rc<NamedMatch>>,
42     repeat_idx: Vec<uint>,
43     repeat_len: Vec<uint>,
44     /* cached: */
45     pub cur_tok: Token,
46     pub cur_span: Span,
47     /// Transform doc comments. Only useful in macro invocations
48     pub desugar_doc_comments: bool,
49 }
50
51 /// This can do Macro-By-Example transcription. On the other hand, if
52 /// `src` contains no `TtSequence`s and `TtNonterminal`s, `interp` can (and
53 /// should) be none.
54 pub fn new_tt_reader<'a>(sp_diag: &'a SpanHandler,
55                          interp: Option<HashMap<Ident, Rc<NamedMatch>>>,
56                          src: Vec<ast::TokenTree> )
57                          -> TtReader<'a> {
58     let mut r = TtReader {
59         sp_diag: sp_diag,
60         stack: vec!(TtFrame {
61             forest: TtSequence(DUMMY_SP, Rc::new(ast::SequenceRepetition {
62                 tts: src,
63                 // doesn't matter. This merely holds the root unzipping.
64                 separator: None, op: ast::ZeroOrMore, num_captures: 0
65             })),
66             idx: 0,
67             dotdotdoted: false,
68             sep: None,
69         }),
70         interpolations: match interp { /* just a convenience */
71             None => HashMap::new(),
72             Some(x) => x,
73         },
74         repeat_idx: Vec::new(),
75         repeat_len: Vec::new(),
76         desugar_doc_comments: false,
77         /* dummy values, never read: */
78         cur_tok: token::Eof,
79         cur_span: DUMMY_SP,
80     };
81     tt_next_token(&mut r); /* get cur_tok and cur_span set up */
82     r
83 }
84
85 fn lookup_cur_matched_by_matched(r: &TtReader, start: Rc<NamedMatch>) -> Rc<NamedMatch> {
86     r.repeat_idx.iter().fold(start, |ad, idx| {
87         match *ad {
88             MatchedNonterminal(_) => {
89                 // end of the line; duplicate henceforth
90                 ad.clone()
91             }
92             MatchedSeq(ref ads, _) => ads[*idx].clone()
93         }
94     })
95 }
96
97 fn lookup_cur_matched(r: &TtReader, name: Ident) -> Option<Rc<NamedMatch>> {
98     let matched_opt = r.interpolations.get(&name).cloned();
99     matched_opt.map(|s| lookup_cur_matched_by_matched(r, s))
100 }
101
102 #[derive(Clone)]
103 enum LockstepIterSize {
104     LisUnconstrained,
105     LisConstraint(uint, Ident),
106     LisContradiction(String),
107 }
108
109 impl Add for LockstepIterSize {
110     type Output = LockstepIterSize;
111
112     fn add(self, other: LockstepIterSize) -> LockstepIterSize {
113         match self {
114             LisUnconstrained => other,
115             LisContradiction(_) => self,
116             LisConstraint(l_len, ref l_id) => match other {
117                 LisUnconstrained => self.clone(),
118                 LisContradiction(_) => other,
119                 LisConstraint(r_len, _) if l_len == r_len => self.clone(),
120                 LisConstraint(r_len, r_id) => {
121                     let l_n = token::get_ident(l_id.clone());
122                     let r_n = token::get_ident(r_id);
123                     LisContradiction(format!("inconsistent lockstep iteration: \
124                                               '{}' has {} items, but '{}' has {}",
125                                               l_n, l_len, r_n, r_len).to_string())
126                 }
127             },
128         }
129     }
130 }
131
132 fn lockstep_iter_size(t: &TokenTree, r: &TtReader) -> LockstepIterSize {
133     match *t {
134         TtDelimited(_, ref delimed) => {
135             delimed.tts.iter().fold(LisUnconstrained, |size, tt| {
136                 size + lockstep_iter_size(tt, r)
137             })
138         },
139         TtSequence(_, ref seq) => {
140             seq.tts.iter().fold(LisUnconstrained, |size, tt| {
141                 size + lockstep_iter_size(tt, r)
142             })
143         },
144         TtToken(_, SubstNt(name, _)) | TtToken(_, MatchNt(name, _, _, _)) =>
145             match lookup_cur_matched(r, name) {
146                 Some(matched) => match *matched {
147                     MatchedNonterminal(_) => LisUnconstrained,
148                     MatchedSeq(ref ads, _) => LisConstraint(ads.len(), name),
149                 },
150                 _ => LisUnconstrained
151             },
152         TtToken(..) => LisUnconstrained,
153     }
154 }
155
156 /// Return the next token from the TtReader.
157 /// EFFECT: advances the reader's token field
158 pub fn tt_next_token(r: &mut TtReader) -> TokenAndSpan {
159     // FIXME(pcwalton): Bad copy?
160     let ret_val = TokenAndSpan {
161         tok: r.cur_tok.clone(),
162         sp: r.cur_span.clone(),
163     };
164     loop {
165         let should_pop = match r.stack.last() {
166             None => {
167                 assert_eq!(ret_val.tok, token::Eof);
168                 return ret_val;
169             }
170             Some(frame) => {
171                 if frame.idx < frame.forest.len() {
172                     break;
173                 }
174                 !frame.dotdotdoted ||
175                     *r.repeat_idx.last().unwrap() == *r.repeat_len.last().unwrap() - 1
176             }
177         };
178
179         /* done with this set; pop or repeat? */
180         if should_pop {
181             let prev = r.stack.pop().unwrap();
182             match r.stack.last_mut() {
183                 None => {
184                     r.cur_tok = token::Eof;
185                     return ret_val;
186                 }
187                 Some(frame) => {
188                     frame.idx += 1;
189                 }
190             }
191             if prev.dotdotdoted {
192                 r.repeat_idx.pop();
193                 r.repeat_len.pop();
194             }
195         } else { /* repeat */
196             *r.repeat_idx.last_mut().unwrap() += 1u;
197             r.stack.last_mut().unwrap().idx = 0;
198             match r.stack.last().unwrap().sep.clone() {
199                 Some(tk) => {
200                     r.cur_tok = tk; /* repeat same span, I guess */
201                     return ret_val;
202                 }
203                 None => {}
204             }
205         }
206     }
207     loop { /* because it's easiest, this handles `TtDelimited` not starting
208               with a `TtToken`, even though it won't happen */
209         let t = {
210             let frame = r.stack.last().unwrap();
211             // FIXME(pcwalton): Bad copy.
212             frame.forest.get_tt(frame.idx)
213         };
214         match t {
215             TtSequence(sp, seq) => {
216                 // FIXME(pcwalton): Bad copy.
217                 match lockstep_iter_size(&TtSequence(sp, seq.clone()),
218                                          r) {
219                     LisUnconstrained => {
220                         r.sp_diag.span_fatal(
221                             sp.clone(), /* blame macro writer */
222                             "attempted to repeat an expression \
223                              containing no syntax \
224                              variables matched as repeating at this depth");
225                     }
226                     LisContradiction(ref msg) => {
227                         // FIXME #2887 blame macro invoker instead
228                         r.sp_diag.span_fatal(sp.clone(), msg[]);
229                     }
230                     LisConstraint(len, _) => {
231                         if len == 0 {
232                             if seq.op == ast::OneOrMore {
233                                 // FIXME #2887 blame invoker
234                                 r.sp_diag.span_fatal(sp.clone(),
235                                                      "this must repeat at least once");
236                             }
237
238                             r.stack.last_mut().unwrap().idx += 1;
239                             return tt_next_token(r);
240                         }
241                         r.repeat_len.push(len);
242                         r.repeat_idx.push(0);
243                         r.stack.push(TtFrame {
244                             idx: 0,
245                             dotdotdoted: true,
246                             sep: seq.separator.clone(),
247                             forest: TtSequence(sp, seq),
248                         });
249                     }
250                 }
251             }
252             // FIXME #2887: think about span stuff here
253             TtToken(sp, SubstNt(ident, namep)) => {
254                 match lookup_cur_matched(r, ident) {
255                     None => {
256                         r.stack.push(TtFrame {
257                             forest: TtToken(sp, SubstNt(ident, namep)),
258                             idx: 0,
259                             dotdotdoted: false,
260                             sep: None
261                         });
262                         // this can't be 0 length, just like TtDelimited
263                     }
264                     Some(cur_matched) => {
265                         r.stack.last_mut().unwrap().idx += 1;
266                         match *cur_matched {
267                             // sidestep the interpolation tricks for ident because
268                             // (a) idents can be in lots of places, so it'd be a pain
269                             // (b) we actually can, since it's a token.
270                             MatchedNonterminal(NtIdent(box sn, b)) => {
271                                 r.cur_span = sp;
272                                 r.cur_tok = token::Ident(sn, b);
273                                 return ret_val;
274                             }
275                             MatchedNonterminal(ref other_whole_nt) => {
276                                 // FIXME(pcwalton): Bad copy.
277                                 r.cur_span = sp;
278                                 r.cur_tok = token::Interpolated((*other_whole_nt).clone());
279                                 return ret_val;
280                             }
281                             MatchedSeq(..) => {
282                                 r.sp_diag.span_fatal(
283                                     r.cur_span, /* blame the macro writer */
284                                     format!("variable '{}' is still repeating at this depth",
285                                             token::get_ident(ident))[]);
286                             }
287                         }
288                     }
289                 }
290             }
291             // TtDelimited or any token that can be unzipped
292             seq @ TtDelimited(..) | seq @ TtToken(_, MatchNt(..)) => {
293                 // do not advance the idx yet
294                 r.stack.push(TtFrame {
295                    forest: seq,
296                    idx: 0,
297                    dotdotdoted: false,
298                    sep: None
299                 });
300                 // if this could be 0-length, we'd need to potentially recur here
301             }
302             TtToken(sp, DocComment(name)) if r.desugar_doc_comments => {
303                 r.stack.push(TtFrame {
304                    forest: TtToken(sp, DocComment(name)),
305                    idx: 0,
306                    dotdotdoted: false,
307                    sep: None
308                 });
309             }
310             TtToken(sp, tok) => {
311                 r.cur_span = sp;
312                 r.cur_tok = tok;
313                 r.stack.last_mut().unwrap().idx += 1;
314                 return ret_val;
315             }
316         }
317     }
318 }