2012-12-11 01:32:48 +00:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-08-09 23:48:31 +00:00
|
|
|
// A test of the macro system. Can we do HTML literals?
|
|
|
|
|
2012-08-10 18:48:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
This is an HTML parser written as a macro. It's all CPS, and we have
|
|
|
|
to carry around a bunch of state. The arguments to macros all look like this:
|
|
|
|
|
|
|
|
{ tag_stack* # expr* # tokens }
|
|
|
|
|
|
|
|
The stack keeps track of where we are in the tree. The expr is a list
|
|
|
|
of children of the current node. The tokens are everything that's
|
|
|
|
left.
|
|
|
|
|
|
|
|
*/
|
2012-08-09 23:48:31 +00:00
|
|
|
|
2012-08-23 00:47:11 +00:00
|
|
|
macro_rules! html (
|
2012-08-23 01:06:54 +00:00
|
|
|
( $($body:tt)* ) => (
|
2012-08-10 18:48:09 +00:00
|
|
|
parse_node!( []; []; $($body)* )
|
|
|
|
)
|
2012-08-23 00:47:11 +00:00
|
|
|
)
|
2012-08-09 23:48:31 +00:00
|
|
|
|
2012-08-23 00:47:11 +00:00
|
|
|
macro_rules! parse_node (
|
2012-08-23 01:06:54 +00:00
|
|
|
(
|
2012-08-10 18:48:09 +00:00
|
|
|
[:$head:ident ($(:$head_nodes:expr),*)
|
|
|
|
$(:$tags:ident ($(:$tag_nodes:expr),*))*];
|
|
|
|
[$(:$nodes:expr),*];
|
|
|
|
</$tag:ident> $($rest:tt)*
|
2012-08-23 01:06:54 +00:00
|
|
|
) => (
|
2012-08-10 18:48:09 +00:00
|
|
|
parse_node!(
|
|
|
|
[$(: $tags ($(:$tag_nodes),*))*];
|
2012-12-14 19:52:11 +00:00
|
|
|
[$(:$head_nodes,)* :tag(stringify!($head).to_owned(),
|
|
|
|
~[$($nodes),*])];
|
2012-08-10 18:48:09 +00:00
|
|
|
$($rest)*
|
|
|
|
)
|
|
|
|
);
|
2012-08-09 23:48:31 +00:00
|
|
|
|
2012-08-23 01:06:54 +00:00
|
|
|
(
|
2012-08-10 18:48:09 +00:00
|
|
|
[$(:$tags:ident ($(:$tag_nodes:expr),*) )*];
|
|
|
|
[$(:$nodes:expr),*];
|
|
|
|
<$tag:ident> $($rest:tt)*
|
2012-08-23 01:06:54 +00:00
|
|
|
) => (
|
2012-08-10 18:48:09 +00:00
|
|
|
parse_node!(
|
|
|
|
[:$tag ($(:$nodes)*) $(: $tags ($(:$tag_nodes),*) )*];
|
|
|
|
[];
|
|
|
|
$($rest)*
|
|
|
|
)
|
|
|
|
);
|
2012-08-09 23:48:31 +00:00
|
|
|
|
2012-08-23 01:06:54 +00:00
|
|
|
(
|
2012-08-10 18:48:09 +00:00
|
|
|
[$(:$tags:ident ($(:$tag_nodes:expr),*) )*];
|
|
|
|
[$(:$nodes:expr),*];
|
|
|
|
. $($rest:tt)*
|
2012-08-23 01:06:54 +00:00
|
|
|
) => (
|
2012-08-10 18:48:09 +00:00
|
|
|
parse_node!(
|
|
|
|
[$(: $tags ($(:$tag_nodes),*))*];
|
|
|
|
[$(:$nodes,)* :text(~".")];
|
|
|
|
$($rest)*
|
|
|
|
)
|
|
|
|
);
|
2012-08-09 23:48:31 +00:00
|
|
|
|
2012-08-23 01:06:54 +00:00
|
|
|
(
|
2012-08-10 18:48:09 +00:00
|
|
|
[$(:$tags:ident ($(:$tag_nodes:expr),*) )*];
|
|
|
|
[$(:$nodes:expr),*];
|
|
|
|
$word:ident $($rest:tt)*
|
2012-08-23 01:06:54 +00:00
|
|
|
) => (
|
2012-08-10 18:48:09 +00:00
|
|
|
parse_node!(
|
|
|
|
[$(: $tags ($(:$tag_nodes),*))*];
|
2012-12-14 19:52:11 +00:00
|
|
|
[$(:$nodes,)* :text(stringify!($word).to_owned())];
|
2012-08-10 18:48:09 +00:00
|
|
|
$($rest)*
|
|
|
|
)
|
|
|
|
);
|
2012-08-09 23:48:31 +00:00
|
|
|
|
2012-08-23 01:06:54 +00:00
|
|
|
( []; [:$e:expr]; ) => ( $e );
|
2012-08-23 00:47:11 +00:00
|
|
|
)
|
2012-08-09 23:48:31 +00:00
|
|
|
|
|
|
|
fn main() {
|
2012-08-23 00:24:52 +00:00
|
|
|
let page = html! (
|
2012-08-09 23:48:31 +00:00
|
|
|
<html>
|
|
|
|
<head><title>This is the title.</title></head>
|
|
|
|
<body>
|
|
|
|
<p>This is some text</p>
|
|
|
|
</body>
|
|
|
|
</html>
|
2012-08-23 00:24:52 +00:00
|
|
|
);
|
2012-08-09 23:48:31 +00:00
|
|
|
}
|
|
|
|
|
2012-08-10 18:48:09 +00:00
|
|
|
enum HTMLFragment {
|
|
|
|
tag(~str, ~[HTMLFragment]),
|
|
|
|
text(~str),
|
2012-08-09 23:48:31 +00:00
|
|
|
}
|