|
| 1 | +import base::*; |
| 2 | +import ast; |
| 3 | +import codemap::span; |
| 4 | +import print::pprust; |
| 5 | + |
| 6 | + |
| 7 | +/* #line(): expands to the current line number */ |
| 8 | +fn expand_line(cx: ext_ctxt, sp: span, arg: ast::mac_arg, |
| 9 | + _body: ast::mac_body) -> @ast::expr { |
| 10 | + get_mac_args(cx, sp, arg, 0u, option::some(0u), "line"); |
| 11 | + let loc = codemap::lookup_char_pos(cx.codemap(), sp.lo); |
| 12 | + ret make_new_lit(cx, sp, ast::lit_uint(loc.line as u64, ast::ty_u)); |
| 13 | +} |
| 14 | + |
| 15 | +/* #col(): expands to the current column number */ |
| 16 | +fn expand_col(cx: ext_ctxt, sp: span, arg: ast::mac_arg, |
| 17 | + _body: ast::mac_body) -> @ast::expr { |
| 18 | + get_mac_args(cx, sp, arg, 0u, option::some(0u), "col"); |
| 19 | + let loc = codemap::lookup_char_pos(cx.codemap(), sp.lo); |
| 20 | + ret make_new_lit(cx, sp, ast::lit_uint(loc.col as u64, ast::ty_u)); |
| 21 | +} |
| 22 | + |
| 23 | +/* #file(): expands to the current filename */ |
| 24 | +/* The filemap (`loc.file`) contains a bunch more information we could spit |
| 25 | + * out if we wanted. */ |
| 26 | +fn expand_file(cx: ext_ctxt, sp: span, arg: ast::mac_arg, |
| 27 | + _body: ast::mac_body) -> @ast::expr { |
| 28 | + get_mac_args(cx, sp, arg, 0u, option::some(0u), "file"); |
| 29 | + let loc = codemap::lookup_char_pos(cx.codemap(), sp.lo); |
| 30 | + ret make_new_lit(cx, sp, ast::lit_str(loc.file.name)); |
| 31 | +} |
| 32 | + |
| 33 | +fn expand_stringify(cx: ext_ctxt, sp: span, arg: ast::mac_arg, |
| 34 | + _body: ast::mac_body) -> @ast::expr { |
| 35 | + let args = get_mac_args(cx, sp, arg, 1u, option::some(1u), "stringify"); |
| 36 | + ret make_new_lit(cx, sp, ast::lit_str(pprust::expr_to_str(args[0]))); |
| 37 | +} |
| 38 | + |
| 39 | +fn expand_include(cx: ext_ctxt, sp: span, arg: ast::mac_arg, |
| 40 | + _body: ast::mac_body) -> @ast::expr { |
| 41 | + let args = get_mac_args(cx, sp, arg, 1u, option::some(1u), "include"); |
| 42 | + let loc = codemap::lookup_char_pos(cx.codemap(), sp.lo); |
| 43 | + let path = path::connect(path::dirname(loc.file.name), |
| 44 | + expr_to_str(cx, args[0], "#include requires a string literal")); |
| 45 | + let p = parse::new_parser_from_file(cx.parse_sess(), cx.cfg(), path, |
| 46 | + parse::parser::SOURCE_FILE); |
| 47 | + ret parse::parser::parse_expr(p) |
| 48 | +} |
0 commit comments