maud_macros 0.10.0

Compile-time HTML templates.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
use std::mem;
use std::rc::Rc;
use syntax::ast::{Expr, LitKind, Stmt};
use syntax::ext::quote::rt::ToTokens;
use syntax::codemap::Span;
use syntax::errors::{DiagnosticBuilder, FatalError};
use syntax::ext::base::ExtCtxt;
use syntax::parse;
use syntax::parse::parser::Parser as RustParser;
use syntax::parse::token::{BinOpToken, DelimToken, Token};
use syntax::parse::token::keywords;
use syntax::ptr::P;
use syntax::tokenstream::{Delimited, TokenTree};

use super::render::Renderer;
use super::PResult;

macro_rules! error {
    ($cx:expr, $sp:expr, $msg:expr) => ({
        $cx.span_err($sp, $msg);
        return Err(::syntax::errors::FatalError);
    })
}
macro_rules! parse_error {
    ($self_:expr, $sp:expr, $msg:expr) => (error!($self_.render.cx, $sp, $msg))
}

macro_rules! at {
    () => (TokenTree::Token(_, Token::At))
}
macro_rules! dot {
    () => (TokenTree::Token(_, Token::Dot))
}
macro_rules! eq {
    () => (TokenTree::Token(_, Token::Eq))
}
macro_rules! pound {
    () => (TokenTree::Token(_, Token::Pound))
}
macro_rules! question {
    () => (TokenTree::Token(_, Token::Question))
}
macro_rules! semi {
    () => (TokenTree::Token(_, Token::Semi))
}
macro_rules! colon {
    () => (TokenTree::Token(_, Token::Colon))
}
macro_rules! comma {
    () => (TokenTree::Token(_, Token::Comma))
}
macro_rules! fat_arrow {
    () => (TokenTree::Token(_, Token::FatArrow))
}
macro_rules! minus {
    () => (TokenTree::Token(_, Token::BinOp(BinOpToken::Minus)))
}
macro_rules! slash {
    () => (TokenTree::Token(_, Token::BinOp(BinOpToken::Slash)))
}
macro_rules! literal {
    () => (TokenTree::Token(_, Token::Literal(..)))
}
macro_rules! ident {
    ($sp:pat, $x:pat) => (TokenTree::Token($sp, Token::Ident($x)))
}
macro_rules! keyword {
    ($sp:pat, $x:ident) => (TokenTree::Token($sp, ref $x @ Token::Ident(..)))
}

pub fn parse(cx: &ExtCtxt, sp: Span, write: &[TokenTree], input: &[TokenTree])
    -> PResult<P<Expr>>
{
    let mut parser = Parser {
        in_attr: false,
        input: input,
        span: sp,
        render: Renderer::new(cx),
    };
    parser.markups()?;
    Ok(parser.into_render().into_expr(write.to_vec()))
}

pub fn split_comma<'a>(cx: &ExtCtxt, sp: Span, mac_name: &str, args: &'a [TokenTree])
    -> PResult<(&'a [TokenTree], &'a [TokenTree])>
{
    fn is_comma(t: &TokenTree) -> bool {
        match *t {
            TokenTree::Token(_, Token::Comma) => true,
            _ => false,
        }
    }
    match args.iter().position(is_comma) {
        Some(i) => Ok((&args[..i], &args[1+i..])),
        None => error!(cx, sp, &format!("expected two arguments to `{}!`", mac_name)),
    }
}

struct Parser<'cx, 'a: 'cx, 'i> {
    in_attr: bool,
    input: &'i [TokenTree],
    span: Span,
    render: Renderer<'cx, 'a>,
}

impl<'cx, 'a, 'i> Parser<'cx, 'a, 'i> {
    /// Finalizes the `Parser`, returning the `Renderer` underneath.
    fn into_render(self) -> Renderer<'cx, 'a> {
        let Parser { render, .. } = self;
        render
    }

    /// Consumes `n` items from the input.
    fn shift(&mut self, n: usize) {
        self.input = &self.input[n..];
    }

