From 77a01054fa5ee5e17faece9d68ecec9bf593778f Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 21 Feb 2012 15:34:26 -0800 Subject: [PATCH] rustc: Generate crates with #ast --- src/comp/syntax/ext/qquote.rs | 18 ++++++++++++++++++ src/comp/syntax/print/pprust.rs | 4 ++++ src/test/run-pass/qquote.rs | 3 +++ 3 files changed, 25 insertions(+) diff --git a/src/comp/syntax/ext/qquote.rs b/src/comp/syntax/ext/qquote.rs index 6d2a9e04d727..3476584aecec 100644 --- a/src/comp/syntax/ext/qquote.rs +++ b/src/comp/syntax/ext/qquote.rs @@ -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) {visit_crate(*self, cx, v);} + fn extract_mac() -> option {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) {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(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)} diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs index 3d0e8a579dcc..7159d7310d67 100644 --- a/src/comp/syntax/print/pprust.rs +++ b/src/comp/syntax/print/pprust.rs @@ -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); diff --git a/src/test/run-pass/qquote.rs b/src/test/run-pass/qquote.rs index 46330d119b6c..f5fa14dc8578 100644 --- a/src/test/run-pass/qquote.rs +++ b/src/test/run-pass/qquote.rs @@ -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(expr: T, f: fn(pprust::ps, T), expect: str) {