From d704fc9196c9c80e1c29a04ef02d1165ad2cd760 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Thu, 9 Aug 2012 16:48:31 -0700 Subject: [PATCH] An attempt at a macro to support HTML literals --- src/test/run-pass/html-literals.rs | 68 ++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/test/run-pass/html-literals.rs diff --git a/src/test/run-pass/html-literals.rs b/src/test/run-pass/html-literals.rs new file mode 100644 index 00000000000..2d6e4ec8c92 --- /dev/null +++ b/src/test/run-pass/html-literals.rs @@ -0,0 +1,68 @@ +// A test of the macro system. Can we do HTML literals? + +// xfail-pretty +// xfail-test + +macro_rules! html { + { $($body:tt)* } => { + let builder = HTMLBuilder(); + build_html!{builder := $($body)*}; + builder.getDoc() + } +} + +macro_rules! build_html { + { $builder:expr := $($rest:tt)* } => { + $builder.endTag(stringify!($tag)); + build_html!{ $builder := $($rest)* }; + }; + + { $builder:expr := <$tag:ident> $($rest:tt)* } => { + $builder.beginTag(stringify!($tag)); + build_html!{ $builder := $($rest)* }; + }; + + { $builder:expr := . $($rest:tt)* } => { + $builder.addText(~"."); + build_html!{ $builder := $($rest)* }; + }; + + { $builder:expr := $word:ident $($rest:tt)* } => { + $builder.addText(stringify!($word)); + build_html!{ $builder := $($rest)* }; + }; + + { $builder:expr := } => { } +} + +fn main() { + + let page = html! { + + This is the title. + +

This is some text

+ + + }; + + // When we can do this, we are successful: + // + //let page = tag(~"html", ~[tag(~"head", ~[...])]) + +} + +enum HTMLFragment { +} + +struct HTMLBuilder { + bar: (); + fn getDoc() -> HTMLFragment { fail } + fn beginTag(tag: ~str) { } + fn endTag(tag: ~str) { } + fn addText(test: ~str) { } +} + +fn HTMLBuilder() -> HTMLBuilder { + HTMLBuilder { bar: () } +} \ No newline at end of file