2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-04 13:37:29 -05:00
|
|
|
use diagnostic::span_handler;
|
|
|
|
use ast::{token_tree, tt_delim, tt_tok, tt_seq, tt_nonterminal,ident};
|
|
|
|
use macro_parser::{named_match, matched_seq, matched_nonterminal};
|
|
|
|
use codemap::span;
|
2012-10-15 16:56:42 -05:00
|
|
|
use parse::token::{EOF, INTERPOLATED, IDENT, Token, nt_ident, ident_interner};
|
2012-09-19 17:13:04 -05:00
|
|
|
use std::map::HashMap;
|
2012-06-27 17:29:35 -05:00
|
|
|
|
|
|
|
export tt_reader, new_tt_reader, dup_tt_reader, tt_next_token;
|
|
|
|
|
|
|
|
enum tt_frame_up { /* to break a circularity */
|
2012-08-20 14:23:37 -05:00
|
|
|
tt_frame_up(Option<tt_frame>)
|
2012-06-27 17:29:35 -05:00
|
|
|
}
|
|
|
|
|
2012-07-12 20:08:55 -05:00
|
|
|
/* FIXME #2811: figure out how to have a uniquely linked stack, and change to
|
|
|
|
`~` */
|
2012-06-27 17:29:35 -05:00
|
|
|
///an unzipping of `token_tree`s
|
|
|
|
type tt_frame = @{
|
2012-06-29 20:26:34 -05:00
|
|
|
readme: ~[ast::token_tree],
|
2012-06-27 17:29:35 -05:00
|
|
|
mut idx: uint,
|
2012-06-29 20:26:34 -05:00
|
|
|
dotdotdoted: bool,
|
2012-10-15 16:56:42 -05:00
|
|
|
sep: Option<Token>,
|
2012-07-05 16:30:56 -05:00
|
|
|
up: tt_frame_up,
|
2012-06-27 17:29:35 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
type tt_reader = @{
|
2012-07-06 20:04:28 -05:00
|
|
|
sp_diag: span_handler,
|
2012-09-19 20:50:24 -05:00
|
|
|
interner: @ident_interner,
|
2012-06-27 17:29:35 -05:00
|
|
|
mut cur: tt_frame,
|
|
|
|
/* for MBE-style macro transcription */
|
2012-09-10 17:38:28 -05:00
|
|
|
interpolations: std::map::HashMap<ident, @named_match>,
|
2012-09-21 20:43:30 -05:00
|
|
|
mut repeat_idx: ~[uint],
|
2012-06-29 20:26:34 -05:00
|
|
|
mut repeat_len: ~[uint],
|
2012-06-27 17:29:35 -05:00
|
|
|
/* cached: */
|
2012-10-15 16:56:42 -05:00
|
|
|
mut cur_tok: Token,
|
2012-06-27 17:29:35 -05:00
|
|
|
mut cur_span: span
|
|
|
|
};
|
|
|
|
|
|
|
|
/** This can do Macro-By-Example transcription. On the other hand, if
|
2012-07-27 21:14:46 -05:00
|
|
|
* `src` contains no `tt_seq`s and `tt_nonterminal`s, `interp` can (and
|
2012-06-27 17:29:35 -05:00
|
|
|
* should) be none. */
|
2012-09-19 20:50:24 -05:00
|
|
|
fn new_tt_reader(sp_diag: span_handler, itr: @ident_interner,
|
2012-09-10 17:38:28 -05:00
|
|
|
interp: Option<std::map::HashMap<ident,@named_match>>,
|
2012-06-29 20:26:34 -05:00
|
|
|
src: ~[ast::token_tree])
|
2012-06-27 17:29:35 -05:00
|
|
|
-> tt_reader {
|
2012-07-06 20:04:28 -05:00
|
|
|
let r = @{sp_diag: sp_diag, interner: itr,
|
2012-06-29 20:26:34 -05:00
|
|
|
mut cur: @{readme: src, mut idx: 0u, dotdotdoted: false,
|
2012-08-20 14:23:37 -05:00
|
|
|
sep: None, up: tt_frame_up(option::None)},
|
2012-08-06 14:34:08 -05:00
|
|
|
interpolations: match interp { /* just a convienience */
|
2012-09-19 11:41:06 -05:00
|
|
|
None => std::map::HashMap(),
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(x) => x
|
2012-06-27 17:29:35 -05:00
|
|
|
},
|
2012-09-21 20:43:30 -05:00
|
|
|
mut repeat_idx: ~[],
|
|
|
|
mut repeat_len: ~[],
|
2012-06-27 17:29:35 -05:00
|
|
|
/* dummy values, never read: */
|
|
|
|
mut cur_tok: EOF,
|
2012-11-12 21:32:48 -06:00
|
|
|
mut cur_span: ast_util::dummy_sp()
|
2012-06-27 17:29:35 -05:00
|
|
|
};
|
|
|
|
tt_next_token(r); /* get cur_tok and cur_span set up */
|
2012-08-01 19:30:05 -05:00
|
|
|
return r;
|
2012-06-27 17:29:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pure fn dup_tt_frame(&&f: tt_frame) -> tt_frame {
|
2012-06-29 20:26:34 -05:00
|
|
|
@{readme: f.readme, mut idx: f.idx, dotdotdoted: f.dotdotdoted,
|
2012-08-06 14:34:08 -05:00
|
|
|
sep: f.sep, up: match f.up {
|
2012-08-20 14:23:37 -05:00
|
|
|
tt_frame_up(Some(up_frame)) => {
|
|
|
|
tt_frame_up(Some(dup_tt_frame(up_frame)))
|
2012-06-27 17:29:35 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
tt_frame_up(none) => tt_frame_up(none)
|
2012-06-27 17:29:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pure fn dup_tt_reader(&&r: tt_reader) -> tt_reader {
|
2012-07-06 20:04:28 -05:00
|
|
|
@{sp_diag: r.sp_diag, interner: r.interner,
|
2012-06-27 17:29:35 -05:00
|
|
|
mut cur: dup_tt_frame(r.cur),
|
|
|
|
interpolations: r.interpolations,
|
2012-06-29 20:26:34 -05:00
|
|
|
mut repeat_idx: copy r.repeat_idx, mut repeat_len: copy r.repeat_len,
|
2012-06-27 17:29:35 -05:00
|
|
|
mut cur_tok: r.cur_tok, mut cur_span: r.cur_span}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-27 21:14:46 -05:00
|
|
|
pure fn lookup_cur_matched_by_matched(r: tt_reader,
|
|
|
|
start: @named_match) -> @named_match {
|
2012-09-28 00:20:47 -05:00
|
|
|
pure fn red(+ad: @named_match, idx: &uint) -> @named_match {
|
2012-08-06 14:34:08 -05:00
|
|
|
match *ad {
|
2012-08-03 21:59:04 -05:00
|
|
|
matched_nonterminal(_) => {
|
2012-07-27 21:14:46 -05:00
|
|
|
// end of the line; duplicate henceforth
|
|
|
|
ad
|
|
|
|
}
|
2012-09-28 00:20:47 -05:00
|
|
|
matched_seq(ads, _) => ads[*idx]
|
2012-06-29 20:26:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
vec::foldl(start, r.repeat_idx, red)
|
|
|
|
}
|
|
|
|
|
2012-07-27 21:14:46 -05:00
|
|
|
fn lookup_cur_matched(r: tt_reader, name: ident) -> @named_match {
|
|
|
|
lookup_cur_matched_by_matched(r, r.interpolations.get(name))
|
2012-06-29 20:26:34 -05:00
|
|
|
}
|
|
|
|
enum lis {
|
2012-07-14 00:57:48 -05:00
|
|
|
lis_unconstrained, lis_constraint(uint, ident), lis_contradiction(~str)
|
2012-06-29 20:26:34 -05:00
|
|
|
}
|
|
|
|
|
2012-07-18 18:18:02 -05:00
|
|
|
fn lockstep_iter_size(t: token_tree, r: tt_reader) -> lis {
|
|
|
|
fn lis_merge(lhs: lis, rhs: lis, r: tt_reader) -> lis {
|
2012-08-06 14:34:08 -05:00
|
|
|
match lhs {
|
2012-08-03 21:59:04 -05:00
|
|
|
lis_unconstrained => rhs,
|
|
|
|
lis_contradiction(_) => lhs,
|
2012-08-06 14:34:08 -05:00
|
|
|
lis_constraint(l_len, l_id) => match rhs {
|
2012-08-03 21:59:04 -05:00
|
|
|
lis_unconstrained => lhs,
|
|
|
|
lis_contradiction(_) => rhs,
|
|
|
|
lis_constraint(r_len, _) if l_len == r_len => lhs,
|
|
|
|
lis_constraint(r_len, r_id) => {
|
2012-07-18 18:18:02 -05:00
|
|
|
let l_n = *r.interner.get(l_id);
|
|
|
|
let r_n = *r.interner.get(r_id);
|
2012-08-22 19:24:52 -05:00
|
|
|
lis_contradiction(fmt!("Inconsistent lockstep iteration: \
|
2012-08-03 21:59:04 -05:00
|
|
|
'%s' has %u items, but '%s' has %u",
|
2012-08-22 19:24:52 -05:00
|
|
|
l_n, l_len, r_n, r_len))
|
2012-06-29 20:26:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-06 14:34:08 -05:00
|
|
|
match t {
|
2012-12-04 12:50:00 -06:00
|
|
|
tt_delim(ref tts) | tt_seq(_, ref tts, _, _) => {
|
|
|
|
vec::foldl(lis_unconstrained, (*tts), |lis, tt|
|
2012-09-28 00:20:47 -05:00
|
|
|
lis_merge(lis, lockstep_iter_size(*tt, r), r))
|
2012-06-29 20:26:34 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
tt_tok(*) => lis_unconstrained,
|
2012-08-06 14:34:08 -05:00
|
|
|
tt_nonterminal(_, name) => match *lookup_cur_matched(r, name) {
|
2012-08-03 21:59:04 -05:00
|
|
|
matched_nonterminal(_) => lis_unconstrained,
|
|
|
|
matched_seq(ads, _) => lis_constraint(ads.len(), name)
|
2012-06-29 20:26:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-15 16:56:42 -05:00
|
|
|
fn tt_next_token(&&r: tt_reader) -> {tok: Token, sp: span} {
|
2012-06-27 17:29:35 -05:00
|
|
|
let ret_val = { tok: r.cur_tok, sp: r.cur_span };
|
2012-07-24 13:44:32 -05:00
|
|
|
while r.cur.idx >= r.cur.readme.len() {
|
2012-06-29 20:26:34 -05:00
|
|
|
/* done with this set; pop or repeat? */
|
|
|
|
if ! r.cur.dotdotdoted
|
|
|
|
|| r.repeat_idx.last() == r.repeat_len.last() - 1 {
|
2012-07-06 20:04:28 -05:00
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match r.cur.up {
|
2012-08-20 14:23:37 -05:00
|
|
|
tt_frame_up(None) => {
|
2012-06-29 20:26:34 -05:00
|
|
|
r.cur_tok = EOF;
|
2012-08-01 19:30:05 -05:00
|
|
|
return ret_val;
|
2012-06-29 20:26:34 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
tt_frame_up(Some(tt_f)) => {
|
2012-07-06 20:04:28 -05:00
|
|
|
if r.cur.dotdotdoted {
|
2012-09-28 00:20:47 -05:00
|
|
|
r.repeat_idx.pop();
|
|
|
|
r.repeat_len.pop();
|
2012-07-06 20:04:28 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 20:26:34 -05:00
|
|
|
r.cur = tt_f;
|
|
|
|
r.cur.idx += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-06 20:04:28 -05:00
|
|
|
} else { /* repeat */
|
2012-06-29 20:26:34 -05:00
|
|
|
r.cur.idx = 0u;
|
|
|
|
r.repeat_idx[r.repeat_idx.len() - 1u] += 1u;
|
2012-08-06 14:34:08 -05:00
|
|
|
match r.cur.sep {
|
2012-12-04 12:50:00 -06:00
|
|
|
Some(ref tk) => {
|
|
|
|
r.cur_tok = (*tk); /* repeat same span, I guess */
|
2012-08-01 19:30:05 -05:00
|
|
|
return ret_val;
|
2012-07-05 16:30:56 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
None => ()
|
2012-07-05 16:30:56 -05:00
|
|
|
}
|
2012-06-27 17:29:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
loop { /* because it's easiest, this handles `tt_delim` not starting
|
2012-07-27 21:14:46 -05:00
|
|
|
with a `tt_tok`, even though it won't happen */
|
2012-08-06 14:34:08 -05:00
|
|
|
match r.cur.readme[r.cur.idx] {
|
2012-12-04 12:50:00 -06:00
|
|
|
tt_delim(ref tts) => {
|
|
|
|
r.cur = @{readme: (*tts), mut idx: 0u, dotdotdoted: false,
|
2012-08-20 14:23:37 -05:00
|
|
|
sep: None, up: tt_frame_up(option::Some(r.cur)) };
|
2012-07-06 20:04:28 -05:00
|
|
|
// if this could be 0-length, we'd need to potentially recur here
|
2012-06-27 17:29:35 -05:00
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
tt_tok(sp, ref tok) => {
|
|
|
|
r.cur_span = sp; r.cur_tok = (*tok);
|
2012-06-27 17:29:35 -05:00
|
|
|
r.cur.idx += 1u;
|
2012-08-01 19:30:05 -05:00
|
|
|
return ret_val;
|
2012-06-27 17:29:35 -05:00
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
tt_seq(sp, ref tts, ref sep, zerok) => {
|
|
|
|
match lockstep_iter_size(tt_seq(sp, (*tts), (*sep), zerok), r) {
|
2012-08-03 21:59:04 -05:00
|
|
|
lis_unconstrained => {
|
2012-07-06 20:04:28 -05:00
|
|
|
r.sp_diag.span_fatal(
|
|
|
|
sp, /* blame macro writer */
|
2012-07-14 00:57:48 -05:00
|
|
|
~"attempted to repeat an expression containing no syntax \
|
2012-06-29 20:26:34 -05:00
|
|
|
variables matched as repeating at this depth");
|
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
lis_contradiction(ref msg) => { /* FIXME #2887 blame macro invoker
|
2012-07-12 20:08:55 -05:00
|
|
|
instead*/
|
2012-12-04 12:50:00 -06:00
|
|
|
r.sp_diag.span_fatal(sp, (*msg));
|
2012-06-29 20:26:34 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
lis_constraint(len, _) => {
|
2012-07-06 20:04:28 -05:00
|
|
|
if len == 0 {
|
|
|
|
if !zerok {
|
2012-07-12 20:08:55 -05:00
|
|
|
r.sp_diag.span_fatal(sp, /* FIXME #2887 blame invoker
|
|
|
|
*/
|
2012-07-14 00:57:48 -05:00
|
|
|
~"this must repeat at least \
|
2012-07-06 20:04:28 -05:00
|
|
|
once");
|
|
|
|
}
|
2012-07-24 13:44:32 -05:00
|
|
|
|
|
|
|
r.cur.idx += 1u;
|
2012-08-01 19:30:05 -05:00
|
|
|
return tt_next_token(r);
|
2012-07-24 13:44:32 -05:00
|
|
|
} else {
|
2012-09-26 19:33:34 -05:00
|
|
|
r.repeat_len.push(len);
|
|
|
|
r.repeat_idx.push(0u);
|
2012-12-04 12:50:00 -06:00
|
|
|
r.cur = @{readme: (*tts), mut idx: 0u, dotdotdoted: true,
|
|
|
|
sep: (*sep), up: tt_frame_up(option::Some(r.cur))};
|
2012-07-06 20:04:28 -05:00
|
|
|
}
|
2012-06-29 20:26:34 -05:00
|
|
|
}
|
|
|
|
}
|
2012-06-27 17:29:35 -05:00
|
|
|
}
|
2012-07-12 20:08:55 -05:00
|
|
|
// FIXME #2887: think about span stuff here
|
2012-08-03 21:59:04 -05:00
|
|
|
tt_nonterminal(sp, ident) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match *lookup_cur_matched(r, ident) {
|
2012-07-03 20:39:37 -05:00
|
|
|
/* sidestep the interpolation tricks for ident because
|
|
|
|
(a) idents can be in lots of places, so it'd be a pain
|
|
|
|
(b) we actually can, since it's a token. */
|
2012-08-03 21:59:04 -05:00
|
|
|
matched_nonterminal(nt_ident(sn,b)) => {
|
2012-07-03 20:39:37 -05:00
|
|
|
r.cur_span = sp; r.cur_tok = IDENT(sn,b);
|
|
|
|
r.cur.idx += 1u;
|
2012-08-01 19:30:05 -05:00
|
|
|
return ret_val;
|
2012-07-03 20:39:37 -05:00
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
matched_nonterminal(ref other_whole_nt) => {
|
|
|
|
r.cur_span = sp; r.cur_tok = INTERPOLATED((*other_whole_nt));
|
2012-07-03 20:39:37 -05:00
|
|
|
r.cur.idx += 1u;
|
2012-08-01 19:30:05 -05:00
|
|
|
return ret_val;
|
2012-06-29 20:26:34 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
matched_seq(*) => {
|
2012-07-06 20:04:28 -05:00
|
|
|
r.sp_diag.span_fatal(
|
2012-06-29 20:26:34 -05:00
|
|
|
copy r.cur_span, /* blame the macro writer */
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("variable '%s' is still repeating at this depth",
|
|
|
|
*r.interner.get(ident)));
|
2012-06-29 20:26:34 -05:00
|
|
|
}
|
|
|
|
}
|
2012-06-27 17:29:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 17:27:06 -05:00
|
|
|
}
|