File Coverage

blib/lib/PPIx/Regexp/Token/Condition.pm
Criterion Covered Total %
statement 32 32 100.0
branch 12 12 100.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 54 54 100.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             PPIx::Regexp::Token::Condition - Represent the condition of a switch
4              
5             =head1 SYNOPSIS
6              
7             use PPIx::Regexp::Dumper;
8             PPIx::Regexp::Dumper->new( 'qr{(?(1)foo|bar)}smx' )
9             ->print();
10              
11             =head1 INHERITANCE
12              
13             C is a
14             L.
15              
16             C has no descendants.
17              
18             =head1 DESCRIPTION
19              
20             This class represents the condition portion of a switch or conditional
21             expression, provided that condition is reasonably represented as a
22             token.
23              
24             =head1 METHODS
25              
26             This class provides no public methods beyond those provided by its
27             superclass.
28              
29             =cut
30              
31             package PPIx::Regexp::Token::Condition;
32              
33 9     9   62 use strict;
  9         28  
  9         263  
34 9     9   43 use warnings;
  9         17  
  9         246  
35              
36 9     9   63 use base qw{ PPIx::Regexp::Token::Reference };
  9         18  
  9         832  
37              
38 9     9   75 use PPIx::Regexp::Constant qw{ RE_CAPTURE_NAME @CARP_NOT };
  9         16  
  9         5535  
39              
40             our $VERSION = '0.087_01';
41              
42             {
43              
44             my %explanation = (
45             '(DEFINE)' => 'Define a group to be recursed into',
46             '(R)' => 'True if recursing',
47             );
48              
49             sub explain {
50 6     6 1 14 my ( $self ) = @_;
51 6         20 my $content = $self->content();
52 6 100       21 if ( defined( my $expl = $explanation{$content} ) ) {
53 2         6 return $expl;
54             }
55 4 100       18 if ( $content =~ m/ \A [(] R /smx ) { # )
56 2 100       8 $self->is_named()
57             and return sprintf
58             q,
59             $self->name();
60 1         6 return sprintf
61             q,
62             $self->absolute();
63             }
64             $self->is_named()
65 2 100       13 and return sprintf
66             q,
67             $self->name();
68 1         6 return sprintf
69             q,
70             $self->absolute();
71             }
72              
73             }
74              
75             sub perl_version_introduced {
76 19     19 1 2771 my ( $self ) = @_;
77 19 100       85 $self->content() =~ m/ \A [(] [0-9]+ [)] \z /smx
78             and return '5.005';
79 13         47 return '5.009005';
80             }
81              
82             my @recognize = (
83             [ qr{ \A \( (?: ( [0-9]+ ) | R ( [0-9]+ ) ) \) }smx,
84             { is_named => 0 } ],
85             [ qr{ \A \( R \) }smx,
86             { is_named => 0, capture => '0' } ],
87             [ qr{ \A \( (?: < ( @{[ RE_CAPTURE_NAME ]} ) > |
88             ' ( @{[ RE_CAPTURE_NAME ]} ) ' |
89             R & ( @{[ RE_CAPTURE_NAME ]} ) ) \) }smxo,
90             { is_named => 1} ],
91             [ qr{ \A \( DEFINE \) }smx,
92             { is_named => 0, capture => '0' } ],
93             );
94              
95             # This must be implemented by tokens which do not recognize themselves.
96             # The return is a list of list references. Each list reference must
97             # contain a regular expression that recognizes the token, and optionally
98             # a reference to a hash to pass to make_token as the class-specific
99             # arguments. The regular expression MUST be anchored to the beginning of
100             # the string.
101             sub __PPIX_TOKEN__recognize {
102 8     8   26 return @recognize;
103             }
104              
105             # Return true if the token can be quantified, and false otherwise
106             # sub can_be_quantified { return };
107              
108             sub __PPIX_TOKENIZER__regexp {
109 32     32   105 my ( undef, $tokenizer ) = @_; # Invocant, $character unused
110              
111 32         106 foreach ( @recognize ) {
112 78         138 my ( $re, $arg ) = @{ $_ };
  78         187  
113 78 100       214 my $accept = $tokenizer->find_regexp( $re ) or next;
114 26         143 return $tokenizer->make_token( $accept, __PACKAGE__, $arg );
115             }
116              
117 6         18 return;
118             }
119              
120             1;
121              
122             __END__