2015-02-27 14:34:57 +13:00
|
|
|
use std::mem;
|
2015-02-09 15:05:50 +13:00
|
|
|
use syntax::ast::{Expr, ExprParen, Lit, Stmt, TokenTree, TtDelimited, TtToken};
|
2015-03-15 16:23:19 +13:00
|
|
|
use syntax::ext::quote::rt::ToTokens;
|
2015-01-11 11:50:52 +13:00
|
|
|
use syntax::codemap::Span;
|
2014-12-18 18:57:55 +13:00
|
|
|
use syntax::ext::base::ExtCtxt;
|
|
|
|
use syntax::parse;
|
2015-01-07 17:43:37 +13:00
|
|
|
use syntax::parse::parser::Parser as RustParser;
|
2015-02-27 09:27:45 +13:00
|
|
|
use syntax::parse::token::{self, DelimToken};
|
2014-12-18 18:57:55 +13:00
|
|
|
use syntax::ptr::P;
|
|
|
|
|
2015-01-11 11:01:39 +13:00
|
|
|
use super::render::{Escape, Renderer};
|
2014-12-19 11:53:40 +13:00
|
|
|
|
2015-01-07 17:43:37 +13:00
|
|
|
macro_rules! dollar {
|
|
|
|
() => (TtToken(_, token::Dollar))
|
|
|
|
}
|
2015-09-12 13:53:14 +12:00
|
|
|
macro_rules! pound {
|
|
|
|
() => (TtToken(_, token::Pound))
|
|
|
|
}
|
2015-01-18 20:18:21 +13:00
|
|
|
macro_rules! dot {
|
|
|
|
() => (TtToken(_, token::Dot))
|
|
|
|
}
|
2015-01-07 17:43:37 +13:00
|
|
|
macro_rules! eq {
|
|
|
|
() => (TtToken(_, token::Eq))
|
|
|
|
}
|
2015-01-13 16:46:37 +13:00
|
|
|
macro_rules! not {
|
|
|
|
() => (TtToken(_, token::Not))
|
|
|
|
}
|
2015-01-29 13:47:11 +13:00
|
|
|
macro_rules! question {
|
|
|
|
() => (TtToken(_, token::Question))
|
|
|
|
}
|
2015-01-07 17:43:37 +13:00
|
|
|
macro_rules! semi {
|
|
|
|
() => (TtToken(_, token::Semi))
|
|
|
|
}
|
|
|
|
macro_rules! minus {
|
|
|
|
() => (TtToken(_, token::BinOp(token::Minus)))
|
|
|
|
}
|
2015-01-11 12:51:35 +13:00
|
|
|
macro_rules! slash {
|
|
|
|
() => (TtToken(_, token::BinOp(token::Slash)))
|
|
|
|
}
|
2015-01-07 17:43:37 +13:00
|
|
|
macro_rules! literal {
|
|
|
|
() => (TtToken(_, token::Literal(..)))
|
|
|
|
}
|
|
|
|
macro_rules! ident {
|
2015-01-11 11:50:52 +13:00
|
|
|
($x:pat) => (ident!(_, $x));
|
|
|
|
($sp:pat, $x:pat) => (TtToken($sp, token::Ident($x, token::IdentStyle::Plain)))
|
2015-01-07 17:43:37 +13:00
|
|
|
}
|
|
|
|
|
2015-09-07 19:43:48 +12:00
|
|
|
pub fn parse(cx: &ExtCtxt, sp: Span, write: &[TokenTree], input: &[TokenTree])
|
|
|
|
-> P<Expr>
|
|
|
|
{
|
2015-02-07 17:48:09 +13:00
|
|
|
let mut parser = Parser {
|
2015-01-30 18:43:53 +13:00
|
|
|
in_attr: false,
|
|
|
|
input: input,
|
|
|
|
span: sp,
|
2015-09-06 12:10:55 +12:00
|
|
|
render: Renderer::new(cx),
|
2015-02-07 17:48:09 +13:00
|
|
|
};
|
|
|
|
parser.markups();
|
2015-09-06 12:10:55 +12:00
|
|
|
parser.into_render().into_expr(write.to_vec())
|
2015-01-07 17:43:37 +13:00
|
|
|
}
|
|
|
|
|
2015-09-07 19:43:48 +12:00
|
|
|
pub fn split_comma<'a>(cx: &ExtCtxt, sp: Span, args: &'a [TokenTree]) -> (&'a [TokenTree], &'a [TokenTree]) {
|
2015-09-01 18:26:50 +12:00
|
|
|
fn is_comma(t: &TokenTree) -> bool {
|
|
|
|
match *t {
|
|
|
|
TtToken(_, token::Comma) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2015-09-07 19:43:48 +12:00
|
|
|
match args.iter().position(is_comma) {
|
|
|
|
Some(i) => (&args[..i], &args[1+i..]),
|
2015-09-03 11:03:45 +12:00
|
|
|
None => cx.span_fatal(sp, "expected two arguments to `html!`"),
|
2015-09-01 18:26:50 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 10:27:44 +13:00
|
|
|
struct Parser<'cx, 'i> {
|
2015-01-10 21:29:58 +13:00
|
|
|
in_attr: bool,
|
2015-01-07 17:43:37 +13:00
|
|
|
input: &'i [TokenTree],
|
2015-01-12 16:24:53 +13:00
|
|
|
span: Span,
|
2015-04-04 10:27:44 +13:00
|
|
|
render: Renderer<'cx>,
|
2015-01-07 17:43:37 +13:00
|
|
|
}
|
|
|
|
|
2015-04-04 10:27:44 +13:00
|
|
|
impl<'cx, 'i> Parser<'cx, 'i> {
|
2015-02-07 17:48:09 +13:00
|
|
|
/// Finalize the `Parser`, returning the `Renderer` underneath.
|
2015-04-04 10:27:44 +13:00
|
|
|
fn into_render(self) -> Renderer<'cx> {
|
2015-02-07 17:48:09 +13:00
|
|
|
let Parser { render, .. } = self;
|
|
|
|
render
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:43:37 +13:00
|
|
|
/// Consume `n` items from the input.
|
2015-01-10 21:29:58 +13:00
|
|
|
fn shift(&mut self, n: usize) {
|
2015-01-25 20:05:43 +13:00
|
|
|
self.input = &self.input[n..];
|
2014-12-21 16:47:40 +13:00
|
|
|
}
|
2015-01-07 17:43:37 +13:00
|
|
|
|
|
|
|
/// Construct a Rust AST parser from the given token tree.
|
2015-03-27 09:49:23 +13:00
|
|
|
fn with_rust_parser<F, T>(&self, tts: Vec<TokenTree>, callback: F) -> T where
|
2015-04-04 10:27:44 +13:00
|
|
|
F: FnOnce(&mut RustParser<'cx>) -> T
|
2015-03-27 09:49:23 +13:00
|
|
|
{
|
|
|
|
let mut parser = parse::tts_to_parser(self.render.cx.parse_sess, tts,
|
|
|
|
self.render.cx.cfg.clone());
|
|
|
|
let result = callback(&mut parser);
|
|
|
|
// Make sure all tokens were consumed
|
|
|
|
if parser.token != token::Eof {
|
|
|
|
let token = parser.this_token_to_string();
|
|
|
|
self.render.cx.span_err(parser.span,
|
|
|
|
&format!("unexpected token: `{}`", token));
|
|
|
|
}
|
|
|
|
result
|
2014-12-20 20:53:38 +13:00
|
|
|
}
|
2015-01-07 17:43:37 +13:00
|
|
|
|
2015-01-12 16:46:12 +13:00
|
|
|
fn markups(&mut self) {
|
2015-01-07 17:43:37 +13:00
|
|
|
loop {
|
|
|
|
match self.input {
|
2015-01-12 16:46:12 +13:00
|
|
|
[] => return,
|
2015-01-07 17:43:37 +13:00
|
|
|
[semi!(), ..] => self.shift(1),
|
2015-01-12 16:46:12 +13:00
|
|
|
[_, ..] => if !self.markup() { return },
|
2015-01-07 17:43:37 +13:00
|
|
|
}
|
|
|
|
}
|
2014-12-20 20:53:38 +13:00
|
|
|
}
|
|
|
|
|
2015-01-10 21:29:58 +13:00
|
|
|
fn markup(&mut self) -> bool {
|
2015-01-11 11:50:52 +13:00
|
|
|
match self.input {
|
|
|
|
// Literal
|
2014-12-20 20:41:53 +13:00
|
|
|
[minus!(), ref tt @ literal!(), ..] => {
|
2015-01-07 17:43:37 +13:00
|
|
|
self.shift(2);
|
2015-03-27 13:57:51 +13:00
|
|
|
self.literal(tt, true);
|
2014-12-20 20:41:53 +13:00
|
|
|
},
|
|
|
|
[ref tt @ literal!(), ..] => {
|
2015-01-07 17:43:37 +13:00
|
|
|
self.shift(1);
|
2015-03-27 13:57:51 +13:00
|
|
|
self.literal(tt, false);
|
2014-12-20 20:41:53 +13:00
|
|
|
},
|
2015-02-27 09:27:45 +13:00
|
|
|
// If
|
2015-09-12 13:53:14 +12:00
|
|
|
[pound!(), ident!(sp, name), ..] if name.name == "if" => {
|
2015-02-27 09:27:45 +13:00
|
|
|
self.shift(2);
|
|
|
|
self.if_expr(sp);
|
|
|
|
},
|
2015-03-14 21:08:08 +13:00
|
|
|
// For
|
2015-09-12 13:53:14 +12:00
|
|
|
[pound!(), ident!(sp, name), ..] if name.name == "for" => {
|
2015-03-14 21:08:08 +13:00
|
|
|
self.shift(2);
|
|
|
|
self.for_expr(sp);
|
|
|
|
},
|
2015-01-11 11:50:52 +13:00
|
|
|
// Splice
|
|
|
|
[ref tt @ dollar!(), dollar!(), ..] => {
|
|
|
|
self.shift(2);
|
2015-01-29 13:47:11 +13:00
|
|
|
let expr = self.splice(tt.get_span());
|
|
|
|
self.render.splice(expr, Escape::PassThru);
|
2015-01-11 11:50:52 +13:00
|
|
|
},
|
|
|
|
[ref tt @ dollar!(), ..] => {
|
|
|
|
self.shift(1);
|
2015-01-29 13:47:11 +13:00
|
|
|
let expr = self.splice(tt.get_span());
|
|
|
|
self.render.splice(expr, Escape::Escape);
|
2015-01-11 11:50:52 +13:00
|
|
|
},
|
|
|
|
// Element
|
|
|
|
[ident!(sp, name), ..] => {
|
|
|
|
self.shift(1);
|
2015-07-31 17:33:50 +12:00
|
|
|
self.element(sp, &name.name.as_str());
|
2015-01-11 11:50:52 +13:00
|
|
|
},
|
|
|
|
// Block
|
2015-04-10 19:00:47 +12:00
|
|
|
[TtDelimited(_, ref d), ..] if d.delim == token::DelimToken::Brace => {
|
2015-01-11 11:50:52 +13:00
|
|
|
self.shift(1);
|
2015-04-10 19:00:47 +12:00
|
|
|
{
|
|
|
|
// Parse the contents of the block, emitting the
|
|
|
|
// result inline
|
|
|
|
let mut i = &*d.tts;
|
|
|
|
mem::swap(&mut self.input, &mut i);
|
|
|
|
self.markups();
|
|
|
|
mem::swap(&mut self.input, &mut i);
|
|
|
|
}
|
2015-01-11 11:50:52 +13:00
|
|
|
},
|
|
|
|
// ???
|
|
|
|
_ => {
|
|
|
|
if let [ref tt, ..] = self.input {
|
|
|
|
self.render.cx.span_err(tt.get_span(), "invalid syntax");
|
2015-01-12 16:24:53 +13:00
|
|
|
} else {
|
|
|
|
self.render.cx.span_err(self.span, "unexpected end of block");
|
2015-01-11 11:50:52 +13:00
|
|
|
}
|
2015-01-12 16:46:12 +13:00
|
|
|
return false;
|
2015-01-11 11:50:52 +13:00
|
|
|
},
|
|
|
|
}
|
2015-01-12 16:46:12 +13:00
|
|
|
true
|
2015-01-11 11:50:52 +13:00
|
|
|
}
|
|
|
|
|
2015-01-12 16:46:12 +13:00
|
|
|
fn literal(&mut self, tt: &TokenTree, minus: bool) {
|
2015-03-27 09:49:23 +13:00
|
|
|
let lit = self.with_rust_parser(vec![tt.clone()], RustParser::parse_lit);
|
2015-04-08 22:43:48 +12:00
|
|
|
let lit = match lit {
|
|
|
|
Ok(lit) => lit,
|
|
|
|
Err(err) => panic!(err),
|
|
|
|
};
|
2015-01-10 21:29:58 +13:00
|
|
|
match lit_to_string(self.render.cx, lit, minus) {
|
2015-02-02 20:53:47 +13:00
|
|
|
Some(s) => self.render.string(&s, Escape::Escape),
|
2015-01-12 16:46:12 +13:00
|
|
|
None => {},
|
2015-01-10 21:29:58 +13:00
|
|
|
}
|
2015-01-07 17:43:37 +13:00
|
|
|
}
|
|
|
|
|
2015-02-27 09:27:45 +13:00
|
|
|
fn if_expr(&mut self, sp: Span) {
|
2015-03-01 19:34:08 -05:00
|
|
|
// Parse the initial if
|
2015-03-15 16:23:19 +13:00
|
|
|
let mut if_cond = vec![];
|
2015-02-27 09:27:45 +13:00
|
|
|
let if_body;
|
|
|
|
loop { match self.input {
|
|
|
|
[TtDelimited(sp, ref d), ..] if d.delim == DelimToken::Brace => {
|
|
|
|
self.shift(1);
|
|
|
|
if_body = self.block(sp, &d.tts);
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
[ref tt, ..] => {
|
|
|
|
self.shift(1);
|
2015-03-15 16:23:19 +13:00
|
|
|
if_cond.push(tt.clone());
|
2015-02-27 09:27:45 +13:00
|
|
|
},
|
2015-09-12 13:53:14 +12:00
|
|
|
[] => self.render.cx.span_fatal(sp, "expected body for this #if"),
|
2015-02-27 09:27:45 +13:00
|
|
|
}}
|
2015-03-01 19:34:08 -05:00
|
|
|
// Parse the (optional) else
|
2015-02-27 09:27:45 +13:00
|
|
|
let else_body = match self.input {
|
2015-09-12 13:53:14 +12:00
|
|
|
[pound!(), ident!(else_), ..] if else_.name == "else" => {
|
2015-02-27 09:27:45 +13:00
|
|
|
self.shift(2);
|
2015-03-01 19:34:08 -05:00
|
|
|
match self.input {
|
2015-07-31 17:33:50 +12:00
|
|
|
[ident!(sp, if_), ..] if if_.name == "if" => {
|
2015-03-01 19:34:08 -05:00
|
|
|
self.shift(1);
|
|
|
|
let else_body = {
|
|
|
|
// Parse an if expression, but capture the result
|
|
|
|
// rather than emitting it right away
|
2015-04-10 19:01:32 +12:00
|
|
|
let mut r = self.render.fork();
|
|
|
|
mem::swap(&mut self.render, &mut r);
|
2015-03-01 19:34:08 -05:00
|
|
|
self.if_expr(sp);
|
2015-04-10 19:01:32 +12:00
|
|
|
mem::swap(&mut self.render, &mut r);
|
|
|
|
r.into_stmts()
|
2015-03-01 19:34:08 -05:00
|
|
|
};
|
|
|
|
Some(else_body)
|
|
|
|
},
|
|
|
|
[TtDelimited(sp, ref d), ..] if d.delim == DelimToken::Brace => {
|
|
|
|
self.shift(1);
|
|
|
|
Some(self.block(sp, &d.tts))
|
|
|
|
},
|
2015-09-12 13:53:14 +12:00
|
|
|
_ => self.render.cx.span_fatal(sp, "expected body for this #else"),
|
2015-03-01 19:34:08 -05:00
|
|
|
}
|
2015-02-27 09:27:45 +13:00
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
};
|
2015-03-01 19:34:08 -05:00
|
|
|
self.render.emit_if(if_cond, if_body, else_body);
|
2015-02-27 09:27:45 +13:00
|
|
|
}
|
|
|
|
|
2015-03-14 21:08:08 +13:00
|
|
|
fn for_expr(&mut self, sp: Span) {
|
|
|
|
let mut pattern = vec![];
|
|
|
|
loop { match self.input {
|
2015-07-31 17:33:50 +12:00
|
|
|
[ident!(in_), ..] if in_.name == "in" => {
|
2015-03-14 21:08:08 +13:00
|
|
|
self.shift(1);
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
[ref tt, ..] => {
|
|
|
|
self.shift(1);
|
|
|
|
pattern.push(tt.clone());
|
|
|
|
},
|
2015-09-12 13:53:14 +12:00
|
|
|
_ => self.render.cx.span_fatal(sp, "invalid #for"),
|
2015-03-14 21:08:08 +13:00
|
|
|
}}
|
2015-03-27 09:49:23 +13:00
|
|
|
let pattern = self.with_rust_parser(pattern, RustParser::parse_pat);
|
2015-03-14 21:08:08 +13:00
|
|
|
let mut iterable = vec![];
|
|
|
|
let body;
|
|
|
|
loop { match self.input {
|
|
|
|
[TtDelimited(sp, ref d), ..] if d.delim == DelimToken::Brace => {
|
|
|
|
self.shift(1);
|
|
|
|
body = self.block(sp, &d.tts);
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
[ref tt, ..] => {
|
|
|
|
self.shift(1);
|
|
|
|
iterable.push(tt.clone());
|
|
|
|
},
|
2015-09-12 13:53:14 +12:00
|
|
|
_ => self.render.cx.span_fatal(sp, "invalid #for"),
|
2015-03-14 21:08:08 +13:00
|
|
|
}}
|
2015-03-27 09:49:23 +13:00
|
|
|
let iterable = self.with_rust_parser(iterable, RustParser::parse_expr);
|
2015-03-14 21:08:08 +13:00
|
|
|
self.render.emit_for(pattern, iterable, body);
|
|
|
|
}
|
|
|
|
|
2015-01-29 13:47:11 +13:00
|
|
|
fn splice(&mut self, sp: Span) -> P<Expr> {
|
2015-01-18 20:18:21 +13:00
|
|
|
let mut tts = vec![];
|
|
|
|
// First, munch a single token tree
|
|
|
|
if let [ref tt, ..] = self.input {
|
|
|
|
self.shift(1);
|
|
|
|
tts.push(tt.clone());
|
|
|
|
}
|
2015-03-15 16:42:34 +13:00
|
|
|
loop { match self.input {
|
|
|
|
// Munch attribute lookups e.g. `$person.address.street`
|
|
|
|
[ref dot @ dot!(), ref ident @ ident!(_), ..] => {
|
|
|
|
self.shift(2);
|
|
|
|
tts.push(dot.clone());
|
|
|
|
tts.push(ident.clone());
|
|
|
|
},
|
|
|
|
// Munch function calls `()` and indexing operations `[]`
|
|
|
|
[TtDelimited(sp, ref d), ..] if d.delim != token::DelimToken::Brace => {
|
|
|
|
self.shift(1);
|
|
|
|
tts.push(TtDelimited(sp, d.clone()));
|
|
|
|
},
|
|
|
|
_ => break,
|
|
|
|
}}
|
2015-01-18 20:18:21 +13:00
|
|
|
if tts.is_empty() {
|
2015-01-29 13:47:11 +13:00
|
|
|
self.render.cx.span_fatal(sp, "expected expression for this splice");
|
2015-01-18 20:18:21 +13:00
|
|
|
} else {
|
2015-03-27 09:49:23 +13:00
|
|
|
self.with_rust_parser(tts, RustParser::parse_expr)
|
2015-01-18 20:18:21 +13:00
|
|
|
}
|
2015-01-07 17:43:37 +13:00
|
|
|
}
|
|
|
|
|
2015-02-07 17:08:08 +13:00
|
|
|
fn element(&mut self, sp: Span, name: &str) {
|
2015-01-11 11:50:52 +13:00
|
|
|
if self.in_attr {
|
|
|
|
self.render.cx.span_err(sp, "unexpected element, you silly bumpkin");
|
2015-01-12 16:46:12 +13:00
|
|
|
return;
|
2015-01-11 11:50:52 +13:00
|
|
|
}
|
2015-01-10 21:29:58 +13:00
|
|
|
self.render.element_open_start(name);
|
2015-01-12 16:46:12 +13:00
|
|
|
self.attrs();
|
2015-01-10 21:29:58 +13:00
|
|
|
self.render.element_open_end();
|
2015-01-11 12:51:35 +13:00
|
|
|
if let [slash!(), ..] = self.input {
|
|
|
|
self.shift(1);
|
|
|
|
} else {
|
2015-01-12 16:46:12 +13:00
|
|
|
self.markup();
|
2015-01-11 12:51:35 +13:00
|
|
|
self.render.element_close(name);
|
|
|
|
}
|
2014-12-18 18:57:55 +13:00
|
|
|
}
|
2015-01-07 17:43:37 +13:00
|
|
|
|
2015-01-12 16:46:12 +13:00
|
|
|
fn attrs(&mut self) {
|
2015-01-29 13:47:11 +13:00
|
|
|
loop { match self.input {
|
|
|
|
[ident!(name), eq!(), ..] => {
|
2015-01-13 16:46:37 +13:00
|
|
|
// Non-empty attribute
|
2015-01-29 13:47:11 +13:00
|
|
|
self.shift(2);
|
2015-07-31 17:33:50 +12:00
|
|
|
self.render.attribute_start(&name.name.as_str());
|
2015-01-13 16:46:37 +13:00
|
|
|
{
|
|
|
|
// Parse a value under an attribute context
|
2015-02-27 09:27:45 +13:00
|
|
|
let mut in_attr = true;
|
2015-02-27 14:34:57 +13:00
|
|
|
mem::swap(&mut self.in_attr, &mut in_attr);
|
2015-01-13 16:46:37 +13:00
|
|
|
self.markup();
|
2015-02-27 14:34:57 +13:00
|
|
|
mem::swap(&mut self.in_attr, &mut in_attr);
|
2015-01-13 16:46:37 +13:00
|
|
|
}
|
|
|
|
self.render.attribute_end();
|
2015-01-29 13:47:11 +13:00
|
|
|
},
|
|
|
|
[ident!(name), question!(), ..] => {
|
|
|
|
// Empty attribute
|
|
|
|
self.shift(2);
|
|
|
|
if let [ref tt @ eq!(), ..] = self.input {
|
|
|
|
// Toggle the attribute based on a boolean expression
|
|
|
|
self.shift(1);
|
2015-02-06 15:43:53 +13:00
|
|
|
let cond = self.splice(tt.get_span());
|
2015-02-09 15:05:50 +13:00
|
|
|
// Silence "unnecessary parentheses" warnings
|
2015-03-15 16:23:19 +13:00
|
|
|
let cond = strip_outer_parens(cond).to_tokens(self.render.cx);
|
2015-02-09 15:05:50 +13:00
|
|
|
let body = {
|
|
|
|
let mut r = self.render.fork();
|
2015-07-31 17:33:50 +12:00
|
|
|
r.attribute_empty(&name.name.as_str());
|
2015-02-09 15:05:50 +13:00
|
|
|
r.into_stmts()
|
|
|
|
};
|
|
|
|
self.render.emit_if(cond, body, None);
|
2015-01-29 13:47:11 +13:00
|
|
|
} else {
|
|
|
|
// Write the attribute unconditionally
|
2015-07-31 17:33:50 +12:00
|
|
|
self.render.attribute_empty(&name.name.as_str());
|
2015-01-29 13:47:11 +13:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => return,
|
|
|
|
}}
|
2014-12-18 18:57:55 +13:00
|
|
|
}
|
|
|
|
|
2015-02-07 17:48:09 +13:00
|
|
|
fn block(&mut self, sp: Span, tts: &[TokenTree]) -> Vec<P<Stmt>> {
|
|
|
|
let mut parse = Parser {
|
2015-01-11 11:50:52 +13:00
|
|
|
in_attr: self.in_attr,
|
|
|
|
input: tts,
|
2015-02-07 17:05:36 +13:00
|
|
|
span: sp,
|
2015-02-27 14:34:57 +13:00
|
|
|
render: self.render.fork(),
|
2015-02-07 17:48:09 +13:00
|
|
|
};
|
|
|
|
parse.markups();
|
|
|
|
parse.into_render().into_stmts()
|
2015-01-07 17:43:37 +13:00
|
|
|
}
|
2014-12-18 18:57:55 +13:00
|
|
|
}
|
|
|
|
|
2015-01-07 17:43:37 +13:00
|
|
|
/// Convert a literal to a string.
|
2015-01-30 18:43:53 +13:00
|
|
|
fn lit_to_string(cx: &ExtCtxt, lit: Lit, minus: bool) -> Option<String> {
|
2014-12-18 18:57:55 +13:00
|
|
|
use syntax::ast::Lit_::*;
|
|
|
|
let mut result = String::new();
|
|
|
|
if minus {
|
|
|
|
result.push('-');
|
|
|
|
}
|
|
|
|
match lit.node {
|
2015-02-09 15:03:54 +13:00
|
|
|
LitStr(s, _) => result.push_str(&s),
|
2015-09-06 11:26:57 +12:00
|
|
|
LitByteStr(..) | LitByte(..) => {
|
2014-12-18 18:57:55 +13:00
|
|
|
cx.span_err(lit.span, "cannot splice binary data");
|
|
|
|
return None;
|
|
|
|
},
|
|
|
|
LitChar(c) => result.push(c),
|
2015-02-02 20:53:47 +13:00
|
|
|
LitInt(x, _) => result.push_str(&x.to_string()),
|
2015-02-09 15:03:54 +13:00
|
|
|
LitFloat(s, _) | LitFloatUnsuffixed(s) => result.push_str(&s),
|
2014-12-18 18:57:55 +13:00
|
|
|
LitBool(b) => result.push_str(if b { "true" } else { "false" }),
|
|
|
|
};
|
|
|
|
Some(result)
|
|
|
|
}
|
2015-02-09 15:05:50 +13:00
|
|
|
|
|
|
|
/// If the expression is wrapped in parentheses, strip them off.
|
|
|
|
fn strip_outer_parens(expr: P<Expr>) -> P<Expr> {
|
|
|
|
expr.and_then(|expr| match expr {
|
|
|
|
Expr { node: ExprParen(inner), .. } => inner,
|
|
|
|
expr => P(expr),
|
|
|
|
})
|
|
|
|
}
|