My first Perl6 regexp grammar in Perl

jozef on 2010-01-21T17:37:53

Once I told potyl - "hey let's have a wiki it will be easy to use for everyone". He wasn't so excited, not at all. Why? What is so bad about wiki? Look at this table of 130+ wiki syntaxes. Anyone still complaining that there are too many simmilar choices on CPAN? The wiki community decided to solve the problem by creating yet another wiki syntax...

What this has to do with Perl6 regexp grammars? After Damian talk at YAPC::EU::2009 I really wanted to try out the Regexp::Grammars. Finally I found some time during the Christmas and here is the result:

use Regexp::Grammars;
use re 'eval';
my $parser = qr@
<Wiki>

<rule: Wiki> <[Block]>*
<rule: Block> <.EmptyLine> | <Code> | <Para>
<token: Para> <Heading> | <List> | <TextLines>
<token: EmptyLine> ^ \h* \R
<token: TextLines> (?:^ (?! <Code> | <Heading> | <List> | <EmptyLine> ) [^\h] .+? \v)+
<token: CodeStart> ^ {{{ \h* \v
<token: CodeEnd> ^ }}} \h* \v
<token: Code> <.CodeStart> <CodeLines> <.CodeEnd>
<token: CodeLines> .+?
<token: Heading> <HeadingStart> \s <HeadingText> \s =+ \h* \v
<token: HeadingStart> ^=+
<token: HeadingText> [^=\v]+
<token: List> <[ListItem]>+
<token: ListItem> ^ <ListItemSpaces> <ListItemType> \h+ <ListItemText> \v
<token: ListItemSpaces> \h+
<token: ListItemType> (\*|\d+\.|a\.|i\.)
<token: ListItemText> .+?
@xms;

It is probably not the best piece of regexp gramar, Perl6 experts will for sure spot some error, but hey it works! "Works on my computer™". I've used it to transform TracWiki syntax to XHTML div and then using XSL to DokuWiki syntax. Here are the scripts that does it completely.