diff options
| author | Graydon Hoare <graydon@mozilla.com> | 2011-05-04 19:05:16 -0700 |
|---|---|---|
| committer | Graydon Hoare <graydon@mozilla.com> | 2011-05-04 19:05:32 -0700 |
| commit | d55fa2a9a3aa00a723f224eda764580a0492abd9 (patch) | |
| tree | a4c337c74adfa68fb33547e19550b2b24a86c801 /src | |
| parent | a7db03272592f4eaf433699786a93e02977d3264 (diff) | |
| download | rust-d55fa2a9a3aa00a723f224eda764580a0492abd9.tar.gz rust-d55fa2a9a3aa00a723f224eda764580a0492abd9.zip | |
Add #env syntax extension for plucking strings out of the compilation environment.
Diffstat (limited to 'src')
| -rw-r--r-- | src/comp/front/extenv.rs | 68 | ||||
| -rw-r--r-- | src/comp/front/parser.rs | 7 | ||||
| -rw-r--r-- | src/comp/rustc.rc | 1 |
3 files changed, 76 insertions, 0 deletions
diff --git a/src/comp/front/extenv.rs b/src/comp/front/extenv.rs new file mode 100644 index 00000000000..6dc9c5ec021 --- /dev/null +++ b/src/comp/front/extenv.rs @@ -0,0 +1,68 @@ +/* + * The compiler code necessary to support the #env extension. Eventually this + * should all get sucked into either the compiler syntax extension plugin + * interface. + */ + +import util.common; + +import std._str; +import std._vec; +import std.option; +import std.GenericOS; + +export expand_syntax_ext; + +// FIXME: Need to thread parser through here to handle errors correctly +fn expand_syntax_ext(parser.parser p, + common.span sp, + vec[@ast.expr] args, + option.t[str] body) -> @ast.expr { + + if (_vec.len[@ast.expr](args) != 1u) { + p.err("malformed #env call"); + } + + auto var = expr_to_str(p, args.(0)); + auto val = GenericOS.getenv(var); + ret make_new_str(sp, val); +} + +// FIXME: duplicate code copied from extfmt. + +fn expr_to_str(parser.parser p, + @ast.expr expr) -> str { + alt (expr.node) { + case (ast.expr_lit(?l, _)) { + alt (l.node) { + case (ast.lit_str(?s)) { + ret s; + } + } + } + } + p.err("malformed #env call"); + fail; +} + +fn make_new_lit(common.span sp, ast.lit_ lit) -> @ast.expr { + auto sp_lit = @rec(node=lit, span=sp); + auto expr = ast.expr_lit(sp_lit, ast.ann_none); + ret @rec(node=expr, span=sp); +} + +fn make_new_str(common.span sp, str s) -> @ast.expr { + auto lit = ast.lit_str(s); + ret make_new_lit(sp, lit); +} + +// +// Local Variables: +// mode: rust +// fill-column: 78; +// indent-tabs-mode: nil +// c-basic-offset: 4 +// buffer-file-coding-system: utf-8-unix +// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'"; +// End: +// diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index e7a86d6efed..a7e355af591 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -949,6 +949,13 @@ fn expand_syntax_ext(parser p, ast.span sp, ast.ann_none); ret newexpr; + } else if (_str.eq(extname, "env")) { + auto expanded = extenv.expand_syntax_ext(p, sp, args, body); + auto newexpr = ast.expr_ext(path, args, body, + expanded, + ast.ann_none); + + ret newexpr; } else { p.err("unknown syntax extension"); fail; diff --git a/src/comp/rustc.rc b/src/comp/rustc.rc index b506947a9c7..3074a84d074 100644 --- a/src/comp/rustc.rc +++ b/src/comp/rustc.rc @@ -24,6 +24,7 @@ mod front { mod ast; mod creader; mod extfmt; + mod extenv; mod codemap; mod lexer; mod parser; |
