Characters: '"'
Comments: /* " */
C++ comments: // "
####
my $parser= qr{
\G # Don't skip anything
(?:
[^'"/]+ # Stuff we don't care about
| '(?:[^\\']+|\\.)' # '"', '\'', '\\', 'param'
| /\* .*? \*/ # A C comment
| //[^\n]+ # A C++ comment
| / # /, not a comment, division
| "((?:[^\\"]+|\\.)*)" # A quoted string ($1)
| (.) # An error ($2)
)
}xs;
my $code= do { local($/); };
my @strings;
while( $code =~ m/$parser/g ) {
if( defined $1 ) {
push @strings, $1;
} elsif( defined $2 ) {
my $char= $2;
my $pos= pos($code)-5;
$pos= 0 if $pos < 0;
my $context= substr( $code, $pos, 10 );
warn "Ignoring unexpected character ($char) in ($context)";
}
}
####
#define ctrl(char) ( 'char' & 31 )
####
#!/usr/bin/perl -p0777 -i.org
my $parser;
BEGIN {
$parser= qr{
\G # Don't skip anything
(
[^'"/]+ # Stuff we don't care about
| '(?:[^\\']+|\\.)' # '"', '\'', '\\', 'param'
| /\* .*? \*/ # A C comment
| //[^\n]+ # A C++ comment
| / # /, not a comment, division
| "((?:[^\\"]+|\\.)*)" # A quoted string ($2)
| (.) # An error ($3)
) # Entire match ($1)
}xs;
}
s{$parser}{
if( defined $3 ) {
my $char= $2;
my $pos= pos($code)-5;
$pos= 0 if $pos < 0;
my $context= substr( $code, $pos, 10 );
warn "Ignoring unexpected character ($char) in ($context)";
}
if( defined $2 ) {
my $string= $2;
#... manipulate $string ...
$string;
} else {
$1;
}
}g;
####
use strict;
use warnings;
BEGIN { @ARGV= map glob($_), @ARGV if "MSWin32" eq $^O }
my $parser= qr{
\G # Don't skip anything
(?:
[^'"/]+ # Stuff we don't care about
| '(?:[^\\']+|\\.)' # '"', '\'', '\\', 'param'
| /\* .*? \*/ # A C comment
| //[^\n]+ # A C++ comment
| / # /, not a comment, division
| "((?:[^\\"]+|\\.)*)" # A quoted string ($1)
| (.) # An error ($2)
)
}xs;
for my $file ( @ARGV ) {
print "$file:\n";
# Note: Dangerous use of <> (until Perl gets fixed)
my $code= do { local(*ARGV,$/); @ARGV= $file; <> };
my @strings;
while( $code =~ m/$parser/g ) {
if( defined $1 ) {
push @strings, $1;
} elsif( defined $2 ) {
my $char= $2;
my $pos= pos($code)-5;
$pos= 0 if $pos < 0;
my $context= substr( $code, $pos, 10 );
warn "Ignoring unexpected character ($char) in ($context)";
}
}
for my $i ( 0 .. $#strings ) {
printf qq[%8d: "%s"\n], 1+$i, $strings[$i];
}
}