Skip to content

Commit 0754fa3

Browse files
kevinabrson
authored andcommitted
Allow ast_fold_precursor to change the span.
This involved changing the prototype for the callbacks to thread the span though. A wrapper function, fold::wrap, can be used to wrap the old style callbacks.
1 parent eee2b68 commit 0754fa3

File tree

6 files changed

+113
-83
lines changed

6 files changed

+113
-83
lines changed

src/comp/front/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn strip_items(crate: @ast::crate, in_cfg: in_cfg_pred)
2727

2828
let precursor =
2929
{fold_mod: bind fold_mod(ctxt, _, _),
30-
fold_block: bind fold_block(ctxt, _, _),
30+
fold_block: fold::wrap(bind fold_block(ctxt, _, _)),
3131
fold_native_mod: bind fold_native_mod(ctxt, _, _)
3232
with *fold::default_ast_fold()};
3333

src/comp/front/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn generate_test_harness(sess: session::session,
4444
mutable testfns: []};
4545

4646
let precursor =
47-
{fold_crate: bind fold_crate(cx, _, _),
47+
{fold_crate: fold::wrap(bind fold_crate(cx, _, _)),
4848
fold_item: bind fold_item(cx, _, _),
4949
fold_mod: bind fold_mod(cx, _, _) with *fold::default_ast_fold()};
5050

src/comp/syntax/ext/expand.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ import syntax::fold::*;
1010
import syntax::ext::base::*;
1111
import syntax::parse::parser::parse_expr_from_source_str;
1212

13-
fn expand_expr(exts: hashmap<str, syntax_extension>, cx: ext_ctxt, e: expr_,
14-
fld: ast_fold, orig: fn@(expr_, ast_fold) -> expr_) -> expr_ {
13+
import codemap::span;
14+
15+
fn expand_expr(exts: hashmap<str, syntax_extension>, cx: ext_ctxt,
16+
e: expr_, s: span, fld: ast_fold,
17+
orig: fn@(expr_, span, ast_fold) -> (expr_, span))
18+
-> (expr_, span)
19+
{
1520
ret alt e {
1621
expr_mac(mac) {
1722
alt mac.node {
@@ -31,19 +36,19 @@ fn expand_expr(exts: hashmap<str, syntax_extension>, cx: ext_ctxt, e: expr_,
3136
let fully_expanded = fld.fold_expr(expanded).node;
3237
cx.bt_pop();
3338

34-
fully_expanded
39+
(fully_expanded, s)
3540
}
3641
some(macro_defining(ext)) {
3742
let named_extension = ext(cx, pth.span, args, body);
3843
exts.insert(named_extension.ident, named_extension.ext);
39-
ast::expr_rec([], none)
44+
(ast::expr_rec([], none), s)
4045
}
4146
}
4247
}
4348
_ { cx.span_bug(mac.span, "naked syntactic bit") }
4449
}
4550
}
46-
_ { orig(e, fld) }
51+
_ { orig(e, s, fld) }
4752
};
4853
}
4954

