rustc: Generate crates with #ast

This commit is contained in:
Brian Anderson 2012-02-21 15:34:26 -08:00
parent a896eb326e
commit 77a01054fa
3 changed files with 25 additions and 0 deletions

View File

@ -30,6 +30,16 @@ iface qq_helper {
fn mk_parse_fn(ext_ctxt,span) -> @ast::expr;
fn get_fold_fn() -> str;
}
impl of qq_helper for @ast::crate {
fn span() -> span {self.span}
fn visit(cx: aq_ctxt, v: vt<aq_ctxt>) {visit_crate(*self, cx, v);}
fn extract_mac() -> option<ast::mac_> {fail}
fn mk_parse_fn(cx: ext_ctxt, sp: span) -> @ast::expr {
mk_path(cx, sp, ["syntax", "ext", "qquote", "parse_crate"])
}
fn get_fold_fn() -> str {"fold_crate"}
}
impl of qq_helper for @ast::expr {
fn span() -> span {self.span}
fn visit(cx: aq_ctxt, v: vt<aq_ctxt>) {visit_expr(self, cx, v);}
@ -145,6 +155,7 @@ fn expand_ast(ecx: ext_ctxt, _sp: span,
let body = get_mac_body(ecx,_sp,body);
ret alt what {
"crate" {finish(ecx, body, parse_crate)}
"expr" {finish(ecx, body, parser::parse_expr)}
"ty" {finish(ecx, body, parse_ty)}
"item" {finish(ecx, body, parse_item)}
@ -154,6 +165,10 @@ fn expand_ast(ecx: ext_ctxt, _sp: span,
};
}
fn parse_crate(p: parser) -> @ast::crate {
parser::parse_crate_mod(p, [])
}
fn parse_ty(p: parser) -> @ast::ty {
parser::parse_ty(p, false)
}
@ -265,6 +280,9 @@ fn replace<T>(node: T, repls: [fragment], ff: fn (ast_fold, T) -> T)
with *aft};
ret ff(make_fold(f_pre), node);
}
fn fold_crate(f: ast_fold, &&n: @ast::crate) -> @ast::crate {
@f.fold_crate(*n)
}
fn fold_expr(f: ast_fold, &&n: @ast::expr) -> @ast::expr {f.fold_expr(n)}
fn fold_ty(f: ast_fold, &&n: @ast::ty) -> @ast::ty {f.fold_ty(n)}
fn fold_item(f: ast_fold, &&n: @ast::item) -> @ast::item {f.fold_item(n)}

View File

@ -71,6 +71,10 @@ fn print_crate(cm: codemap, span_diagnostic: diagnostic::span_handler,
mutable cur_lit: 0u,
mutable boxes: boxes,
ann: ann};
print_crate_(s, crate);
}
fn print_crate_(s: ps, &&crate: @ast::crate) {
print_mod(s, crate.node.module, crate.node.attrs);
print_remaining_comments(s);
eof(s.s);

View File

@ -88,6 +88,9 @@ fn main() {
let y = #ast{2};
let test3 = #ast{$(x) + $(y)};
check_pp(test3, pprust::print_expr, "1 + 2");
let crate = #ast(crate) { fn a() { } };
check_pp(crate, pprust::print_crate_, "fn a() { }\n");
}
fn check_pp<T>(expr: T, f: fn(pprust::ps, T), expect: str) {