File: Simple.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 (324 lines) | stat: -rw-r--r-- 9,082 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
=head1 NAME

PPIx::Regexp::Token::CharClass::Simple - This class represents a simple character class

=head1 SYNOPSIS

 use PPIx::Regexp::Dumper;
 PPIx::Regexp::Dumper->new( 'qr{\w}smx' )
     ->print();

=head1 INHERITANCE

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

C<PPIx::Regexp::Token::CharClass::Simple> has no descendants.

=head1 DESCRIPTION

This class represents one of the simple character classes that can occur
anywhere in a regular expression. This includes not only the truly
simple things like \w, but also Unicode properties, including properties
with wildcard values.

=head1 METHODS

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

=cut

package PPIx::Regexp::Token::CharClass::Simple;

use strict;
use warnings;

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

use PPIx::Regexp::Constant qw{
    COOKIE_CLASS
    LITERAL_LEFT_CURLY_REMOVED_PHASE_1
    LITERAL_LEFT_CURLY_REMOVED_PHASE_2
    MINIMUM_PERL
    TOKEN_LITERAL
    TOKEN_UNKNOWN
    @CARP_NOT
};

our $VERSION = '0.090';

use constant UNICODE_PROPERTY_LITERAL_VALUE => qr/
		\{ \s* \^? \w [\w:=\s-]* \} |
		[CLMNPSZ]	# perluniprops for 5.26.1
    /smx;
use constant UNICODE_PROPERTY_LITERAL =>
    qr/ \A \\ [Pp] (?:
	@{[ UNICODE_PROPERTY_LITERAL_VALUE ]}
    ) /smx;

# CAVEAT: The following regular expression, despite its name, matches
# ALL unicode property values. To actually match a wildcard property you
# must first eliminate anything that matches UNICODE_PROPERTY_LITERAL or
# UNICODE_PROPERTY_NAME_MATCH
use constant UNICODE_PROPERTY_WILDCARD =>
    qr/ \A \\ [Pp] \{ \s* [\w\s-]+ [:=] [^}]+ \} /smx;

use constant UNICODE_PROPERTY_NAME_MATCH =>
    qr< \A \\ [Pp] \{ \s* na (?: me? )? [:=] / [^/]+ / \} >smx;

use constant UNICODE_PROPERTY =>
    qr/ @{[ UNICODE_PROPERTY_LITERAL ]} |
	@{[ UNICODE_PROPERTY_NAME_MATCH ]} |
	@{[ UNICODE_PROPERTY_WILDCARD ]} /smx;

{

    my %kind_of_match = (
	p	=> 'with',
	P	=> 'without',
    );

    my %explanation = (
	'.'	=> 'Match any character',
	'\\C'	=> 'Match a single octet (removed in 5.23.0)',
	'\\D'	=> 'Match any character but a decimal digit',
	'\\H'	=> 'Match a non-horizontal-white-space character',
	'\\N'	=> 'Match any character but a new-line character',
	'\\R'	=> 'Match a generic new-line character',
	'\\S'	=> 'Match non-white-space character',
	'\\V'	=> 'Match a non-vertical-white-space character',
	'\\W'	=> 'Match non-word character',
	'\\X'	=> 'Match a Unicode extended grapheme cluster',
	'\\d'	=> 'Match decimal digit',
	'\\h'	=> 'Match a horizontal-white-space character',
	'\\s'	=> 'Match white-space character',
	'\\v'	=> 'Match a vertical-white-space character',
	'\\w'	=> 'Match word character',
    );

    sub __explanation {
	return \%explanation;
    }

    sub explain {
	my ( $self ) = @_;
	if ( $self->content() =~ m/ \A \\ ( [Pp] ) ( [{] .* [}] | . ) \z /smx ) {
	    my ( $kind, $prop ) = ( $1, $2 );

	    my $literal = ( $prop =~
		m/ \A @{[ UNICODE_PROPERTY_LITERAL_VALUE ]} \z /smx );

	    if ( 1 < length $prop ) {
		$prop =~ s/ \A [{] //smx;
		$prop =~ s/ [}] \z //smx;
	    }

	    $literal
		and return sprintf
		q<Match character %s Unicode or custom property '%s'>,
		$kind_of_match{$kind}, $prop;

	    return sprintf
		q<Match character %s Unicode wildcard property '%s'>,
		$kind_of_match{$kind}, $prop;
	}
	return $self->SUPER::explain();
    }

}

##=head2 is_case_sensitive
##
##This override of the superclass method returns true for Unicode
##properties that specify case, and false (but defined) for all
##other character classes.
##
##The classes that specify case are documented in
##L<perluniprops|/perluniprops>.
##
##B<Known bug:> This method returns false (but defined) for user-defined
##Unicode properties. It should return C<undef>. This bug B<may> be fixed
##if I find a way to identify all system-defined Unicode properties.
##
##=cut
##
##sub is_case_sensitive {
##    my ( $self ) = @_;
##    exists $self->{is_case_sensitive}
##	and return $self->{is_case_sensitive};
##    return ( $self->{is_case_sensitive} = $self->_is_case_sensitive() );
##}

