File: GroupType.pm

package info (click to toggle)
libppix-regexp-perl 0.090-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,524 kB
  • sloc: perl: 8,022; makefile: 8
file content (309 lines) | stat: -rw-r--r-- 8,929 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
=head1 NAME

PPIx::Regexp::Token::GroupType - Represent a grouping parenthesis type.

=head1 SYNOPSIS

 use PPIx::Regexp::Dumper;
 PPIx::Regexp::Dumper->new( 'qr{(?i:foo)}smx' )
     ->print();

=head1 INHERITANCE

C<PPIx::Regexp::Token::GroupType> is a
L<PPIx::Regexp::Token|PPIx::Regexp::Token>.

C<PPIx::Regexp::Token::GroupType> is the parent of
L<PPIx::Regexp::Token::GroupType::Assertion|PPIx::Regexp::Token::GroupType::Assertion>,
L<PPIx::Regexp::Token::GroupType::Atomic_Script_Run|PPIx::Regexp::Token::GroupType::Atomic_Script_Run>,
L<PPIx::Regexp::Token::GroupType::BranchReset|PPIx::Regexp::Token::GroupType::BranchReset>,
L<PPIx::Regexp::Token::GroupType::Code|PPIx::Regexp::Token::GroupType::Code>,
L<PPIx::Regexp::Token::GroupType::Modifier|PPIx::Regexp::Token::GroupType::Modifier>,
L<PPIx::Regexp::Token::GroupType::NamedCapture|PPIx::Regexp::Token::GroupType::NamedCapture>,
L<PPIx::Regexp::Token::GroupType::Script_Run|PPIx::Regexp::Token::GroupType::Script_Run>,
L<PPIx::Regexp::Token::GroupType::Subexpression|PPIx::Regexp::Token::GroupType::Subexpression>
and
L<PPIx::Regexp::Token::GroupType::Switch|PPIx::Regexp::Token::GroupType::Switch>.

=head1 DESCRIPTION

This class represents any of the magic sequences of characters that can
follow an open parenthesis. This particular class is intended to be
abstract.

=head1 METHODS

This class provides no public methods beyond those provided by its
superclass.

=cut

package PPIx::Regexp::Token::GroupType;

use strict;
use warnings;

use base qw{ PPIx::Regexp::Token };

use PPIx::Regexp::Constant qw{ MINIMUM_PERL @CARP_NOT };
use PPIx::Regexp::Util qw{ __ns_can };

our $VERSION = '0.090';

# Return true if the token can be quantified, and false otherwise
sub can_be_quantified { return };

=head2 __defining_string

 my $string = $class->__defining_string();

This method is private to the C<PPIx-Regexp> package, and is documented
for the author's benefit only. It may be changed or revoked without
notice.

This method returns an array of strings that define the specific group
type.  These strings will normally start with C<'?'>.

Optionally, the first returned item may be a hash reference. The only
supported key is C<{suffix}>, which is a string to be suffixed to each
of the regular expressions made by C<__make_group_type_matcher()> out of
the defining strings, inside a C<(?= ... )>, so that it is not included
in the match.

This method B<must> be overridden, unless C<__make_group_type_matcher()>
is. The override B<must> return the same thing each time, since the
results of C<__make_group_type_matcher()> are cached.

=cut

sub __defining_string {
    require Carp;
    Carp::confess(
	'Programming error - __defining_string() must be overridden' );
}

=head2 __make_group_type_matcher

 my $hash_ref = $class->__make_group_type_matcher();

This method is private to the C<PPIx-Regexp> package, and is documented
for the author's benefit only. It may be changed or revoked without
notice.

This method returns a reference to a hash. The keys are regexp delimiter
characters which appear in the defining strings for the group type. For
each key, the value is a reference to an array of C<Regexp> objects,
properly escaped for the key character. Key C<''> provides the regular
expressions to be used if the regexp delimiter does not appear in any of
the defining strings.

If this method is overridden by the subclass, method
C<__defining_string()> need not be, unless the overridden
C<__make_group_type_matcher()> calls C<__defining_string()>.

=cut

sub __make_group_type_matcher {
    my ( $class ) = @_;

    my @defs = $class->__defining_string();

    my $opt = ref $defs[0] ? shift @defs : {};

    my $suffix = defined $opt->{suffix} ?
	qr/ (?= \Q$opt->{suffix}\E ) /smx :
	'';

    my %seen;
    my @chars = grep { ! $seen{$_}++ } split qr{}smx, join '', @defs;

    my %rslt;
    foreach my $str ( @defs ) {
	push @{ $rslt{''} ||= [] }, qr{ \A \Q$str\E $suffix }smx;
	foreach my $chr ( @chars ) {
	    ( my $expr = $str ) =~ s/ (?= \Q$chr\E ) /\\/smxg;
	    push @{ $rslt{$chr} ||= [] }, qr{ \A \Q$expr\E $suffix }smx;
	}
    }
    return \%rslt;
}