@@ -67,7 +72,7 @@ fn expand_crate(sess: session::session, c: @crate) -> @crate {
6772
let afp = default_ast_fold();
6873
let cx: ext_ctxt = mk_ctxt(sess);
6974
let f_pre =
70-
{fold_expr: bind expand_expr(exts, cx, _, _, afp.fold_expr)
75+
{fold_expr: bind expand_expr(exts, cx, _, _, _, afp.fold_expr)
7176
with *afp};
7277
let f = make_fold(f_pre);
7378
let cm = parse_expr_from_source_str("<anon>", core_macros(),

src/comp/syntax/ext/simplext.rs

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,12 @@ fn transcribe(cx: ext_ctxt, b: bindings, body: @expr) -> @expr {
194194
let afp = default_ast_fold();
195195
let f_pre =
196196
{fold_ident: bind transcribe_ident(cx, b, idx_path, _, _),
197-
fold_path: bind transcribe_path(cx, b, idx_path, _, _),
197+
fold_path: bind transcribe_path(cx, b, idx_path, _, _, _),
198198
fold_expr:
199-
bind transcribe_expr(cx, b, idx_path, _, _, afp.fold_expr),
200-
fold_ty: bind transcribe_type(cx, b, idx_path, _, _, afp.fold_ty),
199+
bind transcribe_expr(cx, b, idx_path, _, _, _, afp.fold_expr),
200+
fold_ty: bind transcribe_type(cx, b, idx_path, _, _, _, afp.fold_ty),
201201
fold_block:
202-
bind transcribe_block(cx, b, idx_path, _, _, afp.fold_block),
202+
bind transcribe_block(cx, b, idx_path, _, _, _, afp.fold_block),
203203
map_exprs: bind transcribe_exprs(cx, b, idx_path, _, _),
204204
new_id: bind new_id(_, cx),
205205
new_span: bind new_span(cx, _) with *afp};
@@ -209,7 +209,6 @@ fn transcribe(cx: ext_ctxt, b: bindings, body: @expr) -> @expr {
209209
}
210210

211211

212-
213212
/* helper: descend into a matcher */
214213
fn follow(m: arb_depth<matchable>, idx_path: @mutable [uint]) ->
215214
arb_depth<matchable> {
@@ -334,64 +333,67 @@ fn transcribe_ident(cx: ext_ctxt, b: bindings, idx_path: @mutable [uint],
334333

335334

336335
fn transcribe_path(cx: ext_ctxt, b: bindings, idx_path: @mutable [uint],
337-
p: path_, _fld: ast_fold) -> path_ {
336+
p: path_, s:span, _fld: ast_fold) -> (path_, span) {
338337
// Don't substitute into qualified names.
339-
if vec::len(p.types) > 0u || vec::len(p.idents) != 1u { ret p; }
338+
if vec::len(p.types) > 0u || vec::len(p.idents) != 1u { ret (p, s); }
340339
ret alt follow_for_trans(cx, b.find(p.idents[0]), idx_path) {
341340
some(match_ident(id)) {
342-
{global: false, idents: [id.node], types: []}
341+
({global: false, idents: [id.node], types: []}, s)
343342
}
344-
some(match_path(a_pth)) { a_pth.node }
343+
some(match_path(a_pth)) { (a_pth.node, s) }
345344
some(m) { match_error(cx, m, "a path") }
346-
none { p }
345+
none { (p, s) }
347346
}
348347
}
349348

350349

351350
fn transcribe_expr(cx: ext_ctxt, b: bindings, idx_path: @mutable [uint],
352-
e: ast::expr_, fld: ast_fold,
353-
orig: fn@(ast::expr_, ast_fold) -> ast::expr_) ->
354-
ast::expr_ {
351+
e: ast::expr_, s: span, fld: ast_fold,
352+
orig: fn@(ast::expr_, span, ast_fold)->(ast::expr_, span))
353+
-> (ast::expr_, span)
354+
{
355355
ret alt e {
356356
expr_path(p) {
357357
// Don't substitute into qualified names.
358358
if vec::len(p.node.types) > 0u || vec::len(p.node.idents) != 1u {
359-
e;
359+
(e, s);
360360
}
361361
alt follow_for_trans(cx, b.find(p.node.idents[0]), idx_path) {
362362
some(match_ident(id)) {
363-
expr_path(@respan(id.span,
364-
{global: false,
365-
idents: [id.node],
366-
types: []}))
363+
(expr_path(@respan(id.span,
364+
{global: false,
365+
idents: [id.node],
366+
types: []})), s)
367367
}
368-
some(match_path(a_pth)) { expr_path(a_pth) }
369-
some(match_expr(a_exp)) { a_exp.node }
368+
some(match_path(a_pth)) { (expr_path(a_pth), s) }
369+
some(match_expr(a_exp)) { (a_exp.node, s) }
370370
some(m) { match_error(cx, m, "an expression") }
371-
none { orig(e, fld) }
371+
none { orig(e, s, fld) }
372372
}
373373
}
374-
_ { orig(e, fld) }
374+
_ { orig(e, s, fld) }
375375
}
376376
}
377377

378378
fn transcribe_type(cx: ext_ctxt, b: bindings, idx_path: @mutable [uint],
379-
t: ast::ty_, fld: ast_fold,
380-
orig: fn@(ast::ty_, ast_fold) -> ast::ty_) -> ast::ty_ {
379+
t: ast::ty_, s: span, fld: ast_fold,
380+
orig: fn@(ast::ty_, span, ast_fold) -> (ast::ty_, span))
381+
-> (ast::ty_, span)
382+
{
381383
ret alt t {
382384
ast::ty_path(pth, _) {
383385
alt path_to_ident(pth) {
384386
some(id) {
385387
alt follow_for_trans(cx, b.find(id), idx_path) {
386-
some(match_ty(ty)) { ty.node }
388+
some(match_ty(ty)) { (ty.node, s) }
387389
some(m) { match_error(cx, m, "a type") }
388-
none { orig(t, fld) }
390+
none { orig(t, s, fld) }
389391
}
390392
}
391-
none { orig(t, fld) }
393+
none { orig(t, s, fld) }
392394
}
393395
}
394-
_ { orig(t, fld) }
396+
_ { orig(t, s, fld) }
395397
}
396398
}
397399

@@ -400,12 +402,14 @@ fn transcribe_type(cx: ext_ctxt, b: bindings, idx_path: @mutable [uint],
400402
`{v}` */
401403

402404
fn transcribe_block(cx: ext_ctxt, b: bindings, idx_path: @mutable [uint],
403-
blk: blk_, fld: ast_fold,
404-
orig: fn@(blk_, ast_fold) -> blk_) -> blk_ {
405+
blk: blk_, s: span, fld: ast_fold,
406+
orig: fn@(blk_, span, ast_fold) -> (blk_, span))
407+
-> (blk_, span)
408+
{
405409
ret alt block_to_ident(blk) {
406410
some(id) {
407411
alt follow_for_trans(cx, b.find(id), idx_path) {
408-
some(match_block(new_blk)) { new_blk.node }
412+
some(match_block(new_blk)) { (new_blk.node, s) }
409413

410414

411415

@@ -415,10 +419,10 @@ fn transcribe_block(cx: ext_ctxt, b: bindings, idx_path: @mutable [uint],
415419
some(m) {
416420
match_error(cx, m, "a block")
417421
}
418-
none { orig(blk, fld) }
422+
none { orig(blk, s, fld) }
419423
}
420424
}
421-
none { orig(blk, fld) }
425+
none { orig(blk, s, fld) }
422426
}
423427
}
424428

0 commit comments

Comments
 (0)