Skip to content

Commit 497a8b5

Browse files
committed
syntax: switch tt quoter to emit ~[tt], not tt.
1 parent ba01cd7 commit 497a8b5

File tree

7 files changed

+76
-36
lines changed

7 files changed

+76
-36
lines changed

src/libsyntax/ext/build.rs

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ fn mk_uniq_vec_e(cx: ext_ctxt, sp: span, exprs: ~[@ast::expr]) ->
7979
@ast::expr {
8080
mk_vstore_e(cx, sp, mk_base_vec_e(cx, sp, exprs), ast::expr_vstore_uniq)
8181
}
82+
fn mk_slice_vec_e(cx: ext_ctxt, sp: span, exprs: ~[@ast::expr]) ->
83+
@ast::expr {
84+
mk_vstore_e(cx, sp, mk_base_vec_e(cx, sp, exprs),
85+
ast::expr_vstore_slice)
86+
}
8287
fn mk_fixed_vec_e(cx: ext_ctxt, sp: span, exprs: ~[@ast::expr]) ->
8388
@ast::expr {
8489
mk_vstore_e(cx, sp, mk_base_vec_e(cx, sp, exprs),

src/libsyntax/ext/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ast::{crate, expr_, expr_mac, mac_invoc, mac_invoc_tt,
55
use fold::*;
66
use ext::base::*;
77
use ext::qquote::{qq_helper};
8-
use parse::{parser, parse_expr_from_source_str, new_parser_from_tt};
8+
use parse::{parser, parse_expr_from_source_str, new_parser_from_tts};
99

1010

1111
use codemap::{span, ExpandedFrom};

src/libsyntax/ext/pipes/pipec.rs

+9
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ trait ext_ctxt_parse_utils {
473473
fn parse_item(s: ~str) -> @ast::item;
474474
fn parse_expr(s: ~str) -> @ast::expr;
475475
fn parse_stmt(s: ~str) -> @ast::stmt;
476+
fn parse_tts(s: ~str) -> ~[ast::token_tree];
476477
}
477478
478479
impl ext_ctxt: ext_ctxt_parse_utils {
@@ -508,4 +509,12 @@ impl ext_ctxt: ext_ctxt_parse_utils {
508509
self.cfg(),
509510
self.parse_sess())
510511
}
512+
513+
fn parse_tts(s: ~str) -> ~[ast::token_tree] {
514+
parse::parse_tts_from_source_str(
515+
~"***protocol expansion***",
516+
@(copy s),
517+
self.cfg(),
518+
self.parse_sess())
519+
}
511520
}

src/libsyntax/ext/quote.rs

+33-28
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ use token::*;
1919
pub mod rt {
2020
pub use ast::*;
2121
pub use parse::token::*;
22-
pub use parse::new_parser_from_tt;
22+
pub use parse::new_parser_from_tts;
2323
pub use codemap::BytePos;
2424
pub use codemap::span;
2525
}
2626

2727
pub fn expand_quote_tokens(cx: ext_ctxt,
2828
sp: span,
2929
tts: ~[ast::token_tree]) -> base::mac_result {
30-
base::mr_expr(expand_tt(cx, sp, tts))
30+
base::mr_expr(expand_tts(cx, sp, tts))
3131
}
3232

3333
pub fn expand_quote_expr(cx: ext_ctxt,
@@ -121,8 +121,7 @@ fn mk_span(cx: ext_ctxt, qsp: span, sp: span) -> @ast::expr {
121121
}
122122
};
123123

124-
let span_path = ids_ext(
125-
cx, ~[~"syntax", ~"ext", ~"quote", ~"rt", ~"span"]);
124+
let span_path = ids_ext(cx, ~[~"span"]);
126125

127126
build::mk_struct_e(cx, qsp,
128127
span_path,
@@ -150,7 +149,7 @@ fn mk_ident(cx: ext_ctxt, sp: span, ident: ast::ident) -> @ast::expr {
150149
}
151150

152151
fn mk_bytepos(cx: ext_ctxt, sp: span, bpos: BytePos) -> @ast::expr {
153-
let path = ids_ext(cx, ~[~"syntax", ~"ext", ~"quote", ~"rt", ~"BytePos"]);
152+
let path = ids_ext(cx, ~[~"BytePos"]);
154153
let arg = build::mk_uint(cx, sp, bpos.to_uint());
155154
build::mk_call(cx, sp, path, ~[arg])
156155
}
@@ -318,17 +317,21 @@ fn mk_token(cx: ext_ctxt, sp: span, tok: token::Token) -> @ast::expr {
318317

319318
fn mk_tt(cx: ext_ctxt, sp: span, tt: &ast::token_tree) -> @ast::expr {
320319
match *tt {
321-
ast::tt_tok(sp, tok) =>
322-
build::mk_call(cx, sp,
323-
ids_ext(cx, ~[~"tt_tok"]),
324-
~[mk_span(cx, sp, sp),
325-
mk_token(cx, sp, tok)]),
320+
ast::tt_tok(sp, tok) => {
321+
let e_tok =
322+
build::mk_call(cx, sp,
323+
ids_ext(cx, ~[~"tt_tok"]),
324+
~[mk_span(cx, sp, sp),
325+
mk_token(cx, sp, tok)]);
326+
build::mk_uniq_vec_e(cx, sp, ~[e_tok])
327+
}
326328

327329
ast::tt_delim(tts) => {
328-
let e_tts = tts.map(|tt| mk_tt(cx, sp, tt));
329-
build::mk_call(cx, sp,
330-
ids_ext(cx, ~[~"tt_delim"]),
331-
~[build::mk_uniq_vec_e(cx, sp, e_tts)])
330+
let e_delim =
331+
build::mk_call(cx, sp,
332+
ids_ext(cx, ~[~"tt_delim"]),
333+
~[mk_tts(cx, sp, tts)]);
334+
build::mk_uniq_vec_e(cx, sp, ~[e_delim])
332335
}
333336

334337
ast::tt_seq(*) => fail ~"tt_seq in quote!",
@@ -338,39 +341,41 @@ fn mk_tt(cx: ext_ctxt, sp: span, tt: &ast::token_tree) -> @ast::expr {
338341
}
339342
}
340343
344+
fn mk_tts(cx: ext_ctxt, sp: span, tts: &[ast::token_tree]) -> @ast::expr {
345+
let e_tts = tts.map(|tt| mk_tt(cx, sp, tt));
346+
build::mk_call(cx, sp,
347+
ids_ext(cx, ~[~"vec", ~"concat"]),
348+
~[build::mk_slice_vec_e(cx, sp, e_tts)])
349+
}
341350

342-
fn expand_tt(cx: ext_ctxt,
343-
sp: span,
344-
tts: ~[ast::token_tree]) -> @ast::expr {
351+
fn expand_tts(cx: ext_ctxt,
352+
sp: span,
353+
tts: ~[ast::token_tree]) -> @ast::expr {
345354
// NB: It appears that the main parser loses its mind if we consider
346355
// $foo as a tt_nonterminal during the main parse, so we have to re-parse
347356
// under quote_depth > 0. This is silly and should go away; the _guess_ is
348357
// it has to do with transition away from supporting old-style macros, so
349358
// try removing it when enough of them are gone.
350-
let p = parse::new_parser_from_tt(cx.parse_sess(), cx.cfg(), tts);
359+
let p = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts);
351360
p.quote_depth += 1u;
352-
let tq = dvec::DVec();
353-
while p.token != token::EOF {
354-
tq.push(p.parse_token_tree());
355-
}
356-
let tts = tq.get();
361+
let tts = p.parse_all_token_trees();
362+
p.abort_if_errors();
357363

358364
// We want to emit a block expression that does a sequence of 'use's to
359365
// import the runtime module, followed by a tt expression.
360366
let uses = ~[ build::mk_glob_use(cx, sp, ids_ext(cx, ~[~"syntax",
361367
~"ext",
362368
~"quote",
363369
~"rt"])) ];
364-
build::mk_block(cx, sp, uses, ~[],
365-
Some(mk_tt(cx, sp, &ast::tt_delim(tts))))
370+
build::mk_block(cx, sp, uses, ~[], Some(mk_tts(cx, sp, tts)))
366371
}
367372

368373
fn expand_parse_call(cx: ext_ctxt,
369374
sp: span,
370375
parse_method: ~str,
371376
arg_exprs: ~[@ast::expr],
372377
tts: ~[ast::token_tree]) -> @ast::expr {
373-
let tt_expr = expand_tt(cx, sp, tts);
378+
let tts_expr = expand_tts(cx, sp, tts);
374379

375380
let cfg_call = || build::mk_call_(
376381
cx, sp, build::mk_access(cx, sp, ids_ext(cx, ~[~"ext_cx"]),
@@ -386,10 +391,10 @@ fn expand_parse_call(cx: ext_ctxt,
386391
~"ext",
387392
~"quote",
388393
~"rt",
389-
~"new_parser_from_tt"]),
394+
~"new_parser_from_tts"]),
390395
~[parse_sess_call(),
391396
cfg_call(),
392-
build::mk_uniq_vec_e(cx, sp, ~[tt_expr])]);
397+
tts_expr]);
393398

394399
build::mk_call_(cx, sp,
395400
build::mk_access_(cx, sp, new_parser_call,

src/libsyntax/parse.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ export new_parse_sess, new_parse_sess_special_handler;
55
export next_node_id;
66
export new_parser_from_file, new_parser_etc_from_file;
77
export new_parser_from_source_str;
8-
export new_parser_from_tt;
8+
export new_parser_from_tts;
99
export new_sub_parser_from_file;
1010
export parse_crate_from_file, parse_crate_from_crate_file;
1111
export parse_crate_from_source_str;
1212
export parse_expr_from_source_str, parse_item_from_source_str;
1313
export parse_stmt_from_source_str;
14+
export parse_tts_from_source_str;
1415
export parse_from_source_str;
1516

1617
use parser::Parser;
@@ -127,6 +128,16 @@ fn parse_stmt_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg,
127128
return r;
128129
}
129130
131+
fn parse_tts_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg,
132+
sess: parse_sess) -> ~[ast::token_tree] {
133+
let p = new_parser_from_source_str(sess, cfg, name,
134+
codemap::FssNone, source);
135+
p.quote_depth += 1u;
136+
let r = p.parse_all_token_trees();
137+
p.abort_if_errors();
138+
return r;
139+
}
140+
130141
fn parse_from_source_str<T>(f: fn (p: Parser) -> T,
131142
name: ~str, ss: codemap::FileSubstr,
132143
source: @~str, cfg: ast::crate_cfg,
@@ -199,9 +210,9 @@ fn new_sub_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg,
199210
}
200211
}
201212

202-
fn new_parser_from_tt(sess: parse_sess, cfg: ast::crate_cfg,
203-
tt: ~[ast::token_tree]) -> Parser {
213+
fn new_parser_from_tts(sess: parse_sess, cfg: ast::crate_cfg,
214+
tts: ~[ast::token_tree]) -> Parser {
204215
let trdr = lexer::new_tt_reader(sess.span_diagnostic, sess.interner,
205-
None, tt);
216+
None, tts);
206217
return Parser(sess, cfg, trdr as reader)
207218
}

src/libsyntax/parse/parser.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,14 @@ impl Parser {
13211321
};
13221322
}
13231323
1324+
fn parse_all_token_trees() -> ~[token_tree] {
1325+
let tts = DVec();
1326+
while self.token != token::EOF {
1327+
tts.push(self.parse_token_tree());
1328+
}
1329+
tts.get()
1330+
}
1331+
13241332
fn parse_matchers() -> ~[matcher] {
13251333
// unification of matchers and token_trees would vastly improve
13261334
// the interpolation of matchers
+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
#[allow(non_implicitly_copyable_typarams)];
2+
13
extern mod syntax;
24

35
use syntax::ext::base::ext_ctxt;
46

57
fn syntax_extension(ext_cx: @ext_ctxt) {
6-
let e_toks : syntax::ast::token_tree = quote_tokens!(1 + 2);
7-
let p_toks : syntax::ast::token_tree = quote_tokens!((x, 1 .. 4, *));
8+
let e_toks : ~[syntax::ast::token_tree] = quote_tokens!(1 + 2);
9+
let p_toks : ~[syntax::ast::token_tree] = quote_tokens!((x, 1 .. 4, *));
810

911
let _a: @syntax::ast::expr = quote_expr!(1 + 2);
1012
let _b: Option<@syntax::ast::item> = quote_item!( const foo : int = $e_toks; );
@@ -14,6 +16,6 @@ fn syntax_extension(ext_cx: @ext_ctxt) {
1416
}
1517

1618
fn main() {
17-
let _x: syntax::ast::token_tree = quote_tokens!(a::Foo::foo());
19+
let _x: ~[syntax::ast::token_tree] = quote_tokens!(a::Foo::foo());
1820
}
1921

0 commit comments

Comments
 (0)