File Coverage

blib/lib/PPIx/Regexp/Token.pm
Criterion Covered Total %
statement 43 43 100.0
branch 7 8 87.5
condition 3 6 50.0
subroutine 15 15 100.0
pod 7 7 100.0
total 75 79 94.9


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             PPIx::Regexp::Token - Base class for PPIx::Regexp tokens.
4              
5             =head1 SYNOPSIS
6              
7             use PPIx::Regexp::Dumper;
8             PPIx::Regexp::Dumper->new( 'qr{foo}' )->print();
9              
10             =head1 INHERITANCE
11              
12             C<PPIx::Regexp::Token> is a
13             L<PPIx::Regexp::Element|PPIx::Regexp::Element>.
14              
15             C<PPIx::Regexp::Token> is the parent of
16             L<PPIx::Regexp::Token::Assertion|PPIx::Regexp::Token::Assertion>,
17             L<PPIx::Regexp::Token::Backtrack|PPIx::Regexp::Token::Backtrack>,
18             L<PPIx::Regexp::Token::CharClass|PPIx::Regexp::Token::CharClass>,
19             L<PPIx::Regexp::Token::Code|PPIx::Regexp::Token::Code>,
20             L<PPIx::Regexp::Token::Comment|PPIx::Regexp::Token::Comment>,
21             L<PPIx::Regexp::Token::Control|PPIx::Regexp::Token::Control>,
22             L<PPIx::Regexp::Token::Greediness|PPIx::Regexp::Token::Greediness>,
23             L<PPIx::Regexp::Token::GroupType|PPIx::Regexp::Token::GroupType>,
24             L<PPIx::Regexp::Token::Literal|PPIx::Regexp::Token::Literal>,
25             L<PPIx::Regexp::Token::Modifier|PPIx::Regexp::Token::Modifier>,
26             L<PPIx::Regexp::Token::NoOp|PPIx::Regexp::Token::NoOp>,
27             L<PPIx::Regexp::Token::Operator|PPIx::Regexp::Token::Operator>,
28             L<PPIx::Regexp::Token::Quantifier|PPIx::Regexp::Token::Quantifier>,
29             L<PPIx::Regexp::Token::Reference|PPIx::Regexp::Token::Reference>,
30             L<PPIx::Regexp::Token::Structure|PPIx::Regexp::Token::Structure>,
31             L<PPIx::Regexp::Token::Unknown|PPIx::Regexp::Token::Unknown> and
32             L<PPIx::Regexp::Token::Unmatched|PPIx::Regexp::Token::Unmatched>.
33              
34             =head1 DESCRIPTION
35              
36             This class represents the base of the class hierarchy for tokens in the
37             L<PPIx::Regexp|PPIx::Regexp> package.
38              
39             =head1 METHODS
40              
41             This class provides no public methods beyond those provided by its
42             superclass.
43              
44             =cut
45              
46             package PPIx::Regexp::Token;
47              
48 9     9   56 use strict;
  9         14  
  9         362  
49 9     9   32 use warnings;
  9         13  
  9         348  
50              
51 9     9   29 use base qw{PPIx::Regexp::Element};
  9         12  
  9         675  
52              
53 9     9   36 use Carp qw{ confess };
  9         13  
  9         471  
54 9     9   46 use PPIx::Regexp::Constant qw{ MINIMUM_PERL @CARP_NOT };
  9         12  
  9         916  
55              
56             our $VERSION = '0.091';
57              
58 9     9   42 use constant TOKENIZER_ARGUMENT_REQUIRED => 0;
  9         12  
  9         5196  
59              
60             sub __new {
61 5497     5497   12076 my ( $class, $content, %arg ) = @_;
62              
63             not $class->TOKENIZER_ARGUMENT_REQUIRED()
64             or $arg{tokenizer}
65 5497 50 66     16560 or confess 'Programming error - tokenizer not provided';
66              
67 5497         11134 my $self = {
68             content => $content,
69             };
70              
71 5497         8212 foreach my $key ( qw{
72             explanation perl_version_introduced perl_version_removed
73             } ) {
74             defined $arg{$key}
75 16491 100       25862 and $self->{$key} = $arg{$key};
76             }
77              
78 5497   33     12430 bless $self, ref $class || $class;
79 5497         14108 return $self;
80             }
81              
82             sub content {
83 10843     10843 1 16460 my ( $self ) = @_;
84 10843         27229 return $self->{content};
85             }
86              
87             =head2 first_token
88              
89             This method returns its invocant.
90              
91             =cut
92              
93             sub first_token {
94 13     13 1 18 my ( $self ) = @_;
95 13         46 return $self;
96             }
97              
98             =head2 last_token
99              
100             This method returns its invocant.
101              
102             =cut
103              
104             sub last_token {
105 96     96 1 114 my ( $self ) = @_;
106 96         199 return $self;
107             }
108              
109             sub perl_version_introduced {
110 91     91 1 5370 my ( $self ) = @_;
111             return defined $self->{perl_version_introduced} ?
112             $self->{perl_version_introduced} :
113 91 100       324 MINIMUM_PERL;
114             }
115              
116             sub perl_version_removed {
117 546     546 1 63768 my ( $self ) = @_;
118 546         1197 return $self->{perl_version_removed};
119             }
120              
121             sub unescaped_content {
122 126     126 1 206 my ( $self ) = @_;
123 126         291 my $content = $self->content();
124 126         356 $content =~ s/ \\ (?= . ) //smxg;
125 126         532 return $content;
126             }
127              
128             sub scontent {
129 69     69 1 70 my ( $self ) = @_;
130             $self->significant()
131 69 100       83 and return $self->{content};
132 20         25 return;
133             }
134              
135             # Called by the lexer once it has done its worst to all the tokens.
136             # Called as a method with the lexer as argument. The return is the
137             # number of parse failures discovered when finalizing.
138             sub __PPIX_LEXER__finalize {
139 1618     1618   2063 return 0;
140             }
141              
142             1;
143              
144             __END__
145              
146             =head1 SUPPORT
147              
148             Support is by the author. Please file bug reports at
149             L<https://rt.cpan.org/Public/Dist/Display.html?Name=PPIx-Regexp>,
150             L<https://github.com/trwyant/perl-PPIx-Regexp/issues>, or in
151             electronic mail to the author.
152              
153             =head1 AUTHOR
154              
155             Thomas R. Wyant, III F<wyant at cpan dot org>
156              
157             =head1 COPYRIGHT AND LICENSE
158              
159             Copyright (C) 2009-2023, 2025 by Thomas R. Wyant, III
160              
161             This program is free software; you can redistribute it and/or modify it
162             under the same terms as Perl 5.10.0. For more details, see the full text
163             of the licenses in the directory LICENSES.
164              
165             This program is distributed in the hope that it will be useful, but
166             without any warranty; without even the implied warranty of
167             merchantability or fitness for a particular purpose.
168              
169             =cut
170              
171             # ex: set textwidth=72 :