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.
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.
11 // A test of the macro system. Can we do HTML literals?
13 #[feature(macro_rules)];
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:
20 { tag_stack* # expr* # tokens }
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
30 parse_node!( []; []; $($body)* )
34 macro_rules! parse_node (
36 [:$head:ident ($(:$head_nodes:expr),*)
37 $(:$tags:ident ($(:$tag_nodes:expr),*))*];
39 </$tag:ident> $($rest:tt)*
42 [$(: $tags ($(:$tag_nodes),*))*];
43 [$(:$head_nodes,)* :tag(stringify!($head).to_owned(),
50 [$(:$tags:ident ($(:$tag_nodes:expr),*) )*];
52 <$tag:ident> $($rest:tt)*
55 [:$tag ($(:$nodes)*) $(: $tags ($(:$tag_nodes),*) )*];
62 [$(:$tags:ident ($(:$tag_nodes:expr),*) )*];
67 [$(: $tags ($(:$tag_nodes),*))*];
68 [$(:$nodes,)* :text(~".")];
74 [$(:$tags:ident ($(:$tag_nodes:expr),*) )*];
76 $word:ident $($rest:tt)*
79 [$(: $tags ($(:$tag_nodes),*))*];
80 [$(:$nodes,)* :text(stringify!($word).to_owned())];
85 ( []; [:$e:expr]; ) => ( $e );
91 <head><title>This is the title.</title></head>
93 <p>This is some text</p>
100 tag(~str, Vec<HTMLFragment> ),