PDA

View Full Version : Matching and Extracting Comments


darkname
06-26-03, 06:45
Hello everyone!!

Does anyone knows a module that parses and extracts comments of a given source code file?? (such as C, C++, Java, PL/SQL, PERL, etc)!!?

Thereīs a module in CPAN that was designed to do this (Regexp::Common::comment) but this module donīt preview all cases for example in PERL!!

Because.. there are special cases that should be contemplated such as:

@array=/1 # 2 3 4/;
print"#This is not a comment";
$size=?#array;
@array=("#", 1,2,3);

How can i do this?

There is another module Text::Balanced that should work to.. with the function extract_delimited, where the delimitators would be starting with # and ending with \n! But i canīt make this work! :((

Another issue.. is this regex that should get the comments in Perl but it doesnīt work also!!

~ m{
( \# .*? \n ) # extract a comment wich starts with # to \n
| " (?: [^"\#]* | \#. )* " # skip over "..."
| ' (?: [^'\#]* | \#. )* ' # skip over '...'
| . [^\#"']* # skip over non-comments-or-quotes
}xgs;

Can someone tell me why??

Thank you all very much!


darkname...:p

Snow_Blizzard
07-14-03, 13:47
Hello darkname
If your difficulty is: How to remove '#' from my codes, I try to help.
This script remove all '#' character off determinade script
--------------------------------------------------------------------------
#!/usr/bin/perl -w
$| = 1;

open (CONT, "file");
my @lines = <CONT>;
close (CONT);

open (CONT, "> file");
print CONT "";

my $i = 0;
foreach $line (@lines)
{
$line =~ s/#//g;
$i += 1;
print CONT $line;
}
close (CONT);
--------------------------------------------------------------------------
If need, you can use 'chop' to extract the last character like '\n'
and you probably it wants to modify '#' to '/*' and '*/' to last character,
but if you want to remove all lines with comments you can make one array to enumerate the lines and use de 'system ("sed '$Linenumberd' ")' to delete lines that they contain comments where 'd' character is a command to delete line and '$Linenumber' is a variable to indicate the line to delete. After you need to redirect the sult to fileHandle.
I wait to have helped
See you later.

Snow_Blizzard
07-14-03, 13:55
Hello darkname
If your difficulty is: How to remove '#' from my codes, I try to help.
This script remove all '#' character off determinade script
--------------------------------------------------------------------------
#!/usr/bin/perl -w
$| = 1;

open (CONT, "file");
my @lines = <CONT>;
close (CONT);

open (CONT, "> file");
print CONT "";

my $i = 0;
foreach $line (@lines)
{
$line =~ s/#//g;
$i += 1;
print CONT $line;
}
close (CONT);
--------------------------------------------------------------------------
If need, you can use 'chop' to extract the last character like '\n'
and you probably it wants to modify '#' to '/*' and '*/' to last character,
but if you want to remove all lines with comments you can make one array to enumerate the lines and use de 'system ("sed '$Linenumberd' ")' to delete lines that they contain comments where 'd' character is a command to delete line and '$Linenumber' is a variable to indicate the line to delete. After you need to redirect the sult to fileHandle.
I wait to have helped
See you later.

Snow_Blizzard
07-14-03, 14:02
Hello darkname
If your difficulty is: How to remove '#' from my codes, I try to help.
This script remove all '#' character off determinade script
-----------------------------
#!/usr/bin/perl -w
$| = 1;

open (CONT, "file");
my @lines = <CONT>;
close (CONT);

open (CONT, "> file");
print CONT "";

my $i = 0;
foreach $line (@lines)
{
$line =~ s/#//g;
$i += 1;
print CONT $line;
}
close (CONT);
-----------------------------
If need, you can use 'chop' to extract the last character like '\n'
and you probably it wants to modify '#' to '/*' and '*/' to last character,
but if you want to remove all lines with comments you can make one array to enumerate the lines and use de 'system ("sed '$Linenumberd' ")' to delete lines that they contain comments where 'd' character is a command to delete line and '$Linenumber' is a variable to indicate the line to delete. After you need to redirect the sult to fileHandle.
I wait to have helped
See you later.