]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/html-literals.rs
test: Make manual changes to deal with the fallout from removal of
[rust.git] / src / test / run-pass / html-literals.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // A test of the macro system. Can we do HTML literals?
12
13 #[feature(macro_rules)];
14
15 use std::vec_ng::Vec;
16
17 /*
18
19 This is an HTML parser written as a macro. It's all CPS, and we have
20 to carry around a bunch of state. The arguments to macros all look like this:
21
22 { tag_stack* # expr* # tokens }
23
24 The stack keeps track of where we are in the tree. The expr is a list
25 of children of the current node. The tokens are everything that's
26 left.
27
28 */
29
30 macro_rules! html (
31     ( $($body:tt)* ) => (
32         parse_node!( []; []; $($body)* )
33     )
34 )
35
36 macro_rules! parse_node (
37     (
38         [:$head:ident ($(:$head_nodes:expr),*)
39          $(:$tags:ident ($(:$tag_nodes:expr),*))*];
40         [$(:$nodes:expr),*];
41         </$tag:ident> $($rest:tt)*
42     ) => (
43         parse_node!(
44             [$(: $tags ($(:$tag_nodes),*))*];
45             [$(:$head_nodes,)* :tag(stringify!($head).to_owned(),
46                                     vec!($($nodes),*))];
47             $($rest)*
48         )
49     );
50
51     (
52         [$(:$tags:ident ($(:$tag_nodes:expr),*) )*];
53         [$(:$nodes:expr),*];
54         <$tag:ident> $($rest:tt)*
55     ) => (
56         parse_node!(
57             [:$tag ($(:$nodes)*) $(: $tags ($(:$tag_nodes),*) )*];
58             [];
59             $($rest)*
60         )
61     );
62
63     (
64         [$(:$tags:ident ($(:$tag_nodes:expr),*) )*];
65         [$(:$nodes:expr),*];
66         . $($rest:tt)*
67     ) => (
68         parse_node!(
69             [$(: $tags ($(:$tag_nodes),*))*];
70             [$(:$nodes,)* :text(~".")];
71             $($rest)*
72         )
73     );
74
75     (
76         [$(:$tags:ident ($(:$tag_nodes:expr),*) )*];
77         [$(:$nodes:expr),*];
78         $word:ident $($rest:tt)*
79     ) => (
80         parse_node!(
81             [$(: $tags ($(:$tag_nodes),*))*];
82             [$(:$nodes,)* :text(stringify!($word).to_owned())];
83             $($rest)*
84         )
85     );
86
87     ( []; [:$e:expr]; ) => ( $e );
88 )
89
90 pub fn main() {
91     let _page = html! (
92         <html>
93             <head><title>This is the title.</title></head>
94             <body>
95             <p>This is some text</p>
96             </body>
97         </html>
98     );
99 }
100
101 enum HTMLFragment {
102     tag(~str, Vec<HTMLFragment> ),
103     text(~str),
104 }