I'm trying to parse Obj-c source code with regex. I want to find both declarations and implementations.
First I look for classes like this:
@implementation(.|\n)+@end
@interface(.|\n)+@end
Then I have these patterns for finding instance-methods inside the ranges of the classes:
For interface: -.*;
For implementation: -.*{
However things from: comments, strings and math operations are also included.
Examples where these patterns fail:
//I'm pretending to-be an instance method;
/*
Disabled methods:
- (void)myProgrammerDidntLikeMe;
*/
if (a + b == 2) { ... }
str = @"-----";
How can I make the patterns exclude these pretending-to-be methods, and is there something else I have not foreseen?
Update: When experimenting with parsing a single a method-string I noticed my pattern also worked for identifying them. This is what I came up with:
(-|\+)\s\(([\w|\*|\s]*)\)(?:(?:(?:(\w*)(?:\:\([\w|\s|\*]*)\)(\w*)\s*){1,}))?(\w*)
However it does not, unlike my first attempt find methods without a return type. But I'm okay with that since I have never ever seen one being used.
- noReturnType
I doesn't know anything about comments and ifs, but 1) it's harder too fool with for instance math operators and 2) It also parses the method itself.
Now I'm mutating my question a bit, but I'm trying to achieve capture-group-output like this, which I don't know how to.
1. -
2. void
FOLLOWING_CAN_REPEAT
3. setFoo:
4. Foo*
5. foo
END_REPEAT