| 1 | package Regexp::English; | ||||||||||||
| 2 | |||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 7 | ▶ | use Exporter 'import'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 7 | ▶ | use vars qw( @export @EXPORT_OK %EXPORT_TAGS $VERSION ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 7 | $VERSION = '1.01'; | |||||||||||
| 10 | |||||||||||||
| 11 | 7 | ▶ | use overload '""' => \&compile; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 7 | ▶ | use Scalar::Util 'blessed'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | # REGEX: storage for the raw regex | ||||||||||||
| 15 | # STORE: storage for bound references (see remember()) | ||||||||||||
| 16 | # STACK: used to nest groupings | ||||||||||||
| 17 | 7 | ▶ | use constant REGEX => 0; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 7 | ▶ | use constant STORE => 1; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 7 | ▶ | use constant STACK => 2; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | |||||||||||||
| 21 | # the key is the name of the method to create | ||||||||||||
| 22 | # symbol is the regex token this represents | ||||||||||||
| 23 | # plural is the name of the shortcut method for $symbol+, i.e. \w+ | ||||||||||||
| 24 | # non is the name of the negated token, its shortcut, and a plural, if needed | ||||||||||||
| 25 | 7 | my %chars = ( | |||||||||||
| 26 | word_char => | ||||||||||||
| 27 | { | ||||||||||||
| 28 | symbol => '\w', | ||||||||||||
| 29 | plural => 'word_chars', | ||||||||||||
| 30 | non => [ 'non_word_char', '\W', 'non_word_chars' ], | ||||||||||||
| 31 | }, | ||||||||||||
| 32 | whitespace_char => | ||||||||||||
| 33 | { | ||||||||||||
| 34 | symbol => '\s', | ||||||||||||
| 35 | plural => 'whitespace_chars', | ||||||||||||
| 36 | non => [ 'non_whitespace_char', '\S', 'non_whitespace_chars' ], | ||||||||||||
| 37 | }, | ||||||||||||
| 38 | digit => | ||||||||||||
| 39 | { | ||||||||||||
| 40 | symbol => '\d', | ||||||||||||
| 41 | plural => 'digits', | ||||||||||||
| 42 | non => [ 'non_digit', '\D', 'non_digits' ], | ||||||||||||
| 43 | }, | ||||||||||||
| 44 | word_boundary => | ||||||||||||
| 45 | { | ||||||||||||
| 46 | symbol => '\b', | ||||||||||||
| 47 | non => [ 'non_word_boundary', '\B' ], | ||||||||||||
| 48 | }, | ||||||||||||
| 49 | end_of_string => | ||||||||||||
| 50 | { | ||||||||||||
| 51 | symbol => '\Z', | ||||||||||||
| 52 | non => [ 'very_end_of_string', '\z' ], | ||||||||||||
| 53 | }, | ||||||||||||
| 54 | beginning_of_string => { symbol => '\A', }, | ||||||||||||
| 55 | end_of_previous_match => { symbol => '\G', }, | ||||||||||||
| 56 | |||||||||||||
| 57 | # XXX: non for these ? | ||||||||||||
| 58 | tab => | ||||||||||||
| 59 | { | ||||||||||||
| 60 | symbol => '\t', | ||||||||||||
| 61 | plural => 'tabs', | ||||||||||||
| 62 | non => [ 'non_tab', '[^\t]' ], | ||||||||||||
| 63 | }, | ||||||||||||
| 64 | |||||||||||||
| 65 | # implies /s modifier | ||||||||||||
| 66 | newline => | ||||||||||||
| 67 | { | ||||||||||||
| 68 | symbol => '\n', | ||||||||||||
| 69 | plural => 'newlines', | ||||||||||||
| 70 | non => [ 'non_newline', '(?s)[^\n]' ], | ||||||||||||
| 71 | }, | ||||||||||||
| 72 | |||||||||||||
| 73 | carriage_return => | ||||||||||||
| 74 | { | ||||||||||||
| 75 | symbol => '\r', | ||||||||||||
| 76 | plural => 'carriage_returns', | ||||||||||||
| 77 | non => [ 'non_carriage_return', '[^\r]' ], | ||||||||||||
| 78 | }, | ||||||||||||
| 79 | |||||||||||||
| 80 | form_feed => | ||||||||||||
| 81 | { | ||||||||||||
| 82 | symbol => '\f', | ||||||||||||
| 83 | plural => 'form_feeds', | ||||||||||||
| 84 | non => [ 'non_form_feed', '[^\f]' ], | ||||||||||||
| 85 | }, | ||||||||||||
| 86 | |||||||||||||
| 87 | 'alarm' => | ||||||||||||
| 88 | { | ||||||||||||
| 89 | symbol => '\a', | ||||||||||||
| 90 | plural => 'alarms', | ||||||||||||
| 91 | non => [ 'non_alarm', '[^\a]' ], | ||||||||||||
| 92 | }, | ||||||||||||
| 93 | escape => | ||||||||||||
| 94 | { | ||||||||||||
| 95 | symbol => '\e', | ||||||||||||
| 96 | plural => 'escapes', | ||||||||||||
| 97 | non => [ 'non_escape', '[^\e]' ], | ||||||||||||
| 98 | }, | ||||||||||||
| 99 | start_of_line => { symbol => '^', }, | ||||||||||||
| 100 | end_of_line => { symbol => '$', }, | ||||||||||||
| 101 | ); | ||||||||||||
| 102 | |||||||||||||
| 103 | sub _chars | ||||||||||||
| 104 | 266 | ▶ | { | ||||||||||
|
Subroutine
_chars: called CC=1 SCAR=0.0
| |||||||||||||
| 105 | 266 | my $symbol = shift; | |||||||||||
| 106 | |||||||||||||
| 107 | return sub | ||||||||||||
| 108 | 95 | ▶ | { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 109 | # cannot use $_[0] here, as it trips the overload | ||||||||||||
| 110 | # that can mess with remember/end groups | ||||||||||||
| 111 | 95 | ▶ | return $symbol unless @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | |||||||||||||
| 113 | 77 | my $self = shift; | |||||||||||
| 114 | 77 | ▶ | $self = $self->new() unless blessed( $self ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 115 | |||||||||||||
| 116 | 77 | $self->[REGEX] .= $symbol; | |||||||||||
| 117 | 77 | return $self; | |||||||||||
| 118 | 266 | }; | |||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | 7 | my @char_tags; | |||||||||||
| 122 | |||||||||||||
| 123 | 7 | for my $char ( keys %chars ) | |||||||||||
| 124 | { | ||||||||||||
| 125 | 105 | push @char_tags, $char; | |||||||||||
| 126 | 105 | _install( $char, _chars( $chars{$char}{symbol} ) ); | |||||||||||
| 127 | |||||||||||||
| 128 | 105 | ▶ | if ( $chars{$char}{plural} ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 129 | { | ||||||||||||
| 130 | 63 | _install( $chars{$char}{plural}, _chars( $chars{$char}{symbol} . '+' )); | |||||||||||
| 131 | 63 | push @char_tags, $chars{$char}{plural}; | |||||||||||
| 132 | } | ||||||||||||
| 133 | |||||||||||||
| 134 | 105 | ▶ | if ( $chars{$char}{non} ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 135 | { | ||||||||||||
| 136 | 77 | my ( $nonname, $symbol, $pluralname ) = @{ $chars{$char}{non} }; | |||||||||||
| 137 | 77 | _install( $nonname, _chars($symbol) ); | |||||||||||
| 138 | 77 | push @char_tags, $nonname; | |||||||||||
| 139 | 77 | ▶ | if ($pluralname) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 140 | { | ||||||||||||
| 141 | 21 | _install( $pluralname, _chars( $symbol . '+' ) ); | |||||||||||
| 142 | 21 | push @char_tags, $pluralname; | |||||||||||
| 143 | } | ||||||||||||
| 144 | } | ||||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | # tested in t/quantifiers | ||||||||||||
| 148 | # XXX: | ||||||||||||
| 149 | # the syntax for minimal/optional is slightly awkward | ||||||||||||
| 150 | 7 | my %quantifiers = | |||||||||||
| 151 | ( | ||||||||||||
| 152 | zero_or_more => '*', | ||||||||||||
| 153 | multiple => '+', | ||||||||||||
| 154 | minimal => '?', | ||||||||||||
| 155 | optional => '?', | ||||||||||||
| 156 | ); | ||||||||||||
| 157 | |||||||||||||
| 158 | 7 | for my $quantifier ( keys %quantifiers ) | |||||||||||
| 159 | { | ||||||||||||
| 160 | _install( $quantifier, | ||||||||||||
| 161 | 28 | _standard( '(?:', '', $quantifiers{$quantifier} . ')' ), 1 ); | |||||||||||
| 162 | } | ||||||||||||
| 163 | |||||||||||||
| 164 | # tested in t/groupings | ||||||||||||
| 165 | 7 | my %groupings = | |||||||||||
| 166 | ( | ||||||||||||
| 167 | after => '(?<=', | ||||||||||||
| 168 | group => '(?:', | ||||||||||||
| 169 | comment => '(?#', | ||||||||||||
| 170 | not_after => '(?<!', | ||||||||||||
| 171 | followed_by => '(?=', | ||||||||||||
| 172 | not_followed_by => '(?!', | ||||||||||||
| 173 | ); | ||||||||||||
| 174 | |||||||||||||
| 175 | 7 | for my $group ( keys %groupings ) | |||||||||||
| 176 | { | ||||||||||||
| 177 | 42 | _install( $group, _standard( $groupings{$group}, '', '' ), 1 ); | |||||||||||
| 178 | } | ||||||||||||
| 179 | |||||||||||||
| 180 | sub _standard | ||||||||||||
| 181 | 77 | ▶ | { | ||||||||||
|
Subroutine
_standard: called CC=2 SCAR=6.9
| |||||||||||||
| 182 | 77 | my ( $group, $sep, $symbol ) = @_; | |||||||||||
| 183 | |||||||||||||
| 184 | 77 | ▶ | $symbol ||= ')'; | ||||||||||
|
Condition100%2/2
$symbol ||= ")"
MC/DC100%1/1
$symbol ||= ")"
$symbol
Truth table100%2/2
A
A= $symbol
| |||||||||||||
| 185 | |||||||||||||
| 186 | return sub | ||||||||||||
| 187 | 37 | ▶ | { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 188 | 37 | ▶ | if ( eval { $_[0]->isa( 'Regexp::English' ) } ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 189 | { | ||||||||||||
| 190 | 17 | my $self = shift; | |||||||||||
| 191 | 17 | $self->[REGEX] .= $group; | |||||||||||
| 192 | |||||||||||||
| 193 | 17 | ▶ | if (@_) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 194 | { | ||||||||||||
| 195 | 10 | $self->[REGEX] .= join( "$sep", @_ ) . $symbol; | |||||||||||
| 196 | } | ||||||||||||
| 197 | else | ||||||||||||
| 198 | { | ||||||||||||
| 199 | 7 | push @{ $self->[STACK] }, $symbol; | |||||||||||
| 200 | } | ||||||||||||
| 201 | 17 | return $self; | |||||||||||
| 202 | } | ||||||||||||
| 203 | 20 | return $group . join( $sep, @_ ) . $symbol; | |||||||||||
| 204 | 77 | }; | |||||||||||
| 205 | } | ||||||||||||
| 206 | |||||||||||||
| 207 | # can't be used with standard because of quotemeta() | ||||||||||||
| 208 | sub literal | ||||||||||||
| 209 | 16 | ▶ | { | ||||||||||
|
Subroutine
literal: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 210 | 16 | my $self = shift; | |||||||||||
| 211 | 16 | $self->[REGEX] .= quotemeta( +shift ); | |||||||||||
| 212 | 16 | return $self; | |||||||||||
| 213 | } | ||||||||||||
| 214 | |||||||||||||
| 215 | sub _install | ||||||||||||
| 216 | 350 | ▶ | { | ||||||||||
|
Subroutine
_install: called CC=2 SCAR=6.9
| |||||||||||||
| 217 | 350 | my ( $name, $sub, $export ) = @_; | |||||||||||
| 218 | 7 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 219 | 350 | *{$name} = $sub; | |||||||||||
| 220 | 350 | ▶ | push @export, "&$name" if $export; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 221 | 350 | push @EXPORT_OK, "&$name"; | |||||||||||
| 222 | } | ||||||||||||
| 223 | |||||||||||||
| 224 | _install( | ||||||||||||
| 225 | 'or', | ||||||||||||
| 226 | 6 | ▶ | sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 227 | 6 | ▶ | if ( eval { $_[0]->isa( 'Regexp::English' ) } ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 228 | { | ||||||||||||
| 229 | 4 | my $self = shift; | |||||||||||
| 230 | 4 | ▶ | if (@_) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 231 | { | ||||||||||||
| 232 | 2 | $self->[REGEX] .= '(?:' . join( '|', @_ ) . ')'; | |||||||||||
| 233 | } | ||||||||||||
| 234 | else | ||||||||||||
| 235 | { | ||||||||||||
| 236 | 2 | $self->[REGEX] .= '|'; | |||||||||||
| 237 | } | ||||||||||||
| 238 | 4 | return $self; | |||||||||||
| 239 | } | ||||||||||||
| 240 | 2 | return '(?:' . join( '|', @_ ) . ')'; | |||||||||||
| 241 | }, | ||||||||||||
| 242 | 7 | 1 | |||||||||||
| 243 | ); | ||||||||||||
| 244 | |||||||||||||
| 245 | 7 | _install( 'class', _standard( '[', '', ']' ), 1 ); | |||||||||||
| 246 | |||||||||||||
| 247 | # XXX - not() | ||||||||||||
| 248 | |||||||||||||
| 249 | sub remember | ||||||||||||
| 250 | 29 | ▶ | { | ||||||||||
|
Subroutine
remember: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 251 | 29 | my $self = shift; | |||||||||||
| 252 | 29 | ▶ | $self = $self->new() unless blessed( $self ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 253 | |||||||||||||
| 254 | # the first element may be a reference, so stick it in STORE | ||||||||||||
| 255 | 29 | ▶ | if ( ref( $_[0] ) eq 'SCALAR' ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 256 | { | ||||||||||||
| 257 | 11 | push @{ $self->[STORE] }, shift; | |||||||||||
| 258 | } | ||||||||||||
| 259 | |||||||||||||
| 260 | # if there are other arguments, add them to REGEX | ||||||||||||
| 261 | 29 | ▶ | if (@_) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 262 | { | ||||||||||||
| 263 | 17 | $self->[REGEX] .= '(' . join( '', @_ ) . ')'; | |||||||||||
| 264 | |||||||||||||
| 265 | # otherwise, this is the opening op of a multi-call remember block | ||||||||||||
| 266 | # XXX: might store calling line for verbose debugging | ||||||||||||
| 267 | } | ||||||||||||
| 268 | else | ||||||||||||
| 269 | { | ||||||||||||
| 270 | 12 | $self->[REGEX] .= '('; | |||||||||||
| 271 | 12 | push @{ $self->[STACK] }, ')'; | |||||||||||
| 272 | } | ||||||||||||
| 273 | |||||||||||||
| 274 | 29 | return $self; | |||||||||||
| 275 | } | ||||||||||||
| 276 | |||||||||||||
| 277 | sub end | ||||||||||||
| 278 | 16 | ▶ | { | ||||||||||
|
Subroutine
end: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 279 | 16 | my ( $self, $levels ) = @_; | |||||||||||
| 280 | 16 | ▶ | $levels = 1 unless defined $levels; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 281 | |||||||||||||
| 282 | 16 | ▶ | unless ( @{ $self->[STACK] } ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 283 | { | ||||||||||||
| 284 | 1 | require Carp; | |||||||||||
| 285 | 1 | Carp::confess( 'end() called without remember()' ); | |||||||||||
| 286 | } | ||||||||||||
| 287 | |||||||||||||
| 288 | 15 | $self->[REGEX] .= pop @{ $self->[STACK] } for 1 .. $levels; | |||||||||||
| 289 | |||||||||||||
| 290 | 15 | return $self; | |||||||||||
| 291 | } | ||||||||||||
| 292 | |||||||||||||
| 293 | sub new | ||||||||||||
| 294 | 53 | ▶ | { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 295 | 53 | bless( [ '', [], [] ], $_[0] ); | |||||||||||
| 296 | } | ||||||||||||
| 297 | |||||||||||||
| 298 | sub match | ||||||||||||
| 299 | 118 | ▶ | { | ||||||||||
|
Subroutine
match: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 300 | 118 | my $self = shift; | |||||||||||
| 301 | 118 | $self->[REGEX] = $self->compile(); | |||||||||||
| 302 | |||||||||||||
| 303 | 118 | ▶ | if ( @{ $self->[STORE] } ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 304 | { | ||||||||||||
| 305 | 8 | return $self->capture( $_[0] =~ $self->[REGEX] ); | |||||||||||
| 306 | } | ||||||||||||
| 307 | else | ||||||||||||
| 308 | { | ||||||||||||
| 309 | 110 | ▶ | if ( wantarray() ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 310 | { | ||||||||||||
| 311 | 10 | return $_[0] =~ $self->[REGEX]; | |||||||||||
| 312 | } | ||||||||||||
| 313 | else | ||||||||||||
| 314 | { | ||||||||||||
| 315 | 100 | return ( $_[0] =~ $self->[REGEX] )[0]; | |||||||||||
| 316 | } | ||||||||||||
| 317 | } | ||||||||||||
| 318 | } | ||||||||||||
| 319 | |||||||||||||
| 320 | sub capture | ||||||||||||
| 321 | 8 | ▶ | { | ||||||||||
|
Subroutine
capture: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 322 | 8 | my $self = shift; | |||||||||||
| 323 | |||||||||||||
| 324 | 8 | for my $ref ( @{ $self->[STORE] } ) | |||||||||||
| 325 | { | ||||||||||||
| 326 | 18 | $$ref = shift @_; | |||||||||||
| 327 | } | ||||||||||||
| 328 | 8 | ▶ | if ( wantarray() ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 329 | { | ||||||||||||
| 330 | 2 | return map { $$_ } @{ $self->[STORE] }; | |||||||||||
| 331 | } | ||||||||||||
| 332 | else | ||||||||||||
| 333 | { | ||||||||||||
| 334 | 6 | return ${ ${ $self->[STORE] }[0] }; | |||||||||||
| 335 | } | ||||||||||||
| 336 | } | ||||||||||||
| 337 | |||||||||||||
| 338 | sub compile | ||||||||||||
| 339 | 121 | ▶ | { | ||||||||||
|
Subroutine
compile: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 340 | 121 | my $self = shift; | |||||||||||
| 341 | |||||||||||||
| 342 | 121 | ▶ | if ( my $num = @{ $self->[STACK] } ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 343 | { | ||||||||||||
| 344 | 8 | $self->end($num); | |||||||||||
| 345 | } | ||||||||||||
| 346 | 121 | return qr/$self->[REGEX]/; | |||||||||||
| 347 | } | ||||||||||||
| 348 | |||||||||||||
| 349 | sub debug | ||||||||||||
| 350 | 1 | ▶ | { | ||||||||||
|
Subroutine
debug: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 351 | 1 | my $self = shift; | |||||||||||
| 352 | 1 | return $self->[REGEX]; | |||||||||||
| 353 | } | ||||||||||||
| 354 | |||||||||||||
| 355 | 7 | %EXPORT_TAGS = | |||||||||||
| 356 | ( | ||||||||||||
| 357 | all => [ @char_tags, @export ], | ||||||||||||
| 358 | chars => \@char_tags, | ||||||||||||
| 359 | standard => \@export, | ||||||||||||
| 360 | ); | ||||||||||||
| 361 | |||||||||||||
| 362 | 7 | 1; | |||||||||||
| 363 | |||||||||||||
| 364 | __END__ | ||||||||||||