Scripting Advice Needed
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. Regards, Keith ____________ Never annoy Dragons. For thou art crunchy and taste good with ketchup. - D&D saying
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, -- Brett Parker
Brett wrote:
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.
Let's use sed, the stream editor! sed -n -e '/^function /,/^ *\*\//{;/^function /d;p;}' sourcefile.js Ow. What happened? Check this in the manual... The -n means "no printing unless told" and -e says the next string is the program. In the program, the first two regular expressions with a , between them selects ranges of lines, and the bit between the {}s is run only for them. That deletes the function line, but prints any other one. There's probably a simpler way to do it. -- Hope that helps, MJR/slef http://mjr.towers.org.uk/
On Thu, Feb 03, 2005 at 06:10:01PM +0000, MJ Ray wrote:
Brett wrote:
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.
Let's use sed, the stream editor!
sed -n -e '/^function /,/^ *\*\//{;/^function /d;p;}' sourcefile.js
Mark, some times you remind me of just how useful you are to have around, I like it, very simple, very elegant solution. /me tips his hat at MJR -- Brett Parker
On Thu, 3 Feb 2005 10:39:21 -0000 "Keith Watson" <keith.watson@kewill.com> 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.
Regards,
Keith
Yes I would use Doxygen for C/C++ and it says it supports Java but most Java programmers prefure Javadoc. Does anyone no anything better than Doxygen for C? Regards Owen
participants (4)
-
Brett Parker -
Keith Watson -
MJ Ray -
Owen Synge