I would like to do some re-factoring on assembly files that are compatible with C/C++ comments and preprocessor directives.
Unfortunately I cannot use refactoring tool such as Astyle. I have to manually parse my file.
My refactoring algorithm iterates on each line of a file as shown below:
while(<FH>)
{
next if isComment($_);
$count += s/$search/$replace/; # A refactoring rule
$count += arithmetic($_); # R1=R2+3*4; --> r1 = r2 + 3 * 4;
...
$out .= $_;
}
if($count)
{
open my $fh ">$filename";
print $fh $out;
close $fh;
}
With this method I cannot accurately detect a comment line. So I implemented counter that count on each /*
and decrease on every */
. If the counter is bigger than 0, I ignore that line.
Unfortunately this method won't work in this case:
/* /* <-- Not allowed */ /* */
The counter will be equal to 1 while it should be equal to 0.
So I am looking to an accurate way to detect comment blocks and ignore them. Is there any package or module that can help me?