Why Grammar I Learn Can’t‽

This week I get to spend an entire week off of school for Thanksgiving! (for those unfamiliar with US holidays, clicky the link above). So I plan on working on my secretly-named Pod parser (let’s call it “Project Greenpea” to make it easier to refer to). The only problem is, I don’t want to work on it. I infer that it’s because there’s some secret to P6 grammars I have yet to discover. So maybe you P6ers can help me out.

Currently, I have a module set up, called Greenpea::Parser. Then there’s the main script you call once installing it called greenpea, which loads Greenpea::Parser. Currently, the grammar in the module is exactly like this:


grammar GreenGrammar {
regex docline { ^^ \h* [<directive>|<content>] \h* $$ }
regex directive { <delimited> | <paragraph> | <abbreviated> }
regex delimited { \=begin \h+ <blocktype> }
regex paragraph { \=for \h+ <blocktype> }
 regex abbreviated { \= <blocktype> }
 regex blocktype {
 head | para | code | input | output | item | defn | nested | table | comment | END | DATA
 }
 regex content { \N*? }
}

and there is a class in the module that holds the document itself. It looks like this:

 


class Greenpea::Document {
 has $.document;

 method new($document) {
 self.bless(*, :$document);
 }

 # XXX blocks-in-blocks will be considered content.
 method parsefile() {
 my @doc = $.document.split("\n");
 my $contenttype = "ambient";
 for @doc {
 GreenGrammar.parse($_, :rule<docline>);
 say $/.keys[0];
 say "  " ~ $/<directive>.keys[0];
 say "    " ~ $/<directive><delimited><blocktype>;
 say "      " ~ $_;
 }
 }
}

The file greenpea, after processing the commandline args and importing the module, does this:


sub importformat($input) { #, $module = 'Manpage', $output?) {
 my $file = slurp($input);
 my Greenpea::Document $document .= new($file);
 say "Parsing file...";
 $document.parsefile;
 say "That's all, folks!";
}

Here’s the contents of a quickly created test.pod (which will be removed as soon as things work):


=begin para
This is a bunch of text
=end
=begin Text
This is another bunch of text
but with a newline!
=end

Finally, here’s what happens when I run greenpea test.pod:


$ supernova test.pod
Parsing file...
directive
delimited
para
=begin para
content
0
Any()
This is a bunch of text
content
0
Any()
=end
content
0
Any()
=begin Text
content
0
Any()
This is another bunch of text
content
0
Any()
but with a newline!
content
0
Any()
=end
content
0
Any()

That's all, folks!

the problem here, of course, is that it doesn’t recognize the second =begin as a directive. Can anyone help me here?

Ah well, ’tis a parable of the anglo-saxons*. Once this is fixed, I’ll start blazing through this.

*This is a random phrase I came up with one day drawing comics. I felt it was appropriate here, for no apparent reason.

This entry was posted in Crisis Chamber and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s