diff --git a/src/comp/syntax/ast.rs b/src/comp/syntax/ast.rs index 8ed392d011b7..235485fe3f51 100644 --- a/src/comp/syntax/ast.rs +++ b/src/comp/syntax/ast.rs @@ -272,11 +272,13 @@ enum blk_sort { type mac = spanned; -type mac_body_ = {str: str, span: span}; +type mac_arg = @expr; + +type mac_body_ = {span: span}; type mac_body = option::t; enum mac_ { - mac_invoc(@path, @expr, mac_body), + mac_invoc(@path, mac_arg, mac_body), mac_embed_type(@ty), mac_embed_block(blk), mac_ellipsis, diff --git a/src/comp/syntax/ext/base.rs b/src/comp/syntax/ext/base.rs index d15d74c89518..824931a759b7 100644 --- a/src/comp/syntax/ext/base.rs +++ b/src/comp/syntax/ext/base.rs @@ -6,10 +6,10 @@ import std::map::new_str_hash; import codemap; type syntax_expander = - fn@(ext_ctxt, span, @ast::expr, ast::mac_body) -> @ast::expr; + fn@(ext_ctxt, span, ast::mac_arg, ast::mac_body) -> @ast::expr; type macro_def = {ident: str, ext: syntax_extension}; type macro_definer = - fn@(ext_ctxt, span, @ast::expr, ast::mac_body) -> macro_def; + fn@(ext_ctxt, span, ast::mac_arg, ast::mac_body) -> macro_def; enum syntax_extension { normal(syntax_expander), @@ -118,7 +118,14 @@ fn make_new_lit(cx: ext_ctxt, sp: codemap::span, lit: ast::lit_) -> ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp}; } - +fn get_mac_body(cx: ext_ctxt, sp: span, args: ast::mac_body) + -> ast::mac_body_ +{ + alt (args) { + some(body) {body} + none {cx.span_fatal(sp, "missing macro body")} + } +} // // Local Variables: diff --git a/src/comp/syntax/ext/concat_idents.rs b/src/comp/syntax/ext/concat_idents.rs index 3a4c59e4960c..b451e449b308 100644 --- a/src/comp/syntax/ext/concat_idents.rs +++ b/src/comp/syntax/ext/concat_idents.rs @@ -2,7 +2,7 @@ import option; import base::*; import syntax::ast; -fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: @ast::expr, +fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg, _body: ast::mac_body) -> @ast::expr { let args: [@ast::expr] = alt arg.node { diff --git a/src/comp/syntax/ext/env.rs b/src/comp/syntax/ext/env.rs index 509f721127c9..8cf299c5e67b 100644 --- a/src/comp/syntax/ext/env.rs +++ b/src/comp/syntax/ext/env.rs @@ -9,7 +9,7 @@ import std::generic_os; import base::*; export expand_syntax_ext; -fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: @ast::expr, +fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg, _body: ast::mac_body) -> @ast::expr { let args: [@ast::expr] = alt arg.node { diff --git a/src/comp/syntax/ext/fmt.rs b/src/comp/syntax/ext/fmt.rs index b0a346131d63..b2425079ed8e 100644 --- a/src/comp/syntax/ext/fmt.rs +++ b/src/comp/syntax/ext/fmt.rs @@ -13,7 +13,7 @@ import codemap::span; import syntax::ext::build::*; export expand_syntax_ext; -fn expand_syntax_ext(cx: ext_ctxt, sp: span, arg: @ast::expr, +fn expand_syntax_ext(cx: ext_ctxt, sp: span, arg: ast::mac_arg, _body: ast::mac_body) -> @ast::expr { let args: [@ast::expr] = alt arg.node { diff --git a/src/comp/syntax/ext/ident_to_str.rs b/src/comp/syntax/ext/ident_to_str.rs index f5e61bbae3ed..a5a2dedcbf92 100644 --- a/src/comp/syntax/ext/ident_to_str.rs +++ b/src/comp/syntax/ext/ident_to_str.rs @@ -2,7 +2,7 @@ import core::{vec, option}; import base::*; import syntax::ast; -fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: @ast::expr, +fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg, _body: ast::mac_body) -> @ast::expr { let args: [@ast::expr] = alt arg.node { diff --git a/src/comp/syntax/ext/log_syntax.rs b/src/comp/syntax/ext/log_syntax.rs index bfdd336e1ef0..4047a1b95d31 100644 --- a/src/comp/syntax/ext/log_syntax.rs +++ b/src/comp/syntax/ext/log_syntax.rs @@ -2,9 +2,8 @@ import base::*; import syntax::ast; import std::io::writer_util; -fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: @ast::expr, +fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg, _body: ast::mac_body) -> @ast::expr { - cx.print_backtrace(); std::io::stdout().write_line(print::pprust::expr_to_str(arg)); diff --git a/src/comp/syntax/ext/simplext.rs b/src/comp/syntax/ext/simplext.rs index d35c76838fce..b8722122b696 100644 --- a/src/comp/syntax/ext/simplext.rs +++ b/src/comp/syntax/ext/simplext.rs @@ -6,7 +6,7 @@ import std::map::{hashmap, new_str_hash}; import option::{some, none}; import driver::session::session; -import base::{ext_ctxt, normal}; +import base::*; import fold::*; import ast_util::respan; @@ -669,7 +669,7 @@ fn p_t_s_r_actual_vector(cx: ext_ctxt, elts: [@expr], _repeat_after: bool, } } -fn add_new_extension(cx: ext_ctxt, sp: span, arg: @expr, +fn add_new_extension(cx: ext_ctxt, sp: span, arg: ast::mac_arg, _body: ast::mac_body) -> base::macro_def { let args: [@ast::expr] = alt arg.node { @@ -715,8 +715,10 @@ fn add_new_extension(cx: ext_ctxt, sp: span, arg: @expr, } } clauses += - [@{params: pattern_to_selectors(cx, invoc_arg), + [@{params: pattern_to_selectors + (cx, invoc_arg), body: elts[1u]}]; + // FIXME: check duplicates (or just simplify // the macro arg situation) } @@ -753,7 +755,7 @@ fn add_new_extension(cx: ext_ctxt, sp: span, arg: @expr, }, ext: normal(ext)}; - fn generic_extension(cx: ext_ctxt, sp: span, arg: @expr, + fn generic_extension(cx: ext_ctxt, sp: span, arg: ast::mac_arg, _body: ast::mac_body, clauses: [@clause]) -> @expr { for c: @clause in clauses { alt use_selectors_to_bind(c.params, arg) { diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index ecbae8a1fc24..ae00c9fbce79 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -993,7 +993,24 @@ fn parse_syntax_ext_naked(p: parser, lo: uint) -> @ast::expr { }; let hi = es.span.hi; let e = mk_expr(p, es.span.lo, hi, ast::expr_vec(es.node, ast::imm)); - ret mk_mac_expr(p, lo, hi, ast::mac_invoc(pth, e, none)); + let b = none; + if p.token == token::LBRACE { + p.bump(); + let lo = p.span.lo; + let depth = 1u; + while (depth > 0u) { + alt (p.token) { + token::LBRACE {depth += 1u;} + token::RBRACE {depth -= 1u;} + token::EOF {p.fatal("unexpected EOF in macro body");} + _ {} + } + p.bump(); + } + let hi = p.last_span.hi; + b = some({span: mk_sp(lo,hi)}); + } + ret mk_mac_expr(p, lo, p.span.hi, ast::mac_invoc(pth, e, b)); } fn parse_dot_or_call_expr(p: parser) -> pexpr {