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;
1 Answer 1
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
(from Passing pointers to member function as returned values tovoid (SENDER::*get_func())(double, double);
QObject::connect()
)
(from Differentiate between array and pointer as function parameter)constexpr std::size_t len(const T(&)[length]);
Neither of these succeeded when I tried them.
-
1\$\begingroup\$ I've no way to test it right there but I believe it also lacks
noexcept
support \$\endgroup\$papagaga– papagaga2018年11月22日 14:28:04 +00:00Commented Nov 22, 2018 at 14:28
Explore related questions
See similar questions with these tags.