=head2 __match_setup

 $class->__match_setup( $tokenizer );

This method is private to the C<PPIx-Regexp> package, and is documented
for the author's benefit only. It may be changed or revoked without
notice.

This method performs whatever setup is needed once it is determined that
the given group type has been detected.  This method is called only if
the class matched at the current position in the string being parsed. It
must perform whatever extra setup is needed for the match. It returns
nothing.

This method need not be overridden. The default does nothing.

=cut

sub __match_setup {
    return;
}

=head2 __setup_class

 $class->__setup_class( \%definition, \%opt );

This method is private to the C<PPIx-Regexp> package, and is documented
for the author's benefit only. It may be changed or revoked without
notice.

This method uses the C<%definition> hash to create the
C<__defining_string()>, C<explain()>, C<perl_version_introduced()>, and
C<perl_version_removed()> methods for the calling class. Any of these
that already exist will B<not> be replaced.

The C<%definition> hash defines all the strings that specify tokens of
the invoking class. You can not (unfortunately) use this mechanism if
you need a regular expression to recognize a token that belongs to this
class. The keys of the C<%definition> hash are strings that specify
members of this class. The values are hashes that define the specific
member of the class. The following values are supported:

=over

=item {expl}

This is the explanation of the element, to be returned by the
C<explain()> method.

=item {intro}

This is the Perl version that introduced the element, as a string. The
default is the value of constant
L<MINIMUM_PERL|PPIx::Regexp::Constant/MINIMUM_PERL>.

=item {remov}

This is the Perl version that removed the element, as a string. The
default is C<undef>, meaning that the element is still present in the
highest released version of Perl, whether development or production.

=back

The C<%opt> hash is optional, and defaults to the empty hash. It is
used, basically, for ad-hocery. The supported keys are:

=over

=item {suffix}

If this element is defined, the first element returned by the generated
L<__defining_string()|/__defining_string> method is a hash containing
this key and value.

=back

=cut

sub __setup_class {
    my ( $class, $opt ) = @_;

    $opt ||= {};

    unless ( $class->__ns_can( '__defining_string' ) ) {
	my $method = "${class}::__defining_string";
	my @def_str = sort keys %{ $class->DEF };
	defined $opt->{suffix}
	    and unshift @def_str, {
	    suffix	=> $opt->{suffix},
	};
	$class->DEF->{__defining_string} = \@def_str;
	no strict qw{ refs };
	*$method = sub {
	    my ( $self ) = @_;
	    return @{ $self->DEF->{__defining_string} };
	};
    }

    unless ( $class->__ns_can( 'explain' ) ) {
	my $method = "${class}::explain";
	no strict qw{ refs };
	*$method = sub {
	    my ( $self ) = @_;
	    $DB::single = 1;
	    return $self->DEF->{ $self->unescaped_content() }{expl};
	};
    }

    unless ( $class->__ns_can( 'perl_version_introduced' ) ) {
	my $method = "${class}::perl_version_introduced";
	no strict qw{ refs };
	*$method = sub {
	    my ( $self ) = @_;
	    return $self->DEF->{ $self->unescaped_content() }{intro} || MINIMUM_PERL;
	};
    }

    unless ( $class->__ns_can( 'perl_version_removed' ) ) {
	my $method = "${class}::perl_version_removed";
	no strict qw{ refs };
	*$method = sub {
	    my ( $self ) = @_;
	    return $self->DEF->{ $self->unescaped_content() }{remov};
	};
    }

    return;
}

my %matcher;

sub __PPIX_TOKENIZER__regexp {
    my ( $class, $tokenizer ) = @_;	# $character unused

    my $mtch = $matcher{$class} ||= $class->__make_group_type_matcher();

    my $re_list = $mtch->{ $tokenizer->get_start_delimiter() } ||
	$mtch->{''};

    foreach my $re ( @{ $re_list } ) {
	my $accept = $tokenizer->find_regexp( $re )
	    or next;
	$class->__match_setup( $tokenizer );
	return $accept;
    }

    return;
}

1;

__END__

=head1 SUPPORT

Support is by the author. Please file bug reports at
L<https://rt.cpan.org/Public/Dist/Display.html?Name=PPIx-Regexp>,
L<https://github.com/trwyant/perl-PPIx-Regexp/issues>, or in
electronic mail to the author.

=head1 AUTHOR

Thomas R. Wyant, III F<wyant at cpan dot org>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2009-2023, 2025 by Thomas R. Wyant, III

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl 5.10.0. For more details, see the full text
of the licenses in the directory LICENSES.

This program is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.

=cut

# ex: set textwidth=72 :