-
Notifications
You must be signed in to change notification settings - Fork 4
/
Filters.pm
172 lines (153 loc) · 4.6 KB
/
Filters.pm
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
package Inline::Filters;
use strict;
use Config;
our $VERSION = "0.19";
use File::Spec;
#============================================================================
# Object Interface
#============================================================================
sub new {
my $class = shift;
return bless { filter => shift, coderef => shift }, $class;
}
sub filter {
my ($self, $o, $code) = @_;
return $self->{coderef}->($o, $code);
}
#============================================================================
# Strip POD
#============================================================================
sub Strip_POD {
my $ilsm = shift;
my $code = shift;
$code =~ s/^=\w+[^\n]*\n\n(.*?)(^=cut\n\n|\Z)//gsm;
return $code;
}
#============================================================================
# Strip comments in various languages
#============================================================================
sub _skip_quoted {
my ($text, $index, $closer) = @_;
for (my $i=$index+1; $i<length $text; $i++) {
my $p = substr($text, $i-1, 1);
my $c = substr($text, $i, length($closer));
return $i if ($c eq $closer and ($p ne '\\' or length($closer)>1));
}
return $index; # must not have been a string
}
sub _strip_comments {
my ($txt, $opn, $cls, @quotes) = @_;
my $i = -1;
while (++$i < length $txt) {
my $closer;
if (grep {my $r=substr($txt,$i,length($_)) eq $_; $closer=$_ if $r; $r}
@quotes) {
$i = _skip_quoted($txt, $i, $closer);
next;
}
if (substr($txt, $i, length($opn)) eq $opn) {
my $e = index($txt, $cls, $i) + length($cls);
substr($txt, $i, $e-$i) =~ s/[^\n]/ /g;
$i--;
next;
}
}
return $txt;
}
# Note: strips both C and C++ comments because so many compilers accept
# both styles for C programs. Perhaps a --strict parameter?
sub Strip_C_Comments {
my $ilsm = shift;
my $code = shift;
$code = _strip_comments($code, '//', "\n", '"');
$code = _strip_comments($code, '/*', '*/', '"');
return $code;
}
sub Strip_CPP_Comments {
my $ilsm = shift;
my $code = shift;
$code = _strip_comments($code, '//', "\n", '"');
$code = _strip_comments($code, '/*', '*/', '"');
return $code;
}
sub Strip_Python_Comments {
my $ilsm = shift;
my $code = shift;
$code = _strip_comments($code, '#', "\n", '"', '"""', '\'');
return $code;
}
sub Strip_TCL_Comments {
my $ilsm = shift;
my $code = shift;
return $code;
}
#============================================================================
# Preprocess C and C++
#============================================================================
sub Preprocess {
my $ilsm = shift;
my $code = shift;
my @inc_array;
if (defined($ilsm->{ILSM}{MAKEFILE}{INC})) {
if (ref($ilsm->{ILSM}{MAKEFILE}{INC} eq 'ARRAY')) {
@inc_array = @{$ilsm->{ILSM}{MAKEFILE}{INC}};
}
else {
@inc_array = ($ilsm->{ILSM}{MAKEFILE}{INC});
}
}
else {
@inc_array = ();
}
my $cppflags = q{};
if (defined $ilsm->{CONFIG}->{CPPFLAGS}) {
$cppflags = $ilsm->{CONFIG}->{CPPFLAGS};
}
my $cpp = join ' ', ($Config{cpprun},
$Config{cppflags},
$cppflags,
"-I$Config{archlibexp}/CORE",
@inc_array
);
my $tmpfile = File::Spec->catfile($ilsm->{API}{build_dir}, "Filters$$.c");
$ilsm->mkpath($ilsm->{API}{build_dir});
my ($CSRC, $PROCESSED);
open $CSRC, ">", $tmpfile or die $!;
print $CSRC $code;
close $CSRC;
open $PROCESSED, "$cpp \"$tmpfile\" |" or die $!;
$code = join '', <$PROCESSED>;
close $PROCESSED;
# default yes, will remove Filters*.c files, this is the same config option used by Inline::C(PP);
# must disable when using gdb to debug Inline::C(PP) code
if ((not defined $ilsm->{CONFIG}->{CLEAN_AFTER_BUILD}) or $ilsm->{CONFIG}->{CLEAN_AFTER_BUILD}) {
unlink $tmpfile;
}
return $code;
}
#============================================================================
# Returns a list of key, value pairs; a filter and its code reference.
#============================================================================
my %filters =
(
ALL => [
Strip_POD => \&Strip_POD,
Preprocess => \&Preprocess,
],
C => [
Strip_Comments => \&Strip_C_Comments,
],
CPP => [
Strip_Comments => \&Strip_CPP_Comments,
],
JAVA => [
Strip_Comments => \&Strip_CPP_Comments,
],
);
sub get_filters {
my $language = shift;
my ($all, $lang) = @filters{ALL => $language};
$lang ||= [];
return (@$all, @$lang);
}
1;