Skip to content

Commit 0c6fe64

Browse files
committed
Macro By Example transcription of token trees with interpolations and dotdotdots.
1 parent f4fb975 commit 0c6fe64

File tree

4 files changed

+141
-32
lines changed

4 files changed

+141
-32
lines changed

src/libsyntax/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ enum token_tree {
379379
tt_delim(~[token_tree]),
380380
tt_flat(span, token::token),
381381
/* These only make sense for right-hand-sides of MBE macros*/
382-
tt_dotdotdot(~[token_tree]),
383-
tt_interpolate(ident)
382+
tt_dotdotdot(span, ~[token_tree]),
383+
tt_interpolate(span, ident)
384384
}
385385

386386

src/libsyntax/ext/tt/transcribe.rs

Lines changed: 122 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import util::interner::interner;
22
import diagnostic::span_handler;
3-
import ast::{tt_delim,tt_flat,tt_dotdotdot,tt_interpolate,ident};
4-
import earley_parser::arb_depth;
3+
import ast::{token_tree,tt_delim,tt_flat,tt_dotdotdot,tt_interpolate,ident};
4+
import earley_parser::{arb_depth,seq,leaf};
55
import codemap::span;
6-
import parse::token::{EOF,token};
6+
import parse::token::{EOF,ACTUALLY,token};
7+
import std::map::{hashmap,box_str_hash};
78

89
export tt_reader, new_tt_reader, dup_tt_reader, tt_next_token;
910

