3 # C source compressor. This:
5 # - merges continuation lines
6 # - removes comments (not in strings)
7 # - removes empty lines (not in strings)
12 my $debug = defined $ENV{DEBUG};
13 my $lang = shift @ARGV;
20 # Merge continuation lines
23 # Regexp for things that should be preserved
28 (?: \\. | [^\"])* # Any character, including escaped ones
35 ' # Character start (multi-chars supported)
36 (?: \\. | [^\'])+ # Any character, including escaped ones
41 # Remove comments while preserving strings
43 (?| # All things preserved end up in $1
46 .*? # Contents up until
51 ( # Grouping for the replacement
58 print STDERR "DEBUG: '$&' => '$1'\n" if defined $1;
59 print STDERR "DEBUG: '$&' removed\n" unless defined $1;
66 (?| # All things preserved end up in $1
68 (^|\n)(?:\s*(?:\n|$))+ # Empty lines, preserve one newline
72 ( # Grouping for the replacement
81 (?| # All things preserved end up in $1
83 \h+ # Horizontal spaces replaced with one
87 ( # Grouping for the replacement
94 print STDERR "DEBUG: '$&' => '$1'\n" if defined $1;
95 print STDERR "DEBUG: '$&' => ' '\n" unless defined $1;
100 # Clean up spaces at start and end of lines
103 } elsif ($lang eq 'S') {
104 # Because we use C++ style comments in our .S files, all we can do
107 ^([^\n]*?)//[^\n]*?$ # Any line with a // comment
110 print STDERR "DEBUG: '$&' => '$1'\n" if defined $1;
111 print STDERR "DEBUG: '$&' removed\n" unless defined $1;
116 # Drop all empty lines
118 (^|\n)(?:\s*(?:\n|$))+ # Empty lines, preserve one newline
120 } elsif ($lang eq 'perl') {
121 # Merge continuation lines
124 # Regexp for things that should be preserved
128 <<["']?(\w+)["']? # HERE document start
134 " # Double quoted string start
135 (?: \\. | [^\"])* # Any character, including escaped ones
136 " # Double quoted string end
142 ' # Single quoted string start
143 [^\']* # Any character
144 ' # Single quoted string end
148 # Remove comments while preserving strings
150 (?| # All things preserved end up in $1
152 \#.*?(\n|$) # Perl comments
156 ( # Grouping for the replacement
163 print STDERR "DEBUG: '$&' => '$1'\n" if defined $1;
164 print STDERR "DEBUG: '$&' removed\n" unless defined $1;
171 (?| # All things preserved end up in $1
173 (^|\n)(?:\s*(?:\n|$))+ # Empty lines, preserve one newline
177 ( # Grouping for the replacement