1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 * The compiler code necessary to support the env! extension. Eventually this
13 * should all get sucked into either the compiler syntax extension plugin
18 use syntax::codemap::Span;
19 use syntax::ext::base::*;
20 use syntax::ext::base;
21 use syntax::ext::build::AstBuilder;
22 use syntax::parse::token;
26 pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
27 -> Box<base::MacResult+'cx> {
28 let var = match get_single_str_from_tts(cx, sp, tts, "option_env!") {
29 None => return DummyResult::expr(sp),
33 let e = match env::var(&var[..]) {
35 cx.expr_path(cx.path_all(sp,
37 cx.std_path(&["option", "Option", "None"]),
49 cx.expr_call_global(sp,
50 cx.std_path(&["option", "Option", "Some"]),
52 token::intern_and_get_ident(
59 pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
60 -> Box<base::MacResult+'cx> {
61 let mut exprs = match get_exprs_from_tts(cx, sp, tts) {
62 Some(ref exprs) if exprs.is_empty() => {
63 cx.span_err(sp, "env! takes 1 or 2 arguments");
64 return DummyResult::expr(sp);
66 None => return DummyResult::expr(sp),
67 Some(exprs) => exprs.into_iter()
70 let var = match expr_to_string(cx,
71 exprs.next().unwrap(),
72 "expected string literal") {
73 None => return DummyResult::expr(sp),
74 Some((v, _style)) => v
76 let msg = match exprs.next() {
78 token::intern_and_get_ident(&format!("environment variable `{}` \
83 match expr_to_string(cx, second, "expected string literal") {
84 None => return DummyResult::expr(sp),
85 Some((s, _style)) => s
93 cx.span_err(sp, "env! takes 1 or 2 arguments");
94 return DummyResult::expr(sp);
98 let e = match env::var(&var[..]) {
100 cx.span_err(sp, &msg);
103 Ok(s) => cx.expr_str(sp, token::intern_and_get_ident(&s))