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<PPIx::Regexp::Token::Condition> is a
14             L<PPIx::Regexp::Token::Reference|PPIx::Regexp::Token::Reference>.
15              
16             C<PPIx::Regexp::Token::Condition> 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   58 use strict;
  9         13  
  9         271  
34 9     9   33 use warnings;
  9         16  
  9         381  
35              
36 9     9   40 use base qw{ PPIx::Regexp::Token::Reference };
  9         13  
  9         889  
37              
38 9     9   85 use PPIx::Regexp::Constant qw{ RE_CAPTURE_NAME @CARP_NOT };
  9         16  
  9         5345  
39              
40             our $VERSION = '0.091';
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 13 my ( $self ) = @_;
51 6         20 my $content = $self->content();
52 6 100       15 if ( defined( my $expl = $explanation{$content} ) ) {
53 2         5 return $expl;
54             }
55 4 100       15 if ( $content =~ m/ \A [(] R /smx ) { # )
56 2 100       8 $self->is_named()
57             and return sprintf
58             q<True if recursing directly inside capture group '%s'>,
59             $self->name();
60 1         4 return sprintf
61             q<True if recursing directly inside capture group %d>,
62             $self->absolute();
63             }
64             $self->is_named()
65 2 100       11 and return sprintf
66             q<True if capture group '%s' matched>,
67             $self->name();
68 1         8 return sprintf
69             q<True if capture group %d matched>,
70             $self->absolute();
71             }
72              
73             }
74              
75             sub perl_version_introduced {
76 19     19 1 1957 my ( $self ) = @_;
77 19 100       61 $self->content() =~ m/ \A [(] [0-9]+ [)] \z /smx
78             and return '5.005';
79 13         29 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   21 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   67 my ( undef, $tokenizer ) = @_; # Invocant, $character unused
110              
111 32         74 foreach ( @recognize ) {
112 78         98 my ( $re, $arg ) = @{ $_ };
  78         136  
113 78 100       157 my $accept = $tokenizer->find_regexp( $re ) or next;
114 26         71 return $tokenizer->make_token( $accept, __PACKAGE__, $arg );
115             }
116              
117 6         13 return;
118             }
119              
120             1;
121              
122             __END__
123              
124             =head1 SUPPORT
125              
126             Support is by the author. Please file bug reports at
127             L<https://rt.cpan.org/Public/Dist/Display.html?Name=PPIx-Regexp>,
128             L<https://github.com/trwyant/perl-PPIx-Regexp/issues>, or in
129             electronic mail to the author.
130              
131             =head1 AUTHOR
132              
133             Thomas R. Wyant, III F<wyant at cpan dot org>
134              
135             =head1 COPYRIGHT AND LICENSE
136              
137             Copyright (C) 2009-2023, 2025 by Thomas R. Wyant, III
138              
139             This program is free software; you can redistribute it and/or modify it
140             under the same terms as Perl 5.10.0. For more details, see the full text
141             of the licenses in the directory LICENSES.
142              
143             This program is distributed in the hope that it will be useful, but
144             without any warranty; without even the implied warranty of
145             merchantability or fitness for a particular purpose.
146              
147             =cut
148              
149             # ex: set textwidth=72 :