Common/comment.pm

filter: Statementstmt 100% 89 / 89 Branchbran 100% 14 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 5 totaltotal 95.9% 117 / 122 CC 7 SCAR 6.9
CC
7
Cov
100%
nested
4.0
__ANON__
3.0
combine
2.0
CRAP 2.0
« Common/URI/RFC1808.pm ↑ Summary Common/URI/ftp.pm »
1 package Regexp::Common::comment;
2  
3 71 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 71 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 71 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 71 ▶no warnings 'syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 71 ▶use Regexp::Common qw /pattern clean no_defaults/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 71 our $VERSION = '2024080801';
12  
13 71 my @generic = (
14 {languages => [qw /ABC Forth/],
15 to_eol => ['\\\\']}, # This is for just a *single* backslash.
16  
17 {languages => [qw /Ada Alan Eiffel lua/],
18 to_eol => ['--']},
19  
20 {languages => [qw /Advisor/],
21 to_eol => ['#|//']},
22  
23 {languages => [qw /Advsys CQL Lisp LOGO M MUMPS REBOL Scheme
24 SMITH zonefile/],
25 to_eol => [';']},
26  
27 {languages => ['Algol 60'],
28 from_to => [[qw /comment ;/]]},
29  
30 {languages => [qw {ALPACA B C C-- LPC PL/I}],
31 from_to => [[qw {/* */}]]},
32  
33 {languages => [qw /awk fvwm2 Icon m4 mutt Perl Python QML
34 R Ruby shell Tcl/],
35 to_eol => ['#']},
36  
37 {languages => [[BASIC => 'mvEnterprise']],
38 to_eol => ['[*!]|REM']},
39  
40 {languages => [qw /Befunge-98 Funge-98 Shelta/],
41 id => [';']},
42  
43 {languages => ['beta-Juliet', 'Crystal Report', 'Portia', 'Ubercode'],
44 to_eol => ['//']},
45  
46 {languages => ['BML'],
47 from_to => [['<?_c', '_c?>']],
48 },
49  
50 {languages => [qw /C++/, 'C#', qw /Cg ECMAScript FPL Java JavaScript/],
51 to_eol => ['//'],
52 from_to => [[qw {/* */}]]},
53  
54 {languages => [qw /CLU LaTeX slrn TeX/],
55 to_eol => ['%']},
56  
57 {languages => [qw /False/],
58 from_to => [[qw !{ }!]]},
59  
60 {languages => [qw /Fortran/],
61 to_eol => ['!']},
62  
63 {languages => [qw /Haifu/],
64 id => [',']},
65  
66 {languages => [qw /ILLGOL/],
67 to_eol => ['NB']},
68  
69 {languages => [qw /INTERCAL/],
70 to_eol => [q{(?:(?:PLEASE(?:\s+DO)?|DO)\s+)?(?:NOT|N'T)}]},
71  
72 {languages => [qw /J/],
73 to_eol => ['NB[.]']},
74  
75 {languages => [qw /JavaDoc/],
76 from_to => [[qw {/** */}]]},
77  
78 {languages => [qw /Nickle/],
79 to_eol => ['#'],
80 from_to => [[qw {/* */}]]},
81  
82 {languages => [qw /Oberon/],
83 from_to => [[qw /(* *)/]]},
84
85 {languages => [[qw /Pascal Delphi/], [qw /Pascal Free/], [qw /Pascal GPC/]],
86 to_eol => ['//'],
87 from_to => [[qw !{ }!], [qw !(* *)!]]},
88  
89 {languages => [[qw /Pascal Workshop/]],
90 id => [qw /"/],
91 from_to => [[qw !{ }!], [qw !(* *)!], [qw !/* */!]]},
92  
93 {languages => [qw /PEARL/],
94 to_eol => ['!'],
95 from_to => [[qw {/* */}]]},
96  
97 {languages => [qw /PHP/],
98 to_eol => ['#', '//'],
99 from_to => [[qw {/* */}]]},
100  
101 {languages => [qw !PL/B!],
102 to_eol => ['[.;]']},
103  
104 {languages => [qw !PL/SQL!],
105 to_eol => ['--'],
106 from_to => [[qw {/* */}]]},
107  
108 {languages => [qw /Q-BAL/],
109 to_eol => ['`']},
110  
111 {languages => [qw /Smalltalk/],
112 id => ['"']},
113  
114 {languages => [qw /SQL/],
115 to_eol => ['-{2,}']},
116  
117 {languages => [qw /troff/],
118 to_eol => ['\\\"']},
119  
120 {languages => [qw /vi/],
121 to_eol => ['"']},
122  
123 {languages => [qw /*W/],
124 from_to => [[qw {|| !!}]]},
125  
126 {languages => [qw /ZZT-OOP/],
127 to_eol => ["'"]},
128 );
129  
130 71 my @plain_or_nested = (
131 [Caml => undef, "(*" => "*)"],
132 [Dylan => "//", "/*" => "*/"],
133 [Haskell => "-{2,}", "{-" => "-}"],
134 [Hugo => "!(?!\\\\)", "!\\" => "\\!"],
135 [SLIDE => "#", "(*" => "*)"],
136 ['Modula-2' => undef, "(*" => "*)"],
137 ['Modula-3' => undef, "(*" => "*)"],
138 );
139  
140 #
141 # Helper subs.
142 #
143  
144 2485 ▶sub combine {
Subroutine combine: called CC=2 SCAR=6.9
Pod undocumented
145 2485 local $_ = join "|", @_;
146 2485 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 497 1988 2485
147 497 s/\(\?k:/(?:/g;
148 497 $_ = "(?k:$_)";
149 }
150 $_
151 2485 }
152  
153 1775 ▶sub to_eol ($) {"(?k:(?k:$_[0])(?k:[^\\n]*)(?k:\\n))"}
Subroutine to_eol: called CC=1 SCAR=0.0
Pod undocumented
154 284 ▶sub id ($) {"(?k:(?k:$_[0])(?k:[^$_[0]]*)(?k:$_[0]))"} # One char only!
Subroutine id: called CC=1 SCAR=0.0
Pod undocumented
155 1207 ▶sub from_to {
Subroutine from_to: called CC=1 SCAR=0.0
Pod undocumented
156 1207 my ($begin, $end) = @_;
157  
158 1207 my $qb = quotemeta $begin;
159 1207 my $qe = quotemeta $end;
160 1207 my $fe = quotemeta substr $end => 0, 1;
161 1207 my $te = quotemeta substr $end => 1;
162  
163 1207 "(?k:(?k:$qb)(?k:(?:[^$fe]+|$fe(?!$te))*)(?k:$qe))";
164 }
165  
166  
167 71 my $count = 0;
168 9186 ▶sub nested {
Subroutine nested: called CC=4 SCAR=13.9
Pod undocumented
169 9186 my ($begin, $end) = @_;
170  
171 9186 $count ++;
172 9186 my $r = '(??{$Regexp::Common::comment ['. $count . ']})';
173  
174 9186 my $qb = quotemeta $begin;
175 9186 my $qe = quotemeta $end;
176 9186 my $fb = quotemeta substr $begin => 0, 1;
177 9186 my $fe = quotemeta substr $end => 0, 1;
178  
179 9186 my $tb = quotemeta substr $begin => 1;
180 9186 my $te = quotemeta substr $end => 1;
181  
182 71 ▶ use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
183  
184 9186 my $re;
185 9186 ▶ if ($fb eq $fe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fb eq $fe) { } 586 8600 9186
186 586 $re = qr /(?:$qb(?:(?>[^$fb]+)|$fb(?!$tb)(?!$te)|$r)*$qe)/;
187 }
188 else {
189 8600 local $" = "|";
190 8600 my @clauses = "(?>[^$fb$fe]+)";
191 8600 ▶ push @clauses => "$fb(?!$tb)" if length $tb;
Branch: 1 branch
BranchTrueFalseTotal
if length $tb 8051 549 8600
192 8600 ▶ push @clauses => "$fe(?!$te)" if length $te;
Branch: 1 branch
BranchTrueFalseTotal
if length $te 8051 549 8600
193 8600 push @clauses => $r;
194 8600 $re = qr /(?:$qb(?:@clauses)*$qe)/;
195 }
196  
197 9186 $Regexp::Common::comment [$count] = qr/$re/;
198 }
199  
200 #
201 # Process data.
202 #
203  
204 71 foreach my $info (@plain_or_nested) {
205 497 my ($language, $mark, $begin, $end) = @$info;
206 pattern name => [comment => $language],
207 create =>
208 8051 ▶ sub {my $re = nested $begin => $end;
Subroutine __ANON__: called CC=3 SCAR=11.0
209 8051 ▶ my $prefix = defined $mark ? $mark . "[^\n]*\n|" : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $mark ? : 4868 3183 8051
210 8051 ▶ exists $_ [1] -> {-keep} ? qr /($prefix$re)/
Branch: 1 branch
BranchTrueFalseTotal
exists $_[1]{'-keep'} ? : 307 7744 8051
211 : qr /$prefix$re/
212 },
213 497 ;
214 }
215  
216  
217 71 foreach my $group (@generic) {
218 1775 my $pattern = combine +(map {to_eol $_} @{$group -> {to_eol}}),
219 1207 (map {from_to @$_} @{$group -> {from_to}}),
220 2485 (map {id $_} @{$group -> {id}}),
221 ;
222 2485 foreach my $language (@{$group -> {languages}}) {
223 5680 ▶ pattern name => [comment => ref $language ? @$language : $language],
Branch: 1 branch
BranchTrueFalseTotal
ref $language ? : 355 5325 5680
224 create => $pattern,
225 ;
226 }
227 }
228
229  
230
231 #
232 # Other languages.
233 #
234  
235 # http://www.pascal-central.com/docs/iso10206.txt
236 71 pattern name => [qw /comment Pascal/],
237 create => '(?k:' . '(?k:[{]|[(][*])'
238 . '(?k:[^}*]*(?:[*](?![)])[^}*]*)*)'
239 . '(?k:[}]|[*][)])'
240 . ')'
241 ;
242  
243 # http://www.templetons.com/brad/alice/language/
244 71 pattern name => [qw /comment Pascal Alice/],
245 create => '(?k:(?k:[{])(?k:[^}\n]*)(?k:[}]))'
246 ;
247  
248  
249 # http://westein.arb-phys.uni-dortmund.de/~wb/a68s.txt
250 71 pattern name => [qw (comment), 'Algol 68'],
251 create => q {(?k:(?:#[^#]*#)|} .
252 q {(?:\bco\b(?:[^c]+|\Bc|\bc(?!o\b))*\bco\b)|} .
253 q {(?:\bcomment\b(?:[^c]+|\Bc|\bc(?!omment\b))*\bcomment\b))}
254 ;
255  
256  
257 # See rules 91 and 92 of ISO 8879 (SGML).
258 # Charles F. Goldfarb: "The SGML Handbook".
259 # Oxford: Oxford University Press. 1990. ISBN 0-19-853737-9.
260 # Ch. 10.3, pp 390.
261 71 pattern name => [qw (comment HTML)],
262 create => q {(?k:(?k:<!)(?k:(?:--(?k:[^-]*(?:-[^-]+)*)--\s*)*)(?k:>))},
263 ;
264  
265 # MySQL comments:
266 # -- Till end of line (whitespace must follow --)
267 # # Till end of line
268 # /* Multiline comment */
269 # https://dev.mysql.com/doc/refman/8.4/en/comments.html
270 #
271 # Note the use of (?u:\s) instead of \s. If Unicode isn't in effect, \s does
272 # not quite match the same characters as [\v\h]. See the beginning of the
273 # section "Whitespace" section in perlrecharclass manual.
274 71 pattern name => [qw /comment SQL MySQL/],
275 create => q {(?k:(?:#|--(?=(?u:\s)))[^\n]*\n|} .
276 q {/\*(?:(?>[^*]+)|\*(?!/))*(?:;|\*/))},
277 ;
278  
279 # Anything that isn't <>[]+-.,
280 # http://home.wxs.nl/~faase009/Ha_BF.html
281 71 pattern name => [qw /comment Brainfuck/],
282 create => '(?k:[^<>\[\]+\-.,]+)'
283 ;
284  
285 # Squeak is a variant of Smalltalk-80.
286 # http://www.squeak.
287 # http://mucow.com/squeak-qref.html
288 71 pattern name => [qw /comment Squeak/],
289 create => '(?k:(?k:")(?k:[^"]*(?:""[^"]*)*)(?k:"))'
290 ;
291  
292 #
293 # Scores of less than 5 or above 17....
294 # http://www.cliff.biffle.org/esoterica/beatnik.html
295 71 @Regexp::Common::comment::scores = (1, 3, 3, 2, 1, 4, 2, 4, 1, 8,
296 5, 1, 3, 1, 1, 3, 10, 1, 1, 1,
297 1, 4, 4, 8, 4, 10);
298 {
299 71 my ($s, $x);
300 pattern name => [qw /comment Beatnik/],
301 1 ▶ create => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
302 71 ▶ use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
303 my $re = qr {\b([A-Za-z]+)\b
304 (?(?{($s, $x) = (0, lc $^N);
305 $s += $Regexp::Common::comment::scores
306 [ord (chop $x) - ord ('a')] while length $x;
307 1 $s >= 5 && $s < 18})XXX|)}x;
308 1 $re;
309 },
310 71 ;
311 }
312  
313  
314 # http://www.cray.com/craydoc/manuals/007-3692-005/html-007-3692-005/
315 # (Goto table of contents/3.3 Source Form)
316 # Fortran, in fixed format. Comments start with a C, c or * in the first
317 # column, or a ! anywhere, but the sixth column. Then end with a newline.
318 71 pattern name => [qw /comment Fortran fixed/],
319 create => '(?k:(?k:(?:^[Cc*]|(?<!^.....)!))(?k:[^\n]*)(?k:\n))'
320 ;
321  
322  
323 # http://www.csis.ul.ie/cobol/Course/COBOLIntro.htm
324 # Traditionally, comments in COBOL were indicated with an asteriks in
325 # the seventh column. Modern compilers may be more lenient.
326 71 pattern name => [qw /comment COBOL/],
327 create => '(?<=^......)(?k:(?k:[*])(?k:[^\n]*)(?k:\n))',
328 ;
329  
330 71 1;
331  
332  
333 __END__
« Common/URI/RFC1808.pm ↑ Summary Common/URI/ftp.pm »