Regexp/Token/Structure.pm

filter: Statementstmt 99.1% 120 / 121 Branchbran 87.0% 47 / 54 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 18 / 18 Podpod 100% 3 / 3 totaltotal 96.1% 200 / 208 CC 33 SCAR 25.0
CC
33
Cov
96%
__PPIX_TOKENIZER__regexp
23.1
__ANON__
7.0
perl_version_introduced
3.0
CRAP 12.2
« Regexp/Structure.pm ↑ Summary Regexp/Token/Literal.pm »
1 =head1 NAME
2  
3 PPIx::Regexp::Token::Structure - Represent structural elements.
4  
5 =head1 SYNOPSIS
6  
7 use PPIx::Regexp::Dumper;
8 PPIx::Regexp::Dumper->new( 'qr{(foo)}smx' )
9 ->print();
10  
11 =head1 INHERITANCE
12  
13 C<PPIx::Regexp::Token::Structure> is a
14 L<PPIx::Regexp::Token|PPIx::Regexp::Token>.
15  
16 C<PPIx::Regexp::Token::Structure> is the parent of
17 L<PPIx::Regexp::Token::Delimiter|PPIx::Regexp::Token::Delimiter>.
18  
19 =head1 DESCRIPTION
20  
21 This class represents things that define the structure of the regular
22 expression. This typically means brackets of various sorts, but to
23 prevent proliferation of token classes the type of the regular
24 expression is stored here.
25  
26 =head1 METHODS
27  
28 This class provides no public methods beyond those provided by its
29 superclass.
30  
31 =cut
32  
33 package PPIx::Regexp::Token::Structure;
34  
35 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 9 ▶use base qw{ PPIx::Regexp::Token };
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 9 ▶use PPIx::Regexp::Constant qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
41 COOKIE_CLASS
42 COOKIE_QUANT
43 COOKIE_REGEX_SET
44 MINIMUM_PERL
45 TOKEN_LITERAL
46 @CARP_NOT
47 9 };
48  
49 # Tokens we are responsible for making, under at least some
50 # circumstances.
51 9 ▶use PPIx::Regexp::Token::Comment ();
Subroutine BEGIN: called CC=1 SCAR=0.0
52 9 ▶use PPIx::Regexp::Token::Modifier ();
Subroutine BEGIN: called CC=1 SCAR=0.0
53 9 ▶use PPIx::Regexp::Token::Backreference ();
Subroutine BEGIN: called CC=1 SCAR=0.0
54 9 ▶use PPIx::Regexp::Token::Backtrack ();
Subroutine BEGIN: called CC=1 SCAR=0.0
55 9 ▶use PPIx::Regexp::Token::Recursion ();
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 9 our $VERSION = '0.092';
58  
59 # Return true if the token can be quantified, and false otherwise
60  
61 9 my %quant = map { $_ => 1 } ')', ']';
62 524 ▶sub can_be_quantified {
Subroutine can_be_quantified: called CC=2 SCAR=7.1
Pod documented
63 524 my ( $self ) = @_;
64 524 ▶ ref $self or return;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or return 524 0 524
65 524 return $quant{ $self->content() };
66 };
67  
68 {
69  
70 9 my %explanation = (
71 '' => 'Match regexp',
72 '(' => 'Capture or grouping',
73 '(?[' => 'Extended character class',
74 ')' => 'End capture or grouping',
75 '[' => 'Character class',
76 ']' => 'End character class',
77 '])' => 'End extended character class',
78 'm' => 'Match regexp',
79 'qr' => 'Regexp object definition',
80 's' => 'Replace regexp with string or expression',
81 '{' => 'Explicit quantifier',
82 '}' => 'End explicit quantifier',
83 );
84  
85 7 ▶ sub __explanation {
Subroutine __explanation: called CC=1 SCAR=0.0
86 7 return \%explanation;
87 }
88  
89 }
90  
91 9 ▶sub is_quantifier {
Subroutine is_quantifier: called CC=2 SCAR=7.0
Pod documented
92 540 my ( $self ) = @_;
93 540 ▶ ref $self or return;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or return 540 0 540
94 540 return $self->{is_quantifier};
95 }
96  
97 {
98  
99 # Note that the implementation equivocates on the ::Token::Structure
100 # class, using it both for the initial token that determines the
101 # type of the regex and things like parentheses internal to the
102 # regex. Rather than sort out this equivocation, I have relied on
103 # the currently-true assumption that 'qr' will not satisfy the
104 # ::Token::Structure recognition logic, and the only way this class
105 # can acquire this content is by the brute-force approach used to
106 # generate the initial token object.
107  
108 9 my %perl_version_introduced = (
109 qr => '5.005',
110 '(?[' => '5.017008',
111 );
112  
113 222 ▶ sub perl_version_introduced {
Subroutine perl_version_introduced: called CC=3 SCAR=11.0
Pod documented
114 222 my ( $self ) = @_;
115 return $self->{perl_version_introduced} ||
116 222 ▶ $perl_version_introduced{ $self->content() } ||
Condition100%5/5
$self->{'perl_version_introduced'} || $perl_version_introduced{$self->content} || '5.000'
l!l
12210
$self->{"perl_version_introduced"} or $perl_version_introduced{$self->content}
l!l&&r!l&&!r
66210
MC/DC100%2/2
$self->{'perl_version_introduced'} || $perl_version_introduced{$self->content} || '5.000'
$self->{"perl_version_introduced"} $perl_version_introduced{$self->content}
Truth table100%3/3
A or B
A= $self->{"perl_version_introduced"}B= $perl_version_introduced{$self->content}
ABresult
1X1
011
000
117 MINIMUM_PERL;
118 }
119 }
120
121 {
122  
123 9 my %delim = map { $_ => 1 } qw/ ( ) { } [ ] /;
124  
125 # Regular expressions to match various parenthesized tokens, and the
126 # classes to make them into.
127  
128 my @paren_token = map {
129 9 [ $_ => $_->__PPIX_TOKEN__recognize() ]
130 }
131 'PPIx::Regexp::Token::Comment',
132 'PPIx::Regexp::Token::Modifier',
133 'PPIx::Regexp::Token::Backreference',
134 'PPIx::Regexp::Token::Backtrack',
135 'PPIx::Regexp::Token::Recursion',
136 ;
137  
138 1046 ▶ sub __PPIX_TOKENIZER__regexp {
Subroutine __PPIX_TOKENIZER__regexp: called CC=23 SCAR=31.4
139 1046 my ( undef, $tokenizer, $character ) = @_;
140  
141 # We are not interested in anything but delimiters.
142 1046 ▶ $delim{$character} or return;
Branch: 1 branch
BranchTrueFalseTotal
$delim{$character} or return 893 153 1046
143  
144 # Inside a character class, all the delimiters are normal characters
145 # except for the close square bracket.
146 893 ▶ if ( $tokenizer->cookie( COOKIE_CLASS ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokenizer->cookie("]")) 59 834 893
147 59 ▶ $character eq ']'
Branch: 1 branch
BranchTrueFalseTotal
$character eq "]" or return $tokenizer->make_token(1, "PPIx::Regexp::Token::Literal") 53 6 59
148 or return $tokenizer->make_token( 1, TOKEN_LITERAL );
149 53 $tokenizer->cookie( COOKIE_CLASS, undef );
150 53 return 1;
151 }
152  
153 # Open parentheses have various interesting possibilities ...
154 834 ▶ if ( $character eq '(' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($character eq "(") 347 487 834
155  
156 # Sometimes the whole bunch of parenthesized characters seems
157 # naturally to be a token.
158 347 foreach ( @paren_token ) {
159 1659 my ( $class, @recognize ) = @{ $_ };
160 1659 foreach ( @recognize ) {
161 2595 my ( $regexp, $arg ) = @{ $_ };
162 2595 ▶ my $accept = $tokenizer->find_regexp( $regexp ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $accept = $tokenizer->find_regexp($regexp) or next 55 2540 2595
163 55 return $tokenizer->make_token( $accept, $class, $arg );
164 }
165 }
166  
167 # Modifier changes are local to this parenthesis group
168 292 $tokenizer->modifier_duplicate();
169  
170 # The regex-set functionality introduced with 5.17.8 is most
171 # conveniently handled by treating the initial '(?[' and
172 # final '])' as ::Structure tokens. Fortunately for us,
173 # perl5178delta documents that these may not have interior
174 # spaces.
175  
176 292 ▶ if ( my $accept = $tokenizer->find_regexp(
Branch: 1 branch
BranchTrueFalseTotal
if (my $accept = $tokenizer->find_regexp(qr/ \A [(] [?] [[] /msx)) 8 284 292
177 qr{ \A [(] [?] [[] }smx # ] ) - help for vim
178 )
179 ) {
180 8 ▶ $tokenizer->cookie( COOKIE_REGEX_SET, sub { return 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
181 8 $tokenizer->modifier_modify( x => 1 ); # Implicitly /x
182 8 return $accept;
183 }
184  
185 # We expect certain tokens only after a left paren.
186 $tokenizer->expect(
187 284 'PPIx::Regexp::Token::GroupType::Modifier',
188 'PPIx::Regexp::Token::GroupType::NamedCapture',
189 'PPIx::Regexp::Token::GroupType::Assertion',
190 'PPIx::Regexp::Token::GroupType::Code',
191 'PPIx::Regexp::Token::GroupType::BranchReset',
192 'PPIx::Regexp::Token::GroupType::Subexpression',
193 'PPIx::Regexp::Token::GroupType::Switch',
194 'PPIx::Regexp::Token::GroupType::Script_Run',
195 'PPIx::Regexp::Token::GroupType::Atomic_Script_Run',
196 );
197  
198 # Accept the parenthesis.
199 284 return 1;
200 }
201  
202 # Close parentheses end modifier localization
203 487 ▶ if ( $character eq ')' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($character eq ")") 288 199 487
204 288 $tokenizer->modifier_pop();
205 288 return 1;
206 }
207  
208 # Open curlys are complicated because they may or may not represent
209 # the beginning of a quantifier, depending on what comes before the
210 # close curly. So we set a cookie to monitor the token stream for
211 # interlopers. If all goes well, the right curly will find the
212 # cookie and know it is supposed to be a quantifier.
213 199 ▶ if ( $character eq '{' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($character eq "{") 71 128 199
214  
215 # If the prior token can not be quantified, all this is
216 # unnecessary.
217 71 ▶ $tokenizer->prior_significant_token( 'can_be_quantified' )
Branch: 1 branch
BranchTrueFalseTotal
$tokenizer->prior_significant_token("can_be_quantified") or return 1 61 10 71
218 or return 1;
219  
220 # We make our token now, before setting the cookie. Otherwise
221 # the cookie has to deal with this token.
222 61 my $token = $tokenizer->make_token( 1 );
223  
224 # A cookie for the next '}'.
225 61 my $commas = 0;
226 61 my $allow_digit = 1;
227 115 ▶ $tokenizer->cookie( COOKIE_QUANT, sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
228 115 my ( $tokenizer, $token ) = @_;
229 115 ▶ $token or return 1;
Branch: 1 branch
BranchTrueFalseTotal
$token or return 1 115 0 115
230  
231 # Code for 5.33.6 and after.
232 # We allow {,...}, and we allow space inside and
233 # adjacent to the curlys, and around the comma if
234 # any. But not interior to the numbers.
235 115 ▶ if ( $token->isa( TOKEN_LITERAL ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->isa("PPIx::Regexp::Token::Literal")) 105 10 115
236 105 my $character = $token->content();
237 105 ▶ if ( $character =~ m/ \A \s \z /smx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($character =~ / \A \s \z /msx) 4 101 105
238 # Digits only allowed if the prior
239 # significant was an open curly or a comma.
240 4 $allow_digit = $tokenizer->prior_significant_token(
241 'content' ) =~ m/ \A [{,] \z /smx; # }
242 4 return 1;
243 }
244 101 ▶ $character eq ','
Branch: 1 branch
BranchTrueFalseTotal
$character eq "," and return !$commas++ 33 68 101
245 and return( ! $commas++ );
246 68 ▶ $allow_digit
Branch: 1 branch
BranchTrueFalseTotal
$allow_digit or return 68 0 68
247 or return;
248 68 return $character =~ m/ \A [0-9] \z /smx;
249 }
250  
251 # Since we do not know what is in an interpolation, we
252 # trustingly accept it.
253 10 ▶ if ( $token->isa( 'PPIx::Regexp::Token::Interpolation' )
Branch: 1 branch
BranchTrueFalseTotal
if ($token->isa("PPIx::Regexp::Token::Interpolation")) 5 5 10
254 ) {
255 5 return 1;
256 }
257  
258 5 return;
259 },
260 61 );
261  
262 61 return $token;
263 }
264  
265 # The close curly bracket is a little complicated because if the
266 # cookie posted by the left curly bracket is still around, we are a
267 # quantifier, otherwise not.
268 128 ▶ if ( $character eq '}' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($character eq "}") 63 65 128
269 63 ▶ $tokenizer->cookie( COOKIE_QUANT, undef )
Branch: 1 branch
BranchTrueFalseTotal
$tokenizer->cookie("}", undef) or return 1 50 13 63
270 or return 1;
271 50 ▶ $tokenizer->prior_significant_token( 'class' )->isa( __PACKAGE__ )
Branch: 1 branch
BranchTrueFalseTotal
$tokenizer->prior_significant_token("class")->isa(__PACKAGE__) and return 1 3 47 50
272 and return 1;
273 47 my $token = $tokenizer->make_token( 1 );
274 47 $token->{is_quantifier} = 1;
275 47 return $token;
276 }
277  
278 # The parse rules are different inside a character class, so we set
279 # another cookie. Sigh. If your tool is a hammer ...
280 65 ▶ if ( $character eq '[' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($character eq "[") 53 12 65
281  
282 # Set our cookie. Since it always returns 1, it does not matter
283 # where in the following mess we set it.
284 53 ▶ $tokenizer->cookie( COOKIE_CLASS, sub { return 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
285  
286 # Make our token now, since the easiest place to deal with the
287 # beginning-of-character-class strangeness seems to be right
288 # here.
289 53 my @tokens = $tokenizer->make_token( 1 );
290  
291 # Get the next character, returning tokens if there is none.
292 53 ▶ defined ( $character = $tokenizer->peek() )
Branch: 1 branch
BranchTrueFalseTotal
defined($character = $tokenizer->peek) or return @tokens 53 0 53
293 or return @tokens;
294  
295 # If we have a caret, it is a negation operator. Make its token
296 # and fetch the next character, returning if none.
297 53 ▶ if ( $character eq '^' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($character eq "^") 5 48 53
298 5 push @tokens, $tokenizer->make_token(
299 1, 'PPIx::Regexp::Token::Operator' );
300 5 ▶ defined ( $character = $tokenizer->peek() )
Branch: 1 branch
BranchTrueFalseTotal
defined($character = $tokenizer->peek) or return @tokens 5 0 5
301 or return @tokens;
302 }
303  
304 # If we have a close square at this point, it is not the end of
305 # the class, but just a literal. Make its token.
306 53 ▶ $character eq ']'
Branch: 1 branch
BranchTrueFalseTotal
$character eq "]" and push @tokens, $tokenizer->make_token(1, "PPIx::Regexp::Token::Literal") 2 51 53
307 and push @tokens, $tokenizer->make_token( 1, TOKEN_LITERAL );
308  
309 # Return all tokens made.
310 53 return @tokens;
311 }
312 # per perlop, the metas inside a [] are -]\^$.
313 # per perlop, the metas outside a [] are {}[]()^$.|*+?\
314 # The difference is that {}[().|*+? are not metas in [], but - is.
315  
316 # Close bracket is complicated by the addition of regex sets.
317 # And more complicated by the fact that you can have an
318 # old-style character class inside a regex set. Fortunately they
319 # have not (yet!) permitted nested regex sets.
320 12 ▶ if ( $character eq ']' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($character eq "]") 12 0 12
321  
322 # If we find '])' and COOKIE_REGEX_SET is present, we have a
323 # regex set. We need to delete the cookie and accept both
324 # characters.
325 12 ▶ if ( ( my $accept = $tokenizer->find_regexp(
Branch: 1 branch
BranchTrueFalseTotal
if (my $accept = $tokenizer->find_regexp(qr/ \A []] [)] /msx) and $tokenizer->cookie("])")) 8 4 12
Condition100%3/3
my $accept = $tokenizer->find_regexp(qr/ \A []] [)] /msx) and $tokenizer->cookie("])")
!ll&&!rl&&r
228
MC/DC100%2/2
my $accept = $tokenizer->find_regexp(qr/ \A []] [)] /msx) and $tokenizer->cookie("])")
my $accept = $tokenizer->find_regexp(qr/ \A []] [)] /msx) $tokenizer->cookie("])")
Truth table100%3/3
A and B
A= my $accept = $tokenizer->find_regexp(qr/ \A []] [)] /msx)B= $tokenizer->cookie("])")
ABresult
0X0
100
111
326 # help vim - ( [
327 qr{ \A []] [)] }smx
328 ) )
329 && $tokenizer->cookie( COOKIE_REGEX_SET )
330  
331 ) {
332 8 $tokenizer->cookie( COOKIE_REGEX_SET, undef );
333 8 return $accept;
334 }
335  
336 # Otherwise we assume we're in a bracketed character class,
337 # delete the cookie, and accept the close bracket.
338 4 $tokenizer->cookie( COOKIE_CLASS, undef );
339 4 return 1;
340 }
341  
342 0 return 1;
343 }
344  
345 }
346  
347 # Called by the lexer once it has done its worst to all the tokens.
348 # Called as a method with no arguments. The return is the number of
349 # parse failures discovered when finalizing.
350 9 ▶sub __PPIX_LEXER__finalize {
Subroutine __PPIX_LEXER__finalize: called CC=1 SCAR=0.0
351 1460 my ( $self ) = @_;
352 1460 delete $self->{is_quantifier};
353 1460 return 0;
354 }
355  
356 9 1;
357  
358 __END__
« Regexp/Structure.pm ↑ Summary Regexp/Token/Literal.pm »