Add #env syntax extension for plucking strings out of the compilation environment.
This commit is contained in:
parent
a7db032725
commit
d55fa2a9a3
3 changed files with 76 additions and 0 deletions
68
src/comp/front/extenv.rs
Normal file
68
src/comp/front/extenv.rs
Normal file
|
@ -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:
|
||||||
|
//
|
|
@ -948,6 +948,13 @@ fn expand_syntax_ext(parser p, ast.span sp,
|
||||||
expanded,
|
expanded,
|
||||||
ast.ann_none);
|
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;
|
ret newexpr;
|
||||||
} else {
|
} else {
|
||||||
p.err("unknown syntax extension");
|
p.err("unknown syntax extension");
|
||||||
|
|
|
@ -24,6 +24,7 @@ mod front {
|
||||||
mod ast;
|
mod ast;
|
||||||
mod creader;
|
mod creader;
|
||||||
mod extfmt;
|
mod extfmt;
|
||||||
|
mod extenv;
|
||||||
mod codemap;
|
mod codemap;
|
||||||
mod lexer;
|
mod lexer;
|
||||||
mod parser;
|
mod parser;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue