Skip to content

Commit 80cf4ec

Browse files
committed
Add nodes for embedding types and blocks in expressions for macros.
1 parent fd24fd5 commit 80cf4ec

File tree

7 files changed

+58
-45
lines changed

7 files changed

+58
-45
lines changed

src/comp/syntax/ast.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ tag expr_ {
323323
to expr_if_check. */
324324
expr_if_check(@expr, block, option::t[@expr]);
325325
expr_port(option::t[@ty]);
326-
expr_chan(@expr);
327326
expr_anon_obj(anon_obj, ty_param[]);
328327
}
329328

src/comp/syntax/fold.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,12 @@ fn noop_fold_expr(&expr_ e, ast_fold fld) -> expr_ {
445445
case (expr_anon_obj(?ao, ?typms)) {
446446
expr_anon_obj(fold_anon_obj(ao), typms)
447447
}
448+
case (expr_embeded_type(?ty)) {
449+
expr_embeded_type(fld.fold_ty(ty))
450+
}
451+
case (expr_embeded_block(?blk)) {
452+
expr_embeded_block(fld.fold_block(blk))
453+
}
448454
}
449455
}
450456

@@ -695,22 +701,6 @@ fn make_fold(&ast_fold_precursor afp) -> ast_fold {
695701
fold_path = bind f_path(afp,result,_),
696702
fold_local = bind f_local(afp,result,_));
697703
ret result;
698-
/*
699-
ret rec(fold_crate = noop_fold_crate,
700-
fold_crate_directive = noop_fold_crate_drective,
701-
fold_view_item = noop_fold_view_item,
702-
fold_native_item = noop_fold_native_item,
703-
fold_item = noop_fold_item,
704-
fold_method = noop_fold_method,
705-
fold_block = noop_fold_block,
706-
fold_stmt = noop_fold_stmt,
707-
fold_arm = noop_fold_arm,
708-
fold_pat = noop_fold_pat,
709-
fold_decl = noop_fold_decl,
710-
fold_expr = noop_fold_expr,
711-
fold_ty = noop_fold_ty,
712-
fold_constr = noop_fold_constr,
713-
fold_fn = noop_fold_fn);*/
714704
}
715705

716706

