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.
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::*;
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};
20 use parse::lexer::TokenAndSpan;
24 use std::collections::HashMap;
26 ///an unzipping of `TokenTree`s
36 pub struct TtReader<'a> {
37 pub sp_diag: &'a SpanHandler,
38 /// the unzipped tree:
40 /* for MBE-style macro transcription */
41 interpolations: HashMap<Ident, Rc<NamedMatch>>,
42 repeat_idx: Vec<uint>,
43 repeat_len: Vec<uint>,
47 /// Transform doc comments. Only useful in macro invocations
48 pub desugar_doc_comments: bool,
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
54 pub fn new_tt_reader<'a>(sp_diag: &'a SpanHandler,
55 interp: Option<HashMap<Ident, Rc<NamedMatch>>>,
56 src: Vec<ast::TokenTree> )
58 let mut r = TtReader {
61 forest: TtSequence(DUMMY_SP, Rc::new(ast::SequenceRepetition {
63 // doesn't matter. This merely holds the root unzipping.
64 separator: None, op: ast::ZeroOrMore, num_captures: 0
70 interpolations: match interp { /* just a convenience */
71 None => HashMap::new(),
74 repeat_idx: Vec::new(),
75 repeat_len: Vec::new(),
76 desugar_doc_comments: false,
77 /* dummy values, never read: */
81 tt_next_token(&mut r); /* get cur_tok and cur_span set up */
85 fn lookup_cur_matched_by_matched(r: &TtReader, start: Rc<NamedMatch>) -> Rc<NamedMatch> {
86 r.repeat_idx.iter().fold(start, |ad, idx| {
88 MatchedNonterminal(_) => {
89 // end of the line; duplicate henceforth
92 MatchedSeq(ref ads, _) => ads[*idx].clone()
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))
103 enum LockstepIterSize {
105 LisConstraint(uint, Ident),
106 LisContradiction(String),
109 impl Add for LockstepIterSize {
110 type Output = LockstepIterSize;
112 fn add(self, other: LockstepIterSize) -> LockstepIterSize {
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())
132 fn lockstep_iter_size(t: &TokenTree, r: &TtReader) -> LockstepIterSize {
134 TtDelimited(_, ref delimed) => {
135 delimed.tts.iter().fold(LisUnconstrained, |size, tt| {
136 size + lockstep_iter_size(tt, r)
139 TtSequence(_, ref seq) => {
140 seq.tts.iter().fold(LisUnconstrained, |size, tt| {
141 size + lockstep_iter_size(tt, r)
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),
150 _ => LisUnconstrained
152 TtToken(..) => LisUnconstrained,
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(),
165 let should_pop = match r.stack.last() {
167 assert_eq!(ret_val.tok, token::Eof);
171 if frame.idx < frame.forest.len() {
174 !frame.dotdotdoted ||
175 *r.repeat_idx.last().unwrap() == *r.repeat_len.last().unwrap() - 1
179 /* done with this set; pop or repeat? */
181 let prev = r.stack.pop().unwrap();
182 match r.stack.last_mut() {
184 r.cur_tok = token::Eof;
191 if prev.dotdotdoted {
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() {
200 r.cur_tok = tk; /* repeat same span, I guess */
207 loop { /* because it's easiest, this handles `TtDelimited` not starting
208 with a `TtToken`, even though it won't happen */
210 let frame = r.stack.last().unwrap();
211 // FIXME(pcwalton): Bad copy.
212 frame.forest.get_tt(frame.idx)
215 TtSequence(sp, seq) => {
216 // FIXME(pcwalton): Bad copy.
217 match lockstep_iter_size(&TtSequence(sp, seq.clone()),
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");
226 LisContradiction(ref msg) => {
227 // FIXME #2887 blame macro invoker instead
228 r.sp_diag.span_fatal(sp.clone(), msg[]);
230 LisConstraint(len, _) => {
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");
238 r.stack.last_mut().unwrap().idx += 1;
239 return tt_next_token(r);
241 r.repeat_len.push(len);
242 r.repeat_idx.push(0);
243 r.stack.push(TtFrame {
246 sep: seq.separator.clone(),
247 forest: TtSequence(sp, seq),
252 // FIXME #2887: think about span stuff here
253 TtToken(sp, SubstNt(ident, namep)) => {
254 match lookup_cur_matched(r, ident) {
256 r.stack.push(TtFrame {
257 forest: TtToken(sp, SubstNt(ident, namep)),
262 // this can't be 0 length, just like TtDelimited
264 Some(cur_matched) => {
265 r.stack.last_mut().unwrap().idx += 1;
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)) => {
272 r.cur_tok = token::Ident(sn, b);
275 MatchedNonterminal(ref other_whole_nt) => {
276 // FIXME(pcwalton): Bad copy.
278 r.cur_tok = token::Interpolated((*other_whole_nt).clone());
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))[]);
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 {
300 // if this could be 0-length, we'd need to potentially recur here
302 TtToken(sp, DocComment(name)) if r.desugar_doc_comments => {
303 r.stack.push(TtFrame {
304 forest: TtToken(sp, DocComment(name)),
310 TtToken(sp, tok) => {
313 r.stack.last_mut().unwrap().idx += 1;