Skip to content

Commit

Permalink
Refactor Builder
Browse files Browse the repository at this point in the history
I'm unsure about the set_span call, but I did not want to throw away the
passed in Span. Refs lambda-fairy#121
  • Loading branch information
anxiousmodernman committed Apr 9, 2018
1 parent e908f76 commit e09ec27
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions maud_macros/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use proc_macro::{Delimiter, Literal, Span, TokenStream, TokenTree};
use proc_macro::{Delimiter, Group, Literal, Span, TokenStream, TokenTree};
use proc_macro::quote;
use proc_macro::token_stream;

use std::iter::FromIterator;
use maud_htmlescape::Escaper;

pub struct Builder {
Expand All @@ -24,7 +26,7 @@ impl Builder {
if !self.tail.is_empty() {
let expr = {
let output_ident = self.output_ident.clone();
let string = TokenNode::Literal(Literal::string(&self.tail));
let string = TokenTree::Literal(Literal::string(&self.tail));
quote!($output_ident.push_str($string);)
};
self.stmts.push(expr);
Expand All @@ -35,7 +37,11 @@ impl Builder {
/// Reifies the `Builder` into a raw list of statements.
pub fn build(mut self) -> TokenStream {
let Builder { stmts, .. } = { self.flush(); self };
stmts.into_iter().collect()
let mut i: token_stream::IntoIter;
for ts in stmts {
i.chain(ts);
}
TokenStream::from_iter(i.into_iter())
}

/// Pushes a statement, flushing the tail buffer in the process.
Expand Down Expand Up @@ -111,14 +117,14 @@ impl Builder {
) {
// If the condition contains an opening brace `{`,
// wrap it in parentheses to avoid parse errors
if cond.clone().into_iter().any(|token| match token.kind {
TokenNode::Group(Delimiter::Brace, _) => true,
if cond.clone().into_iter().any(|token| match token {
TokenTree::Group(Group { delimiter: Delimiter::Brace, .. }) => true,
_ => false,
}) {
cond = TokenStream::from(TokenTree {
kind: TokenNode::Group(Delimiter::Parenthesis, cond),
span: cond_span,
});
let mut g = Group::new(Delimiter::Parenthesis, cond);
// NOTE: Do we need to do this?
g.set_span(cond_span);
cond = TokenStream::from(TokenTree::Group(g));
}
self.push(quote!(if $cond { $body }));
}
Expand Down

0 comments on commit e09ec27

Please sign in to comment.