    /// Constructs a Rust AST parser from the given token tree.
    fn with_rust_parser<F, T>(&self, tts: Vec<TokenTree>, callback: F) -> PResult<T> where
        F: FnOnce(&mut RustParser<'cx>) -> Result<T, DiagnosticBuilder<'cx>>
    {
        let mut parser = parse::tts_to_parser(self.render.cx.parse_sess, tts,
                                              self.render.cx.cfg.clone());
        let result = callback(&mut parser).map_err(|mut e| { e.emit(); FatalError });
        // 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
    }

    /// Parses and renders multiple blocks of markup.
    fn markups(&mut self) -> PResult<()> {
        loop {
            match *self.input {
                [] => return Ok(()),
                [semi!(), ..] => self.shift(1),
                [_, ..] => self.markup()?,
            }
        }
    }

    /// Parses and renders a single block of markup.
    fn markup(&mut self) -> PResult<()> {
        match *self.input {
            // Literal
            [ref tt @ literal!(), ..] => {
                self.shift(1);
                self.literal(tt)?;
            },
            // If
            [at!(), keyword!(sp, k), ..] if k.is_keyword(keywords::If) => {
                self.shift(2);
                self.if_expr(sp)?;
            },
            // For
            [at!(), keyword!(sp, k), ..] if k.is_keyword(keywords::For) => {
                self.shift(2);
                self.for_expr(sp)?;
            },
            // Match
            [at!(), keyword!(sp, k), ..] if k.is_keyword(keywords::Match) => {
                self.shift(2);
                self.match_expr(sp)?;
            },
            // Call
            [at!(), ident!(_, name), TokenTree::Delimited(_, ref d), ..]
                if name.name.as_str() == "call" && d.delim == DelimToken::Paren =>
            {
                self.shift(3);
                let func = self.with_rust_parser(d.tts.clone(), RustParser::parse_expr)?;
                self.render.emit_call(func);
            },
            // Element
            [ident!(sp, _), ..] => {
                let name = self.namespaced_name().unwrap();
                self.element(sp, &name)?;
            },
            // Splice
            [TokenTree::Delimited(_, ref d), ..] if d.delim == DelimToken::Paren => {
                self.shift(1);
                let expr = self.with_rust_parser(d.tts.clone(), RustParser::parse_expr)?;
                self.render.splice(expr);
            }
            // Block
            [TokenTree::Delimited(_, ref d), ..] if d.delim == DelimToken::Brace => {
                self.shift(1);
                {
                    // 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);
                }
            },
            // ???
            _ => {
                if let [ref tt, ..] = *self.input {
                    parse_error!(self, tt.get_span(), "invalid syntax");
                } else {
                    parse_error!(self, self.span, "unexpected end of block");
                }
            },
        }
        Ok(())
    }

    /// Parses and renders a literal string.
    fn literal(&mut self, tt: &TokenTree) -> PResult<()> {
        let lit = self.with_rust_parser(vec![tt.clone()], RustParser::parse_lit)?;
        if let LitKind::Str(s, _) = lit.node {
            self.render.string(&s);
            Ok(())
        } else {
            parse_error!(self, lit.span, "literal strings must be surrounded by quotes (\"like this\")")
        }
    }

    /// Parses and renders an `@if` expression.
    ///
    /// The leading `@if` should already be consumed.
    fn if_expr(&mut self, sp: Span) -> PResult<()> {
        // Parse the initial if
        let mut if_cond = vec![];
        let if_body;
        loop { match *self.input {
            [TokenTree::Delimited(sp, ref d), ..] if d.delim == DelimToken::Brace => {
                self.shift(1);
                if_body = self.block(sp, &d.tts)?;
                break;
            },
            [ref tt, ..] => {
                self.shift(1);
                if_cond.push(tt.clone());
            },
            [] => parse_error!(self, sp, "expected body for this @if"),
        }}
        // Parse the (optional) @else
        let else_body = match *self.input {
            [at!(), keyword!(_, k), ..] if k.is_keyword(keywords::Else) => {
                self.shift(2);
                match *self.input {
                    [keyword!(sp, k), ..] if k.is_keyword(keywords::If) => {
                        self.shift(1);
                        let else_body = {
                            // Parse an if expression, but capture the result
                            // rather than emitting it right away
                            let mut r = self.render.fork();
                            mem::swap(&mut self.render, &mut r);
                            self.if_expr(sp)?;
                            mem::swap(&mut self.render, &mut r);
                            r.into_stmts()
                        };
                        Some(else_body)
                    },
                    [TokenTree::Delimited(sp, ref d), ..] if d.delim == DelimToken::Brace => {
                        self.shift(1);
                        Some(self.block(sp, &d.tts)?)
                    },
                    _ => parse_error!(self, sp, "expected body for this @else"),
                }
            },
            _ => None,
        };
        self.render.emit_if(if_cond, if_body, else_body);
        Ok(())
    }

    /// Parses and renders a `@for` expression.
    ///
    /// The leading `@for` should already be consumed.
    fn for_expr(&mut self, sp: Span) -> PResult<()> {
        let mut pattern = vec![];
        loop { match *self.input {
            [keyword!(_, k), ..] if k.is_keyword(keywords::In) => {
                self.shift(1);
                break;
            },
            [ref tt, ..] => {
                self.shift(1);
                pattern.push(tt.clone());
            },
            _ => parse_error!(self, sp, "invalid @for"),
        }}
        let pattern = self.with_rust_parser(pattern, RustParser::parse_pat)?;
        let mut iterable = vec![];
        let body;
        loop { match *self.input {
            [TokenTree::Delimited(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());
            },
            _ => parse_error!(self, sp, "invalid @for"),
        }}
        let iterable = self.with_rust_parser(iterable, RustParser::parse_expr)?;
        self.render.emit_for(pattern, iterable, body);
        Ok(())
    }

    /// Parses and renders a `@match` expression.
    ///
    /// The leading `@match` should already be consumed.
    fn match_expr(&mut self, sp: Span) -> PResult<()> {
        // Parse the initial match
        let mut match_var = vec![];
        let match_bodies;
        loop { match *self.input {
            [TokenTree::Delimited(sp, ref d), ..] if d.delim == DelimToken::Brace => {
                self.shift(1);
                match_bodies = Parser {
                    in_attr: self.in_attr,
                    input: &d.tts,
                    span: sp,
                    render: self.render.fork(),
                }.match_bodies()?;
                break;
            },
            [ref tt, ..] => {
                self.shift(1);
                match_var.push(tt.clone());
            },
            [] => parse_error!(self, sp, "expected body for this @match"),
        }}
        let match_var = self.with_rust_parser(match_var, RustParser::parse_expr)?;
        self.render.emit_match(match_var, match_bodies);
        Ok(())
    }

    fn match_bodies(&mut self) -> PResult<Vec<TokenTree>> {
        let mut bodies = Vec::new();
        loop { match *self.input {
            [] => break,
            [ref tt @ comma!(), ..] => {
                self.shift(1);
                bodies.push(tt.clone());
            },
            [TokenTree::Token(sp, _), ..] | [TokenTree::Delimited(sp, _), ..] | [TokenTree::Sequence(sp, _), ..] => {
                bodies.append(&mut self.match_body(sp)?);
            },
        }}
        Ok(bodies)
    }

    fn match_body(&mut self, sp: Span) -> PResult<Vec<TokenTree>> {
        let mut body = vec![];
        loop { match *self.input {
            [ref tt @ fat_arrow!(), ..] => {
                self.shift(1);
                body.push(tt.clone());
                break;
            },
            [ref tt, ..] => {
                self.shift(1);
                body.push(tt.clone());
            },
            _ => parse_error!(self, sp, "invalid @match pattern"),
        }}
        let mut expr = Vec::new();
        loop { match *self.input {
            [TokenTree::Delimited(sp, ref d), ..] if d.delim == DelimToken::Brace => {
                if expr.is_empty() {
                    self.shift(1);
                    expr = self.block(sp, &d.tts)?.to_tokens(self.render.cx);
                    break;
                } else {
                    self.shift(1);
                    expr.push(TokenTree::Delimited(sp, d.clone()));
                }
            },
            [comma!(), ..] | [] => {
                if expr.is_empty() {
                    parse_error!(self, sp, "expected body for this @match arm");
                } else {
                    expr = self.block(sp, &expr)?.to_tokens(self.render.cx);
                    break;
                }
            },
            [ref tt, ..] => {
                self.shift(1);
                expr.push(tt.clone());
            },
        }}
        body.push(TokenTree::Delimited(sp, Rc::new(Delimited {
            delim: DelimToken::Brace,
            open_span: sp,
            tts: expr,
            close_span: sp,
        })));
        Ok(body)
    }

    /// Parses and renders an element node.
    ///
    /// The element name should already be consumed.
    fn element(&mut self, sp: Span, name: &str) -> PResult<()> {
        if self.in_attr {
            parse_error!(self, sp, "unexpected element, you silly bumpkin");
        }
        self.render.element_open_start(name);
        self.attrs()?;
        self.render.element_open_end();
        if let [slash!(), ..] = *self.input {
            self.shift(1);
        } else {
            self.markup()?;
            self.render.element_close(name);
        }
        Ok(())
    }

    /// Parses and renders the attributes of an element.
    fn attrs(&mut self) -> PResult<()> {
        let mut classes = Vec::new();
        let mut ids = Vec::new();
        loop {
            let old_input = self.input;
            let maybe_name = self.namespaced_name();
            match (maybe_name, self.input) {
                (Ok(name), &[eq!(), ..]) => {
                    // Non-empty attribute
                    self.shift(1);
                    self.render.attribute_start(&name);
                    {
                        // Parse a value under an attribute context
                        let mut in_attr = true;
                        mem::swap(&mut self.in_attr, &mut in_attr);
                        self.markup()?;
                        mem::swap(&mut self.in_attr, &mut in_attr);
                    }
                    self.render.attribute_end();
                },
                (Ok(name), &[question!(), ..]) => {
                    // Empty attribute
                    self.shift(1);
                    match *self.input {
                        [TokenTree::Delimited(_, ref d), ..] if d.delim == DelimToken::Paren => {
                            // Toggle the attribute based on a boolean expression
                            self.shift(1);
                            let cond = self.with_rust_parser(d.tts.clone(), RustParser::parse_expr)?;
                            let cond = cond.to_tokens(self.render.cx);
                            let body = {
                                let mut r = self.render.fork();
                                r.attribute_empty(&name);
                                r.into_stmts()
                            };
                            self.render.emit_if(cond, body, None);
                        },
                        _ => {
                            // Write the attribute unconditionally
                            self.render.attribute_empty(&name);
                        },
                    }
                },
                (Err(_), &[dot!(), ident!(_, _), ..]) => {
                    // Class shorthand
                    self.shift(1);
                    classes.push(self.name().unwrap());
                },
                (Err(_), &[pound!(), ident!(_, _), ..]) => {
                    // ID shorthand
                    self.shift(1);
                    ids.push(self.name().unwrap());
                },
                _ => {
                    self.input = old_input;
                    break;
                },
            }
        }
        if !classes.is_empty() {
            self.render.attribute_start("class");
            self.render.string(&classes.join(" "));
            self.render.attribute_end();
        }
        if !ids.is_empty() {
            self.render.attribute_start("id");
            self.render.string(&ids.join(" "));
            self.render.attribute_end();
        }
        Ok(())
    }

    /// Parses an identifier, without dealing with namespaces.
    fn name(&mut self) -> PResult<String> {
        let mut s = match *self.input {
            [ident!(_, name), ..] => {
                self.shift(1);
                String::from(&name.name.as_str() as &str)
            },
            _ => return Err(FatalError),
        };
        let mut expect_ident = false;
        loop {
            expect_ident = match *self.input {
                [minus!(), ..] => {
                    self.shift(1);
                    s.push('-');
                    true
                },
                [ident!(_, name), ..] if expect_ident => {
                    self.shift(1);
                    s.push_str(&name.name.as_str());
                    false
                },
                _ => break,
            };
        }
        Ok(s)
    }

    /// Parses a HTML element or attribute name, along with a namespace
    /// if necessary.
    fn namespaced_name(&mut self) -> PResult<String> {
        let mut s = self.name()?;
        if let [colon!(), ident!(_, _), ..] = *self.input {
            self.shift(1);
            s.push(':');
            s.push_str(&self.name().unwrap());
        }
        Ok(s)
    }

    /// Parses the given token tree, returning a vector of statements.
    fn block(&mut self, sp: Span, tts: &[TokenTree]) -> PResult<Vec<Stmt>> {
        let mut parse = Parser {
            in_attr: self.in_attr,
            input: tts,
            span: sp,
            render: self.render.fork(),
        };
        parse.markups()?;
        Ok(parse.into_render().into_stmts())
    }
}