gecko-dev/servo/components/plugins/casing.rs
Simon Sapin da87abc762 servo: Merge #4893 - Upgrade to rustc ba2f13ef0 2015-02-04 (from servo:rustup_2015-01-31); r=Ms2ger,glennw
Ready for review.

Final link step on android fails, but we know how to fix it and will add it to this branch soon.

Source-Repo: https://github.com/servo/servo
Source-Revision: 2cc08f289ab909de44fa09a07b2c43b70ce379b9

--HG--
rename : servo/ports/gonk/build.rs => servo/support/rust-task_info/build.rs
2015-02-11 17:24:45 -07:00

60 lines
2 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use syntax::ext::base::ExtCtxt;
use syntax::ext::build::AstBuilder;
use syntax::codemap::Span;
use syntax::ast;
use syntax::ext::base;
use syntax::parse::token;
pub fn expand_lower<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
-> Box<base::MacResult + 'cx> {
expand_cased(cx, sp, tts, |c| { c.to_lowercase() })
}
pub fn expand_upper<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
-> Box<base::MacResult + 'cx> {
expand_cased(cx, sp, tts, |c| { c.to_uppercase() })
}
fn expand_cased<'cx, T>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree], transform: T)
-> Box<base::MacResult + 'cx>
where T: Fn(char) -> char
{
let es = match base::get_exprs_from_tts(cx, sp, tts) {
Some(e) => e,
None => return base::DummyResult::expr(sp)
};
let mut it = es.iter();
let res = if let Some(expr) = it.next() {
if let ast::ExprLit(ref lit) = expr.node {
if let ast::LitStr(ref s, _) = lit.node {
Some((s, lit.span))
} else {
cx.span_err(expr.span, "expected a string literal");
None
}
} else {
cx.span_err(expr.span, "expected a string literal");
None
}
} else {
cx.span_err(sp, "expected 1 argument, found 0");
None
};
match (res, it.count()) {
(Some((s, span)), 0) => {
let new_s = s.chars().map(transform).collect::<String>();
base::MacExpr::new(cx.expr_str(span, token::intern_and_get_ident(new_s.as_slice())))
}
(_, rest) => {
if rest > 0 {
cx.span_err(sp, format!("expected 1 argument, found {}", rest+1).as_slice());
}
base::DummyResult::expr(sp)
}
}
}