##{
##    my %case_sensitive = map { $_ => 1 } qw{
##        generalcategory=lowercaseletter generalcategory=ll
##	    gc=lowercaseletter gc=ll
##        generalcategory=titlecaseletter generalcategory=lt
##	    gc=titlecaseletter gc=lt
##        generalcategory=uppercaseletter generalcategory=lu
##	    gc=uppercaseletter gc=lu
##	lowercaseletter lowercase lower ll
##	titlecaseletter titlecase title lt
##	uppercaseletter uppercase upper lu
##	lowercase=y lower=y lowercase=n lower=n
##	titlecase=y title=y titlecase=n title=n
##	uppercase=y upper=y uppercase=n upper=n
##    };
##
##    sub _is_case_sensitive {
##	my ( $self ) = @_;
##	my $content = $self->content();
##	$content =~ m/ \A \\ p [{] ( .* ) [}] /smxi
##	    or return 0;
##	$content = lc $1;
##	$content =~ s/ \A ^ //smx;
##	$content =~ s/ [\s_-] //smxg;
##	$content =~ s/ \A is //smx;
##	$content =~ s/ : /=/smxg;
##	$content =~ s/ = (?: yes | t | true ) \b /=y/smxg;
##	$content =~ s/ = (?: no | f | false ) \b /=n/smxg;
##	return $case_sensitive{$content} || 0;
##    }
##
##}

{

    my %introduced = (
	'\\h'	=> '5.009005',	# Before this, parsed as 'h'
	'\\v'	=> '5.009005',	# Before this, parsed as 'v'
	'\\H'	=> '5.009005',	# Before this, parsed as 'H'
	'\\N'	=> '5.011',	# Before this, an error.
	'\\V'	=> '5.009005',	# Before this, parsed as 'V'
	'\\R'	=> '5.009005',
	'\\C'	=> '5.006',
	'\\X'	=> '5.006',
    );

    sub perl_version_introduced {
	my ( $self ) = @_;
	my $content = $self->content();
	if ( defined( my $minver = $introduced{$content} ) ) {
	    return $minver;
	}
	# I must have read perl5113delta and thought this
	# represented the change they were talking about, but I sure
	# don't see it now. So, until things become clearer ...
#	$content =~ m/ \G .*? [\s=-] /smxgc
#	    and return '5.011003';
	$content  =~ UNICODE_PROPERTY_LITERAL
	    and return '5.006001';
	$content =~ UNICODE_PROPERTY_NAME_MATCH
	    and return '5.031010';
	$content =~ UNICODE_PROPERTY_WILDCARD
	    and return '5.029009';
	return MINIMUM_PERL;
    }

}

{
    my %removed	 = (
	'\\C'	=> '5.023',	# Before this, matched an octet
    );

    sub perl_version_removed {
	my ( $self ) = @_;
	return $removed{ $self->content() };
    }
}

# This is one of the larger complications of
# https://rt.perl.org/Public/Bug/Display.html?id=128213
# where it transpired that un-escaped literal left curlies were not
# giving warnings/errors in /.{/, /\p{...}{/, and /\P{...}{/, but were
# for all the others that bin into this class (e.g. /\s{/).
# Note that the perldelta for 5.25.1 and 5.26.0 do not acknowledge tha
# phased deprecation, and pretend that everything was done on the phase
# 1 schedule. This appears to be deliberate per
# https://rt.perl.org/Ticket/Display.html?id=131352
sub __following_literal_left_curly_disallowed_in {
    my ( $self ) = @_;
    q<.> eq ( my $content = $self->content() )
	and return LITERAL_LEFT_CURLY_REMOVED_PHASE_2;
    $content =~ m/ \A \\ p \{ /smxi
	and return LITERAL_LEFT_CURLY_REMOVED_PHASE_2;
    return LITERAL_LEFT_CURLY_REMOVED_PHASE_1;
}

sub __PPIX_TOKENIZER__regexp {
    my ( undef, $tokenizer, $character ) = @_;

    my $in_class = $tokenizer->cookie( COOKIE_CLASS );

    if ( $character eq '.' ) {
	$in_class
	    and return $tokenizer->make_token( 1, TOKEN_LITERAL );
	return 1;
    }

    if ( my $accept = $tokenizer->find_regexp(
	    qr{ \A \\ [wWsSdDvVhHXRNC] }smx
	) ) {
	if ( $in_class ) {
	    my $match = $tokenizer->match();
	    # As of Perl 5.11.5, [\N] is a fatal error.
	    '\\N' eq $match
		and return $tokenizer->make_token(
		    $accept, TOKEN_UNKNOWN, {
			    error	=> '\\N invalid inside character class',
			},
		    );
	    # \R is not recognized inside a character class. It
	    # eventually ends up as a literal.
	    '\\R' eq $match and return;
	}
	return $accept;
    }

    if ( my $accept = $tokenizer->find_regexp( UNICODE_PROPERTY ) ) {
	return $accept;
    }

    if ( my $accept = $tokenizer->find_regexp( qr< \A \\ p [{] \s* [}] >smx )
    ) {
	return $tokenizer->make_token( $accept, TOKEN_UNKNOWN, {
		error => 'Empty \\p{} is an error',
	    },
	);
    }

    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 :