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)];
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:
22 { tag_stack* # expr* # tokens }
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
32 parse_node!( []; []; $($body)* )
36 macro_rules! parse_node (
38 [:$head:ident ($(:$head_nodes:expr),*)
39 $(:$tags:ident ($(:$tag_nodes:expr),*))*];
41 </$tag:ident> $($rest:tt)*
44 [$(: $tags ($(:$tag_nodes),*))*];
45 [$(:$head_nodes,)* :tag(stringify!($head).to_owned(),
52 [$(:$tags:ident ($(:$tag_nodes:expr),*) )*];
54 <$tag:ident> $($rest:tt)*
57 [:$tag ($(:$nodes)*) $(: $tags ($(:$tag_nodes),*) )*];
64 [$(:$tags:ident ($(:$tag_nodes:expr),*) )*];
69 [$(: $tags ($(:$tag_nodes),*))*];
70 [$(:$nodes,)* :text(~".")];
76 [$(:$tags:ident ($(:$tag_nodes:expr),*) )*];
78 $word:ident $($rest:tt)*
81 [$(: $tags ($(:$tag_nodes),*))*];
82 [$(:$nodes,)* :text(stringify!($word).to_owned())];
87 ( []; [:$e:expr]; ) => ( $e );
93 <head><title>This is the title.</title></head>
95 <p>This is some text</p>
102 tag(~str, Vec<HTMLFragment> ),