Created
June 26, 2012 01:10
-
-
Save anonymous/2992436 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs | |
index 6c93dbc..c2b9e33 100644 | |
--- a/src/libsyntax/ext/base.rs | |
+++ b/src/libsyntax/ext/base.rs | |
@@ -30,11 +30,14 @@ enum syntax_extension { | |
fn syntax_expander_table() -> hashmap<str, syntax_extension> { | |
fn builtin(f: syntax_expander_) -> syntax_extension | |
{normal({expander: f, span: none})} | |
+ fn builtin_tt(f: syntax_expander_tt_) -> syntax_extension | |
+ {normal_tt({expander: f, span: none})} | |
let syntax_expanders = str_hash::<syntax_extension>(); | |
syntax_expanders.insert("fmt", builtin(ext::fmt::expand_syntax_ext)); | |
syntax_expanders.insert("auto_serialize", | |
item_decorator(ext::auto_serialize::expand)); | |
syntax_expanders.insert("env", builtin(ext::env::expand_syntax_ext)); | |
+ syntax_expanders.insert("env2", builtin_tt(ext::env2::expand_syntax_ext)); | |
syntax_expanders.insert("macro", | |
macro_defining(ext::simplext::add_new_extension)); | |
syntax_expanders.insert("concat_idents", | |
diff --git a/src/libsyntax/ext/env2.rs b/src/libsyntax/ext/env2.rs | |
new file mode 100644 | |
index 0000000..0a5da2e | |
--- /dev/null | |
+++ b/src/libsyntax/ext/env2.rs | |
@@ -0,0 +1,46 @@ | |
+import base::*; | |
+export expand_syntax_ext; | |
+import ast::{matcher, matcher_, mtc_tok, mtc_rep, mtc_bb, tt_flat, tt_delim, | |
+ token_tree}; | |
+import earley_parser::{parse,leaf}; | |
+import parse::lexer::{reader,new_tt_reader,tt_reader_as_reader}; | |
+import parse::token::{token, w_ident, LBRACE, RBRACE}; | |
+ | |
+/* spans are ignored anyways */ | |
+fn ms(m: matcher_) -> matcher { | |
+ {node: m, span: {lo: 0u, hi: 0u, expn_info: none}} | |
+} | |
+fn ttf(t: token) -> token_tree { | |
+ tt_flat({lo: 0u, hi: 0u, expn_info: none}, t) | |
+} | |
+ | |
+fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, body: ast::token_tree) | |
+ -> @ast::expr { | |
+ | |
+ let body_core = alt body { tt_delim(tts) { tts } _ {fail}}; | |
+ let tt_rdr = new_tt_reader(cx.parse_sess().span_diagnostic, | |
+ cx.parse_sess().interner, body_core); | |
+ let args = parse(cx.parse_sess(), cx.cfg(), tt_rdr as reader, | |
+ [ms(mtc_tok(LBRACE)), | |
+ ms(mtc_bb(@"irrelevant", @"ident", 0u)), | |
+ ms(mtc_tok(RBRACE))]); | |
+ let var = alt args[0u] { @leaf(w_ident(@name)) { name } _ { fail }}; | |
+ | |
+ alt os::getenv(var) { | |
+ option::none { ret make_new_str(cx, sp, ""); } | |
+ option::some(s) { ret make_new_str(cx, sp, s); } | |
+ } | |
+} | |
+ | |
+fn make_new_str(cx: ext_ctxt, sp: codemap::span, +s: str) -> @ast::expr { | |
+ ret make_new_lit(cx, sp, ast::lit_str(@s)); | |
+} | |
+// | |
+// Local Variables: | |
+// mode: rust | |
+// fill-column: 78; | |
+// indent-tabs-mode: nil | |
+// c-basic-offset: 4 | |
+// buffer-file-coding-system: utf-8-unix | |
+// End: | |
+// | |
diff --git a/src/libsyntax/syntax.rc b/src/libsyntax/syntax.rc | |
index cf4d24b..7f9f872 100644 | |
--- a/src/libsyntax/syntax.rc | |
+++ b/src/libsyntax/syntax.rc | |
@@ -75,4 +75,6 @@ mod ext { | |
mod log_syntax; | |
mod auto_serialize; | |
mod source_util; | |
+ | |
+ mod env2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment