in reply to Rec::Descent Woes - Parsing JavaScript Files and Other Issues
I'm not sure how to help you with your Parse::RecDescent problem (I've never used it), but here is a pure-regex solution that I whipped up (half to help, half for fun) for your original problem:
my $data = join('',<DATA>); my $quoted_string = qr/ " (?: (?: [^"]* ) | (?: (?<= \\ ) " ) )* " /x; my $balenced_brackets = qr/ \{ (?: (?> [^{}"] *) | (??{$quoted_string}) | (??{$balenced_brackets}) )* \} /x; my $function_header = qr/ function \s+ \w+ \s* \( [^)]* \) \s* /x; my @matches = $data =~ /($function_header $balenced_brackets)/gx; print join("\n\n",@matches);
It worked with the sample code that you provided, but I have a feeling it will blow up when presented when it encounters something whacky (an unclosed brace, a brace inside quotes, etc). Consider it a preliminary solution :)
Update I added a bit of robustness to consider the case of the unclosed brace, but it now needs re 'eval'. I also added a bit of explanation... Its still going to blow up on a brace inside quotes, however...
Update: Update: With some better structure, it can now handle brackets in quotes with no problem.
|
|---|