Add support for recognizing macro body, completely untested.
This commit is contained in:
parent
e76fdeb3a6
commit
5ea04c65c1
9 changed files with 43 additions and 16 deletions
|
|
@ -272,11 +272,13 @@ enum blk_sort {
|
|||
|
||||
type mac = spanned<mac_>;
|
||||
|
||||
type mac_body_ = {str: str, span: span};
|
||||
type mac_arg = @expr;
|
||||
|
||||
type mac_body_ = {span: span};
|
||||
type mac_body = option::t<mac_body_>;
|
||||
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue