File Coverage

blib/lib/Locale/Babelfish/Phrase/PluralFormsParser.pm
Criterion Covered Total %
statement 41 41 100.0
branch 6 8 75.0
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 59 61 96.7


line stmt bran cond sub pod time code
1             package Locale::Babelfish::Phrase::PluralFormsParser;
2              
3             # ABSTRACT: Babelfish plurals syntax parser.
4              
5 7     7   419290 use utf8;
  7         350  
  7         47  
6 7     7   380 use strict;
  7         15  
  7         406  
7 7     7   48 use warnings;
  7         39  
  7         419  
8 7     7   58 use feature 'state';
  7         13  
  7         955  
9              
10 7     7   986 use Locale::Babelfish::Phrase::Parser ();
  7         15  
  7         439  
11              
12              
13             our $VERSION = '2.13'; # VERSION
14              
15 7     7   58 use parent qw( Class::Accessor::Fast );
  7         12  
  7         50  
16              
17             __PACKAGE__->mk_accessors( qw( phrase strict_forms regular_forms ) );
18              
19              
20             sub new {
21 292     292 1 284475 my ( $class, $phrase ) = @_;
22 292         819 my $parser = bless {}, $class;
23 292 50       805 $parser->init( $phrase ) if defined $phrase;
24 292         769 return $parser;
25             }
26              
27              
28             sub init {
29 35     35 1 795 my ( $self, $phrase ) = @_;
30 35         964 $self->phrase( $phrase );
31 35         1047 $self->regular_forms( [] );
32 35         990 $self->strict_forms( {} );
33 35         270 return $self;
34             }
35              
36              
37             sub parse {
38 34     34 1 41656 my ( $self, $phrase ) = @_;
39              
40 34 50       208 $self->init( $phrase ) if defined $phrase;
41 34         101 state $phrase_parser = Locale::Babelfish::Phrase::Parser->new();
42              
43             # тут проще регуляркой
44 34         260 my @forms = split( m/(?<!\\)\|/s, $phrase );
45              
46 34         133 for my $form ( @forms ) {
47 92         417 my $value = undef;
48 92 100       299 if ( $form =~ m/\A=([0-9]+)\p{PerlSpace}*(.+)\z/s ) {
49 7         29 ( $value, $form ) = ( $1, $2 );
50             }
51 92         312 $form = $phrase_parser->parse( $form );
52              
53 92 100       714 if ( defined $value ) {
54 7         119 $self->strict_forms->{$value} = $form;
55             }
56             else {
57 85         150 push @{ $self->regular_forms }, $form;
  85         1714  
58             }
59             }
60              
61             return {
62 34         981 strict => $self->strict_forms,
63             regular => $self->regular_forms,
64             };
65             }
66              
67             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.13
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