Regexp/Token/GroupType.pm

filter: Statementstmt 97.7% 86 / 88 Branchbran 75.0% 12 / 16 Conditioncond 85.7% 12 / 14 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 94.7% 18 / 19 Podpod 100% 1 / 1 totaltotal 91.1% 133 / 146 CC 18 SCAR 13.8
CC
18
Cov
93%
__setup_class
7.0
__make_group_type_matcher
7.0
__PPIX_TOKENIZER__regexp
5.0
CRAP 4.0
« Regexp.pm ↑ Summary Regexp/Node/Range.pm »
1 =head1 NAME
2  
3 PPIx::Regexp::Token::GroupType - Represent a grouping parenthesis type.
4  
5 =head1 SYNOPSIS
6  
7 use PPIx::Regexp::Dumper;
8 PPIx::Regexp::Dumper->new( 'qr{(?i:foo)}smx' )
9 ->print();
10  
11 =head1 INHERITANCE
12  
13 C<PPIx::Regexp::Token::GroupType> is a
14 L<PPIx::Regexp::Token|PPIx::Regexp::Token>.
15  
16 C<PPIx::Regexp::Token::GroupType> is the parent of
17 L<PPIx::Regexp::Token::GroupType::Assertion|PPIx::Regexp::Token::GroupType::Assertion>,
18 L<PPIx::Regexp::Token::GroupType::Atomic_Script_Run|PPIx::Regexp::Token::GroupType::Atomic_Script_Run>,
19 L<PPIx::Regexp::Token::GroupType::BranchReset|PPIx::Regexp::Token::GroupType::BranchReset>,
20 L<PPIx::Regexp::Token::GroupType::Code|PPIx::Regexp::Token::GroupType::Code>,
21 L<PPIx::Regexp::Token::GroupType::Modifier|PPIx::Regexp::Token::GroupType::Modifier>,
22 L<PPIx::Regexp::Token::GroupType::NamedCapture|PPIx::Regexp::Token::GroupType::NamedCapture>,
23 L<PPIx::Regexp::Token::GroupType::Script_Run|PPIx::Regexp::Token::GroupType::Script_Run>,
24 L<PPIx::Regexp::Token::GroupType::Subexpression|PPIx::Regexp::Token::GroupType::Subexpression>
25 and
26 L<PPIx::Regexp::Token::GroupType::Switch|PPIx::Regexp::Token::GroupType::Switch>.
27  
28 =head1 DESCRIPTION
29  
30 This class represents any of the magic sequences of characters that can
31 follow an open parenthesis. This particular class is intended to be
32 abstract.
33  
34 =head1 METHODS
35  
36 This class provides no public methods beyond those provided by its
37 superclass.
38  
39 =cut
40  
41 package PPIx::Regexp::Token::GroupType;
42  
43 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 9 ▶use base qw{ PPIx::Regexp::Token };
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 9 ▶use PPIx::Regexp::Constant qw{ MINIMUM_PERL @CARP_NOT };
Subroutine BEGIN: called CC=1 SCAR=0.0
49 9 ▶use PPIx::Regexp::Util qw{ __ns_can };
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 9 our $VERSION = '0.092';
52  
53 # Return true if the token can be quantified, and false otherwise
54 19 ▶sub can_be_quantified { return };
Subroutine can_be_quantified: called CC=1 SCAR=0.0
Pod documented
55  
56 =head2 __defining_string
57  
58 my $string = $class->__defining_string();
59  
60 This method is private to the C<PPIx-Regexp> package, and is documented
61 for the author's benefit only. It may be changed or revoked without
62 notice.
63  
64 This method returns an array of strings that define the specific group
65 type. These strings will normally start with C<'?'>.
66  
67 Optionally, the first returned item may be a hash reference. The only
68 supported key is C<{suffix}>, which is a string to be suffixed to each
69 of the regular expressions made by C<__make_group_type_matcher()> out of
70 the defining strings, inside a C<(?= ... )>, so that it is not included
71 in the match.
72  
73 This method B<must> be overridden, unless C<__make_group_type_matcher()>
74 is. The override B<must> return the same thing each time, since the
75 results of C<__make_group_type_matcher()> are cached.
76  
77 =cut
78  
79 0 ▶sub __defining_string {
Subroutine __defining_string: not called CC=1 SCAR=6.9
80 0 require Carp;
81 0 Carp::confess(
82 'Programming error - __defining_string() must be overridden' );
83 }
84  
85 =head2 __make_group_type_matcher
86  
87 my $hash_ref = $class->__make_group_type_matcher();
88  
89 This method is private to the C<PPIx-Regexp> package, and is documented
90 for the author's benefit only. It may be changed or revoked without
91 notice.
92  
93 This method returns a reference to a hash. The keys are regexp delimiter
94 characters which appear in the defining strings for the group type. For
95 each key, the value is a reference to an array of C<Regexp> objects,
96 properly escaped for the key character. Key C<''> provides the regular
97 expressions to be used if the regexp delimiter does not appear in any of
98 the defining strings.
99  
100 If this method is overridden by the subclass, method
101 C<__defining_string()> need not be, unless the overridden
102 C<__make_group_type_matcher()> calls C<__defining_string()>.
103  
104 =cut
105  
106 63 ▶sub __make_group_type_matcher {
Subroutine __make_group_type_matcher: called CC=7 SCAR=19.5
107 63 my ( $class ) = @_;
108  
109 63 my @defs = $class->__defining_string();
110  
111 63 ▶ my $opt = ref $defs[0] ? shift @defs : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $defs[0] ? : 16 47 63
112  
113 my $suffix = defined $opt->{suffix} ?
114 63 ▶ qr/ (?= \Q$opt->{suffix}\E ) /smx :
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'suffix'} ? : 16 47 63
115 '';
116  
117 63 my %seen;
118 63 my @chars = grep { ! $seen{$_}++ } split qr{}smx, join '', @defs;
119  
120 63 my %rslt;
121 63 foreach my $str ( @defs ) {
122 318 ▶ push @{ $rslt{''} ||= [] }, qr{ \A \Q$str\E $suffix }smx;
Condition100%2/2
$rslt{""} ||= []
l!l
25563
MC/DC100%1/1
$rslt{""} ||= []
$rslt{""}
Truth table100%2/2
A
A= $rslt{""}
Aresult
11
00
123 318 foreach my $chr ( @chars ) {
124 5505 ( my $expr = $str ) =~ s/ (?= \Q$chr\E ) /\\/smxg;
125 5505 ▶ push @{ $rslt{$chr} ||= [] }, qr{ \A \Q$expr\E $suffix }smx;
Condition100%2/2
$rslt{$chr} ||= []
l!l
4797708
MC/DC100%1/1
$rslt{$chr} ||= []
$rslt{$chr}
Truth table100%2/2
A
A= $rslt{$chr}
Aresult
11
00
126 }
127 }
128 63 return \%rslt;
129 }
130  
131 =head2 __match_setup
132  
133 $class->__match_setup( $tokenizer );
134  
135 This method is private to the C<PPIx-Regexp> package, and is documented
136 for the author's benefit only. It may be changed or revoked without
137 notice.
138  
139 This method performs whatever setup is needed once it is determined that
140 the given group type has been detected. This method is called only if
141 the class matched at the current position in the string being parsed. It
142 must perform whatever extra setup is needed for the match. It returns
143 nothing.
144  
145 This method need not be overridden. The default does nothing.
146  
147 =cut
148  
149 67 ▶sub __match_setup {
Subroutine __match_setup: called CC=1 SCAR=0.0
150 67 return;
151 }
152  
153 =head2 __setup_class
154  
155 $class->__setup_class( \%definition, \%opt );
156  
157 This method is private to the C<PPIx-Regexp> package, and is documented
158 for the author's benefit only. It may be changed or revoked without
159 notice.
160  
161 This method uses the C<%definition> hash to create the
162 C<__defining_string()>, C<explain()>, C<perl_version_introduced()>, and
163 C<perl_version_removed()> methods for the calling class. Any of these
164 that already exist will B<not> be replaced.
165  
166 The C<%definition> hash defines all the strings that specify tokens of
167 the invoking class. You can not (unfortunately) use this mechanism if
168 you need a regular expression to recognize a token that belongs to this
169 class. The keys of the C<%definition> hash are strings that specify
170 members of this class. The values are hashes that define the specific
171 member of the class. The following values are supported:
172  
173 =over
174  
175 =item {expl}
176  
177 This is the explanation of the element, to be returned by the
178 C<explain()> method.
179  
180 =item {intro}
181  
182 This is the Perl version that introduced the element, as a string. The
183 default is the value of constant
184 L<MINIMUM_PERL|PPIx::Regexp::Constant/MINIMUM_PERL>.
185  
186 =item {remov}
187  
188 This is the Perl version that removed the element, as a string. The
189 default is C<undef>, meaning that the element is still present in the
190 highest released version of Perl, whether development or production.
191  
192 =back
193  
194 The C<%opt> hash is optional, and defaults to the empty hash. It is
195 used, basically, for ad-hocery. The supported keys are:
196  
197 =over
198  
199 =item {suffix}
200  
201 If this element is defined, the first element returned by the generated
202 L<__defining_string()|/__defining_string> method is a hash containing
203 this key and value.
204  
205 =back
206  
207 =cut
208  
209 63 ▶sub __setup_class {
Subroutine __setup_class: called CC=7 SCAR=19.5
210 63 my ( $class, $opt ) = @_;
211  
212 63 ▶ $opt ||= {};
Condition100%2/2
$opt ||= {}
l!l
1845
MC/DC100%1/1
$opt ||= {}
$opt
Truth table100%2/2
A
A= $opt
Aresult
11
00
213  
214 63 ▶ unless ( $class->__ns_can( '__defining_string' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->__ns_can("__defining_string")) 63 0 63
215 63 my $method = "${class}::__defining_string";
216 63 my @def_str = sort keys %{ $class->DEF };
217 defined $opt->{suffix}
218 and unshift @def_str, {
219 suffix => $opt->{suffix},
220 63 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{"suffix"} and unshift @def_str, {"suffix", $opt->{"suffix"}} 18 45 63
221 63 $class->DEF->{__defining_string} = \@def_str;
222 9 ▶ no strict qw{ refs };
Subroutine BEGIN: called CC=1 SCAR=0.0
223 63 ▶ *$method = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
224 63 my ( $self ) = @_;
225 63 return @{ $self->DEF->{__defining_string} };
226 63 };
227 }
228  
229 63 ▶ unless ( $class->__ns_can( 'explain' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->__ns_can("explain")) 63 0 63
230 63 my $method = "${class}::explain";
231 9 ▶ no strict qw{ refs };
Subroutine BEGIN: called CC=1 SCAR=0.0
232 10 ▶ *$method = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
233 10 my ( $self ) = @_;
234 10 $DB::single = 1;
235 10 return $self->DEF->{ $self->unescaped_content() }{expl};
236 63 };
237 }
238  
239 63 ▶ unless ( $class->__ns_can( 'perl_version_introduced' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->__ns_can("perl_version_introduced")) 63 0 63
240 63 my $method = "${class}::perl_version_introduced";
241 9 ▶ no strict qw{ refs };
Subroutine BEGIN: called CC=1 SCAR=0.0
242 40 ▶ *$method = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
243 40 my ( $self ) = @_;
244 40 ▶ return $self->DEF->{ $self->unescaped_content() }{intro} || MINIMUM_PERL;
Condition100%2/2
$self->DEF->{$self->unescaped_content}{'intro'} || '5.000'
l!l
382
MC/DC100%1/1
$self->DEF->{$self->unescaped_content}{'intro'} || '5.000'
$self->DEF->{$self->unescaped_content}{'intro'}
Truth table100%2/2
A
A= $self->DEF->{$self->unescaped_content}{'intro'}
Aresult
11
00
245 63 };
246 }
247  
248 63 ▶ unless ( $class->__ns_can( 'perl_version_removed' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->__ns_can("perl_version_removed")) 63 0 63
249 63 my $method = "${class}::perl_version_removed";
250 9 ▶ no strict qw{ refs };
Subroutine BEGIN: called CC=1 SCAR=0.0
251 50 ▶ *$method = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
252 50 my ( $self ) = @_;
253 50 return $self->DEF->{ $self->unescaped_content() }{remov};
254 63 };
255 }
256  
257 63 return;
258 }
259  
260 9 my %matcher;
261  
262 1794 ▶sub __PPIX_TOKENIZER__regexp {
Subroutine __PPIX_TOKENIZER__regexp: called CC=5 SCAR=16.2
263 1794 my ( $class, $tokenizer ) = @_; # $character unused
264  
265 1794 ▶ my $mtch = $matcher{$class} ||= $class->__make_group_type_matcher();
Condition66%2/3
$matcher{$class} ||= $class->__make_group_type_matcher
l!l&&r!l&&!r
1742520
MC/DC0%0/2
$matcher{$class} ||= $class->__make_group_type_matcher
$matcher{$class} $class->__make_group_type_matcher
Truth table66%2/3
A ||= B
A= $matcher{$class}B= $class->__make_group_type_matcher
ABresult
1X1
011
000
266  
267 my $re_list = $mtch->{ $tokenizer->get_start_delimiter() } ||
268 1794 ▶ $mtch->{''};
Condition66%2/3
$$mtch{$tokenizer->get_start_delimiter} || $mtch->{''}
l!l&&r!l&&!r
1417800
MC/DC0%0/2
$$mtch{$tokenizer->get_start_delimiter} || $mtch->{''}
$$mtch{$tokenizer->get_start_delimiter} $mtch->{''}
Truth table66%2/3
A || B
A= $$mtch{$tokenizer->get_start_delimiter}B= $mtch->{''}
ABresult
1X1
011
000
269  
270 1794 foreach my $re ( @{ $re_list } ) {
271 5990 ▶ my $accept = $tokenizer->find_regexp( $re )
Branch: 1 branch
BranchTrueFalseTotal
my $accept = $tokenizer->find_regexp($re) or next 161 5829 5990
272 or next;
273 161 $class->__match_setup( $tokenizer );
274 161 return $accept;
275 }
276  
277 1633 return;
278 }
279  
280 9 1;
281  
282 __END__
« Regexp.pm ↑ Summary Regexp/Node/Range.pm »