]> git.proxmox.com Git - rustc.git/blame - src/libsyntax/ext/env.rs
Imported Upstream version 1.4.0+dfsg1
[rustc.git] / src / libsyntax / ext / env.rs
CommitLineData
1a4d82fc 1// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
223e47cc
LB
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
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.
10
223e47cc
LB
11/*
12 * The compiler code necessary to support the env! extension. Eventually this
13 * should all get sucked into either the compiler syntax extension plugin
14 * interface.
15 */
16
223e47cc 17use ast;
1a4d82fc 18use codemap::Span;
223e47cc
LB
19use ext::base::*;
20use ext::base;
970d7e83 21use ext::build::AstBuilder;
1a4d82fc 22use parse::token;
970d7e83 23
85aaf69f 24use std::env;
223e47cc 25
1a4d82fc
JJ
26pub 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),
30 Some(v) => v
31 };
32
85aaf69f
SL
33 let e = match env::var(&var[..]) {
34 Err(..) => {
1a4d82fc
JJ
35 cx.expr_path(cx.path_all(sp,
36 true,
e9174d1e 37 cx.std_path(&["option", "Option", "None"]),
1a4d82fc
JJ
38 Vec::new(),
39 vec!(cx.ty_rptr(sp,
40 cx.ty_ident(sp,
41 cx.ident_of("str")),
42 Some(cx.lifetime(sp,
43 cx.ident_of(
44 "'static").name)),
45 ast::MutImmutable)),
46 Vec::new()))
47 }
85aaf69f 48 Ok(s) => {
1a4d82fc 49 cx.expr_call_global(sp,
e9174d1e 50 cx.std_path(&["option", "Option", "Some"]),
1a4d82fc
JJ
51 vec!(cx.expr_str(sp,
52 token::intern_and_get_ident(
85aaf69f 53 &s[..]))))
1a4d82fc
JJ
54 }
55 };
c34b1796 56 MacEager::expr(e)
1a4d82fc 57}
223e47cc 58
1a4d82fc
JJ
59pub 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) {
9346a6ac 62 Some(ref exprs) if exprs.is_empty() => {
1a4d82fc
JJ
63 cx.span_err(sp, "env! takes 1 or 2 arguments");
64 return DummyResult::expr(sp);
65 }
66 None => return DummyResult::expr(sp),
67 Some(exprs) => exprs.into_iter()
68 };
69
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
75 };
76 let msg = match exprs.next() {
77 None => {
78 token::intern_and_get_ident(&format!("environment variable `{}` \
79 not defined",
c34b1796 80 var))
1a4d82fc
JJ
81 }
82 Some(second) => {
83 match expr_to_string(cx, second, "expected string literal") {
84 None => return DummyResult::expr(sp),
85 Some((s, _style)) => s
86 }
87 }
88 };
223e47cc 89
1a4d82fc
JJ
90 match exprs.next() {
91 None => {}
92 Some(_) => {
93 cx.span_err(sp, "env! takes 1 or 2 arguments");
94 return DummyResult::expr(sp);
95 }
96 }
223e47cc 97
85aaf69f
SL
98 let e = match env::var(&var[..]) {
99 Err(_) => {
100 cx.span_err(sp, &msg);
101 cx.expr_usize(sp, 0)
1a4d82fc 102 }
85aaf69f 103 Ok(s) => cx.expr_str(sp, token::intern_and_get_ident(&s))
223e47cc 104 };
c34b1796 105 MacEager::expr(e)
223e47cc 106}