I'm writing a plugin for sublime text editor (using Python) that will fold only functions/methods on file load. In order to do this I have to parse file contents, detect function blocks and get start and end position of each block (this is needed for API to do folding). Regex was the first thing that came to my mind but I learned quickly that it will not work for nested functions, especially for c style syntax.
The way I'm doing this currently is going line by line looking for '{' and '}' and checking if function definition is on the same line or above '{'. If can't find function definition I assume it's a control structure. This is far from perfect since unmatched brace symbols inside comments will cause this logic to fail. This is probably wrong way of doing this altogether by I don't know of any other option. Any ideas?