|
| 1 | +// xfail-pretty |
| 2 | + |
| 3 | +use std; |
| 4 | +use rustc; |
| 5 | + |
| 6 | +import rustc::*; |
| 7 | +import std::io::*; |
| 8 | + |
| 9 | +import rustc::driver::diagnostic; |
| 10 | +import rustc::syntax::ast; |
| 11 | +import rustc::syntax::codemap; |
| 12 | +import rustc::syntax::parse::parser; |
| 13 | +import rustc::syntax::print::*; |
| 14 | + |
| 15 | +fn new_parse_sess() -> parser::parse_sess { |
| 16 | + let cm = codemap::new_codemap(); |
| 17 | + let handler = diagnostic::mk_handler(option::none); |
| 18 | + let sess = @{ |
| 19 | + cm: cm, |
| 20 | + mutable next_id: 1, |
| 21 | + span_diagnostic: diagnostic::mk_span_handler(handler, cm), |
| 22 | + mutable chpos: 0u, |
| 23 | + mutable byte_pos: 0u |
| 24 | + }; |
| 25 | + ret sess; |
| 26 | +} |
| 27 | + |
| 28 | +iface fake_ext_ctxt { |
| 29 | + fn session() -> fake_session; |
| 30 | +} |
| 31 | + |
| 32 | +type fake_options = {cfg: ast::crate_cfg}; |
| 33 | + |
| 34 | +type fake_session = {opts: @fake_options, |
| 35 | + parse_sess: parser::parse_sess}; |
| 36 | + |
| 37 | +impl of fake_ext_ctxt for fake_session { |
| 38 | + fn session() -> fake_session {self} |
| 39 | +} |
| 40 | + |
| 41 | +fn mk_ctxt() -> fake_ext_ctxt { |
| 42 | + let opts : fake_options = {cfg: []}; |
| 43 | + {opts: @opts, parse_sess: new_parse_sess()} as fake_ext_ctxt |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +fn main() { |
| 48 | + let ext_cx = mk_ctxt(); |
| 49 | + |
| 50 | + let abc = #ast{23}; |
| 51 | + check_pp(abc, pprust::print_expr, "23"); |
| 52 | + |
| 53 | + let expr = #ast{1 - $0 + 8}; |
| 54 | + check_pp(expr, pprust::print_expr, "1 - $0 + 8"); |
| 55 | + |
| 56 | + let expr2 = rustc::syntax::ext::qquote::replace(expr, [abc]); |
| 57 | + check_pp(expr2, pprust::print_expr, "1 - 23 + 8"); |
| 58 | + |
| 59 | + let expr3 = #ast{2 - $(abc) + 7}; |
| 60 | + check_pp(expr3, pprust::print_expr, "2 - 23 + 7"); |
| 61 | + |
| 62 | + let expr4 = #ast{2 - $(#(3)) + 9}; |
| 63 | + check_pp(expr4, pprust::print_expr, "2 - 3 + 9"); |
| 64 | + |
| 65 | + let ty = #ast(ty){option<int>}; |
| 66 | + check_pp(ty, pprust::print_type, "option<int>"); |
| 67 | + |
| 68 | + let item = #ast(item){const x : int = 10;}; |
| 69 | + check_pp(item, pprust::print_item, "const x: int = 10;"); |
| 70 | + |
| 71 | + let stmt = #ast(stmt){let x = 20;}; |
| 72 | + check_pp(*stmt, pprust::print_stmt, "let x = 20;"); |
| 73 | + |
| 74 | + let pat = #ast(pat){some(_)}; |
| 75 | + check_pp(pat, pprust::print_pat, "some(_)"); |
| 76 | +} |
| 77 | + |
| 78 | +fn check_pp<T>(expr: T, f: fn(pprust::ps, T), expect: str) { |
| 79 | + let buf = mk_mem_buffer(); |
| 80 | + let pp = pprust::rust_printer(buf as std::io::writer); |
| 81 | + f(pp, expr); |
| 82 | + pp::eof(pp.s); |
| 83 | + let str = mem_buffer_str(buf); |
| 84 | + stdout().write_line(str); |
| 85 | + if expect != "" {assert str == expect;} |
| 86 | +} |
| 87 | + |
0 commit comments