3
\$\begingroup\$

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       { [ <attr> \s+ ]? <type> [ \s* <type_mod> ]? 
                        (\s+) <fname> (\s*) "("
                        (\s*) [<parameter> [ "," (\s*) <parameter> ]* ]? (\s*) 
                        ')'(\s*) ';'  
                    }

    token name      { \w+ }
    token namespace { [ "::" ]? [ <name> "::" ]* }
    token attr      { <name>  }
    token type      {  <namespace>? <name> }
    token type_mod  { [ \*|\& ]+ }
    token fname     { <name> }
    token variable  { <name> }
    token parameter { <type> [\s* <type_mod> ]? \s+ <variable> }
}

my $str = "const ::one::std::string ** ma1n( int&& i, two::std::string va1e_ );";
my $parsed = FUNCTION.parse($str);
say $parsed;
\$\endgroup\$
4
\$\begingroup\$

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

Neither of these succeeded when I tried them.

\$\endgroup\$
  • 1
    \$\begingroup\$ I've no way to test it right there but I believe it also lacks noexcept support \$\endgroup\$ – papagaga Nov 22 '18 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.