]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/html-literals.rs
cleanup: s/impl Copy/#[derive(Copy)]/g
[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 // ignore-test FIXME #20673
14
15 /*
16
17 This is an HTML parser written as a macro. It's all CPS, and we have
18 to carry around a bunch of state. The arguments to macros all look like this:
19
20 { tag_stack* # expr* # tokens }
21
22 The stack keeps track of where we are in the tree. The expr is a list
23 of children of the current node. The tokens are everything that's
24 left.
25
26 */
27 use HTMLFragment::{tag, text};
28
29 macro_rules! html {
30     ( $($body:tt)* ) => (
31         parse_node!( []; []; $($body)* )
32     )
33 }
34
35 macro_rules! parse_node {
36     (
37         [:$head:ident ($(:$head_nodes:expr),*)
38          $(:$tags:ident ($(:$tag_nodes:expr),*))*];
39         [$(:$nodes:expr),*];
40         </$tag:ident> $($rest:tt)*
41     ) => (
42         parse_node!(
43             [$(: $tags ($(:$tag_nodes),*))*];
44             [$(:$head_nodes,)* :tag(stringify!($head).to_string(),
45                                     vec!($($nodes),*))];
46             $($rest)*
47         )
48     );
49
50     (
51         [$(:$tags:ident ($(:$tag_nodes:expr),*) )*];
52         [$(:$nodes:expr),*];
53         <$tag:ident> $($rest:tt)*
54     ) => (
55         parse_node!(
56             [:$tag ($(:$nodes)*) $(: $tags ($(:$tag_nodes),*) )*];
57             [];
58             $($rest)*
59         )
60     );
61
62     (
63         [$(:$tags:ident ($(:$tag_nodes:expr),*) )*];
64         [$(:$nodes:expr),*];
65         . $($rest:tt)*
66     ) => (
67         parse_node!(
68             [$(: $tags ($(:$tag_nodes),*))*];
69             [$(:$nodes,)* :text(".".to_string())];
70             $($rest)*
71         )
72     );
73
74     (
75         [$(:$tags:ident ($(:$tag_nodes:expr),*) )*];
76         [$(:$nodes:expr),*];
77         $word:ident $($rest:tt)*
78     ) => (
79         parse_node!(
80             [$(: $tags ($(:$tag_nodes),*))*];
81             [$(:$nodes,)* :text(stringify!($word).to_string())];
82             $($rest)*
83         )
84     );
85
86     ( []; [:$e:expr]; ) => ( $e );
87 }
88
89 pub fn main() {
90     let _page = html! (
91         <html>
92             <head><title>This is the title.</title></head>
93             <body>
94             <p>This is some text</p>
95             </body>
96         </html>
97     );
98 }
99
100 enum HTMLFragment {
101     tag(String, Vec<HTMLFragment> ),
102     text(String),
103 }