Created
April 22, 2009 15:17
-
-
Save masak/99851 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat hitomi-example.xml | |
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml" | |
xmlns:pe="http://github.com/masak/hitomi"> | |
<head> | |
<title>Slurp: News</title> | |
</head> | |
<body class="index"> | |
<div id="header"> | |
<h1>News</h1> | |
</div> | |
<ol pe:if="links"> | |
<li pe:for="links.reverse -> $link"> | |
<a href="${$link.url}">${$link.title}</a> | |
posted by ${$link.username} at ${strftime('%x %X', $link.time)} | |
</li> | |
</ol> | |
<p><a class="action" href="/submit/">Submit new link</a></p> | |
<div id="footer"> | |
<hr /> | |
<p class="legalese">© 2009 The Web.pm authors, Artistic License 2.0</p> | |
</div> | |
</body> | |
</html> | |
$ cat hitomi | |
use v6; | |
grammar XML { | |
regex TOP { ^ <pi>* [ <opening><xmlcontent>+<closing> | <empty> ] $ }; | |
rule xmlcontent { | |
| <opening> <xmlcontent>+ <closing> | |
| <empty> | |
| <content> | |
}; | |
rule pi { '<!' <.ident> <.ident> '>' }; | |
rule opening { '<' <ident> <attrs>? '>' }; | |
rule closing { '</' <ident> '>' }; | |
rule empty { '<' <ident> <attrs>? '/' '>' }; | |
rule attrs { <attr>+ } | |
rule attr { <.ident>[':'<.ident>]? '=' '"' <-["]>+ '"' } | |
token ident { <+alnum + [\-]>+ } | |
regex content { <-[<]>+ } | |
}; | |
my $xml = $*IN.slurp; | |
interp(XML.parse($xml)); | |
sub interp(Match $m) { | |
for %($m).keys -> $key { | |
if $key eq 'opening'|'closing'|'empty'|'content' { | |
print $m{$key}.text; | |
} | |
elsif $key eq 'xmlcontent' { | |
for $m{$key}.values -> $chunk { | |
if $chunk<opening><attrs> | |
&& ~($chunk<opening><attrs>).index('pe:') { # problem here | |
say "omitted", $m{$key}<opening><ident>; | |
} | |
else { | |
interp($chunk); | |
} | |
} | |
} | |
else { | |
given $m{$key} { | |
when Match { | |
interp($m{$key}); | |
} | |
when List { | |
for $m{$key}.values -> $element { | |
interp($element); | |
} | |
} | |
} | |
} | |
} | |
} | |
$ perl6 hitomi < hitomi-example.xml | |
<html xmlns="http://www.w3.org/1999/xhtml" | |
xmlns:pe="http://github.com/masak/hitomi"> | |
<head> | |
<title>Method 'postcircumfix:{ }' not found for invocant of class 'Failure' | |
current instr.: 'postcircumfix:{ }' pc 5649 (src/classes/Associative.pir:133) | |
called from Sub '_block97' pc 8465 (EVAL_23:3169) | |
called from Sub '_block84' pc 8335 (EVAL_23:3128) | |
called from Sub '_block53' pc 7477 (EVAL_23:2821) | |
called from Sub 'interp' pc 7181 (EVAL_23:2722) | |
called from Sub '_block135' pc 8661 (EVAL_23:3239) | |
called from Sub '_block97' pc 8560 (EVAL_23:3210) | |
called from Sub '_block84' pc 8335 (EVAL_23:3128) | |
called from Sub '_block53' pc 7477 (EVAL_23:2821) | |
called from Sub 'interp' pc 7181 (EVAL_23:2722) | |
called from Sub '_block135' pc 8661 (EVAL_23:3239) | |
called from Sub '_block97' pc 8560 (EVAL_23:3210) | |
called from Sub '_block84' pc 8335 (EVAL_23:3128) | |
called from Sub '_block53' pc 7477 (EVAL_23:2821) | |
called from Sub 'interp' pc 7181 (EVAL_23:2722) | |
called from Sub '_block14' pc 118 (EVAL_23:57) | |
called from Sub '!UNIT_START' pc 19469 (src/builtins/guts.pir:380) | |
called from Sub 'parrot;PCT;HLLCompiler;eval' pc 950 (src/PCT/HLLCompiler.pir:529) | |
called from Sub 'parrot;PCT;HLLCompiler;evalfiles' pc 1275 (src/PCT/HLLCompiler.pir:690) | |
called from Sub 'parrot;PCT;HLLCompiler;command_line' pc 1470 (src/PCT/HLLCompiler.pir:791) | |
called from Sub 'parrot;Perl6;Compiler;main' pc 24292 (perl6.pir:164) | |
$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment