1
+ // A test of the macro system. Can we do HTML literals?
2
+
3
+ // xfail-pretty
4
+ // xfail-test
5
+
6
+ macro_rules! html {
7
+ { $( $body: tt) * } => {
8
+ let builder = HTMLBuilder ( ) ;
9
+ build_html!{ builder : = $( $body) * } ;
10
+ builder. getDoc( )
11
+ }
12
+ }
13
+
14
+ macro_rules! build_html {
15
+ { $builder: expr : = </$tag: ident> $( $rest: tt) * } => {
16
+ $builder. endTag( stringify!( $tag) ) ;
17
+ build_html!{ $builder : = $( $rest) * } ;
18
+ } ;
19
+
20
+ { $builder: expr : = <$tag: ident> $( $rest: tt) * } => {
21
+ $builder. beginTag( stringify!( $tag) ) ;
22
+ build_html!{ $builder : = $( $rest) * } ;
23
+ } ;
24
+
25
+ { $builder: expr : = . $( $rest: tt) * } => {
26
+ $builder. addText( ~". ");
27
+ build_html!{ $builder := $($rest)* };
28
+ };
29
+
30
+ { $builder:expr := $word:ident $($rest:tt)* } => {
31
+ $builder.addText(stringify!($word));
32
+ build_html!{ $builder := $($rest)* };
33
+ };
34
+
35
+ { $builder:expr := } => { }
36
+ }
37
+
38
+ fn main() {
39
+
40
+ let page = html! {
41
+ <html>
42
+ <head><title>This is the title.</title></head>
43
+ <body>
44
+ <p>This is some text</p>
45
+ </body>
46
+ </html>
47
+ };
48
+
49
+ // When we can do this, we are successful:
50
+ //
51
+ //let page = tag(~" html", ~[tag(~" head", ~[ ...] ) ] )
52
+
53
+ }
54
+
55
+ enum HTMLFragment {
56
+ }
57
+
58
+ struct HTMLBuilder {
59
+ bar: ( ) ;
60
+ fn getDoc( ) -> HTMLFragment { fail }
61
+ fn beginTag( tag: ~str ) { }
62
+ fn endTag( tag: ~str ) { }
63
+ fn addText( test: ~str ) { }
64
+ }
65
+
66
+ fn HTMLBuilder ( ) -> HTMLBuilder {
67
+ HTMLBuilder { bar: ( ) }
68
+ }
0 commit comments