From f4debc9086d82edec27fd43bdabb5ea362ea17d1 Mon Sep 17 00:00:00 2001 From: Youngmin Yoo Date: Thu, 1 Aug 2013 14:07:21 +0900 Subject: [PATCH 1/4] Add upcase_ident.rs for ident string to upper --- src/libsyntax/ext/upcase_ident.rs | 45 +++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/libsyntax/ext/upcase_ident.rs diff --git a/src/libsyntax/ext/upcase_ident.rs b/src/libsyntax/ext/upcase_ident.rs new file mode 100644 index 0000000000000..638e4310159af --- /dev/null +++ b/src/libsyntax/ext/upcase_ident.rs @@ -0,0 +1,45 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use ast; +use codemap::span; +use ext::base::*; +use ext::base; +use parse::token; +use parse::token::{str_to_ident}; + +pub fn expand_syntax_ext(cx: @ExtCtxt, sp: span, tts: &[ast::token_tree]) + -> base::MacResult { + let mut res_str = ~""; + for tts.iter().advance |elem| { + match *elem { + ast::tt_tok(_, token::IDENT(ident,_)) => res_str.push_str(cx.str_of(ident)), + _ => cx.span_fatal(sp, "upcase_ident! requires ident args.") + } + } + res_str = res_str.to_ascii().to_upper().to_str_ascii(); + + let res = str_to_ident(res_str); + + let e = @ast::expr { + id: cx.next_id(), + node: ast::expr_path( + ast::Path { + span: sp, + global: false, + idents: ~[res], + rp: None, + types: ~[], + } + ), + span: sp, + }; + MRExpr(e) +} From 332cdef24b6194d62acb15d786583756cfe1d9d1 Mon Sep 17 00:00:00 2001 From: Youngmin Yoo Date: Thu, 1 Aug 2013 14:08:45 +0900 Subject: [PATCH 2/4] Add syntax_expander(upcase_ident) --- src/libsyntax/ext/base.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index ea87646e60b0b..3ec0acdb4684a 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -152,6 +152,9 @@ pub fn syntax_expander_table() -> SyntaxEnv { syntax_expanders.insert(intern("concat_idents"), builtin_normal_tt( ext::concat_idents::expand_syntax_ext)); + syntax_expanders.insert(intern("upcase_ident"), + builtin_normal_tt( + ext::upcase_ident::expand_syntax_ext)); syntax_expanders.insert(intern(&"log_syntax"), builtin_normal_tt( ext::log_syntax::expand_syntax_ext)); From 6b35c05dcd531cfcbb17885c2f15bc638b0ffd3f Mon Sep 17 00:00:00 2001 From: Youngmin Yoo Date: Thu, 1 Aug 2013 14:09:21 +0900 Subject: [PATCH 3/4] Add mod upcase_ident --- src/libsyntax/syntax.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libsyntax/syntax.rs b/src/libsyntax/syntax.rs index ae2aa6ae73891..e964a4dc10206 100644 --- a/src/libsyntax/syntax.rs +++ b/src/libsyntax/syntax.rs @@ -74,6 +74,7 @@ pub mod ext { pub mod env; pub mod bytes; pub mod concat_idents; + pub mod upcase_ident; pub mod log_syntax; pub mod auto_encode; pub mod source_util; From f5627a6b3709fa50ad392dca421600c938538e49 Mon Sep 17 00:00:00 2001 From: Youngmin Yoo Date: Thu, 1 Aug 2013 14:48:37 +0900 Subject: [PATCH 4/4] Add tesecase for upcase_ident macro --- src/test/run-pass/test_upcase_ident.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/test/run-pass/test_upcase_ident.rs diff --git a/src/test/run-pass/test_upcase_ident.rs b/src/test/run-pass/test_upcase_ident.rs new file mode 100644 index 0000000000000..23d086dfa29ce --- /dev/null +++ b/src/test/run-pass/test_upcase_ident.rs @@ -0,0 +1,14 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + let AA = "upcase_ident test"; + assert!(AA == upcase_ident!(aa)); +}