Greetings honoured monks,
I'm working on a regex that returns me a list of function names of a c++ file.
(Returns a array of strings or a string )
But there are so many possibilities of function definitions.
E.g. :
are ok :
void CLASS1::Function0 (void){ } const void CLASS1::Function1 (void) { } const CLASS2::Type CLASS1::Function3 (void){ } std::vector<std::string>& CLASS1::Function4 (void){ } const CLASS2::Type<Type2>* CLASS1::Function5 (void){ }
are not ok :
const std::string Class1::Member1 = ""; foo = Class2::function(FOO,BAA);
etc
All functions (that are ok) should be in the array.
To simplify it, I have set the function parameters to void.
Does there a module exists for parsing c++ source files?
Is there a better way to get the function names without a regex?
my code so far :
#!/usr/local/bin/perl -w use strict; use warnings; while (<DATA>) { chomp(); my $reg=""; $reg.="^[a-zA-Z0-9]+"; $reg.="\\s*"; $reg.="[^:]*::\\s*"; $reg.="([^ ]*\\s)?"; $reg.="([^(]*)"; $reg.="\\([^)]*\\)[{]?\$"; print "$2\n" if (/$reg/); } __DATA__ void CLASS1::Function0 (void){ } const void CLASS1::Function1 (void) { } const CLASS2::Type CLASS1::Function3 (void){ } std::vector<std::string>& CLASS1::Function4 (void){ } const CLASS2::Type<Type2>* CLASS1::Function5 (void){ } const std::string Class1::Member1 = "" " more data "; std::vector<std::string>& CLASS1::Function4 (void){ foo = Class2::function(FOO,BAA); }
I hope the great spirit of monk wisdom can help a unworthy little apprentice like me.

In reply to need regex to parse c++ functions by Chief of Chaos

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.