Skip to content

Commit 0637fb7

Browse files
committed
---
yaml --- r: 36681 b: refs/heads/try2 c: 0138d87 h: refs/heads/master i: 36679: 7167f33 v: v3
1 parent 8a54916 commit 0637fb7

File tree

30 files changed

+1708
-126
lines changed

30 files changed

+1708
-126
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: eb8fd119c65c67f3b1b8268cc7341c22d39b7b61
55
refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: e24ae85025e40aa17915f6c604d89aefbca274bd
8+
refs/heads/try2: 0138d87f8f1b9f9614e439deb14cbaabad6d104c
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
1010
refs/heads/dist-snap: 22efa39382d41b084fde1719df7ae8ce5697d8c9
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596

branches/try2/doc/rust.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,10 +847,25 @@ fn main() {
847847

848848
Like items, `use` declarations are private to the containing module, by default.
849849
Also like items, a `use` declaration can be public, if qualified by the `pub` keyword.
850+
Such a `use` declaration serves to _re-export_ a name.
850851
A public `use` declaration can therefore be used to _redirect_ some public name to a different target definition,
851852
even a definition with a private canonical path, inside a different module.
852853
If a sequence of such redirections form a cycle or cannot be unambiguously resolved, they represent a compile-time error.
853854

855+
An example of re-exporting:
856+
~~~~
857+
mod quux {
858+
mod foo {
859+
pub fn bar() { }
860+
pub fn baz() { }
861+
}
862+
863+
pub use foo::*;
864+
}
865+
~~~~
866+
867+
In this example, the module `quux` re-exports all of the public names defined in `foo`.
868+
854869
### Functions
855870

856871
A _function item_ defines a sequence of [statements](#statements) and an optional final [expression](#expressions), along with a name and a set of parameters.

branches/try2/src/librustc/middle/astencode.rs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,8 +1033,6 @@ fn decode_item_ast(par_doc: ebml::Doc) -> @ast::item {
10331033
trait fake_ext_ctxt {
10341034
fn cfg() -> ast::crate_cfg;
10351035
fn parse_sess() -> parse::parse_sess;
1036-
fn call_site() -> span;
1037-
fn ident_of(st: ~str) -> ast::ident;
10381036
}
10391037

10401038
#[cfg(test)]
@@ -1044,16 +1042,6 @@ type fake_session = parse::parse_sess;
10441042
impl fake_session: fake_ext_ctxt {
10451043
fn cfg() -> ast::crate_cfg { ~[] }
10461044
fn parse_sess() -> parse::parse_sess { self }
1047-
fn call_site() -> span {
1048-
codemap::span {
1049-
lo: codemap::BytePos(0),
1050-
hi: codemap::BytePos(0),
1051-
expn_info: None
1052-
}
1053-
}
1054-
fn ident_of(st: ~str) -> ast::ident {
1055-
self.interner.intern(@st)
1056-
}
10571045
}
10581046

10591047
#[cfg(test)]
@@ -1062,8 +1050,7 @@ fn mk_ctxt() -> fake_ext_ctxt {
10621050
}
10631051

10641052
#[cfg(test)]
1065-
fn roundtrip(in_item: Option<@ast::item>) {
1066-
let in_item = in_item.get();
1053+
fn roundtrip(in_item: @ast::item) {
10671054
let bytes = do io::with_bytes_writer |wr| {
10681055
let ebml_w = writer::Serializer(wr);
10691056
encode_item_ast(ebml_w, in_item);
@@ -1087,45 +1074,45 @@ fn roundtrip(in_item: Option<@ast::item>) {
10871074
#[test]
10881075
fn test_basic() {
10891076
let ext_cx = mk_ctxt();
1090-
roundtrip(quote_item!(
1077+
roundtrip(#ast[item]{
10911078
fn foo() {}
1092-
));
1079+
});
10931080
}
10941081

10951082
#[test]
10961083
fn test_smalltalk() {
10971084
let ext_cx = mk_ctxt();
1098-
roundtrip(quote_item!(
1085+
roundtrip(#ast[item]{
10991086
fn foo() -> int { 3 + 4 } // first smalltalk program ever executed.
1100-
));
1087+
});
11011088
}
11021089

11031090
#[test]
11041091
fn test_more() {
11051092
let ext_cx = mk_ctxt();
1106-
roundtrip(quote_item!(
1093+
roundtrip(#ast[item]{
11071094
fn foo(x: uint, y: uint) -> uint {
11081095
let z = x + y;
11091096
return z;
11101097
}
1111-
));
1098+
});
11121099
}
11131100

11141101
#[test]
11151102
fn test_simplification() {
11161103
let ext_cx = mk_ctxt();
1117-
let item_in = ast::ii_item(quote_item!(
1104+
let item_in = ast::ii_item(#ast[item] {
11181105
fn new_int_alist<B: Copy>() -> alist<int, B> {
11191106
fn eq_int(&&a: int, &&b: int) -> bool { a == b }
11201107
return {eq_fn: eq_int, mut data: ~[]};
11211108
}
1122-
).get());
1109+
});
11231110
let item_out = simplify_ast(item_in);
1124-
let item_exp = ast::ii_item(quote_item!(
1111+
let item_exp = ast::ii_item(#ast[item] {
11251112
fn new_int_alist<B: Copy>() -> alist<int, B> {
11261113
return {eq_fn: eq_int, mut data: ~[]};
11271114
}
1128-
).get());
1115+
});
11291116
match (item_out, item_exp) {
11301117
(ast::ii_item(item_out), ast::ii_item(item_exp)) => {
11311118
assert pprust::item_to_str(item_out, ext_cx.parse_sess().interner)

branches/try2/src/libsyntax/ast.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,13 @@ type mac_body = Option<mac_body_>;
842842
#[auto_serialize]
843843
#[auto_deserialize]
844844
enum mac_ {
845+
mac_invoc(@path, mac_arg, mac_body), // old macro-invocation
845846
mac_invoc_tt(@path,~[token_tree]), // new macro-invocation
847+
mac_ellipsis, // old pattern-match (obsolete)
848+
849+
// the span is used by the quoter/anti-quoter ...
850+
mac_aq(span /* span of quote */, @expr), // anti-quote
851+
mac_var(uint)
846852
}
847853

848854
type lit = spanned<lit_>;

branches/try2/src/libsyntax/ext/auto_serialize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ priv impl ext_ctxt {
309309
fn lambda(blk: ast::blk) -> @ast::expr {
310310
let ext_cx = self;
311311
let blk_e = self.expr(blk.span, ast::expr_block(blk));
312-
quote_expr!( || $blk_e )
312+
#ast{ || $(blk_e) }
313313
}
314314

315315
fn blk(span: span, stmts: ~[@ast::stmt]) -> ast::blk {

branches/try2/src/libsyntax/ext/base.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ use ast_util::dummy_sp;
1616

1717
// obsolete old-style #macro code:
1818
//
19-
// syntax_expander, normal, builtin
19+
// syntax_expander, normal, macro_defining, macro_definer,
20+
// builtin
2021
//
2122
// new-style macro! tt code:
2223
//
2324
// syntax_expander_tt, syntax_expander_tt_item, mac_result,
2425
// normal_tt, item_tt
2526
//
26-
// also note that ast::mac used to have a bunch of extraneous cases and
27-
// is now probably a redundant AST node, can be merged with
28-
// ast::mac_invoc_tt.
27+
// also note that ast::mac has way too many cases and can probably
28+
// be trimmed down substantially.
2929

3030
// second argument is the span to blame for general argument problems
3131
type syntax_expander_ =
@@ -35,6 +35,10 @@ type syntax_expander = {expander: syntax_expander_, span: Option<span>};
3535

3636
type macro_def = {name: ~str, ext: syntax_extension};
3737

38+
// macro_definer is obsolete, remove when #old_macros go away.
39+
type macro_definer =
40+
fn@(ext_ctxt, span, ast::mac_arg, ast::mac_body) -> macro_def;
41+
3842
type item_decorator =
3943
fn@(ext_ctxt, span, ast::meta_item, ~[@ast::item]) -> ~[@ast::item];
4044

@@ -59,6 +63,9 @@ enum syntax_extension {
5963
// normal() is obsolete, remove when #old_macros go away.
6064
normal(syntax_expander),
6165

66+
// macro_defining() is obsolete, remove when #old_macros go away.
67+
macro_defining(macro_definer),
68+
6269
// #[auto_serialize] and such. will probably survive death of #old_macros
6370
item_decorator(item_decorator),
6471

@@ -82,6 +89,8 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
8289
item_tt({expander: f, span: None})
8390
}
8491
let syntax_expanders = HashMap();
92+
syntax_expanders.insert(~"macro",
93+
macro_defining(ext::simplext::add_new_extension));
8594
syntax_expanders.insert(~"macro_rules",
8695
builtin_item_tt(
8796
ext::tt::macro_rules::add_new_extension));
@@ -100,6 +109,8 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
100109
syntax_expanders.insert(~"log_syntax",
101110
builtin_normal_tt(
102111
ext::log_syntax::expand_syntax_ext));
112+
syntax_expanders.insert(~"ast",
113+
builtin(ext::qquote::expand_ast));
103114
syntax_expanders.insert(~"deriving_eq",
104115
item_decorator(
105116
ext::deriving::expand_deriving_eq));

branches/try2/src/libsyntax/ext/expand.rs

Lines changed: 82 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111
use std::map::HashMap;
1212

13-
use ast::{crate, expr_, expr_mac, mac_invoc_tt,
13+
use ast::{crate, expr_, expr_mac, mac_invoc, mac_invoc_tt,
1414
tt_delim, tt_tok, item_mac, stmt_, stmt_mac, stmt_expr, stmt_semi};
1515
use fold::*;
1616
use ext::base::*;
17+
use ext::qquote::{qq_helper};
1718
use parse::{parser, parse_expr_from_source_str, new_parser_from_tts};
1819

1920

@@ -31,6 +32,51 @@ fn expand_expr(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt,
3132
expr_mac(ref mac) => {
3233

3334
match (*mac).node {
35+
// Old-style macros. For compatibility, will erase this whole
36+
// block once we've transitioned.
37+
mac_invoc(pth, args, body) => {
38+
assert (vec::len(pth.idents) > 0u);
39+
/* using idents and token::special_idents would make the
40+
the macro names be hygienic */
41+
let extname = cx.parse_sess().interner.get(pth.idents[0]);
42+
match exts.find(*extname) {
43+
None => {
44+
cx.span_fatal(pth.span,
45+
fmt!("macro undefined: '%s'", *extname))
46+
}
47+
Some(item_decorator(_)) => {
48+
cx.span_fatal(
49+
pth.span,
50+
fmt!("%s can only be used as a decorator", *extname));
51+
}
52+
Some(normal({expander: exp, span: exp_sp})) => {
53+
54+
cx.bt_push(ExpandedFrom({call_site: s,
55+
callie: {name: *extname, span: exp_sp}}));
56+
let expanded = exp(cx, (*mac).span, args, body);
57+
58+
//keep going, outside-in
59+
let fully_expanded = fld.fold_expr(expanded).node;
60+
cx.bt_pop();
61+
62+
(fully_expanded, s)
63+
}
64+
Some(macro_defining(ext)) => {
65+
let named_extension = ext(cx, (*mac).span, args, body);
66+
exts.insert(named_extension.name, named_extension.ext);
67+
(ast::expr_rec(~[], None), s)
68+
}
69+
Some(normal_tt(_)) => {
70+
cx.span_fatal(pth.span,
71+
fmt!("this tt-style macro should be \
72+
invoked '%s!(...)'", *extname))
73+
}
74+
Some(item_tt(*)) => {
75+
cx.span_fatal(pth.span,
76+
~"cannot use item macros in this context");
77+
}
78+
}
79+
}
3480

3581
// Token-tree macros, these will be the only case when we're
3682
// finished transitioning.
@@ -85,6 +131,7 @@ fn expand_expr(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt,
85131

86132
}
87133
}
134+
_ => cx.span_bug((*mac).span, ~"naked syntactic bit")
88135
}
89136
}
90137
_ => orig(e, s, fld)
@@ -119,15 +166,10 @@ fn expand_mod_items(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt,
119166
ast::meta_list(ref n, _) => (*n)
120167
};
121168
match exts.find(mname) {
122-
None | Some(normal(_))
123-
| Some(normal_tt(_)) | Some(item_tt(*)) => items,
169+
None | Some(normal(_)) | Some(macro_defining(_))
170+
| Some(normal_tt(_)) | Some(item_tt(*)) => items,
124171
Some(item_decorator(dec_fn)) => {
125-
cx.bt_push(ExpandedFrom({call_site: attr.span,
126-
callie: {name: copy mname,
127-
span: None}}));
128-
let r = dec_fn(cx, attr.span, attr.node.value, items);
129-
cx.bt_pop();
130-
r
172+
dec_fn(cx, attr.span, attr.node.value, items)
131173
}
132174
}
133175
}
@@ -163,16 +205,36 @@ fn expand_item(exts: HashMap<~str, syntax_extension>,
163205
}
164206
}
165207

208+
// avoid excess indentation when a series of nested `match`es
209+
// has only one "good" outcome
210+
macro_rules! biased_match (
211+
( ($e :expr) ~ ($p :pat) else $err :stmt ;
212+
$( ($e_cdr:expr) ~ ($p_cdr:pat) else $err_cdr:stmt ; )*
213+
=> $body:expr
214+
) => (
215+
match $e {
216+
$p => {
217+
biased_match!($( ($e_cdr) ~ ($p_cdr) else $err_cdr ; )*
218+
=> $body)
219+
}
220+
_ => { $err }
221+
}
222+
);
223+
( => $body:expr ) => ( $body )
224+
)
225+
226+
166227
// Support for item-position macro invocations, exactly the same
167228
// logic as for expression-position macro invocations.
168229
fn expand_item_mac(exts: HashMap<~str, syntax_extension>,
169230
cx: ext_ctxt, &&it: @ast::item,
170231
fld: ast_fold) -> Option<@ast::item> {
171-
172-
let (pth, tts) = match it.node {
173-
item_mac({node: mac_invoc_tt(pth, ref tts), _}) => (pth, (*tts)),
174-
_ => cx.span_bug(it.span, ~"invalid item macro invocation")
175-
};
232+
let (pth, tts) = biased_match!(
233+
(it.node) ~ (item_mac({node: mac_invoc_tt(pth, ref tts), _})) else {
234+
cx.span_bug(it.span, ~"invalid item macro invocation")
235+
};
236+
=> (pth, (*tts))
237+
);
176238
177239
let extname = cx.parse_sess().interner.get(pth.idents[0]);
178240
let expanded = match exts.find(*extname) {
@@ -227,15 +289,12 @@ fn expand_stmt(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt,
227289
orig: fn@(&&s: stmt_, span, ast_fold) -> (stmt_, span))
228290
-> (stmt_, span)
229291
{
230-
231-
let (mac, pth, tts, semi) = match s {
232-
stmt_mac(ref mac, semi) => {
233-
match (*mac).node {
234-
mac_invoc_tt(pth, ref tts) => ((*mac), pth, (*tts), semi)
235-
}
236-
}
237-
_ => return orig(s, sp, fld)
238-
};
292+
let (mac, pth, tts, semi) = biased_match! (
293+
(s) ~ (stmt_mac(ref mac, semi)) else return orig(s, sp, fld);
294+
((*mac).node) ~ (mac_invoc_tt(pth, ref tts)) else {
295+
cx.span_bug((*mac).span, ~"naked syntactic bit")
296+
};
297+
=> ((*mac), pth, (*tts), semi));
239298
240299
assert(vec::len(pth.idents) == 1u);
241300
let extname = cx.parse_sess().interner.get(pth.idents[0]);

0 commit comments

Comments
 (0)