Skip to content

Commit 88f4d06

Browse files
committed
New syntax extensions: #line[], #col[], #file[], #stringify[], #include[]
1 parent 9fe547d commit 88f4d06

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

src/librustsyntax/ext/base.rs

+10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ fn syntax_expander_table() -> hashmap<str, syntax_extension> {
4343
builtin(ext::log_syntax::expand_syntax_ext));
4444
syntax_expanders.insert("ast",
4545
builtin(ext::qquote::expand_ast));
46+
syntax_expanders.insert("line",
47+
builtin(ext::source_util::expand_line));
48+
syntax_expanders.insert("col",
49+
builtin(ext::source_util::expand_col));
50+
syntax_expanders.insert("file",
51+
builtin(ext::source_util::expand_file));
52+
syntax_expanders.insert("stringify",
53+
builtin(ext::source_util::expand_stringify));
54+
syntax_expanders.insert("include",
55+
builtin(ext::source_util::expand_include));
4656
ret syntax_expanders;
4757
}
4858

src/librustsyntax/ext/source_util.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

src/librustsyntax/rustsyntax.rc

+1
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,5 @@ mod ext {
6666
mod include;
6767
mod log_syntax;
6868
mod auto_serialize;
69+
mod source_util;
6970
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* this is for run-pass/syntax-extension-source-utils.rs */
2+
3+
{
4+
assert(#file[].ends_with("utils-files/includeme.fragment"));
5+
assert(#line[] == 5u);
6+
#fmt["victory robot %u", #line[]]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This test is brittle!
2+
// xfail-pretty - the pretty tests lose path information, breaking #include
3+
4+
fn main() {
5+
assert(#line[] == 5u);
6+
assert(#col[] == 12u);
7+
assert(#file[].ends_with("syntax-extension-source-utils.rs"));
8+
assert(#stringify[(2*3) + 5] == "2 * 3 + 5");
9+
assert(#include["syntax-extension-source-utils-files/includeme.fragment"]
10+
== "victory robot 6")
11+
}

0 commit comments

Comments
 (0)