src/comp/syntax/parse/lexer.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,8 @@ fn next_token(&reader rdr) -> token::token {
361361
} else { ret token::BINOP(op); }
362362
}
363363
alt (c) {
364-
case (
365-
// One-byte tokens.
366-
'?') {
367-
rdr.bump();
368-
ret token::QUES;
369-
}
364+
// One-byte tokens.
365+
case ('?') { rdr.bump(); ret token::QUES; }
370366
case (';') { rdr.bump(); ret token::SEMI; }
371367
case (',') { rdr.bump(); ret token::COMMA; }
372368
case ('.') { rdr.bump(); ret token::DOT; }
@@ -377,7 +373,18 @@ fn next_token(&reader rdr) -> token::token {
377373
case ('[') { rdr.bump(); ret token::LBRACKET; }
378374
case (']') { rdr.bump(); ret token::RBRACKET; }
379375
case ('@') { rdr.bump(); ret token::AT; }
380-
case ('#') { rdr.bump(); ret token::POUND; }
376+
case ('#') {
377+
rdr.bump();
378+
if (rdr.curr() == '<') {
379+
rdr.bump();
380+
ret token::POUND_LT;
381+
}
382+
if (rdr.curr() == '{') {
383+
rdr.bump();
384+
ret token::POUND_LBRACE;
385+
}
386+
ret token::POUND;
387+
}
381388
case ('~') { rdr.bump(); ret token::TILDE; }
382389
case (':') {
383390
rdr.bump();
@@ -386,9 +393,8 @@ fn next_token(&reader rdr) -> token::token {
386393
ret token::MOD_SEP;
387394
} else { ret token::COLON; }
388395
}
389-
case (
390-
// Multi-byte tokens.
391-
'=') {
396+
// Multi-byte tokens.
397+
case ('=') {
392398
rdr.bump();
393399
if (rdr.curr() == '=') {
394400
rdr.bump();

src/comp/syntax/parse/parser.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,13 @@ fn parse_bottom_expr(&parser p) -> @ast::expr {
804804
parse_seq_to_end_ivec(token::RBRACKET, some(token::COMMA),
805805
parse_expr, p);
806806
ex = ast::expr_vec(es, mut, ast::sk_rc);
807+
} else if (p.peek() == token::POUND_LT) {
808+
p.bump();
809+
ex = ast::expr_embeded_type(parse_ty(p));
810+
expect(p, token::GT);
811+
} else if (p.peek() == token::POUND_LBRACE) {
812+
p.bump();
813+
ex = ast::expr_embeded_block(parse_block_tail(p));
807814
} else if (p.peek() == token::TILDE) {
808815
p.bump();
809816
alt (p.peek()) {
@@ -1715,10 +1722,15 @@ fn stmt_ends_with_semi(&ast::stmt stmt) -> bool {
17151722
}
17161723

17171724
fn parse_block(&parser p) -> ast::block {
1725+
expect(p, token::LBRACE);
1726+
be parse_block_tail(p);
1727+
}
1728+
1729+
// some blocks start with "#{"...
1730+
fn parse_block_tail(&parser p) -> ast::block {
17181731
auto lo = p.get_lo_pos();
17191732
let (@ast::stmt)[] stmts = ~[];
17201733
let option::t[@ast::expr] expr = none;
1721-
expect(p, token::LBRACE);
17221734
while (p.peek() != token::RBRACE) {
17231735
alt (p.peek()) {
17241736
case (token::SEMI) {
@@ -2204,8 +2216,10 @@ fn parse_outer_attrs_or_ext(&parser p) -> attr_or_ext {
22042216
if (p.peek() == token::LBRACKET) {
22052217
auto first_attr = parse_attribute_naked(p, ast::attr_outer, lo);
22062218
ret some(left(~[first_attr] + parse_outer_attributes(p)));
2207-
} else {
2219+
} else if (! (p.peek() == token::LT || p.peek() == token::LBRACKET)) {
22082220
ret some(right(parse_syntax_ext_naked(p, lo)));
2221+
} else {
2222+
ret none;
22092223
}
22102224
} else {
22112225
ret none;

src/comp/syntax/parse/token.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ tag token {
6060
LBRACE;
6161
RBRACE;
6262
POUND;
63+
POUND_LBRACE;
64+
POUND_LT;
6365

6466
/* Literals */
6567
LIT_INT(int);
@@ -110,11 +112,8 @@ fn to_str(lexer::reader r, token t) -> str {
110112
case (ANDAND) { ret "&&"; }
111113
case (BINOP(?op)) { ret binop_to_str(op); }
112114
case (BINOPEQ(?op)) { ret binop_to_str(op) + "="; }
113-
case (
114115
/* Structural symbols */
115-
AT) {
116-
ret "@";
117-
}
116+
case (AT) { ret "@"; }
118117
case (DOT) { ret "."; }
119118
case (COMMA) { ret ","; }
120119
case (SEMI) { ret ";"; }
@@ -133,11 +132,10 @@ fn to_str(lexer::reader r, token t) -> str {
133132
case (LBRACE) { ret "{"; }
134133
case (RBRACE) { ret "}"; }
135134
case (POUND) { ret "#"; }
136-
case (
135+
case (POUND_LBRACE) { ret "#{"; }
136+
case (POUND_LT) { ret "#<"; }
137137
/* Literals */
138-
LIT_INT(?i)) {
139-
ret int::to_str(i, 10u);
140-
}
138+
case (LIT_INT(?i)) { ret int::to_str(i, 10u); }
141139
case (LIT_UINT(?u)) { ret uint::to_str(u, 10u); }
142140
case (LIT_MACH_INT(?tm, ?i)) {
143141
ret int::to_str(i, 10u) + "_" + ty_mach_to_str(tm);
@@ -147,25 +145,19 @@ fn to_str(lexer::reader r, token t) -> str {
147145
ty_mach_to_str(tm);
148146
}
149147
case (LIT_FLOAT(?s)) { ret interner::get[str](*r.get_interner(), s); }
150-
case (LIT_STR(?s)) {
151-
// FIXME: escape.
152-
148+
case (LIT_STR(?s)) { // FIXME: escape.
153149
ret "\"" + interner::get[str](*r.get_interner(), s) + "\"";
154150
}
155151
case (LIT_CHAR(?c)) {
156152
// FIXME: escape.
157-
158153
auto tmp = "'";
159154
str::push_char(tmp, c);
160155
str::push_byte(tmp, '\'' as u8);
161156
ret tmp;
162157
}
163158
case (LIT_BOOL(?b)) { if (b) { ret "true"; } else { ret "false"; } }
164-
case (
165159
/* Name components */
166-
IDENT(?s, _)) {
167-
ret interner::get[str](*r.get_interner(), s);
168-
}
160+
case (IDENT(?s, _)) { ret interner::get[str](*r.get_interner(), s); }
169161
case (IDX(?i)) { ret "_" + int::to_str(i, 10u); }
170162
case (UNDERSCORE) { ret "_"; }
171163
case (BRACEQUOTE(_)) { ret "<bracequote>"; }

src/comp/syntax/visit.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,12 @@ fn visit_expr[E](&@expr ex, &E e, &vt[E] v) {
397397
m.node.id, e, v);
398398
}
399399
}
400+
case (expr_embeded_type(?ty)) {
401+
vt(v).visit_ty(ty, e, v);
402+
}
403+
case (expr_embeded_block(?blk)) {
404+
vt(v).visit_block(blk, e, v);
405+
}
400406
}
401407
}
402408

src/comp/syntax/walk.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,12 @@ fn walk_expr(&ast_visitor v, @ast::expr e) {
410410
v.visit_method_post(m);
411411
}
412412
}
413+
case (ast::expr_embeded_type(?ty)) {
414+
walk_ty(v, ty);
415+
}
416+
case (ast::expr_embeded_block(?blk)) {
417+
walk_block(v, blk);
418+
}
413419
}
414420
v.visit_expr_post(e);
415421
}

0 commit comments

Comments
 (0)