Regexp/Constant.pm

filter: Statementstmt 98.0% 151 / 154 Branchbran 60.0% 6 / 10 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 92.7% 51 / 55 Podpod n/a totaltotal 92.5% 212 / 229 CC 8 SCAR 3.0
CC
8
Cov
94%
__subtract
3.1
__space_ship
3.0
BEGIN
2.1
CRAP 1.4
« Regexp/Token/Delimiter.pm ↑ Summary Regexp/Token/CharClass/POSIX/Unknown.pm »
1 package PPIx::Regexp::Constant;
2  
3 # Yes, I know this is horrible style. But I need a separate package for
4 # overloading to work, and I do *NOT* want stray files lying around if I
5 # end up ditching this mess.
6 package PPIx::Regexp::Constant::Inf; ## no critic (ProhibitMultiplePackages)
7  
8 10 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Scalar::Util qw{ refaddr };
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 our $VERSION = '0.092';
17  
18 use overload
19 # Arithmetic
20 10 ▶ '+' => \&__preserve,
Subroutine BEGIN: called CC=1 SCAR=0.0
21 '-' => \&__subtract,
22 '*' => \&__preserve,
23 neg => \&__err_neg_inf,
24 # Comparison
25 '<=>' => \&__space_ship,
26 cmp => \&__cmp,
27 # Conversion
28 '""' => \&__stringify,
29 '0+' => \&__stringify, # For looks_like_number
30 bool => \&__bool,
31 # Catch all
32 nomethod => \&__bug_unimplemented,
33 10 ;
34  
35 {
36 10 my $inf;
37  
38 # Scheduled block because __pos_inf() can be called at compile time.
39 10 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
40 10 $inf = bless( \( my $x = 1 ), __PACKAGE__ );
41 }
42  
43 1 ▶ sub __pos_inf { return $inf }
Subroutine __pos_inf: called CC=1 SCAR=0.0
44  
45 5 ▶ sub __is_inf { return ref $_[0] && refaddr( $_[0] ) == refaddr( $inf ) }
Subroutine __is_inf: called CC=2 SCAR=7.2
Condition66%2/3
ref $_[0] && builtin::refaddr($_[0]) == builtin::refaddr($inf)
!ll&&!rl&&r
203
MC/DC50%1/2
ref $_[0] && builtin::refaddr($_[0]) == builtin::refaddr($inf)
ref $_[0] builtin::refaddr($_[0]) == builtin::refaddr($inf)
Truth table66%2/3
A && B
A= ref $_[0]B= builtin::refaddr($_[0]) == builtin::refaddr($inf)
ABresult
0X0
100
111
46 }
47  
48 1 ▶sub __bool { return 1 }
Subroutine __bool: called CC=1 SCAR=0.0
49  
50 0 ▶sub __bug_unimplemented {
Subroutine __bug_unimplemented: not called CC=1 SCAR=6.9
51 0 confess "Bug - Operation '$_[3]' is unimplemented";
52 }
53  
54 1 ▶sub __cmp {
Subroutine __cmp: called CC=2 SCAR=7.1
55 1 my ( $x, $y, $swap ) = @_;
56 1 ▶ $swap
Branch: 1 branch
BranchTrueFalseTotal
$swap and return "$y" cmp "$x" 0 1 1
57 and return "$y" cmp "$x";
58 1 return "$x" cmp "$y";
59 }
60  
61 0 ▶sub __err_nan {
Subroutine __err_nan: not called CC=1 SCAR=6.9
62 0 croak 'NaN not supported';
63 }
64  
65 0 ▶sub __err_neg_inf {
Subroutine __err_neg_inf: not called CC=1 SCAR=6.9
66 0 croak 'Negative infinity not supported';
67 }
68  
69 # Any operation that does not change the value
70 1 ▶sub __preserve { return $_[0] };
Subroutine __preserve: called CC=1 SCAR=0.0
71  
72 4 ▶sub __space_ship {
Subroutine __space_ship: called CC=3 SCAR=11.0
73 4 my ( undef, $y, $swap ) = @_; # We don't need our invocant
74 # Infinity is equal to itself
75 4 ▶ __is_inf( $y )
Branch: 1 branch
BranchTrueFalseTotal
__is_inf($y) and return 0 3 1 4
76 and return 0;
77 # Any number is less than infinity
78 1 ▶ $swap
Branch: 1 branch
BranchTrueFalseTotal
$swap and return -1 0 1 1
79 and return -1;
80 # Infinity is greater than any number
81 1 return 1;
82 }
83  
84 1 ▶sub __stringify {
Subroutine __stringify: called CC=1 SCAR=0.0
85 1 return 'Inf';
86 }
87  
88 1 ▶sub __subtract {
Subroutine __subtract: called CC=3 SCAR=11.4
89 1 my ( $x, $y, $swap ) = @_;
90 1 ▶ __is_inf( $y )
Branch: 1 branch
BranchTrueFalseTotal
__is_inf($y) and __err_nan() 0 1 1
91 and __err_nan(); # Which croaks
92 1 ▶ $swap
Branch: 1 branch
BranchTrueFalseTotal
$swap and __err_neg_inf() 0 1 1
93 and __err_neg_inf(); # Which croaks
94 1 return $x;
95 }
96  
97 10 1;
98  
99 package PPIx::Regexp::Constant; ## no critic (ProhibitMultiplePackages)
100  
101 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 10 ▶use base qw{ Exporter };
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 # CAVEAT: do not include any other PPIx-Regexp modules in this one, or
107 # you will end up with a circular dependency.
108  
109 10 our $VERSION = '0.085_04';
110  
111 10 our @EXPORT_OK = qw{
112 ARRAY_REF
113 CODE_REF
114 COOKIE_CLASS
115 COOKIE_LOOKAROUND_ASSERTION
116 COOKIE_QUANT
117 COOKIE_QUOTE
118 COOKIE_REGEX_SET
119 FALSE
120 HASH_REF
121 INFINITY
122 LITERAL_LEFT_CURLY_ALLOWED
123 LITERAL_LEFT_CURLY_REMOVED_PHASE_1
124 LITERAL_LEFT_CURLY_REMOVED_PHASE_2
125 LITERAL_LEFT_CURLY_REMOVED_PHASE_3
126 LOCATION_LINE
127 LOCATION_CHARACTER
128 LOCATION_COLUMN
129 LOCATION_LOGICAL_LINE
130 LOCATION_LOGICAL_FILE
131 MINIMUM_PERL
132 MODIFIER_GROUP_MATCH_SEMANTICS
133 MSG_LOOK_BEHIND_TOO_LONG
134 MSG_PROHIBITED_BY_STRICT
135 NODE_UNKNOWN
136 RE_CAPTURE_NAME
137 REGEXP_REF
138 SCALAR_REF
139 STRUCTURE_UNKNOWN
140 SUFFICIENT_UTF8_SUPPORT_FOR_WEIRD_DELIMITERS
141 TOKEN_LITERAL
142 TOKEN_UNKNOWN
143 TRUE
144 VARIABLE_LENGTH_LOOK_BEHIND_INTRODUCED
145 @CARP_NOT
146 };
147  
148 10 our @CARP_NOT = qw{
149 PPIx::Regexp
150 PPIx::Regexp::Constant
151 PPIx::Regexp::Dumper
152 PPIx::Regexp::Element
153 PPIx::Regexp::Lexer
154 PPIx::Regexp::Node
155 PPIx::Regexp::Node::Range
156 PPIx::Regexp::Node::Unknown
157 PPIx::Regexp::Structure
158 PPIx::Regexp::Structure::Assertion
159 PPIx::Regexp::Structure::Atomic_Script_Run
160 PPIx::Regexp::Structure::BranchReset
161 PPIx::Regexp::Structure::Capture
162 PPIx::Regexp::Structure::CharClass
163 PPIx::Regexp::Structure::Code
164 PPIx::Regexp::Structure::Main
165 PPIx::Regexp::Structure::Modifier
166 PPIx::Regexp::Structure::NamedCapture
167 PPIx::Regexp::Structure::Quantifier
168 PPIx::Regexp::Structure::RegexSet
169 PPIx::Regexp::Structure::Regexp
170 PPIx::Regexp::Structure::Replacement
171 PPIx::Regexp::Structure::Script_Run
172 PPIx::Regexp::Structure::Subexpression
173 PPIx::Regexp::Structure::Switch
174 PPIx::Regexp::Structure::Unknown
175 PPIx::Regexp::Support
176 PPIx::Regexp::Token
177 PPIx::Regexp::Token::Assertion
178 PPIx::Regexp::Token::Backreference
179 PPIx::Regexp::Token::Backtrack
180 PPIx::Regexp::Token::CharClass
181 PPIx::Regexp::Token::CharClass::POSIX
182 PPIx::Regexp::Token::CharClass::POSIX::Unknown
183 PPIx::Regexp::Token::CharClass::Simple
184 PPIx::Regexp::Token::Code
185 PPIx::Regexp::Token::Comment
186 PPIx::Regexp::Token::Condition
187 PPIx::Regexp::Token::Control
188 PPIx::Regexp::Token::Delimiter
189 PPIx::Regexp::Token::Greediness
190 PPIx::Regexp::Token::GroupType
191 PPIx::Regexp::Token::GroupType::Assertion
192 PPIx::Regexp::Token::GroupType::Atomic_Script_Run
193 PPIx::Regexp::Token::GroupType::BranchReset
194 PPIx::Regexp::Token::GroupType::Code
195 PPIx::Regexp::Token::GroupType::Modifier
196 PPIx::Regexp::Token::GroupType::NamedCapture
197 PPIx::Regexp::Token::GroupType::Script_Run
198 PPIx::Regexp::Token::GroupType::Subexpression
199 PPIx::Regexp::Token::GroupType::Switch
200 PPIx::Regexp::Token::Interpolation
201 PPIx::Regexp::Token::Literal
202 PPIx::Regexp::Token::Modifier
203 PPIx::Regexp::Token::NoOp
204 PPIx::Regexp::Token::Operator
205 PPIx::Regexp::Token::Quantifier
206 PPIx::Regexp::Token::Recursion
207 PPIx::Regexp::Token::Reference
208 PPIx::Regexp::Token::Structure
209 PPIx::Regexp::Token::Unknown
210 PPIx::Regexp::Token::Unmatched
211 PPIx::Regexp::Token::Whitespace
212 PPIx::Regexp::Tokenizer
213 PPIx::Regexp::Util
214 };
215  
216 10 ▶use constant COOKIE_CLASS => ']';
Subroutine BEGIN: called CC=1 SCAR=0.0
217 10 ▶use constant COOKIE_QUANT => '}';
Subroutine BEGIN: called CC=1 SCAR=0.0
218 10 ▶use constant COOKIE_QUOTE => '\\E';
Subroutine BEGIN: called CC=1 SCAR=0.0
219 10 ▶use constant COOKIE_REGEX_SET => '])';
Subroutine BEGIN: called CC=1 SCAR=0.0
220 10 ▶use constant COOKIE_LOOKAROUND_ASSERTION => 'lookaround';
Subroutine BEGIN: called CC=1 SCAR=0.0
221  
222 10 ▶use constant FALSE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
223 10 ▶use constant TRUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
224  
225 # This hack is because it appears that Strawberry Perl evaluates 0 +
226 # 'inf' as zero under Perl 5.12.3 and below. But the real problem is
227 # from a pure portability standpoint I can not count on IEEE 754 being
228 # in use. So if 0 + 'Inf' (documented in perldata) is zero or an error I
229 # fall back to an object that mimics its behavior to the extent I think
230 # I need.
231 # NOTE that the only way I have to test the ::Inf object is to comment
232 # out the eval{} code. This is ugly, but I can not think of anything
233 # better.
234 10 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.4
235 10 local $@ = undef;
236 10 require constant;
237 constant->import( INFINITY =>
238 10 ▶ eval { 0 + 'Inf' } ||
Condition33%1/3
eval { do { 9**9**9 } } || 'PPIx::Regexp::Constant::Inf'->__pos_inf
l!l&&r!l&&!r
1000
MC/DC0%0/2
eval { do { 9**9**9 } } || 'PPIx::Regexp::Constant::Inf'->__pos_inf
eval { do { 9**9**9 } } 'PPIx::Regexp::Constant::Inf'->__pos_inf
Truth table33%1/3
A || B
A= eval { do { 9**9**9 } }B= 'PPIx::Regexp::Constant::Inf'->__pos_inf
ABresult
1X1
011
000
239 PPIx::Regexp::Constant::Inf->__pos_inf() );
240 }
241  
242 10 ▶use constant ARRAY_REF => ref [];
Subroutine BEGIN: called CC=1 SCAR=0.0
243 10 ▶use constant CODE_REF => ref sub {};
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: not called CC=1 SCAR=0.0
244 10 ▶use constant HASH_REF => ref {};
Subroutine BEGIN: called CC=1 SCAR=0.0
245 10 ▶use constant REGEXP_REF => ref qr{};
Subroutine BEGIN: called CC=1 SCAR=0.0
246 10 ▶use constant SCALAR_REF => ref \0;
Subroutine BEGIN: called CC=1 SCAR=0.0
247  
248 # In the cases where an unescaped literal left curly 'could not' be a
249 # quantifier, they are allowed. At least, that was the original idea.
250 # But read on.
251 10 ▶use constant LITERAL_LEFT_CURLY_ALLOWED => undef;
Subroutine BEGIN: called CC=1 SCAR=0.0
252  
253 # 'Most' unescaped literal left curlys were removed in 5.26.
254 10 ▶use constant LITERAL_LEFT_CURLY_REMOVED_PHASE_1 => '5.025001';
Subroutine BEGIN: called CC=1 SCAR=0.0
255  
256 # Unescaped literal left curlys after literals and certain other
257 # elements are scheduled to be removed in 5.30.
258 10 ▶use constant LITERAL_LEFT_CURLY_REMOVED_PHASE_2 => undef; # x{ 5.30
Subroutine BEGIN: called CC=1 SCAR=0.0
259  
260 # In 5.27.8 it was decided that unescaped literal left curlys after an
261 # open paren will be removed in 5.32. This does not include the case
262 # where the entire regex is delimited by parens -- they are still legal
263 # there.
264 10 ▶use constant LITERAL_LEFT_CURLY_REMOVED_PHASE_3 => undef; # ({ 5.32
Subroutine BEGIN: called CC=1 SCAR=0.0
265  
266 # Location constants. Must align with PPI
267 10 ▶use constant LOCATION_LINE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
268 10 ▶use constant LOCATION_CHARACTER => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
269 10 ▶use constant LOCATION_COLUMN => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
270 10 ▶use constant LOCATION_LOGICAL_LINE => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
271 10 ▶use constant LOCATION_LOGICAL_FILE => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
272  
273 10 ▶use constant MINIMUM_PERL => '5.000';
Subroutine BEGIN: called CC=1 SCAR=0.0
274  
275 10 ▶use constant MODIFIER_GROUP_MATCH_SEMANTICS => 'match_semantics';
Subroutine BEGIN: called CC=1 SCAR=0.0
276  
277 10 ▶use constant MSG_LOOK_BEHIND_TOO_LONG =>
Subroutine BEGIN: called CC=1 SCAR=0.0
278 10 'Lookbehind longer than 255 not implemented';
279 10 ▶use constant MSG_PROHIBITED_BY_STRICT =>
Subroutine BEGIN: called CC=1 SCAR=0.0
280 10 q<prohibited by "use re 'strict'">;
281  
282 10 ▶use constant NODE_UNKNOWN => 'PPIx::Regexp::Node::Unknown';
Subroutine BEGIN: called CC=1 SCAR=0.0
283  
284 # The perlre for Perl 5.010 says:
285 #
286 # Currently NAME is restricted to simple identifiers only. In
287 # other words, it must match "/^[_A-Za-z][_A-Za-z0-9]*\z/" or
288 # its Unicode extension (see utf8), though it isn't extended by
289 # the locale (see perllocale).
290  
291 10 ▶use constant RE_CAPTURE_NAME => ' [_[:alpha:]] \w* ';
Subroutine BEGIN: called CC=1 SCAR=0.0
292  
293 10 ▶use constant STRUCTURE_UNKNOWN => 'PPIx::Regexp::Structure::Unknown';
Subroutine BEGIN: called CC=1 SCAR=0.0
294  
295 10 ▶use constant SUFFICIENT_UTF8_SUPPORT_FOR_WEIRD_DELIMITERS => $] ge '5.008003';
Subroutine BEGIN: called CC=1 SCAR=0.0
296  
297 10 ▶use constant TOKEN_LITERAL => 'PPIx::Regexp::Token::Literal';
Subroutine BEGIN: called CC=1 SCAR=0.0
298 10 ▶use constant TOKEN_UNKNOWN => 'PPIx::Regexp::Token::Unknown';
Subroutine BEGIN: called CC=1 SCAR=0.0
299  
300 10 ▶use constant VARIABLE_LENGTH_LOOK_BEHIND_INTRODUCED => '5.029009';
Subroutine BEGIN: called CC=1 SCAR=0.0
301  
302 10 1;
303  
304 __END__
« Regexp/Token/Delimiter.pm ↑ Summary Regexp/Token/CharClass/POSIX/Unknown.pm »