Regexp/Token/CharClass/Simple.pm

filter: Statementstmt 100% 82 / 82 Branchbran 85.2% 29 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 96.2% 129 / 134 CC 18 SCAR 14.9
CC
18
Cov
96%
__PPIX_TOKENIZER__regexp
9.1
perl_version_introduced
5.0
explain
4.0
CRAP 4.5
« Regexp/Structure/BranchReset.pm ↑ Summary Regexp.pm »
1 =head1 NAME
2  
3 PPIx::Regexp::Token::CharClass::Simple - This class represents a simple character class
4  
5 =head1 SYNOPSIS
6  
7 use PPIx::Regexp::Dumper;
8 PPIx::Regexp::Dumper->new( 'qr{\w}smx' )
9 ->print();
10  
11 =head1 INHERITANCE
12  
13 C<PPIx::Regexp::Token::CharClass::Simple> is a
14 L<PPIx::Regexp::Token::CharClass|PPIx::Regexp::Token::CharClass>.
15  
16 C<PPIx::Regexp::Token::CharClass::Simple> has no descendants.
17  
18 =head1 DESCRIPTION
19  
20 This class represents one of the simple character classes that can occur
21 anywhere in a regular expression. This includes not only the truly
22 simple things like \w, but also Unicode properties, including properties
23 with wildcard values.
24  
25 =head1 METHODS
26  
27 This class provides no public methods beyond those provided by its
28 superclass.
29  
30 =cut
31  
32 package PPIx::Regexp::Token::CharClass::Simple;
33  
34 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 9 ▶use base qw{ PPIx::Regexp::Token::CharClass };
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 9 ▶use PPIx::Regexp::Constant qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
40 COOKIE_CLASS
41 LITERAL_LEFT_CURLY_REMOVED_PHASE_1
42 LITERAL_LEFT_CURLY_REMOVED_PHASE_2
43 MINIMUM_PERL
44 TOKEN_LITERAL
45 TOKEN_UNKNOWN
46 @CARP_NOT
47 9 };
48  
49 9 our $VERSION = '0.092';
50  
51 9 ▶use constant UNICODE_PROPERTY_LITERAL_VALUE => qr/
Subroutine BEGIN: called CC=1 SCAR=0.0
52 \{ \s* \^? \w [\w:=\s-]* \} |
53 [CLMNPSZ] # perluniprops for 5.26.1
54 9 /smx;
55 9 ▶use constant UNICODE_PROPERTY_LITERAL =>
Subroutine BEGIN: called CC=1 SCAR=0.0
56 qr/ \A \\ [Pp] (?:
57 9 @{[ UNICODE_PROPERTY_LITERAL_VALUE ]}
58 9 ) /smx;
59  
60 # CAVEAT: The following regular expression, despite its name, matches
61 # ALL unicode property values. To actually match a wildcard property you
62 # must first eliminate anything that matches UNICODE_PROPERTY_LITERAL or
63 # UNICODE_PROPERTY_NAME_MATCH
64 9 ▶use constant UNICODE_PROPERTY_WILDCARD =>
Subroutine BEGIN: called CC=1 SCAR=0.0
65 9 qr/ \A \\ [Pp] \{ \s* [\w\s-]+ [:=] [^}]+ \} /smx;
66  
67 9 ▶use constant UNICODE_PROPERTY_NAME_MATCH =>
Subroutine BEGIN: called CC=1 SCAR=0.0
68 9 qr< \A \\ [Pp] \{ \s* na (?: me? )? [:=] / [^/]+ / \} >smx;
69  
70 9 ▶use constant UNICODE_PROPERTY =>
Subroutine BEGIN: called CC=1 SCAR=0.0
71 9 qr/ @{[ UNICODE_PROPERTY_LITERAL ]} |
72 9 @{[ UNICODE_PROPERTY_NAME_MATCH ]} |
73 9 @{[ UNICODE_PROPERTY_WILDCARD ]} /smx;
74  
75 {
76  
77 9 my %kind_of_match = (
78 p => 'with',
79 P => 'without',
80 );
81  
82 9 my %explanation = (
83 '.' => 'Match any character',
84 '\\C' => 'Match a single octet (removed in 5.23.0)',
85 '\\D' => 'Match any character but a decimal digit',
86 '\\H' => 'Match a non-horizontal-white-space character',
87 '\\N' => 'Match any character but a new-line character',
88 '\\R' => 'Match a generic new-line character',
89 '\\S' => 'Match non-white-space character',
90 '\\V' => 'Match a non-vertical-white-space character',
91 '\\W' => 'Match non-word character',
92 '\\X' => 'Match a Unicode extended grapheme cluster',
93 '\\d' => 'Match decimal digit',
94 '\\h' => 'Match a horizontal-white-space character',
95 '\\s' => 'Match white-space character',
96 '\\v' => 'Match a vertical-white-space character',
97 '\\w' => 'Match word character',
98 );
99  
100 15 ▶ sub __explanation {
Subroutine __explanation: called CC=1 SCAR=0.0
101 15 return \%explanation;
102 }
103  
104 18 ▶ sub explain {
Subroutine explain: called CC=4 SCAR=13.9
Pod documented
105 18 my ( $self ) = @_;
106 18 ▶ if ( $self->content() =~ m/ \A \\ ( [Pp] ) ( [{] .* [}] | . ) \z /smx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->content =~ / \A \\ ( [Pp] ) ( [{] .* [}] | . ) \z /msx) 3 15 18
107 3 my ( $kind, $prop ) = ( $1, $2 );
108  
109 3 my $literal = ( $prop =~
110 3 m/ \A @{[ UNICODE_PROPERTY_LITERAL_VALUE ]} \z /smx );
111  
112 3 ▶ if ( 1 < length $prop ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 < length $prop) 3 0 3
113 3 $prop =~ s/ \A [{] //smx;
114 3 $prop =~ s/ [}] \z //smx;
115 }
116  
117 $literal
118 and return sprintf
119 q<Match character %s Unicode or custom property '%s'>,
120 3 ▶ $kind_of_match{$kind}, $prop;
Branch: 1 branch
BranchTrueFalseTotal
$literal and return sprintf(q[Match character %s Unicode or custom property '%s'], $kind_of_match{$kind}, $prop) 2 1 3
121  
122 return sprintf
123 q<Match character %s Unicode wildcard property '%s'>,
124 1 $kind_of_match{$kind}, $prop;
125 }
126 15 return $self->SUPER::explain();
127 }
128  
129 }
130  
131 ##=head2 is_case_sensitive
132 ##
133 ##This override of the superclass method returns true for Unicode
134 ##properties that specify case, and false (but defined) for all
135 ##other character classes.
136 ##
137 ##The classes that specify case are documented in
138 ##L<perluniprops|/perluniprops>.
139 ##
140 ##B<Known bug:> This method returns false (but defined) for user-defined
141 ##Unicode properties. It should return C<undef>. This bug B<may> be fixed
142 ##if I find a way to identify all system-defined Unicode properties.
143 ##
144 ##=cut
145 ##
146 ##sub is_case_sensitive {
147 ## my ( $self ) = @_;
148 ## exists $self->{is_case_sensitive}
149 ## and return $self->{is_case_sensitive};
150 ## return ( $self->{is_case_sensitive} = $self->_is_case_sensitive() );
151 ##}
152  
153 ##{
154 ## my %case_sensitive = map { $_ => 1 } qw{
155 ## generalcategory=lowercaseletter generalcategory=ll
156 ## gc=lowercaseletter gc=ll
157 ## generalcategory=titlecaseletter generalcategory=lt
158 ## gc=titlecaseletter gc=lt
159 ## generalcategory=uppercaseletter generalcategory=lu
160 ## gc=uppercaseletter gc=lu
161 ## lowercaseletter lowercase lower ll
162 ## titlecaseletter titlecase title lt
163 ## uppercaseletter uppercase upper lu
164 ## lowercase=y lower=y lowercase=n lower=n
165 ## titlecase=y title=y titlecase=n title=n
166 ## uppercase=y upper=y uppercase=n upper=n
167 ## };
168 ##
169 ## sub _is_case_sensitive {
170 ## my ( $self ) = @_;
171 ## my $content = $self->content();
172 ## $content =~ m/ \A \\ p [{] ( .* ) [}] /smxi
173 ## or return 0;
174 ## $content = lc $1;
175 ## $content =~ s/ \A ^ //smx;
176 ## $content =~ s/ [\s_-] //smxg;
177 ## $content =~ s/ \A is //smx;
178 ## $content =~ s/ : /=/smxg;
179 ## $content =~ s/ = (?: yes | t | true ) \b /=y/smxg;
180 ## $content =~ s/ = (?: no | f | false ) \b /=n/smxg;
181 ## return $case_sensitive{$content} || 0;
182 ## }
183 ##
184 ##}
185  
186 {
187  
188 9 my %introduced = (
189 '\\h' => '5.009005', # Before this, parsed as 'h'
190 '\\v' => '5.009005', # Before this, parsed as 'v'
191 '\\H' => '5.009005', # Before this, parsed as 'H'
192 '\\N' => '5.011', # Before this, an error.
193 '\\V' => '5.009005', # Before this, parsed as 'V'
194 '\\R' => '5.009005',
195 '\\C' => '5.006',
196 '\\X' => '5.006',
197 );
198  
199 31 ▶ sub perl_version_introduced {
Subroutine perl_version_introduced: called CC=5 SCAR=16.1
Pod documented
200 31 my ( $self ) = @_;
201 31 my $content = $self->content();
202 31 ▶ if ( defined( my $minver = $introduced{$content} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $minver = $introduced{$content})) 9 22 31
203 9 return $minver;
204 }
205 # I must have read perl5113delta and thought this
206 # represented the change they were talking about, but I sure
207 # don't see it now. So, until things become clearer ...
208 # $content =~ m/ \G .*? [\s=-] /smxgc
209 # and return '5.011003';
210 22 ▶ $content =~ UNICODE_PROPERTY_LITERAL
Branch: 1 branch
BranchTrueFalseTotal
$content =~ / \A \\ [Pp] (?: (?^msx: \{ \s* \^? \w [\w:=\s-]* \} | [CLMNPSZ] # perluniprops for 5.26.1 ) ) /msx and return "5.006001" 3 19 22
211 and return '5.006001';
212 19 ▶ $content =~ UNICODE_PROPERTY_NAME_MATCH
Branch: 1 branch
BranchTrueFalseTotal
$content =~ m[ \A \\ [Pp] \{ \s* na (?: me? )? [:=] / [^/]+ / \} ]msx and return "5.031010" 0 19 19
213 and return '5.031010';
214 19 ▶ $content =~ UNICODE_PROPERTY_WILDCARD
Branch: 1 branch
BranchTrueFalseTotal
$content =~ / \A \\ [Pp] \{ \s* [\w\s-]+ [:=] [^}]+ \} /msx and return "5.029009" 1 18 19
215 and return '5.029009';
216 18 return MINIMUM_PERL;
217 }
218  
219 }
220  
221 {
222 9 my %removed = (
223 '\\C' => '5.023', # Before this, matched an octet
224 );
225  
226 31 ▶ sub perl_version_removed {
Subroutine perl_version_removed: called CC=1 SCAR=0.0
Pod documented
227 31 my ( $self ) = @_;
228 31 return $removed{ $self->content() };
229 }
230 }
231  
232 # This is one of the larger complications of
233 # https://rt.perl.org/Public/Bug/Display.html?id=128213
234 # where it transpired that un-escaped literal left curlies were not
235 # giving warnings/errors in /.{/, /\p{...}{/, and /\P{...}{/, but were
236 # for all the others that bin into this class (e.g. /\s{/).
237 # Note that the perldelta for 5.25.1 and 5.26.0 do not acknowledge tha
238 # phased deprecation, and pretend that everything was done on the phase
239 # 1 schedule. This appears to be deliberate per
240 # https://rt.perl.org/Ticket/Display.html?id=131352
241 9 ▶sub __following_literal_left_curly_disallowed_in {
Subroutine __following_literal_left_curly_disallowed_in: called CC=3 SCAR=11.0
242 4 my ( $self ) = @_;
243 4 ▶ q<.> eq ( my $content = $self->content() )
Branch: 1 branch
BranchTrueFalseTotal
"." eq (my $content = $self->content) and return undef 1 3 4
244 and return LITERAL_LEFT_CURLY_REMOVED_PHASE_2;
245 3 ▶ $content =~ m/ \A \\ p \{ /smxi
Branch: 1 branch
BranchTrueFalseTotal
$content =~ / \A \\ p \{ /imsx and return undef 2 1 3
246 and return LITERAL_LEFT_CURLY_REMOVED_PHASE_2;
247 1 return LITERAL_LEFT_CURLY_REMOVED_PHASE_1;
248 }
249  
250 1286 ▶sub __PPIX_TOKENIZER__regexp {
Subroutine __PPIX_TOKENIZER__regexp: called CC=9 SCAR=22.0
251 1286 my ( undef, $tokenizer, $character ) = @_;
252  
253 1286 my $in_class = $tokenizer->cookie( COOKIE_CLASS );
254  
255 1286 ▶ if ( $character eq '.' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($character eq ".") 18 1268 1286
256 18 ▶ $in_class
Branch: 1 branch
BranchTrueFalseTotal
$in_class and return $tokenizer->make_token(1, "PPIx::Regexp::Token::Literal") 0 18 18
257 and return $tokenizer->make_token( 1, TOKEN_LITERAL );
258 18 return 1;
259 }
260  
261 1268 ▶ if ( my $accept = $tokenizer->find_regexp(
Branch: 1 branch
BranchTrueFalseTotal
if (my $accept = $tokenizer->find_regexp(qr/ \A \\ [wWsSdDvVhHXRNC] /msx)) 83 1185 1268
262 qr{ \A \\ [wWsSdDvVhHXRNC] }smx
263 ) ) {
264 83 ▶ if ( $in_class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_class) 9 74 83
265 9 my $match = $tokenizer->match();
266 # As of Perl 5.11.5, [\N] is a fatal error.
267 9 ▶ '\\N' eq $match
Branch: 1 branch
BranchTrueFalseTotal
"\\N" eq $match and return $tokenizer->make_token($accept, "PPIx::Regexp::Token::Unknown", {"error", "\\N invalid inside character class"}) 0 9 9
268 and return $tokenizer->make_token(
269 $accept, TOKEN_UNKNOWN, {
270 error => '\\N invalid inside character class',
271 },
272 );
273 # \R is not recognized inside a character class. It
274 # eventually ends up as a literal.
275 9 ▶ '\\R' eq $match and return;
Branch: 1 branch
BranchTrueFalseTotal
"\\R" eq $match and return 0 9 9
276 }
277 83 return $accept;
278 }
279  
280 1185 ▶ if ( my $accept = $tokenizer->find_regexp( UNICODE_PROPERTY ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $accept = $tokenizer->find_regexp(qr/ (?^msx: \A \\ [Pp] (?: (?^msx: \{ \s* \^? \w [\w:=\s-]* \} | [CLMNPSZ] # perluniprops for 5.26.1 ) ) ) | (?^msx: \A \\ [Pp] \{ \s* na (?: me? )? [:=] \/ [^\/]+ \/ \} ) | (?^msx: \A \\ [Pp] \{ \s* [\w\s-]+ [:=] [^}]+ \} ) /msx)) 13 1172 1185
281 13 return $accept;
282 }
283  
284 1172 ▶ if ( my $accept = $tokenizer->find_regexp( qr< \A \\ p [{] \s* [}] >smx )
Branch: 1 branch
BranchTrueFalseTotal
if (my $accept = $tokenizer->find_regexp(qr/ \A \\ p [{] \s* [}] /msx)) 1 1171 1172
285 ) {
286 1 return $tokenizer->make_token( $accept, TOKEN_UNKNOWN, {
287 error => 'Empty \\p{} is an error',
288 },
289 );
290 }
291  
292 1171 return;
293 }
294  
295 9 1;
296  
297 __END__
« Regexp/Structure/BranchReset.pm ↑ Summary Regexp.pm »