2013-08-07 00:50:23 -04:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08: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 19:05:16 -07:00
|
|
|
/*
|
2012-10-12 12:32:36 -07:00
|
|
|
* The compiler code necessary to support the env! extension. Eventually this
|
2011-05-04 19:05:16 -07:00
|
|
|
* should all get sucked into either the compiler syntax extension plugin
|
|
|
|
* interface.
|
|
|
|
*/
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2013-02-25 14:11:21 -05:00
|
|
|
use ast;
|
2013-08-31 18:13:04 +02:00
|
|
|
use codemap::Span;
|
2012-12-13 13:05:22 -08:00
|
|
|
use ext::base::*;
|
2012-12-23 17:41:37 -05:00
|
|
|
use ext::base;
|
2013-05-18 00:19:28 +10:00
|
|
|
use ext::build::AstBuilder;
|
2014-01-10 14:02:36 -08:00
|
|
|
use parse::token;
|
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
|
|
|
|
2014-08-27 21:46:52 -04:00
|
|
|
pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
|
|
|
-> Box<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),
|
2014-01-18 01:53:10 +11:00
|
|
|
Some(v) => v
|
|
|
|
};
|
2013-08-15 02:06:33 -04:00
|
|
|
|
2015-02-11 11:47:53 -08:00
|
|
|
let e = match env::var(&var[]) {
|
2015-01-27 12:20:58 -08:00
|
|
|
Err(..) => {
|
2014-02-28 12:54:01 -08:00
|
|
|
cx.expr_path(cx.path_all(sp,
|
|
|
|
true,
|
2014-09-07 14:57:26 -07:00
|
|
|
vec!(cx.ident_of_std("core"),
|
2014-02-28 12:54:01 -08:00
|
|
|
cx.ident_of("option"),
|
2014-11-28 11:57:41 -05:00
|
|
|
cx.ident_of("Option"),
|
2014-02-28 12:54:01 -08:00
|
|
|
cx.ident_of("None")),
|
2014-03-07 16:50:40 +01:00
|
|
|
Vec::new(),
|
2014-02-28 12:54:01 -08:00
|
|
|
vec!(cx.ty_rptr(sp,
|
|
|
|
cx.ty_ident(sp,
|
|
|
|
cx.ident_of("str")),
|
|
|
|
Some(cx.lifetime(sp,
|
|
|
|
cx.ident_of(
|
2014-06-10 13:54:13 -07:00
|
|
|
"'static").name)),
|
2014-11-29 17:08:30 +13:00
|
|
|
ast::MutImmutable)),
|
|
|
|
Vec::new()))
|
2014-02-28 12:54:01 -08:00
|
|
|
}
|
2015-01-27 12:20:58 -08:00
|
|
|
Ok(s) => {
|
2014-02-28 12:54:01 -08:00
|
|
|
cx.expr_call_global(sp,
|
2014-09-07 14:57:26 -07:00
|
|
|
vec!(cx.ident_of_std("core"),
|
2014-02-28 12:54:01 -08:00
|
|
|
cx.ident_of("option"),
|
2014-11-28 11:57:41 -05:00
|
|
|
cx.ident_of("Option"),
|
2014-02-28 12:54:01 -08:00
|
|
|
cx.ident_of("Some")),
|
|
|
|
vec!(cx.expr_str(sp,
|
|
|
|
token::intern_and_get_ident(
|
2015-01-07 11:58:31 -05:00
|
|
|
&s[]))))
|
2014-02-28 12:54:01 -08:00
|
|
|
}
|
2013-08-15 02:06:33 -04:00
|
|
|
};
|
2014-04-15 22:00:14 +10:00
|
|
|
MacExpr::new(e)
|
2013-08-15 02:06:33 -04:00
|
|
|
}
|
2012-05-14 15:32:32 -07:00
|
|
|
|
2014-08-27 21:46:52 -04:00
|
|
|
pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
|
|
|
-> Box<base::MacResult+'cx> {
|
2014-09-13 19:06:01 +03:00
|
|
|
let mut exprs = match get_exprs_from_tts(cx, sp, tts) {
|
2014-02-13 09:46:46 -08:00
|
|
|
Some(ref exprs) if exprs.len() == 0 => {
|
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),
|
2014-09-14 20:27:36 -07:00
|
|
|
Some(exprs) => exprs.into_iter()
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
2013-08-07 00:50:23 -04:00
|
|
|
|
2014-06-21 03:39:03 -07:00
|
|
|
let var = match expr_to_string(cx,
|
2014-09-13 19:06:01 +03:00
|
|
|
exprs.next().unwrap(),
|
2014-02-28 12:54:01 -08:00
|
|
|
"expected string literal") {
|
2014-04-15 22:00:14 +10:00
|
|
|
None => return DummyResult::expr(sp),
|
2014-01-18 01:53:10 +11:00
|
|
|
Some((v, _style)) => v
|
|
|
|
};
|
2014-09-13 19:06:01 +03:00
|
|
|
let msg = match exprs.next() {
|
|
|
|
None => {
|
2015-01-07 11:58:31 -05:00
|
|
|
token::intern_and_get_ident(&format!("environment variable `{}` \
|
2014-01-10 14:02:36 -08:00
|
|
|
not defined",
|
2015-01-07 11:58:31 -05:00
|
|
|
var)[])
|
2014-01-10 14:02:36 -08:00
|
|
|
}
|
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),
|
2014-01-18 01:53:10 +11:00
|
|
|
Some((s, _style)) => s
|
|
|
|
}
|
|
|
|
}
|
2014-09-13 19:06:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
match exprs.next() {
|
|
|
|
None => {}
|
|
|
|
Some(_) => {
|
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);
|
2013-10-08 02:49:10 +02:00
|
|
|
}
|
2014-09-13 19:06:01 +03:00
|
|
|
}
|
2011-05-06 16:30:39 -07:00
|
|
|
|
2015-02-11 11:47:53 -08:00
|
|
|
let e = match env::var(&var[]) {
|
2015-02-05 01:03:12 +01:00
|
|
|
Err(_) => {
|
2015-02-04 21:48:12 +01:00
|
|
|
cx.span_err(sp, &msg);
|
2015-01-17 23:49:08 +00:00
|
|
|
cx.expr_usize(sp, 0)
|
2014-01-18 01:53:10 +11:00
|
|
|
}
|
2015-02-05 01:03:12 +01:00
|
|
|
Ok(s) => cx.expr_str(sp, token::intern_and_get_ident(&s))
|
2012-12-12 17:08:09 -08:00
|
|
|
};
|
2014-04-15 22:00:14 +10:00
|
|
|
MacExpr::new(e)
|
2011-05-04 19:05:16 -07:00
|
|
|
}
|