2016-06-06 20:22:48 +05:30
|
|
|
// The compiler code necessary to support the env! extension. Eventually this
|
|
|
|
// should all get sucked into either the compiler syntax extension plugin
|
|
|
|
// interface.
|
|
|
|
//
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2018-05-27 20:07:09 +01:00
|
|
|
use syntax::ast::{self, Ident, GenericArg};
|
2019-02-04 21:49:54 +09:00
|
|
|
use syntax::ext::base::{self, *};
|
2015-12-10 23:23:14 +09:00
|
|
|
use syntax::ext::build::AstBuilder;
|
2018-03-08 14:27:23 +03:00
|
|
|
use syntax::symbol::{keywords, Symbol};
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2016-06-20 08:49:33 -07:00
|
|
|
use syntax::tokenstream;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2015-01-27 12:20:58 -08:00
|
|
|
use std::env;
|
2013-05-24 19:35:29 -07:00
|
|
|
|
2019-02-04 21:49:54 +09:00
|
|
|
pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt<'_>,
|
2016-06-06 20:22:48 +05:30
|
|
|
sp: Span,
|
|
|
|
tts: &[tokenstream::TokenTree])
|
2018-07-12 11:58:16 +02:00
|
|
|
-> Box<dyn base::MacResult + 'cx> {
|
2014-01-18 01:53:10 +11:00
|
|
|
let var = match get_single_str_from_tts(cx, sp, tts, "option_env!") {
|
2014-04-15 22:00:14 +10:00
|
|
|
None => return DummyResult::expr(sp),
|
2016-06-06 20:22:48 +05:30
|
|
|
Some(v) => v,
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
2013-08-15 02:06:33 -04:00
|
|
|
|
2018-03-18 23:51:53 +03:00
|
|
|
let sp = sp.apply_mark(cx.current_expansion.mark);
|
2016-11-16 10:52:37 +00:00
|
|
|
let e = match env::var(&*var.as_str()) {
|
2016-06-06 20:22:48 +05:30
|
|
|
Err(..) => {
|
2018-03-08 14:27:23 +03:00
|
|
|
let lt = cx.lifetime(sp, keywords::StaticLifetime.ident());
|
2016-06-06 20:22:48 +05:30
|
|
|
cx.expr_path(cx.path_all(sp,
|
|
|
|
true,
|
|
|
|
cx.std_path(&["option", "Option", "None"]),
|
2018-05-27 20:07:09 +01:00
|
|
|
vec![GenericArg::Type(cx.ty_rptr(sp,
|
2017-03-25 21:14:18 +00:00
|
|
|
cx.ty_ident(sp, Ident::from_str("str")),
|
2018-03-08 14:27:23 +03:00
|
|
|
Some(lt),
|
2018-02-23 17:48:54 +00:00
|
|
|
ast::Mutability::Immutable))],
|
2018-05-27 21:54:10 +01:00
|
|
|
vec![]))
|
2016-06-06 20:22:48 +05:30
|
|
|
}
|
|
|
|
Ok(s) => {
|
|
|
|
cx.expr_call_global(sp,
|
|
|
|
cx.std_path(&["option", "Option", "Some"]),
|
2016-11-16 10:52:37 +00:00
|
|
|
vec![cx.expr_str(sp, Symbol::intern(&s))])
|
2016-06-06 20:22:48 +05:30
|
|
|
}
|
2013-08-15 02:06:33 -04:00
|
|
|
};
|
2015-02-27 11:14:42 -08:00
|
|
|
MacEager::expr(e)
|
2013-08-15 02:06:33 -04:00
|
|
|
}
|
2012-05-14 15:32:32 -07:00
|
|
|
|
2019-02-04 21:49:54 +09:00
|
|
|
pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt<'_>,
|
2016-06-06 20:22:48 +05:30
|
|
|
sp: Span,
|
|
|
|
tts: &[tokenstream::TokenTree])
|
2018-07-12 11:58:16 +02:00
|
|
|
-> Box<dyn base::MacResult + 'cx> {
|
2014-09-13 19:06:01 +03:00
|
|
|
let mut exprs = match get_exprs_from_tts(cx, sp, tts) {
|
2015-03-24 16:53:34 -07:00
|
|
|
Some(ref exprs) if exprs.is_empty() => {
|
2014-01-18 01:53:10 +11:00
|
|
|
cx.span_err(sp, "env! takes 1 or 2 arguments");
|
2014-04-15 22:00:14 +10:00
|
|
|
return DummyResult::expr(sp);
|
2014-01-18 01:53:10 +11:00
|
|
|
}
|
2014-04-15 22:00:14 +10:00
|
|
|
None => return DummyResult::expr(sp),
|
2016-06-06 20:22:48 +05:30
|
|
|
Some(exprs) => exprs.into_iter(),
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
2013-08-07 00:50:23 -04:00
|
|
|
|
2016-06-06 20:22:48 +05:30
|
|
|
let var = match expr_to_string(cx, exprs.next().unwrap(), "expected string literal") {
|
2014-04-15 22:00:14 +10:00
|
|
|
None => return DummyResult::expr(sp),
|
2016-06-06 20:22:48 +05:30
|
|
|
Some((v, _style)) => v,
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
2014-09-13 19:06:01 +03:00
|
|
|
let msg = match exprs.next() {
|
2016-11-16 10:52:37 +00:00
|
|
|
None => Symbol::intern(&format!("environment variable `{}` not defined", var)),
|
2014-09-13 19:06:01 +03:00
|
|
|
Some(second) => {
|
|
|
|
match expr_to_string(cx, second, "expected string literal") {
|
2014-04-15 22:00:14 +10:00
|
|
|
None => return DummyResult::expr(sp),
|
2016-06-06 20:22:48 +05:30
|
|
|
Some((s, _style)) => s,
|
2014-01-18 01:53:10 +11:00
|
|
|
}
|
|
|
|
}
|
2014-09-13 19:06:01 +03:00
|
|
|
};
|
|
|
|
|
2018-07-27 13:20:13 +02:00
|
|
|
if exprs.next().is_some() {
|
2016-07-03 14:38:37 -07:00
|
|
|
cx.span_err(sp, "env! takes 1 or 2 arguments");
|
|
|
|
return DummyResult::expr(sp);
|
2014-09-13 19:06:01 +03:00
|
|
|
}
|
2011-05-06 16:30:39 -07:00
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
let e = match env::var(&*var.as_str()) {
|
2015-02-05 01:03:12 +01:00
|
|
|
Err(_) => {
|
2016-11-16 10:52:37 +00:00
|
|
|
cx.span_err(sp, &msg.as_str());
|
2018-12-30 00:56:55 +03:00
|
|
|
return DummyResult::expr(sp);
|
2014-01-18 01:53:10 +11:00
|
|
|
}
|
2016-11-16 10:52:37 +00:00
|
|
|
Ok(s) => cx.expr_str(sp, Symbol::intern(&s)),
|
2012-12-12 17:08:09 -08:00
|
|
|
};
|
2015-02-27 11:14:42 -08:00
|
|
|
MacEager::expr(e)
|
2011-05-04 19:05:16 -07:00
|
|
|
}
|