@@ -3,6 +3,14 @@ import ast;
3
3
import codemap:: span;
4
4
import print:: pprust;
5
5
6
+ export expand_line;
7
+ export expand_col;
8
+ export expand_file;
9
+ export expand_stringify;
10
+ export expand_mod;
11
+ export expand_include;
12
+ export expand_include_str;
13
+
6
14
/* #line(): expands to the current line number */
7
15
fn expand_line ( cx : ext_ctxt , sp : span , arg : ast:: mac_arg ,
8
16
_body : ast:: mac_body ) -> @ast:: expr {
@@ -35,19 +43,53 @@ fn expand_stringify(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
35
43
ret make_new_lit ( cx, sp, ast:: lit_str ( pprust:: expr_to_str ( args[ 0 ] ) ) ) ;
36
44
}
37
45
46
+ fn expand_mod ( cx : ext_ctxt , sp : span , arg : ast:: mac_arg , _body : ast:: mac_body )
47
+ -> @ast:: expr {
48
+ get_mac_args ( cx, sp, arg, 0 u, option:: some ( 0 u) , "file" ) ;
49
+ ret make_new_lit ( cx, sp, ast:: lit_str ( str:: connect ( cx. mod_path ( ) , "::" ) ) ) ;
50
+ }
51
+
38
52
fn expand_include ( cx : ext_ctxt , sp : span , arg : ast:: mac_arg ,
39
53
_body : ast:: mac_body ) -> @ast:: expr {
40
54
let args = get_mac_args ( cx, sp, arg, 1 u, option:: some ( 1 u) , "include" ) ;
41
- let loc = codemap:: lookup_char_pos ( cx. codemap ( ) , sp. lo ) ;
42
- let path = path:: connect ( path:: dirname ( loc. file . name ) ,
43
- expr_to_str ( cx, args[ 0 ] , "#include requires a string literal" ) ) ;
44
- let p = parse:: new_parser_from_file ( cx. parse_sess ( ) , cx. cfg ( ) , path,
55
+ let file = expr_to_str ( cx, args[ 0 ] , "#include_str requires a string" ) ;
56
+ let p = parse:: new_parser_from_file ( cx. parse_sess ( ) , cx. cfg ( ) ,
57
+ res_rel_file ( cx, sp, file) ,
45
58
parse:: parser:: SOURCE_FILE ) ;
46
59
ret parse:: parser:: parse_expr ( p)
47
60
}
48
61
49
- fn expand_mod ( cx : ext_ctxt , sp : span , arg : ast:: mac_arg , _body : ast:: mac_body )
50
- -> @ast:: expr {
51
- get_mac_args ( cx, sp, arg, 0 u, option:: some ( 0 u) , "file" ) ;
52
- ret make_new_lit ( cx, sp, ast:: lit_str ( str:: connect ( cx. mod_path ( ) , "::" ) ) ) ;
62
+ fn expand_include_str ( cx : ext_ctxt , sp : codemap:: span , arg : ast:: mac_arg ,
63
+ _body : ast:: mac_body ) -> @ast:: expr {
64
+ let args = get_mac_args ( cx, sp, arg, 1 u, option:: some ( 1 u) , "include_str" ) ;
65
+
66
+ let file = expr_to_str ( cx, args[ 0 ] , "#include_str requires a string" ) ;
67
+
68
+ alt io:: read_whole_file_str ( res_rel_file ( cx, sp, file) ) {
69
+ result:: ok ( src) { ret make_new_lit ( cx, sp, ast:: lit_str ( src) ) ; }
70
+ result:: err ( e) {
71
+ cx. parse_sess ( ) . span_diagnostic . handler ( ) . fatal ( e)
72
+ }
73
+ }
74
+ }
75
+
76
+ fn res_rel_file ( cx : ext_ctxt , sp : codemap:: span , arg : path ) -> path {
77
+ // NB: relative paths are resolved relative to the compilation unit
78
+ if !path:: path_is_absolute ( arg) {
79
+ let cu = codemap:: span_to_filename ( sp, cx. codemap ( ) ) ;
80
+ let dir = path:: dirname ( cu) ;
81
+ ret path:: connect ( dir, arg) ;
82
+ } else {
83
+ ret arg;
84
+ }
53
85
}
86
+
87
+ //
88
+ // Local Variables:
89
+ // mode: rust
90
+ // fill-column: 78;
91
+ // indent-tabs-mode: nil
92
+ // c-basic-offset: 4
93
+ // buffer-file-coding-system: utf-8-unix
94
+ // End:
95
+ //
0 commit comments