// We do not use /*-style comments #### // We don't use old C-style comments #### /"([^"]|\\­")*"/ #### #!/usr/bin/perl -w use strict; $|= 1; # Useful for ad-hoc testing my $canNest= 1; # Whether /*-style comments can be nested my $depth= 0; my $output= ""; while( ) { while( ! m[\G\z]gc ) { while( $depth && m[/[*]|[*]/]gc ) { if( "/" eq substr( $_, $-[0], 1 ) ) { $depth++; } elsif( $canNest ) { $depth--; } else { $depth= 0; } } last if $depth; if( m[ \G (?: [^'"/]+ | ' (?: [^'\\]+ | \\. )* ' | " (?: [^"\\]+ | \\. )* " | /(?![/*]) )+ ]xgc ) { $output .= substr( $_, $-[0], $+[0] - $-[0] ); } elsif( m[\G//.*]gc ) { # skip C++ comments } elsif( m[\G/[*]]gc ) { $depth++; } elsif( m[\G['"]]gc ) { warn "Ignoring unclosed quote: $_"; } else { die $_, ' ' x pos($_), "^\nCouldn't be parsed"; } } print $output; $output= ""; } warn "$depth unclosed /*-comments\n" if $depth; __END__ #include "StdAfx.h" // Tail comment #include "Utility\perftime.h" #pragma hdrstop /* Comment before MACRO */ /* Comment /* and nested comment */ lines */ #define MACRO 10\ + 3 // Multi line macro with comment #define __DEBUG /* comment */ 1 #define STRING 'This is a string' /* comment */ #define BACKSLASH '\\' #define COMMENT "/* comment in \"a\" string */" // c++ comment line /* Comment at start for a number of lines */ /* multi-line comment /* nested */ block */ // cpp block char PerfTimer::Buf[64]; // Don't use contractions // /*-style comment below over multiple lines: test/*ing how newlines work when a comment spans lines, does it st*/ing? total/*divide*//count//*comment #### #include "StdAfx.h" #include "Utility\perftime.h" #pragma hdrstop #define MACRO 10\ + 3 #define __DEBUG 1 #define STRING 'This is a string' #define BACKSLASH '\\' #define COMMENT "/* comment in \"a\" string */" char PerfTimer::Buf[64]; testing? total/count