@@ -14,8 +15,9 @@ enum tt_frame_up { /* to break a circularity */
1415
/* TODO: figure out how to have a uniquely linked stack, and change to `~` */
1516
///an unzipping of `token_tree`s
1617
type tt_frame = @{
17-
readme: [ast::token_tree]/~,
18+
readme: ~[ast::token_tree],
1819
mut idx: uint,
20+
dotdotdoted: bool,
1921
up: tt_frame_up
2022
};
2123

@@ -25,6 +27,8 @@ type tt_reader = @{
2527
mut cur: tt_frame,
2628
/* for MBE-style macro transcription */
2729
interpolations: std::map::hashmap<ident, @arb_depth>,
30+
mut repeat_idx: ~[mut uint],
31+
mut repeat_len: ~[uint],
2832
/* cached: */
2933
mut cur_tok: token,
3034
mut cur_span: span
@@ -35,15 +39,16 @@ type tt_reader = @{
3539
* should) be none. */
3640
fn new_tt_reader(span_diagnostic: span_handler, itr: @interner<@str>,
3741
interp: option<std::map::hashmap<ident,@arb_depth>>,
38-
src: [ast::token_tree]/~)
42+
src: ~[ast::token_tree])
3943
-> tt_reader {
4044
let r = @{span_diagnostic: span_diagnostic, interner: itr,
41-
mut cur: @{readme: src, mut idx: 0u,
45+
mut cur: @{readme: src, mut idx: 0u, dotdotdoted: false,
4246
up: tt_frame_up(option::none)},
4347
interpolations: alt interp { /* just a convienience */
4448
none { std::map::box_str_hash::<@arb_depth>() }
4549
some(x) { x }
4650
},
51+
mut repeat_idx: ~[mut], mut repeat_len: ~[],
4752
/* dummy values, never read: */
4853
mut cur_tok: EOF,
4954
mut cur_span: ast_util::mk_sp(0u,0u)
@@ -53,7 +58,7 @@ fn new_tt_reader(span_diagnostic: span_handler, itr: @interner<@str>,
5358
}
5459

5560
pure fn dup_tt_frame(&&f: tt_frame) -> tt_frame {
56-
@{readme: f.readme, mut idx: f.idx,
61+
@{readme: f.readme, mut idx: f.idx, dotdotdoted: f.dotdotdoted,
5762
up: alt f.up {
5863
tt_frame_up(some(up_frame)) {
5964
tt_frame_up(some(dup_tt_frame(up_frame)))
@@ -67,46 +72,138 @@ pure fn dup_tt_reader(&&r: tt_reader) -> tt_reader {
6772
@{span_diagnostic: r.span_diagnostic, interner: r.interner,
6873
mut cur: dup_tt_frame(r.cur),
6974
interpolations: r.interpolations,
75+
mut repeat_idx: copy r.repeat_idx, mut repeat_len: copy r.repeat_len,
7076
mut cur_tok: r.cur_tok, mut cur_span: r.cur_span}
7177
}
7278

7379

80+
pure fn lookup_cur_ad_by_ad(r: tt_reader, start: @arb_depth) -> @arb_depth {
81+
pure fn red(&&ad: @arb_depth, &&idx: uint) -> @arb_depth {
82+
alt *ad {
83+
leaf(_) { ad /* end of the line; duplicate henceforth */ }
84+
seq(ads, _) { ads[idx] }
85+
}
86+
}
87+
vec::foldl(start, r.repeat_idx, red)
88+
}
89+
90+
fn lookup_cur_ad(r: tt_reader, name: ident) -> @arb_depth {
91+
lookup_cur_ad_by_ad(r, r.interpolations.get(name))
92+
}
93+
enum lis {
94+
lis_unconstrained, lis_constraint(uint, ident), lis_contradiction(str)
95+
}
96+
97+
fn lockstep_iter_size(&&t: token_tree, &&r: tt_reader) -> lis {
98+
fn lis_merge(lhs: lis, rhs: lis) -> lis {
99+
alt lhs {
100+
lis_unconstrained { rhs }
101+
lis_contradiction(_) { lhs }
102+
lis_constraint(l_len, l_id) {
103+
alt rhs {
104+
lis_unconstrained { lhs }
105+
lis_contradiction(_) { rhs }
106+
lis_constraint(r_len, _) if l_len == r_len { lhs }
107+
lis_constraint(r_len, r_id) {
108+
lis_contradiction(#fmt["Inconsistent lockstep iteration: \
109+
'%s' has %u items, but '%s' has %u",
110+
*l_id, l_len, *r_id, r_len])
111+
}
112+
}
113+
}
114+
}
115+
}
116+
alt t {
117+
tt_delim(tts) | tt_dotdotdot(_, tts) {
118+
vec::foldl(lis_unconstrained, tts, {|lis, tt|
119+
lis_merge(lis, lockstep_iter_size(tt, r)) })
120+
}
121+
tt_flat(*) { lis_unconstrained }
122+
tt_interpolate(_, name) {
123+
alt *lookup_cur_ad(r, name) {
124+
leaf(_) { lis_unconstrained }
125+
seq(ads, _) { lis_constraint(ads.len(), name) }
126+
}
127+
}
128+
}
129+
}
130+
131+
74132
fn tt_next_token(&&r: tt_reader) -> {tok: token, sp: span} {
75133
let ret_val = { tok: r.cur_tok, sp: r.cur_span };
76134
if r.cur.idx >= vec::len(r.cur.readme) {
77-
/* done with this set; pop */
78-
alt r.cur.up {
79-
tt_frame_up(none) {
80-
r.cur_tok = EOF;
81-
ret ret_val;
82-
}
83-
tt_frame_up(some(tt_f)) {
84-
r.cur = tt_f;
85-
/* the above `if` would need to be a `while` if we didn't know
86-
that the last thing in a `tt_delim` is always a `tt_flat` */
87-
r.cur.idx += 1u;
88-
}
135+
/* done with this set; pop or repeat? */
136+
if ! r.cur.dotdotdoted
137+
|| r.repeat_idx.last() == r.repeat_len.last() - 1 {
138+
if r.cur.dotdotdoted {
139+
vec::pop(r.repeat_idx); vec::pop(r.repeat_len);
140+
}
141+
alt r.cur.up {
142+
tt_frame_up(none) {
143+
r.cur_tok = EOF;
144+
ret ret_val;
145+
}
146+
tt_frame_up(some(tt_f)) {
147+
r.cur = tt_f;
148+
/* the outermost `if` would need to be a `while` if we
149+
didn't know that the last thing in a `tt_delim` is always
150+
a `tt_flat`, and that a `tt_dotdotdot` is never empty */
151+
r.cur.idx += 1u;
152+
}
153+
}
154+
155+
} else {
156+
r.cur.idx = 0u;
157+
r.repeat_idx[r.repeat_idx.len() - 1u] += 1u;
89158
}
90159
}
91160
/* if `tt_delim`s could be 0-length, we'd need to be able to switch
92161
between popping and pushing until we got to an actual `tt_flat` */
93162
loop { /* because it's easiest, this handles `tt_delim` not starting
94163
with a `tt_flat`, even though it won't happen */
95-
alt copy r.cur.readme[r.cur.idx] {
164+
alt r.cur.readme[r.cur.idx] {
96165
tt_delim(tts) {
97-
r.cur = @{readme: tts, mut idx: 0u,
166+
r.cur = @{readme: tts, mut idx: 0u, dotdotdoted: false,
98167
up: tt_frame_up(option::some(r.cur)) };
99168
}
100169
tt_flat(sp, tok) {
101170
r.cur_span = sp; r.cur_tok = tok;
102171
r.cur.idx += 1u;
103172
ret ret_val;
104173
}
105-
tt_dotdotdot(tts) {
106-
fail;
174+
tt_dotdotdot(sp, tts) {
175+
alt lockstep_iter_size(tt_dotdotdot(sp, tts), r) {
176+
lis_unconstrained {
177+
r.span_diagnostic.span_fatal(
178+
copy r.cur_span, /* blame macro writer */
179+
"attempted to repeat an expression containing no syntax \
180+
variables matched as repeating at this depth");
181+
}
182+
lis_contradiction(msg) { /* blame macro invoker */
183+
r.span_diagnostic.span_fatal(sp, msg);
184+
}
185+
lis_constraint(len, _) {
186+
vec::push(r.repeat_len, len);
187+
vec::push(r.repeat_idx, 0u);
188+
r.cur = @{readme: tts, mut idx: 0u, dotdotdoted: true,
189+
up: tt_frame_up(option::some(r.cur)) };
190+
}
191+
}
107192
}
108-
tt_interpolate(ident) {
109-
fail;
193+
// TODO: think about span stuff here
194+
tt_interpolate(sp, ident) {
195+
alt *lookup_cur_ad(r, ident) {
196+
leaf(w_nt) {
197+
r.cur_span = sp; r.cur_tok = ACTUALLY(w_nt);
198+
ret ret_val;
199+
}
200+
seq(*) {
201+
r.span_diagnostic.span_fatal(
202+
copy r.cur_span, /* blame the macro writer */
203+
#fmt["variable '%s' is still repeating at this depth",
204+
*ident]);
205+
}
206+
}
110207
}
111208
}
112209
}

src/libsyntax/parse/parser.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,9 +1070,10 @@ class parser {
10701070

10711071
fn parse_tt_flat(p: parser, delim_ok: bool) -> token_tree {
10721072
if p.eat_keyword("many") && p.quote_depth > 0u {
1073-
ret tt_dotdotdot(
1074-
p.parse_seq(token::LPAREN, token::RPAREN, seq_sep_none(),
1075-
|p| p.parse_token_tree()).node);
1073+
let seq = p.parse_seq(token::LPAREN, token::RPAREN,
1074+
seq_sep_none(),
1075+
|p| p.parse_token_tree());
1076+
ret tt_dotdotdot(seq.span, seq.node);
10761077
}
10771078
alt p.token {
10781079
token::RPAREN | token::RBRACE | token::RBRACKET
@@ -1086,7 +1087,8 @@ class parser {
10861087
/* we ought to allow different depths of unquotation */
10871088
token::DOLLAR if p.quote_depth > 0u {
10881089
p.bump();
1089-
ret tt_interpolate(p.parse_ident());
1090+
let sp = p.span;
1091+
ret tt_interpolate(sp, p.parse_ident());
10901092
}
10911093
_ { /* ok */ }
10921094
}

src/libsyntax/parse/token.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ enum token {
7878
IDENT(str_num, bool),
7979
UNDERSCORE,
8080

81-
//ACTUALLY(whole_nonterminal),
81+
/* For interpolation */
82+
ACTUALLY(whole_nt),
8283

8384
DOC_COMMENT(str_num),
8485
EOF,
@@ -181,6 +182,15 @@ fn to_str(in: interner<@str>, t: token) -> str {
181182
/* Other */
182183
DOC_COMMENT(s) { *interner::get(in, s) }
183184
EOF { "<eof>" }
185+
ACTUALLY(w_nt) {
186+
"an interpolated " +
187+
alt w_nt {
188+
w_item(*) { "item" } w_block(*) { "block" }
189+
w_stmt(*) { "statement" } w_pat(*) { "pattern" }
190+
w_expr(*) { "expression" } w_ty(*) { "type" }
191+
w_ident(*) { "identifier" } w_path(*) { "path" }
192+
}
193+
}
184194
}
185195
}
186196

0 commit comments

Comments
 (0)