On Thu, Feb 03, 2005 at 10:39:21AM -0000, Keith Watson wrote:
I have a stack of JavaScript files from which I want to extract the function definition lines and the following comment block for documentation purposes,
e.g. in the source files functions are defined thusly;
function thisIsAfunction (firstArg, secondArg, thirdArg) { /*
- a description of the purpose of the function, what
- argument values are expected and some examples
- of use.
*/ [then the body of the code for the function, which may contain further comment blocks that I want to ignore] }
Question is can I do this with the ubiquitous egrep? or will I need to use something like perl or awk?
Any ideas or suggestions? Anyone know of something that already does this?
And before someone asks, yes I have tried google and found one possibility, an awk script that will extract _all_ the comments but not the function definitions [eXtract Source Code Comment http://members.tripod.com/vgoenka/unixscripts/xscc.html]. Most hits were from people who wanted to remove comments to either just compress the source or count lines of code.
I'd probably go the perl route, but that's because I can't write awk ;) If I get a chance later on tonight, I'll throw something out that should work.
I assume that the comments are always straight after the function definition? I'd have expected it to be /** at the top of the comment for Java, but I suppose as this is javascript I'm not suprised that it's not. It should be relatively simple... parse the file for the function keyword at the beginning of a line, then select everything up to the first occurance of */, should be relatively simple.
Cheers,