2013-08-06 23:50:23 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2011-05-04 21:05:16 -05:00
|
|
|
/*
|
2012-10-12 14:32:36 -05:00
|
|
|
* The compiler code necessary to support the env! extension. Eventually this
|
2011-05-04 21:05:16 -05:00
|
|
|
* should all get sucked into either the compiler syntax extension plugin
|
|
|
|
* interface.
|
|
|
|
*/
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-02-25 13:11:21 -06:00
|
|
|
use ast;
|
2013-08-31 11:13:04 -05:00
|
|
|
use codemap::Span;
|
2012-12-13 15:05:22 -06:00
|
|
|
use ext::base::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ext::base;
|
2013-05-17 09:19:28 -05:00
|
|
|
use ext::build::AstBuilder;
|
2014-01-10 16:02:36 -06:00
|
|
|
use parse::token;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2015-01-27 14:20:58 -06:00
|
|
|
use std::env;
|
2013-05-24 21:35:29 -05:00
|
|
|
|
2014-08-27 20:46:52 -05:00
|
|
|
pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
|
|
|
-> Box<base::MacResult+'cx> {
|
2014-01-17 08:53:10 -06:00
|
|
|
let var = match get_single_str_from_tts(cx, sp, tts, "option_env!") {
|
2014-04-15 07:00:14 -05:00
|
|
|
None => return DummyResult::expr(sp),
|
2014-01-17 08:53:10 -06:00
|
|
|
Some(v) => v
|
|
|
|
};
|
2013-08-15 01:06:33 -05:00
|
|
|
|
2015-02-18 13:48:57 -06:00
|
|
|
let e = match env::var(&var[..]) {
|
2015-01-27 14:20:58 -06:00
|
|
|
Err(..) => {
|
2014-02-28 14:54:01 -06:00
|
|
|
cx.expr_path(cx.path_all(sp,
|
|
|
|
true,
|
2014-09-07 16:57:26 -05:00
|
|
|
vec!(cx.ident_of_std("core"),
|
2014-02-28 14:54:01 -06:00
|
|
|
cx.ident_of("option"),
|
2014-11-28 10:57:41 -06:00
|
|
|
cx.ident_of("Option"),
|
2014-02-28 14:54:01 -06:00
|
|
|
cx.ident_of("None")),
|
2014-03-07 09:50:40 -06:00
|
|
|
Vec::new(),
|
2014-02-28 14:54:01 -06:00
|
|
|
vec!(cx.ty_rptr(sp,
|
|
|
|
cx.ty_ident(sp,
|
|
|
|
cx.ident_of("str")),
|
|
|
|
Some(cx.lifetime(sp,
|
|
|
|
cx.ident_of(
|
2014-06-10 15:54:13 -05:00
|
|
|
"'static").name)),
|
2014-11-28 22:08:30 -06:00
|
|
|
ast::MutImmutable)),
|
|
|
|
Vec::new()))
|
2014-02-28 14:54:01 -06:00
|
|
|
}
|
2015-01-27 14:20:58 -06:00
|
|
|
Ok(s) => {
|
2014-02-28 14:54:01 -06:00
|
|
|
cx.expr_call_global(sp,
|
2014-09-07 16:57:26 -05:00
|
|
|
vec!(cx.ident_of_std("core"),
|
2014-02-28 14:54:01 -06:00
|
|
|
cx.ident_of("option"),
|
2014-11-28 10:57:41 -06:00
|
|
|
cx.ident_of("Option"),
|
2014-02-28 14:54:01 -06:00
|
|
|
cx.ident_of("Some")),
|
|
|
|
vec!(cx.expr_str(sp,
|
|
|
|
token::intern_and_get_ident(
|
2015-02-18 13:48:57 -06:00
|
|
|
&s[..]))))
|
2014-02-28 14:54:01 -06:00
|
|
|
}
|
2013-08-15 01:06:33 -05:00
|
|
|
};
|
2015-02-27 13:14:42 -06:00
|
|
|
MacEager::expr(e)
|
2013-08-15 01:06:33 -05:00
|
|
|
}
|
2012-05-14 17:32:32 -05:00
|
|
|
|
2014-08-27 20:46:52 -05:00
|
|
|
pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
|
|
|
-> Box<base::MacResult+'cx> {
|
2014-09-13 11:06:01 -05:00
|
|
|
let mut exprs = match get_exprs_from_tts(cx, sp, tts) {
|
2014-02-13 11:46:46 -06:00
|
|
|
Some(ref exprs) if exprs.len() == 0 => {
|
2014-01-17 08:53:10 -06:00
|
|
|
cx.span_err(sp, "env! takes 1 or 2 arguments");
|
2014-04-15 07:00:14 -05:00
|
|
|
return DummyResult::expr(sp);
|
2014-01-17 08:53:10 -06:00
|
|
|
}
|
2014-04-15 07:00:14 -05:00
|
|
|
None => return DummyResult::expr(sp),
|
2014-09-14 22:27:36 -05:00
|
|
|
Some(exprs) => exprs.into_iter()
|
2014-01-17 08:53:10 -06:00
|
|
|
};
|
2013-08-06 23:50:23 -05:00
|
|
|
|
2014-06-21 05:39:03 -05:00
|
|
|
let var = match expr_to_string(cx,
|
2014-09-13 11:06:01 -05:00
|
|
|
exprs.next().unwrap(),
|
2014-02-28 14:54:01 -06:00
|
|
|
"expected string literal") {
|
2014-04-15 07:00:14 -05:00
|
|
|
None => return DummyResult::expr(sp),
|
2014-01-17 08:53:10 -06:00
|
|
|
Some((v, _style)) => v
|
|
|
|
};
|
2014-09-13 11:06:01 -05:00
|
|
|
let msg = match exprs.next() {
|
|
|
|
None => {
|
2015-01-07 10:58:31 -06:00
|
|
|
token::intern_and_get_ident(&format!("environment variable `{}` \
|
2014-01-10 16:02:36 -06:00
|
|
|
not defined",
|
2015-02-20 13:08:14 -06:00
|
|
|
var))
|
2014-01-10 16:02:36 -06:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
Some(second) => {
|
|
|
|
match expr_to_string(cx, second, "expected string literal") {
|
2014-04-15 07:00:14 -05:00
|
|
|
None => return DummyResult::expr(sp),
|
2014-01-17 08:53:10 -06:00
|
|
|
Some((s, _style)) => s
|
|
|
|
}
|
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
match exprs.next() {
|
|
|
|
None => {}
|
|
|
|
Some(_) => {
|
2014-01-17 08:53:10 -06:00
|
|
|
cx.span_err(sp, "env! takes 1 or 2 arguments");
|
2014-04-15 07:00:14 -05:00
|
|
|
return DummyResult::expr(sp);
|
2013-10-07 19:49:10 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
}
|
2011-05-06 18:30:39 -05:00
|
|
|
|
2015-02-18 13:48:57 -06:00
|
|
|
let e = match env::var(&var[..]) {
|
2015-02-04 18:03:12 -06:00
|
|
|
Err(_) => {
|
2015-02-04 14:48:12 -06:00
|
|
|
cx.span_err(sp, &msg);
|
2015-01-17 17:49:08 -06:00
|
|
|
cx.expr_usize(sp, 0)
|
2014-01-17 08:53:10 -06:00
|
|
|
}
|
2015-02-04 18:03:12 -06:00
|
|
|
Ok(s) => cx.expr_str(sp, token::intern_and_get_ident(&s))
|
2012-12-12 19:08:09 -06:00
|
|
|
};
|
2015-02-27 13:14:42 -06:00
|
|
|
MacEager::expr(e)
|
2011-05-04 21:05:16 -05:00
|
|
|
}
|