| 1 | package Locale::Babelfish::Phrase::PluralFormsParser; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: Babelfish plurals syntax parser. | ||||||||||
| 4 | |||||||||||
| 5 | 7 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 7 | ▶ | use feature 'state'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 7 | ▶ | use Locale::Babelfish::Phrase::Parser (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | |||||||||||
| 13 | 7 | our $VERSION = '2.13'; # VERSION | |||||||||
| 14 | |||||||||||
| 15 | 7 | ▶ | use parent qw( Class::Accessor::Fast ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 7 | __PACKAGE__->mk_accessors( qw( phrase strict_forms regular_forms ) ); | |||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | 292 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 21 | 292 | my ( $class, $phrase ) = @_; | |||||||||
| 22 | 292 | my $parser = bless {}, $class; | |||||||||
| 23 | 292 | ▶ | $parser->init( $phrase ) if defined $phrase; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 292 | return $parser; | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | 35 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 29 | 35 | my ( $self, $phrase ) = @_; | |||||||||
| 30 | 35 | $self->phrase( $phrase ); | |||||||||
| 31 | 35 | $self->regular_forms( [] ); | |||||||||
| 32 | 35 | $self->strict_forms( {} ); | |||||||||
| 33 | 35 | return $self; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | 34 | ▶ | sub parse { | ||||||||
|
Subroutine
parse: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 38 | 34 | my ( $self, $phrase ) = @_; | |||||||||
| 39 | |||||||||||
| 40 | 34 | ▶ | $self->init( $phrase ) if defined $phrase; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 34 | state $phrase_parser = Locale::Babelfish::Phrase::Parser->new(); | |||||||||
| 42 | |||||||||||
| 43 | # тут проще регуляркой | ||||||||||
| 44 | 34 | my @forms = split( m/(?<!\\)\|/s, $phrase ); | |||||||||
| 45 | |||||||||||
| 46 | 34 | for my $form ( @forms ) { | |||||||||
| 47 | 92 | my $value = undef; | |||||||||
| 48 | 92 | ▶ | if ( $form =~ m/\A=([0-9]+)\p{PerlSpace}*(.+)\z/s ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 7 | ( $value, $form ) = ( $1, $2 ); | |||||||||
| 50 | } | ||||||||||
| 51 | 92 | $form = $phrase_parser->parse( $form ); | |||||||||
| 52 | |||||||||||
| 53 | 92 | ▶ | if ( defined $value ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 7 | $self->strict_forms->{$value} = $form; | |||||||||
| 55 | } | ||||||||||
| 56 | else { | ||||||||||
| 57 | 85 | push @{ $self->regular_forms }, $form; | |||||||||
| 58 | } | ||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | return { | ||||||||||
| 62 | 34 | strict => $self->strict_forms, | |||||||||
| 63 | regular => $self->regular_forms, | ||||||||||
| 64 | }; | ||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 7 | 1; | |||||||||
| 68 | |||||||||||
| 69 | __END__ | ||||||||||