2013-08-07 04:50:23 +00:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-04 00:48:01 +00: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.
|
|
|
|
|
2016-06-06 14:52:48 +00:00
|
|
|
// 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 22:41:37 +00:00
|
|
|
|
2017-03-25 21:14:18 +00:00
|
|
|
use syntax::ast::{self, Ident};
|
2015-12-10 14:23:14 +00:00
|
|
|
use syntax::ext::base::*;
|
|
|
|
use syntax::ext::base;
|
|
|
|
use syntax::ext::build::AstBuilder;
|
2016-11-16 10:52:37 +00:00
|
|
|
use syntax::symbol::Symbol;
|
2016-06-21 22:08:13 +00:00
|
|
|
use syntax_pos::Span;
|
2016-06-20 15:49:33 +00:00
|
|
|
use syntax::tokenstream;
|
2012-12-23 22:41:37 +00:00
|
|
|
|
2015-01-27 20:20:58 +00:00
|
|
|
use std::env;
|
2013-05-25 02:35:29 +00:00
|
|
|
|
2016-06-06 14:52:48 +00:00
|
|
|
pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt,
|
|
|
|
sp: Span,
|
|
|
|
tts: &[tokenstream::TokenTree])
|
|
|
|
-> Box<base::MacResult + 'cx> {
|
2014-01-17 14:53:10 +00:00
|
|
|
let var = match get_single_str_from_tts(cx, sp, tts, "option_env!") {
|
2014-04-15 12:00:14 +00:00
|
|
|
None => return DummyResult::expr(sp),
|
2016-06-06 14:52:48 +00:00
|
|
|
Some(v) => v,
|
2014-01-17 14:53:10 +00:00
|
|
|
};
|
2013-08-15 06:06:33 +00:00
|
|
|
|
2017-09-01 15:45:46 +00:00
|
|
|
let sp = sp.with_ctxt(sp.ctxt().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 14:52:48 +00:00
|
|
|
Err(..) => {
|
|
|
|
cx.expr_path(cx.path_all(sp,
|
|
|
|
true,
|
|
|
|
cx.std_path(&["option", "Option", "None"]),
|
|
|
|
Vec::new(),
|
|
|
|
vec![cx.ty_rptr(sp,
|
2017-03-25 21:14:18 +00:00
|
|
|
cx.ty_ident(sp, Ident::from_str("str")),
|
2016-06-06 14:52:48 +00:00
|
|
|
Some(cx.lifetime(sp,
|
2017-03-25 21:14:18 +00:00
|
|
|
Ident::from_str("'static"))),
|
2016-06-06 14:52:48 +00:00
|
|
|
ast::Mutability::Immutable)],
|
|
|
|
Vec::new()))
|
|
|
|
}
|
|
|
|
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 14:52:48 +00:00
|
|
|
}
|
2013-08-15 06:06:33 +00:00
|
|
|
};
|
2015-02-27 19:14:42 +00:00
|
|
|
MacEager::expr(e)
|
2013-08-15 06:06:33 +00:00
|
|
|
}
|
2012-05-14 22:32:32 +00:00
|
|
|
|
2016-06-06 14:52:48 +00:00
|
|
|
pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt,
|
|
|
|
sp: Span,
|
|
|
|
tts: &[tokenstream::TokenTree])
|
|
|
|
-> Box<base::MacResult + 'cx> {
|
2014-09-13 16:06:01 +00:00
|
|
|
let mut exprs = match get_exprs_from_tts(cx, sp, tts) {
|
2015-03-24 23:53:34 +00:00
|
|
|
Some(ref exprs) if exprs.is_empty() => {
|
2014-01-17 14:53:10 +00:00
|
|
|
cx.span_err(sp, "env! takes 1 or 2 arguments");
|
2014-04-15 12:00:14 +00:00
|
|
|
return DummyResult::expr(sp);
|
2014-01-17 14:53:10 +00:00
|
|
|
}
|
2014-04-15 12:00:14 +00:00
|
|
|
None => return DummyResult::expr(sp),
|
2016-06-06 14:52:48 +00:00
|
|
|
Some(exprs) => exprs.into_iter(),
|
2014-01-17 14:53:10 +00:00
|
|
|
};
|
2013-08-07 04:50:23 +00:00
|
|
|
|
2016-06-06 14:52:48 +00:00
|
|
|
let var = match expr_to_string(cx, exprs.next().unwrap(), "expected string literal") {
|
2014-04-15 12:00:14 +00:00
|
|
|
None => return DummyResult::expr(sp),
|
2016-06-06 14:52:48 +00:00
|
|
|
Some((v, _style)) => v,
|
2014-01-17 14:53:10 +00:00
|
|
|
};
|
2014-09-13 16:06:01 +00: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 16:06:01 +00:00
|
|
|
Some(second) => {
|
|
|
|
match expr_to_string(cx, second, "expected string literal") {
|
2014-04-15 12:00:14 +00:00
|
|
|
None => return DummyResult::expr(sp),
|
2016-06-06 14:52:48 +00:00
|
|
|
Some((s, _style)) => s,
|
2014-01-17 14:53:10 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-13 16:06:01 +00:00
|
|
|
};
|
|
|
|
|
2016-07-03 21:38:37 +00:00
|
|
|
if let Some(_) = exprs.next() {
|
|
|
|
cx.span_err(sp, "env! takes 1 or 2 arguments");
|
|
|
|
return DummyResult::expr(sp);
|
2014-09-13 16:06:01 +00:00
|
|
|
}
|
2011-05-06 23:30:39 +00:00
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
let e = match env::var(&*var.as_str()) {
|
2015-02-05 00:03:12 +00:00
|
|
|
Err(_) => {
|
2016-11-16 10:52:37 +00:00
|
|
|
cx.span_err(sp, &msg.as_str());
|
2015-01-17 23:49:08 +00:00
|
|
|
cx.expr_usize(sp, 0)
|
2014-01-17 14:53:10 +00:00
|
|
|
}
|
2016-11-16 10:52:37 +00:00
|
|
|
Ok(s) => cx.expr_str(sp, Symbol::intern(&s)),
|
2012-12-13 01:08:09 +00:00
|
|
|
};
|
2015-02-27 19:14:42 +00:00
|
|
|
MacEager::expr(e)
|
2011-05-05 02:05:16 +00:00
|
|
|
}
|