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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
# Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 Kevin Ryde
# This file is part of Perl-Critic-Pulp.
# Perl-Critic-Pulp is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# Perl-Critic-Pulp is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
# perlcritic -s ProhibitUnbalancedParens ProhibitUnbalancedParens.pm
# unclosed:
# perlcritic -s ProhibitUnbalancedParens /usr/share/perl/5.12/CGI.pm
# smiley close:
# perlcritic -s ProhibitUnbalancedParens /usr/share/perl5/accessors.pm
package Perl::Critic::Policy::Documentation::ProhibitUnbalancedParens;
use 5.006;
use strict;
use warnings;
use base 'Perl::Critic::Policy';
use Perl::Critic::Utils;
# uncomment this to run the ### lines
# use Smart::Comments;
our $VERSION = 96;
use constant supported_parameters => ();
use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
use constant default_themes => qw(pulp cosmetic);
use constant applies_to => 'PPI::Document';
sub violates {
my ($self, $elem, $document) = @_;
### ProhibitUnbalancedParens on: $elem->content
my $parser = Perl::Critic::Pulp::PodParser::ProhibitUnbalancedParens->new
(policy => $self);
$parser->parse_from_elem ($elem);
return $parser->violations;
}
package Perl::Critic::Pulp::PodParser::ProhibitUnbalancedParens;
use strict;
use warnings;
use Pod::ParseLink;
use base 'Perl::Critic::Pulp::PodParser';
sub command {
my $self = shift;
my ($command, $text, $linenum, $paraobj) = @_;
$self->SUPER::command(@_); # maintain 'in_begin'
if ($command eq 'for'
&& $text =~ /^ProhibitUnbalancedParens\b\s*(.*)/) {
my $directive = $1;
### $directive
if ($directive =~ /^allow next( (\d+))?/) {
# numbered "allow next 5" means up to that many following
# unnumbered "allow next" means one following
$self->{'allow_next'} = (defined $2 ? $2 : 1);
}
}
return $self->command_as_textblock(@_);
}
my %open_to_close = ('(' => ')',
'[' => ']',
'{' => '}');
my %close_to_open = reverse %open_to_close;
sub textblock {
my ($self, $text, $linenum, $paraobj) = @_;
### textblock: "linenum=$linenum"
if (($self->{'allow_next'}||0) > 0) {
$self->{'allow_next'}--;
return '';
}
# process outside =begin, and inside =begin which is ":" markup
unless ($self->{'in_begin'} eq '' || $self->{'in_begin'} =~ /^:/) {
return '';
}
my $interpolated = $self->interpolate($text, $linenum);
### $text
### $interpolated
my @parens;
while ($interpolated
=~ m/
([][({})]) # $1 open or close
|([:;]-?\) # $2 smiley face optional close
|\b[a-zA-Z1-9]\) # "middle a) or 1) item"
|(?<!\$)\$\) # perlvar $), and not $$
)
|(["'])[][(){}]+\3 # $3 "(" quoted
|[:;]-?[(] # smiley face not an open
|(?<!\$)\$\$ # perlvar $$ consumed
|\$\(\w*\) # makefile var $(abc)
|\$\[\w*\] # perhaps template $[abc]
|(?<!\$)\$[][(] # perlvars $[, $(, $], and not $$
|^\s*(\d+|[A-Za-z])\.?\) # initial "1.5) something"
/xg) {
### match: $&
### $1
### $2
### $3
if (defined $1) {
push @parens, { char => $1,
pos => pos($interpolated)-1,
};
} elsif (defined $2) {
push @parens, { char => ')',
pos => pos($interpolated)-1,
optional => 1,
};
}
}
### @parens
# sort optional closes to after hard closes
{
my @optional;
my @new;
foreach my $p (@parens) {
if (@optional && $optional[0]->{'char'} ne $p->{'char'}) {
push @new, splice @optional;
}
if ($p->{'optional'}) {
push @optional, $p;
} else {
push @new, $p;
}
}
@parens = (@new, @optional);
}
### sorted: @parens
my @opens;
foreach my $p (@parens) {
### $p
my $char = $p->{'char'};
if (my $want_openchar = $close_to_open{$char}) {
# a close
if (my $o = pop @opens) {
my $openchar = $o->{'char'};
if ($openchar ne $want_openchar) {
if ($p->{'optional'}) {
### mismatched optional close, skip
push @opens, $o;
next;
}
$self->violation_at_linenum_and_textpos
("Mismatched closing paren \"$char\" expected \"$open_to_close{$openchar}\"",
$linenum, $interpolated, $p->{'pos'});
}
} else {
if ($p->{'optional'}) {
### unopened optional close, skip
next;
}
$self->violation_at_linenum_and_textpos
("Unopened close paren \"$char\"",
$linenum, $interpolated, $p->{'pos'});
}
} else {
# an open
push @opens, $p;
}
}
foreach my $p (@opens) {
$self->violation_at_linenum_and_textpos
("Unclosed paren \"$p->{'char'}\"",
$linenum, $interpolated, $p->{'pos'});
}
return '';
}
*interior_sequence = \&interior_sequence_as_displayed_noncode_text;
sub interior_sequence_as_displayed_noncode_text {
my ($self, $cmd, $text, $pod_seq) = @_;
if ($cmd eq 'X' || $cmd eq 'C') {
### $cmd
### X,C keep only the newlines: $text
$text =~ tr/\n//cd;
} elsif ($cmd eq 'L') {
my ($display, $inferred, $name, $section, $type)
= Pod::ParseLink::parselink ($text);
### $text
### $display
### $inferred
### $name
return $inferred; # the display part, or the name part if no display
}
return $text;
}
1;
__END__
=for stopwords Ryde paren parens ie deref there'd backslashing Parens
=head1 NAME
Perl::Critic::Policy::Documentation::ProhibitUnbalancedParens - don't leave an open bracket or paren
=head1 DESCRIPTION
This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
add-on. It reports unbalanced or mismatched parentheses, brackets and braces
in POD text paragraphs,
Blah blah (and something. # bad
Blah blah ( [ ). # bad
Blah blah brace }. # bad
This is only cosmetic and normally only a minor irritant to readability so
this policy is low severity and under the "cosmetic" theme (see
L<Perl::Critic/POLICY THEMES>).
Text and command paragraphs are checked, but verbatim paragraphs can have
anything. There are some exceptions to paren balancing. The intention is
to be forgiving of common or reasonable constructs. Currently this means,
=over
=item *
Anything in C<CE<lt>E<gt>> code markup is ignored
=for ProhibitVerbatimMarkup allow next
In code C<anything [ is allowed>. # ok
Perhaps this will change, though there'd have to be extra exceptions in
C<CE<lt>E<gt>>, such as various backslashing.
Sometimes a prematurely ending C<CE<lt>E<gt>> may look like an unbalanced
paren, for example
=for ProhibitVerbatimMarkup allow next
Call C<foo(key=>value)> ... # bad
=for ProhibitUnbalancedParens allow next
This is bad because the C<CE<lt>E<gt>> ends at the C<=E<gt>>, leaving
"value)" unbalanced plain text. This is an easy mistake to make. (The
author's C<perl-pod-gt.el> can show warning face on this in Emacs.)
=item *
Quoted "(" is taken to be describing the char and is not an open or close.
Any of "(" or '[' or "[{]". # ok
This only applies to quoted parens alone (one or more), not larger quoted
text.
=item *
Item parens
a) the first thing, or b) the second thing # ok
1) one, 2) two # ok
Exactly how much is recognised as an "a)" etc is not quite settled. In the
current code a "1.5)" is recognised at the start of a paragraph, but in the
middle only "1)" style.
=item *
Smiley faces are an "optional" close,
(Some thing :-). # ok
Bare smiley :). # ok
(Or smile :-) and also close.) # ok
=item *
Sad smiley faces are not an opening paren,
:( :-(. # ok
=item *
Perl variables C<$(> and C<$[> are not opening parens,
Default is group $( blah blah. # ok
C<${> brace is still an open and expected to have a matching close, because
it's likely to be a deref or delimiter,
Deref with ${foo()} etc etc.
Variables or expressions like this will often be in C<CE<lt>E<gt>> markup
and skipped for that reason instead, as described above.
=item *
C<$)> and C<$]> are optional closes, since they might be Perl variables to
skip, or might be "$" at the end of a parens,
blah blah (which in TeX is $1\cdot2$).
Perhaps the conditions for these will be restricted a bit, though again
C<CE<lt>E<gt>> markup around sample code like this will be usual.
=item *
C<LE<lt>display|linkE<gt>> links are processed as the "display" text part.
The link target (POD document name and section) can have anything.
=back
C<=begin :foo> ... C<=end :foo> sections with a format name ":foo" starting
with a ":" are POD markup and are processed accordingly. Other C<=begin>
sections are skipped.
=head2 Unrecognised Forms
A mathematical half-open range like the following is not recognised.
[1,2) # bad, currently
Perhaps just numbers like this would be unambiguous, but if it's an
expression then it's hard to distinguish a parens typo from some
mathematics. The suggestion for now is an C<=for> per below to flag it as
an exception. Another way would be to write S<1 E<lt>= X E<lt> 2>, which
might be clearer to mathematically unsophisticated readers.
Parens spanning multiple paragraphs are not recognised,
(This is some # bad
thing.) # bad
Hopefully this is uncommon, and probably better style not to be
parenthetical about something so big that it runs to multiple paragraphs or
has a verbatim block in the middle etc.
=head2 Disabling
If an unbalanced paren is intended you can add an C<=for> to tell
C<ProhibitUnbalancedParens> to allow it.
=for ProhibitUnbalancedParens allow next
Something ( deliberately unclosed.
Or with a count of paragraphs to ignore,
=for ProhibitUnbalancedParens allow next 2
First deliberate [ unclosed.
Second (.
The usual no critic
## no critic (ProhibitUnbalancedParens)
works too as a whole-file disable, but the annotation must be before any
C<__END__> token, and if the POD is after the C<__END__> then
C<Perl::Critic> 1.112 up is required. Individual C<=for> has the advantage
of being with an exception.
As always if you don't care about this at all you can disable
C<ProhibitUnbalancedParens> completely from your F<.perlcriticrc> in
the usual way (see L<Perl::Critic/CONFIGURATION>),
[-Documentation::ProhibitUnbalancedParens]
=head1 SEE ALSO
L<Perl::Critic::Pulp>,
L<Perl::Critic>
L<http://user42.tuxfamily.org/perl-pod-gt/index.html>
=head1 HOME PAGE
http://user42.tuxfamily.org/perl-critic-pulp/index.html
=head1 COPYRIGHT
Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 Kevin Ryde
Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
Perl-Critic-Pulp is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
=cut
|