File Coverage

blib/lib/PPIx/Regexp/Constant.pm
Criterion Covered Total %
statement 142 145 97.9
branch 6 10 60.0
condition 3 6 50.0
subroutine 51 54 94.4
pod n/a
total 202 215 93.9


line stmt bran cond sub pod time code
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     10   1243 use 5.006;
  10         47  
9              
10 10     10   62 use strict;
  10         21  
  10         248  
11 10     10   58 use warnings;
  10         26  
  10         362  
12              
13 10     10   56 use Carp;
  10         18  
  10         726  
14 10     10   60 use Scalar::Util qw{ refaddr };
  10         19  
  10         1980  
15              
16             our $VERSION = '0.088';
17              
18             use overload
19             # Arithmetic
20 10         131 '+' => \&__preserve,
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     10   7528 ;
  10         11524  
34              
35             {
36             my $inf;
37              
38             # Scheduled block because __pos_inf() can be called at compile time.
39             BEGIN {
40 10     10   5621 $inf = bless( \( my $x = 1 ), __PACKAGE__ );
41             }
42              
43 1     1   64771 sub __pos_inf { return $inf }
44              
45 5   66 5   63 sub __is_inf { return ref $_[0] && refaddr( $_[0] ) == refaddr( $inf ) }
46             }
47              
48 1     1   287 sub __bool { return 1 }
49              
50             sub __bug_unimplemented {
51 0     0   0 confess "Bug - Operation '$_[3]' is unimplemented";
52             }
53              
54             sub __cmp {
55 1     1   641 my ( $x, $y, $swap ) = @_;
56 1 50       4 $swap
57             and return "$y" cmp "$x";
58 1         4 return "$x" cmp "$y";
59             }
60              
61             sub __err_nan {
62 0     0   0 croak 'NaN not supported';
63             }
64              
65             sub __err_neg_inf {
66 0     0   0 croak 'Negative infinity not supported';
67             }
68              
69             # Any operation that does not change the value
70 1     1   21 sub __preserve { return $_[0] };
71              
72             sub __space_ship {
73 4     4   1831 my ( undef, $y, $swap ) = @_; # We don't need our invocant
74             # Infinity is equal to itself
75 4 100       12 __is_inf( $y )
76             and return 0;
77             # Any number is less than infinity
78 1 50       7 $swap
79             and return -1;
80             # Infinity is greater than any number
81 1         15 return 1;
82             }
83              
84             sub __stringify {
85 1     1   13 return 'Inf';
86             }
87              
88             sub __subtract {
89 1     1   7 my ( $x, $y, $swap ) = @_;
90 1 50       3 __is_inf( $y )
91             and __err_nan(); # Which croaks
92 1 50       5 $swap
93             and __err_neg_inf(); # Which croaks
94 1         1407 return $x;
95             }
96              
97             1;
98              
99             package PPIx::Regexp::Constant; ## no critic (ProhibitMultiplePackages)
100              
101 10     10   79 use strict;
  10         18  
  10         244  
102 10     10   48 use warnings;
  10         18  
  10         392  
103              
104 10     10   57 use base qw{ Exporter };
  10         25  
  10         2808  
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             our $VERSION = '0.085_04';
110              
111             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             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     10   89 use constant COOKIE_CLASS => ']';
  10         21  
  10         963  
217 10     10   71 use constant COOKIE_QUANT => '}';
  10         35  
  10         562  
218 10     10   62 use constant COOKIE_QUOTE => '\\E';
  10         19  
  10         548  
219 10     10   67 use constant COOKIE_REGEX_SET => '])';
  10         18  
  10         568  
220 10     10   57 use constant COOKIE_LOOKAROUND_ASSERTION => 'lookaround';
  10         18  
  10         494  
221              
222 10     10   55 use constant FALSE => 0;
  10         22  
  10         527  
223 10     10   68 use constant TRUE => 1;
  10         31  
  10         1052  
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             BEGIN {
235 10     10   39 local $@ = undef;
236 10         63 require constant;
237             constant->import( INFINITY =>
238 10   33     22 eval { 0 + 'Inf' } ||
239             PPIx::Regexp::Constant::Inf->__pos_inf() );
240             }
241              
242 10     10   1750 use constant ARRAY_REF => ref [];
  10         21  
  10         2372  
243 10     10   69 use constant CODE_REF => ref sub {};
  10         37  
  10         584  
244 10     10   56 use constant HASH_REF => ref {};
  10         33  
  10         623  
245 10     10   69 use constant REGEXP_REF => ref qr{};
  10         21  
  10         604  
246 10     10   78 use constant SCALAR_REF => ref \0;
  10         25  
  10         539  
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     10   89 use constant LITERAL_LEFT_CURLY_ALLOWED => undef;
  10         16  
  10         500  
252              
253             # 'Most' unescaped literal left curlys were removed in 5.26.
254 10     10   69 use constant LITERAL_LEFT_CURLY_REMOVED_PHASE_1 => '5.025001';
  10         17  
  10         512  
255              
256             # Unescaped literal left curlys after literals and certain other
257             # elements are scheduled to be removed in 5.30.
258 10     10   60 use constant LITERAL_LEFT_CURLY_REMOVED_PHASE_2 => undef; # x{ 5.30
  10         22  
  10         541  
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     10   57 use constant LITERAL_LEFT_CURLY_REMOVED_PHASE_3 => undef; # ({ 5.32
  10         20  
  10         506  
265              
266             # Location constants. Must align with PPI
267 10     10   78 use constant LOCATION_LINE => 0;
  10         16  
  10         519  
268 10     10   60 use constant LOCATION_CHARACTER => 1;
  10         38  
  10         492  
269 10     10   62 use constant LOCATION_COLUMN => 2;
  10         19  
  10         482  
270 10     10   54 use constant LOCATION_LOGICAL_LINE => 3;
  10         27  
  10         479  
271 10     10   80 use constant LOCATION_LOGICAL_FILE => 4;
  10         24  
  10         471  
272              
273 10     10   54 use constant MINIMUM_PERL => '5.000';
  10         38  
  10         552  
274              
275 10     10   60 use constant MODIFIER_GROUP_MATCH_SEMANTICS => 'match_semantics';
  10         19  
  10         519  
276              
277 10         511 use constant MSG_LOOK_BEHIND_TOO_LONG =>
278 10     10   57 'Lookbehind longer than 255 not implemented';
  10         26  
279 10         503 use constant MSG_PROHIBITED_BY_STRICT =>
280 10     10   56 q;
  10         41  
281              
282 10     10   55 use constant NODE_UNKNOWN => 'PPIx::Regexp::Node::Unknown';
  10         27  
  10         681  
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     10   63 use constant RE_CAPTURE_NAME => ' [_[:alpha:]] \w* ';
  10         25  
  10         558  
292              
293 10     10   61 use constant STRUCTURE_UNKNOWN => 'PPIx::Regexp::Structure::Unknown';
  10         28  
  10         543  
294              
295 10     10   64 use constant SUFFICIENT_UTF8_SUPPORT_FOR_WEIRD_DELIMITERS => $] ge '5.008003';
  10         21  
  10         545  
296              
297 10     10   58 use constant TOKEN_LITERAL => 'PPIx::Regexp::Token::Literal';
  10         27  
  10         475  
298 10     10   56 use constant TOKEN_UNKNOWN => 'PPIx::Regexp::Token::Unknown';
  10         28  
  10         505  
299              
300 10     10   56 use constant VARIABLE_LENGTH_LOOK_BEHIND_INTRODUCED => '5.029009';
  10         21  
  10         624  
301              
302             1;
303              
304             __END__