blib/lib/Inline/Filters.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 130 CC 7 SCAR 20.5
CC
7
Cov
0%
_strip_comments
20.0
Preprocess
12.0
_skip_quoted
6.0
CRAP 7.8
1 package Inline::Filters;
2 use strict;
3 use Config;
4 $Inline::Filters::VERSION = "0.12_03";
5  
6 #============================================================================
7 # Object Interface
8 #============================================================================
9 sub new {
10 my $class = shift;
11 return bless { filter => shift, coderef => shift }, $class;
12 }
13  
14 sub filter {
15 my ($self, $o, $code) = @_;
16 return $self->{coderef}->($o, $code);
17 }
18  
19 #============================================================================
20 # Strip POD
21 #============================================================================
22 sub Strip_POD {
23 my $ilsm = shift;
24 my $code = shift;
25 $code =~ s/^=\w+[^\n]*\n\n(.*?)(^=cut\n\n|\Z)//gsm;
26 return $code;
27 }
28  
29 #============================================================================
30 # Strip comments in various languages
31 #============================================================================
32 sub _skip_quoted {
33 my ($text, $index, $closer) = @_;
34 for (my $i=$index+1; $i<length $text; $i++) {
35 my $p = substr($text, $i-1, 1);
36 my $c = substr($text, $i, length($closer));
37 return $i if ($c eq $closer and ($p ne '\\' or length($closer)>1));
38 }
39 return $index; # must not have been a string
40 }
41  
42 sub _strip_comments {
43 my ($txt, $opn, $cls, @quotes) = @_;
44 my $i = -1;
45 while (++$i < length $txt) {
46 my $closer;
47 if (grep {my $r=substr($txt,$i,length($_)) eq $_; $closer=$_ if $r; $r}
48 @quotes) {
49 $i = _skip_quoted($txt, $i, $closer);
50 next;
51 }
52 if (substr($txt, $i, length($opn)) eq $opn) {
53 my $e = index($txt, $cls, $i) + length($cls);
54 substr($txt, $i, $e-$i) =~ s/[^\n]/ /g;
55 $i--;
56 next;
57 }
58 }
59 return $txt;
60 }
61  
62 # Note: strips both C and C++ comments because so many compilers accept
63 # both styles for C programs. Perhaps a --strict parameter?
64 sub Strip_C_Comments {
65 my $ilsm = shift;
66 my $code = shift;
67 $code = _strip_comments($code, '//', "\n", '"');
68 $code = _strip_comments($code, '/*', '*/', '"');
69 return $code;
70 }
71  
72 sub Strip_CPP_Comments {
73 my $ilsm = shift;
74 my $code = shift;
75 $code = _strip_comments($code, '//', "\n", '"');
76 $code = _strip_comments($code, '/*', '*/', '"');
77 return $code;
78 }
79  
80 sub Strip_Python_Comments {
81 my $ilsm = shift;
82 my $code = shift;
83 $code = _strip_comments($code, '#', "\n", '"', '"""', '\'');
84 return $code;
85 }
86  
87 sub Strip_TCL_Comments {
88 my $ilsm = shift;
89 my $code = shift;
90  
91 return $code;
92 }
93  
94 #============================================================================
95 # Preprocess C and C++
96 #============================================================================
97 sub Preprocess {
98 my $ilsm = shift;
99 my $code = shift;
100  
101 my @inc_array;
102 if (defined($ilsm->{ILSM}{MAKEFILE}{INC})) {
103 if (ref($ilsm->{ILSM}{MAKEFILE}{INC} eq 'ARRAY')) {
104 @inc_array = @{$ilsm->{ILSM}{MAKEFILE}{INC}};
105 }
106 else {
107 @inc_array = ($ilsm->{ILSM}{MAKEFILE}{INC});
108 }
109 }
110 else {
111 @inc_array = ();
112 }
113  
114 my $cpp = join ' ', ($Config{cpprun},
115 $Config{cppflags},
116 "-I$Config{archlibexp}/CORE",
117 @inc_array
118 );
119  
120 my $tmpfile = $ilsm->{API}{build_dir} . "/Filters.c";
121 $ilsm->mkpath($ilsm->{API}{build_dir});
122 open CSRC, ">$tmpfile" or die $!;
123 print CSRC $code;
124 close CSRC;
125 open PROCESSED, "$cpp $tmpfile |" or die $!;
126 $code = join'', <PROCESSED>;
127 close PROCESSED;
128 unlink $tmpfile;
129 return $code;
130 }
131  
132 #============================================================================
133 # Returns a list of key, value pairs; a filter and its code reference.
134 #============================================================================
135 my %filters =
136 (
137 ALL => [
138 Strip_POD => \&Strip_POD,
139 Preprocess => \&Preprocess,
140 ],
141 C => [
142 Strip_Comments => \&Strip_C_Comments,
143 ],
144 CPP => [
145 Strip_Comments => \&Strip_CPP_Comments,
146 ],
147 JAVA => [
148 Strip_Comments => \&Strip_CPP_Comments,
149 ],
150 );
151  
152 sub get_filters {
153 my $language = shift;
154 my ($all, $lang) = @filters{ALL => $language};
155 $lang ||= [];
156 return (@$all, @$lang);
157 }
158  
159 1;