blob: ddf1d1ec6fb0a4e5ee10e5a6e4a664dc58e0f713 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#! /usr/bin/env perl
#
# C source compressor. This:
#
# - merges continuation lines
# - removes comments (not in strings)
# - removes empty lines (not in strings)
use strict;
use warnings;
my $debug = defined $ENV{DEBUG};
my $lang = shift @ARGV;
# Slurp the file
$/ = undef;
$_ = <>;
if ($lang eq 'C') {
# Merge continuation lines
s{\\\n}{}g;
# Regexp for things that should be preserved
my $preserved =
qr{
(?:
" # String start
(?: \\. | [^\"])* # Any character, including escaped ones
" # String end
)
| # OR
(?:
' # Character start (multi-chars supported)
(?: \\. | [^\'])+ # Any character, including escaped ones
' # String end
)
}x;
# Remove comments while preserving strings
s{
(?| # All things preserved end up in $1
/\* # C comment start
.*? # Contents up until
\*/ # C comment end
| # OR
( # Grouping for the replacement
$preserved
)
)
}{
if ($debug) {
print STDERR "DEBUG: '$&' => '$1'\n" if defined $1;
print STDERR "DEBUG: '$&' removed\n" unless defined $1;
}
defined $1 ? $1 : ""
}gsxe;
# Remove empty lines
s{
(?| # All things preserved end up in $1
(^|\n)(?:\s*(?:\n|$))+ # Empty lines, preserve one newline
| # OR
( # Grouping for the replacement
$preserved
)
)
}{$1}gsx;
# Remove extra spaces
s{
(?| # All things preserved end up in $1
\h+ # Horizontal spaces replaced with one
| # OR
( # Grouping for the replacement
$preserved
)
)
}{
if ($debug) {
print STDERR "DEBUG: '$&' => '$1'\n" if defined $1;
print STDERR "DEBUG: '$&' => ' '\n" unless defined $1;
}
defined $1 ? $1 : " "
}gsxe;
# Clean up spaces at start and end of lines
s/^ //mg;
s/ $//mg;
} elsif ($lang eq 'S') {
# Because we use C++ style comments in our .S files, all we can do
# is to drop them
s{
^([^\n]*?)//[^\n]*?$ # Any line with a // comment
}{
if ($debug) {
print STDERR "DEBUG: '$&' => '$1'\n" if defined $1;
print STDERR "DEBUG: '$&' removed\n" unless defined $1;
}
defined $1 ? $1 : ""
}mgsxe;
# Drop all empty lines
s{
(^|\n)(?:\s*(?:\n|$))+ # Empty lines, preserve one newline
}{$1}gsx;
} elsif ($lang eq 'perl') {
# Merge continuation lines
s{\\\n}{}g;
# Regexp for things that should be preserved
my $preserved =
qr{
(?:
<<["']?(\w+)["']? # HERE document start
.*? # Its contents
^\g{-1}$
)
|
(?:
" # Double quoted string start
(?: \\. | [^\"])* # Any character, including escaped ones
" # Double quoted string end
)
| # OR
(?:
' # Single quoted string start
[^\']* # Any character
' # Single quoted string end
)
}msx;
# Remove comments while preserving strings
s{
(?| # All things preserved end up in $1
\#.*?(\n|$) # Perl comments
| # OR
( # Grouping for the replacement
$preserved
)
)
}{
if ($debug) {
print STDERR "DEBUG: '$&' => '$1'\n" if defined $1;
print STDERR "DEBUG: '$&' removed\n" unless defined $1;
}
defined $1 ? $1 : ""
}gsxe;
# Remove empty lines
s{
(?| # All things preserved end up in $1
(^|\n)(?:\s*(?:\n|$))+ # Empty lines, preserve one newline
| # OR
( # Grouping for the replacement
$preserved
)
)
}{$1}gsx;
}
print;
|