Perl 6 Grammars – not only for parsing

Yesterday on #perl6 edenc, (a Catalyst dev), together with ruoso started discussing an idea of dispatching http requests with Perl 6 grammars. My nose smelled some awesomeness being baked around, so I decided to work something out too.

And that’s it. A grammar with paths (being regexes), methods-controllers building responses, in this case only plaintext ones, but that could have been objects as well.

grammar Mysite {
    token TOP {
        <main>    |
        <about>   |
        <contact> |
        <cookie>  |
        <api>     |
        <e404>
    }
    token main    { ^ '/' $ }
    token about   { ^ '/about' $ }
    token contact { ^ '/about/contact' $ }
    token cookie  { ^ '/cookie' $ }
    token api     { ^ '/api/' $<type>=[ \S ]+ $ }
    token e404    { }
}

class Mysite::Actions {
    method TOP($/) {
        make $/.values[0].ast;
    }
    method main($/)    { make "Hello from the main page!" }
    method about($/)   { make "About me" }
    method contact($/) { make "Reach me at 555 01 23" }
    method cookie($/)  { make $*REQUEST.cookie }
    method api($/)     {
        given $<type>.Str {
            when 'json' { make '{"foo":"bar"}' }
            when 'xml'  { make '<foo>bar</foo>' }
            default     { make 'Wrong serialization method passed' }
        }
    }
    method e404($/)    { make "404: Not found" }
}

class Request is Cool {
    has $.path;
    has $.cookie;
    method Str {
        $.path
    }
}

my ($*REQUEST, $req, $res);
$*REQUEST = $req = Request.new(path => '/cookie', cookie => 'monster');
$res = Mysite.parse($req, :actions(Mysite::Actions.new));
say $res.ast; # monster

$*REQUEST = $req = Request.new(path => '/nope');
$res = Mysite.parse($req, :actions(Mysite::Actions.new));
say $res.ast; # 404: Not found

Few interesting things:

  • method TOP is just picking out the right result from the matched token
  • token e404 is empty so it matches everything that wasn’t alredy matched
  • Request is a class acting as a String, so it can store things like cookies, POST etc. aside from the query string
  • $*REQUEST is a contextual variable (no worries, it’s not global although it may look like one) so we can extract the Request fields from inside the action methods

And there it is, shiny and pretty. Look out for some future posts, once I get more $time I’ll probably code some reusable components and present them here as well.


8 Comments on “Perl 6 Grammars – not only for parsing”

  1. Chris Dolan says:

    Cool, but I admit to be a little disappointed because the headline made me expect that you wrote a parser for the HTTP protocol messages instead of just dispatching the paths. Don’t get me wrong, though: I think what you did is novel and interesting.

    • ttjjss says:

      Well volunteered! :)
      But seriously, sounds like a nice weekend project. I’ll just have to see whether it’s alredy implemented in Web.pm or somewhere.

  2. acidcycles says:

    This is neat. Where can we read more about contextual variables? They look a bit like they may be similar to dynamicly scoped variables?

  3. DarkoP says:

    What is make()? No luck with google.

  4. […] may remember the idea of dispatching HTTP requests with Perl 6 grammars. Turned out to be funny and interesting, […]


Leave a reply to Chris Dolan Cancel reply