2019-07-26 21:54:25 +00:00
|
|
|
// run-pass
|
|
|
|
|
2018-09-14 10:20:28 +00:00
|
|
|
#![allow(non_camel_case_types)]
|
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.
|
|
|
|
|
|
|
|
*/
|
2014-11-06 08:05:53 +00:00
|
|
|
use HTMLFragment::{tag, text};
|
2012-08-09 23:48:31 +00:00
|
|
|
|
2015-01-02 22:44:21 +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)* )
|
|
|
|
)
|
2015-01-02 22:44:21 +00:00
|
|
|
}
|
2012-08-09 23:48:31 +00:00
|
|
|
|
2015-01-02 22:44:21 +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),*))*];
|
2014-05-25 10:17:19 +00:00
|
|
|
[$(:$head_nodes,)* :tag(stringify!($head).to_string(),
|
2016-10-29 21:54:04 +00:00
|
|
|
vec![$($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),*))*];
|
2014-05-25 10:17:19 +00:00
|
|
|
[$(:$nodes,)* :text(".".to_string())];
|
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),*];
|
|
|
|
$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),*))*];
|
2014-05-25 10:17:19 +00:00
|
|
|
[$(:$nodes,)* :text(stringify!($word).to_string())];
|
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 );
|
2015-01-02 22:44:21 +00:00
|
|
|
}
|
2012-08-09 23:48:31 +00:00
|
|
|
|
2013-02-02 03:43:17 +00:00
|
|
|
pub fn main() {
|
2013-08-17 15:37:42 +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
|
|
|
}
|
|
|
|
|
2022-07-25 20:36:03 +00:00
|
|
|
#[allow(unused_tuple_struct_fields)]
|
2012-08-10 18:48:09 +00:00
|
|
|
enum HTMLFragment {
|
2014-05-22 23:57:53 +00:00
|
|
|
tag(String, Vec<HTMLFragment> ),
|
|
|
|
text(String),
|
2012-08-09 23:48:31 +00:00
|
|
|
}
|