0

I need to find method bodies in iOS source code. I've started from writing simple regex: \{.*\} but things goes complex when I need to handle nested parentheses.

So I started to think from another direction. I tried to detect method declaration with something like this: [+-].+ \{, so I can find end of method with next pattern: \}\s+[+-], but here's bad case for it:

- (void)method:(id)arg0 :(id)arg1 third:(id)arg3 {
...
}

#pragma mark - Bla-bla

- method2:(id)arg0 {
   code(
   {
   });
   if (1) {
   }
   -1;
}

Can you have any ideas or algorithms or regex to detect method bodies correctly?

Ossir
  • 3,109
  • 1
  • 34
  • 52
  • possible duplicate of [Using RegEx to balance match parenthesis](http://stackoverflow.com/questions/7898310/using-regex-to-balance-match-parenthesis) – HamZa Apr 12 '14 at 18:32
  • @HamZa So how can I implement balanced group on obj-c? – Ossir Apr 12 '14 at 19:18
  • I'm now hesitating. I read it as C#. So I'm not sure if it's possible in objective-c. I will remove my closevote... – HamZa Apr 12 '14 at 19:20
  • 1
    http://stackoverflow.com/questions/3741279/how-do-you-remove-parentheses-words-within-a-string-using-nsregularexpression It was said there, that there is no matching nested parenthesis in obj-c regex – Ossir Apr 12 '14 at 19:51
  • 1
    It seems to me that you need a parser - that seems to be the only sure way to deal with this. The moment you have a string constant with a `}` or such in it, code that relies on just regular expressions is likely to break down - unless you make it "clever enough" to understand the entire context. At which point you have a parser... – Floris Apr 13 '14 at 03:43
  • @Floris I think I have to learn how to use clang tools. From what I know about it, it should resolve my problem – Ossir Apr 13 '14 at 07:31

0 Answers0