| 1 | package Locale::Babelfish::Phrase::PluralFormsParser; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: Babelfish plurals syntax parser. | ||||||||||
| 4 | |||||||||||
| 5 | 6 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 6 | ▶ | use feature 'state'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 6 | ▶ | use Locale::Babelfish::Phrase::Parser (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | |||||||||||
| 13 | 6 | our $VERSION = '2.10'; # VERSION | |||||||||
| 14 | |||||||||||
| 15 | 6 | ▶ | use parent qw( Class::Accessor::Fast ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 6 | __PACKAGE__->mk_accessors( qw( phrase strict_forms regular_forms ) ); | |||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | sub new { | ||||||||||
| 21 | 96 | ▶ | my ( $class, $phrase ) = @_; | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 22 | 96 | my $parser = bless {}, $class; | |||||||||
| 23 | 96 | ▶ | $parser->init( $phrase ) if defined $phrase; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 96 | return $parser; | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | sub init { | ||||||||||
| 29 | 23 | ▶ | my ( $self, $phrase ) = @_; | ||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 30 | 23 | $self->phrase( $phrase ); | |||||||||
| 31 | 23 | $self->regular_forms( [] ); | |||||||||
| 32 | 23 | $self->strict_forms( {} ); | |||||||||
| 33 | 23 | return $self; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | sub parse { | ||||||||||
| 38 | 22 | ▶ | my ( $self, $phrase ) = @_; | ||||||||
|
Subroutine
parse: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 39 | |||||||||||
| 40 | 22 | ▶ | $self->init( $phrase ) if defined $phrase; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 22 | state $phrase_parser = Locale::Babelfish::Phrase::Parser->new(); | |||||||||
| 42 | |||||||||||
| 43 | # тут проще регуляркой | ||||||||||
| 44 | 22 | my @forms = split( m/(?<!\\)\|/s, $phrase ); | |||||||||
| 45 | |||||||||||
| 46 | 22 | for my $form ( @forms ) { | |||||||||
| 47 | 58 | my $value = undef; | |||||||||
| 48 | 58 | ▶ | if ( $form =~ m/\A=([0-9]+)\p{PerlSpace}*(.+)\z/s ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 7 | ( $value, $form ) = ( $1, $2 ); | |||||||||
| 50 | } | ||||||||||
| 51 | 58 | $form = $phrase_parser->parse( $form ); | |||||||||
| 52 | |||||||||||
| 53 | 58 | ▶ | if ( defined $value ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 7 | $self->strict_forms->{$value} = $form; | |||||||||
| 55 | } | ||||||||||
| 56 | else { | ||||||||||
| 57 | 51 | push @{ $self->regular_forms }, $form; | |||||||||
| 58 | } | ||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | return { | ||||||||||
| 62 | 22 | strict => $self->strict_forms, | |||||||||
| 63 | regular => $self->regular_forms, | ||||||||||
| 64 | }; | ||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 6 | 1; | |||||||||
| 68 | |||||||||||
| 69 | __END__ | ||||||||||
| 70 | |||||||||||
| 71 | =pod | ||||||||||
| 72 | |||||||||||
| 73 | =encoding UTF-8 | ||||||||||
| 74 | |||||||||||
| 75 | =head1 NAME | ||||||||||
| 76 | |||||||||||
| 77 | Locale::Babelfish::Phrase::PluralFormsParser - Babelfish plurals syntax parser. | ||||||||||
| 78 | |||||||||||
| 79 | =head1 VERSION | ||||||||||
| 80 | |||||||||||
| 81 | version 2.10 | ||||||||||
| 82 | |||||||||||
| 83 | =head1 DESCRIPTION | ||||||||||
| 84 | |||||||||||
| 85 | Returns { script_forms => {}, regular_forms = [] } | ||||||||||
| 86 | |||||||||||
| 87 | Every plural form represented as AST. | ||||||||||
| 88 | |||||||||||
| 89 | =head1 METHODS | ||||||||||
| 90 | |||||||||||
| 91 | =head2 new | ||||||||||
| 92 | |||||||||||
| 93 | $class->new() | ||||||||||
| 94 | $class->new( $phrase ) | ||||||||||
| 95 | |||||||||||
| 96 | Instantiates parser. | ||||||||||
| 97 | |||||||||||
| 98 | =head2 init | ||||||||||
| 99 | |||||||||||
| 100 | Initializes parser. Should not be called directly. | ||||||||||
| 101 | |||||||||||
| 102 | =head2 parse | ||||||||||
| 103 | |||||||||||
| 104 | $parser->parse() | ||||||||||
| 105 | $parser->parse( $phrase ) | ||||||||||
| 106 | |||||||||||
| 107 | Parses specified phrase. | ||||||||||
| 108 | |||||||||||
| 109 | =head1 AUTHORS | ||||||||||
| 110 | |||||||||||
| 111 | =over 4 | ||||||||||
| 112 | |||||||||||
| 113 | =item * | ||||||||||
| 114 | |||||||||||
| 115 | Akzhan Abdulin <akzhan@cpan.org> | ||||||||||
| 116 | |||||||||||
| 117 | =item * | ||||||||||
| 118 | |||||||||||
| 119 | Igor Mironov <grif@cpan.org> | ||||||||||
| 120 | |||||||||||
| 121 | =item * | ||||||||||
| 122 | |||||||||||
| 123 | Victor Efimov <efimov@reg.ru> | ||||||||||
| 124 | |||||||||||
| 125 | =item * | ||||||||||
| 126 | |||||||||||
| 127 | REG.RU LLC | ||||||||||
| 128 | |||||||||||
| 129 | =item * | ||||||||||
| 130 | |||||||||||
| 131 | Kirill Sysoev <k.sysoev@me.com> | ||||||||||
| 132 | |||||||||||
| 133 | =item * | ||||||||||
| 134 | |||||||||||
| 135 | Alexandr Tkach <tkach@reg.ru> | ||||||||||
| 136 | |||||||||||
| 137 | =back | ||||||||||
| 138 | |||||||||||
| 139 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 140 | |||||||||||
| 141 | This software is Copyright (c) 2014 by REG.RU LLC. | ||||||||||
| 142 | |||||||||||
| 143 | This is free software, licensed under: | ||||||||||
| 144 | |||||||||||
| 145 | The MIT (X11) License | ||||||||||
| 146 | |||||||||||
| 147 | =cut | ||||||||||