]>
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 | ||
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 | 17 | use ast; |
1a4d82fc | 18 | use codemap::Span; |
223e47cc LB |
19 | use ext::base::*; |
20 | use ext::base; | |
970d7e83 | 21 | use ext::build::AstBuilder; |
1a4d82fc | 22 | use parse::token; |
970d7e83 LB |
23 | |
24 | use std::os; | |
223e47cc | 25 | |
1a4d82fc JJ |
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), | |
30 | Some(v) => v | |
31 | }; | |
32 | ||
33 | let e = match os::getenv(&var[]) { | |
34 | None => { | |
35 | cx.expr_path(cx.path_all(sp, | |
36 | true, | |
37 | vec!(cx.ident_of("std"), | |
38 | cx.ident_of("option"), | |
39 | cx.ident_of("Option"), | |
40 | cx.ident_of("None")), | |
41 | Vec::new(), | |
42 | vec!(cx.ty_rptr(sp, | |
43 | cx.ty_ident(sp, | |
44 | cx.ident_of("str")), | |
45 | Some(cx.lifetime(sp, | |
46 | cx.ident_of( | |
47 | "'static").name)), | |
48 | ast::MutImmutable)), | |
49 | Vec::new())) | |
50 | } | |
51 | Some(s) => { | |
52 | cx.expr_call_global(sp, | |
53 | vec!(cx.ident_of("std"), | |
54 | cx.ident_of("option"), | |
55 | cx.ident_of("Option"), | |
56 | cx.ident_of("Some")), | |
57 | vec!(cx.expr_str(sp, | |
58 | token::intern_and_get_ident( | |
59 | &s[])))) | |
60 | } | |
61 | }; | |
62 | MacExpr::new(e) | |
63 | } | |
223e47cc | 64 | |
1a4d82fc JJ |
65 | pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) |
66 | -> Box<base::MacResult+'cx> { | |
67 | let mut exprs = match get_exprs_from_tts(cx, sp, tts) { | |
68 | Some(ref exprs) if exprs.len() == 0 => { | |
69 | cx.span_err(sp, "env! takes 1 or 2 arguments"); | |
70 | return DummyResult::expr(sp); | |
71 | } | |
72 | None => return DummyResult::expr(sp), | |
73 | Some(exprs) => exprs.into_iter() | |
74 | }; | |
75 | ||
76 | let var = match expr_to_string(cx, | |
77 | exprs.next().unwrap(), | |
78 | "expected string literal") { | |
79 | None => return DummyResult::expr(sp), | |
80 | Some((v, _style)) => v | |
81 | }; | |
82 | let msg = match exprs.next() { | |
83 | None => { | |
84 | token::intern_and_get_ident(&format!("environment variable `{}` \ | |
85 | not defined", | |
86 | var)[]) | |
87 | } | |
88 | Some(second) => { | |
89 | match expr_to_string(cx, second, "expected string literal") { | |
90 | None => return DummyResult::expr(sp), | |
91 | Some((s, _style)) => s | |
92 | } | |
93 | } | |
94 | }; | |
223e47cc | 95 | |
1a4d82fc JJ |
96 | match exprs.next() { |
97 | None => {} | |
98 | Some(_) => { | |
99 | cx.span_err(sp, "env! takes 1 or 2 arguments"); | |
100 | return DummyResult::expr(sp); | |
101 | } | |
102 | } | |
223e47cc | 103 | |
1a4d82fc JJ |
104 | let e = match os::getenv(var.get()) { |
105 | None => { | |
106 | cx.span_err(sp, msg.get()); | |
107 | cx.expr_uint(sp, 0) | |
108 | } | |
109 | Some(s) => cx.expr_str(sp, token::intern_and_get_ident(&s[])) | |
223e47cc | 110 | }; |
1a4d82fc | 111 | MacExpr::new(e) |
223e47cc | 112 | } |