The first thing you need to do is define what you want to look for. I'm assuming you don't want a 'perfect' C++ parser (that would actually be incredibly difficult). So you just want to parse common/simple syntax, right?
So, ask yourself... "what constitutes a method?"
A basic C++ method has the following parts:
|
type methodname ( type param, type param ) ;
|
With the following caveats:
1) The whitespace before and after the parenthesis is optional
2) The semicolon will only exist for prototypes
3) The 'param' text is optional
4) The number of parameters is variable (ie, there might be nothing at all inside those parenthesis)
So maybe even this is too complex. Maybe you just want to look for this:
With this simplistic approach, the only thing you have to look for is the type, the method name, and an opening parenthesis (with optional whitespace before it). That'll do a decent job of catching most methods.
The
really tricky part then.... is how do you recognize 'type'? Types may or may not contain whitespace... may or may not contain weird characters... and might even be custom types generated by the user.
ie:
1 2 3 4 5 6 7 8 9 10 11 12
|
int func(); // <- valid.. easy to recognize
MyClass func(); // <- possibly valid if user previously declared a class/struct
// or type named 'MyClass'
std::string func(); // <- also valid.. trickier to recognize because of :: operator
std :: string func(); // <- also valid... much trickier to recognize
map< int, float > func(); // <- also valid!
const map< std :: string ,MyClass*>& func(); // <- head explodey... but valid!
|
So this problem can get quite complex.
Maybe you dont' want to bother recognizing all of these, and just want to recognize basic method definitions. If that's the case, you'll have to define your own rules for what will and won't be recognized as a method.
Then once you have the rules down... write your code to parse the text using those rules as a guide.