Parsing a C++ function declaration with Perl 6 grammar I wrote a Perl 6 grammar to parse a C++ function. My final goal would be to parse an entire header. The aim is not to correct C++ syntax errors, but to parse valid C++.

Do you have some advice or improvements?

#!/usr/bin/env perl6

grammar FUNCTION {
    token TOP       { [ &lt;attr&gt; \s+ ]? &lt;type&gt; [ \s* &lt;type_mod&gt; ]? 
                        (\s+) &lt;fname&gt; (\s*) "("
                        (\s*) [&lt;parameter&gt; [ "," (\s*) &lt;parameter&gt; ]* ]? (\s*) 
                        ')'(\s*) ';'  
                    }

    token name      { \w+ }
    token namespace { [ "::" ]? [ &lt;name&gt; "::" ]* }
    token attr      { &lt;name&gt;  }
    token type      {  &lt;namespace&gt;? &lt;name&gt; }
    token type_mod  { [ \*|\&amp; ]+ }
    token fname     { &lt;name&gt; }
    token variable  { &lt;name&gt; }
    token parameter { &lt;type&gt; [\s* &lt;type_mod&gt; ]? \s+ &lt;variable&gt; }
}

my $str = "const ::one::std::string ** ma1n( int&amp;&amp; i, two::std::string va1e_ );";
my $parsed = FUNCTION.parse($str);
say $parsed;
I'd expect to see many more tests of any program that addresses a problem as gnarly as parsing a C++ declaration.

Choosing a couple I've recently had cause to write (on Stack Overflow), I would immediately add




void (SENDER::*get_func())(double, double);


(from Passing pointers to member function as returned values to QObject::connect())
constexpr std::size_t len(const T(&amp;)[length]);


(from Differentiate between array and pointer as function parameter)


Neither of these succeeded when I tried them.