]>
Commit | Line | Data |
---|---|---|
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 | ||
9e0c209e SL |
11 | // The compiler code necessary to support the env! extension. Eventually this |
12 | // should all get sucked into either the compiler syntax extension plugin | |
13 | // interface. | |
14 | // | |
223e47cc | 15 | |
9cc50fc6 | 16 | use syntax::ast; |
9cc50fc6 SL |
17 | use syntax::ext::base::*; |
18 | use syntax::ext::base; | |
19 | use syntax::ext::build::AstBuilder; | |
476ff2be | 20 | use syntax::symbol::Symbol; |
3157f602 XL |
21 | use syntax_pos::Span; |
22 | use syntax::tokenstream; | |
970d7e83 | 23 | |
85aaf69f | 24 | use std::env; |
223e47cc | 25 | |
9e0c209e SL |
26 | pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, |
27 | sp: Span, | |
28 | tts: &[tokenstream::TokenTree]) | |
29 | -> Box<base::MacResult + 'cx> { | |
1a4d82fc JJ |
30 | let var = match get_single_str_from_tts(cx, sp, tts, "option_env!") { |
31 | None => return DummyResult::expr(sp), | |
9e0c209e | 32 | Some(v) => v, |
1a4d82fc JJ |
33 | }; |
34 | ||
476ff2be | 35 | let e = match env::var(&*var.as_str()) { |
9e0c209e SL |
36 | Err(..) => { |
37 | cx.expr_path(cx.path_all(sp, | |
38 | true, | |
39 | cx.std_path(&["option", "Option", "None"]), | |
40 | Vec::new(), | |
41 | vec![cx.ty_rptr(sp, | |
42 | cx.ty_ident(sp, cx.ident_of("str")), | |
43 | Some(cx.lifetime(sp, | |
44 | cx.ident_of("'static") | |
45 | .name)), | |
46 | ast::Mutability::Immutable)], | |
47 | Vec::new())) | |
48 | } | |
49 | Ok(s) => { | |
50 | cx.expr_call_global(sp, | |
51 | cx.std_path(&["option", "Option", "Some"]), | |
476ff2be | 52 | vec![cx.expr_str(sp, Symbol::intern(&s))]) |
9e0c209e | 53 | } |
1a4d82fc | 54 | }; |
c34b1796 | 55 | MacEager::expr(e) |
1a4d82fc | 56 | } |
223e47cc | 57 | |
9e0c209e SL |
58 | pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt, |
59 | sp: Span, | |
60 | tts: &[tokenstream::TokenTree]) | |
61 | -> Box<base::MacResult + 'cx> { | |
1a4d82fc | 62 | let mut exprs = match get_exprs_from_tts(cx, sp, tts) { |
9346a6ac | 63 | Some(ref exprs) if exprs.is_empty() => { |
1a4d82fc JJ |
64 | cx.span_err(sp, "env! takes 1 or 2 arguments"); |
65 | return DummyResult::expr(sp); | |
66 | } | |
67 | None => return DummyResult::expr(sp), | |
9e0c209e | 68 | Some(exprs) => exprs.into_iter(), |
1a4d82fc JJ |
69 | }; |
70 | ||
9e0c209e | 71 | let var = match expr_to_string(cx, exprs.next().unwrap(), "expected string literal") { |
1a4d82fc | 72 | None => return DummyResult::expr(sp), |
9e0c209e | 73 | Some((v, _style)) => v, |
1a4d82fc JJ |
74 | }; |
75 | let msg = match exprs.next() { | |
476ff2be | 76 | None => Symbol::intern(&format!("environment variable `{}` not defined", var)), |
1a4d82fc JJ |
77 | Some(second) => { |
78 | match expr_to_string(cx, second, "expected string literal") { | |
79 | None => return DummyResult::expr(sp), | |
9e0c209e | 80 | Some((s, _style)) => s, |
1a4d82fc JJ |
81 | } |
82 | } | |
83 | }; | |
223e47cc | 84 | |
3157f602 XL |
85 | if let Some(_) = exprs.next() { |
86 | cx.span_err(sp, "env! takes 1 or 2 arguments"); | |
87 | return DummyResult::expr(sp); | |
1a4d82fc | 88 | } |
223e47cc | 89 | |
476ff2be | 90 | let e = match env::var(&*var.as_str()) { |
85aaf69f | 91 | Err(_) => { |
476ff2be | 92 | cx.span_err(sp, &msg.as_str()); |
85aaf69f | 93 | cx.expr_usize(sp, 0) |
1a4d82fc | 94 | } |
476ff2be | 95 | Ok(s) => cx.expr_str(sp, Symbol::intern(&s)), |
223e47cc | 96 | }; |
c34b1796 | 97 | MacEager::expr(e) |
223e47cc | 98 | } |