Skip to content

Commit 4f61cd2

Browse files
committed
---
yaml --- r: 15864 b: refs/heads/try c: 88f4d06 h: refs/heads/master v: v3
1 parent 1c4b558 commit 4f61cd2

File tree

6 files changed

+78
-1
lines changed

6 files changed

+78
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: 9fe547d3a795f59217c0093c82aa5a5b86c65f72
5+
refs/heads/try: 88f4d0694187a451173d40e9c44db4499a6c04f4
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/librustsyntax/ext/base.rs

Lines changed: 10 additions & 0 deletions
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

Lines changed: 48 additions & 0 deletions
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+
}

branches/try/src/librustsyntax/rustsyntax.rc

Lines changed: 1 addition & 0 deletions
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
}
Lines changed: 7 additions & 0 deletions
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+
}
Lines changed: 11 additions & 0 deletions
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)