File Coverage

blib/lib/Locale/Babelfish/Phrase/PluralFormsParser.pm
Criterion Covered Total %
statement 44 44 100.0
branch 6 8 75.0
condition n/a
subroutine 10 10 100.0
pod 3 3 100.0
total 63 65 96.9


line stmt bran cond sub pod time code
1             package Locale::Babelfish::Phrase::PluralFormsParser;
2              
3             # ABSTRACT: Babelfish plurals syntax parser.
4              
5 3     3   103053 use utf8;
  3         13  
  3         15  
6 3     3   86 use strict;
  3         6  
  3         49  
7 3     3   12 use warnings;
  3         4  
  3         65  
8 3     3   13 use feature 'state';
  3         5  
  3         175  
9              
10 3     3   260 use Locale::Babelfish::Phrase::Parser ();
  3         6  
  3         106  
11              
12              
13             our $VERSION = '2.003'; # VERSION
14              
15 3     3   13 use parent qw( Class::Accessor::Fast );
  3         6  
  3         15  
16              
17             __PACKAGE__->mk_accessors( qw( phrase strict_forms regular_forms ) );
18              
19              
20             sub new {
21 80     80 1 2700 my ( $class, $phrase ) = @_;
22 80         160 my $parser = bless {}, $class;
23 80 50       167 $parser->init( $phrase ) if defined $phrase;
24 80         156 return $parser;
25             }
26              
27              
28             sub init {
29 23     23 1 718 my ( $self, $phrase ) = @_;
30 23         422 $self->phrase( $phrase );
31 23         535 $self->regular_forms( [] );
32 23         495 $self->strict_forms( {} );
33 23         152 return $self;
34             }
35              
36              
37             sub parse {
38 22     22 1 35903 my ( $self, $phrase ) = @_;
39              
40 22 50       112 $self->init( $phrase ) if defined $phrase;
41 22         71 state $phrase_parser = Locale::Babelfish::Phrase::Parser->new();
42              
43             # тут проще регуляркой
44 22         153 my @forms = split( m/(?<!\\)\|/s, $phrase );
45              
46 22         60 for my $form ( @forms ) {
47 58         225 my $value = undef;
48 2 100   2   501 if ( $form =~ m/\A=([0-9]+)\p{PerlSpace}*(.+)\z/s ) {
  2         4  
  2         23  
  58         151  
49 7         32 ( $value, $form ) = ( $1, $2 );
50             }
51 58         184 $form = $phrase_parser->parse( $form );
52              
53 58 100       362 if ( defined $value ) {
54 7         107 $self->strict_forms->{$value} = $form;
55             }
56             else {
57 51         77 push @{ $self->regular_forms }, $form;
  51         748  
58             }
59             }
60              
61             return {
62 22         424 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.003
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             =back
130              
131             =head1 COPYRIGHT AND LICENSE
132              
133             This software is Copyright (c) 2014 by REG.RU LLC.
134              
135             This is free software, licensed under:
136              
137             The MIT (X11) License
138              
139             =cut