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 (\n)\h+ # Spaces at start of lines removed
87 \h+(\n) # Spaces at end of lines removed
91 \h+ # Other horizontal spaces replaced with one
95 ( # Grouping for the replacement
102 print STDERR "DEBUG: '$&' => '$1'\n" if defined $1;
103 print STDERR "DEBUG: '$&' => ' '\n" unless defined $1;
105 defined $1 ? $1 : " "
107 } elsif ($lang eq 'S') {
108 # Because we use C++ style comments in our .S files, all we can do
111 ^([^\n]*?)//[^\n]*?$ # Any line with a // comment
114 print STDERR "DEBUG: '$&' => '$1'\n" if defined $1;
115 print STDERR "DEBUG: '$&' removed\n" unless defined $1;
120 # Drop all empty lines
122 (^|\n)(?:\s*(?:\n|$))+ # Empty lines, preserve one newline
124 } elsif ($lang eq 'perl') {
125 # Merge continuation lines
128 # Regexp for things that should be preserved
132 <<["']?(\w+)["']? # HERE document start
138 " # Double quoted string start
139 (?: \\. | [^\"])* # Any character, including escaped ones
140 " # Double quoted string end
146 ' # Single quoted string start
147 [^\']* # Any character
148 ' # Single quoted string end
152 # Remove comments while preserving strings
154 (?| # All things preserved end up in $1
156 \#.*?(\n|$) # Perl comments
160 ( # Grouping for the replacement
167 print STDERR "DEBUG: '$&' => '$1'\n" if defined $1;
168 print STDERR "DEBUG: '$&' removed\n" unless defined $1;
175 (?| # All things preserved end up in $1
177 (^|\n)(?:\s*(?:\n|$))+ # Empty lines, preserve one newline
181 ( # Grouping for the replacement