Babelfish/Phrase/Parser.pm

filter: Statementstmt 94.4% 120 / 127 Branchbran 86.5% 45 / 52 Conditioncond 83.3% 15 / 18 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 91.1% 205 / 225 CC 33 SCAR 28.2
CC
33
Cov
91%
parse
26.4
finalize_mode
7.5
new
2.0
CRAP 16.7
« Babelfish/Phrase/Pluralizer.pm ↑ Summary Babelfish.pm »
1 package Locale::Babelfish::Phrase::Parser;
2  
3 # ABSTRACT: Babelfish 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  
9 6 ▶use Locale::Babelfish::Phrase::Literal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Locale::Babelfish::Phrase::Variable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Locale::Babelfish::Phrase::PluralForms ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Locale::Babelfish::Phrase::PluralFormsParser ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use parent qw( Locale::Babelfish::Phrase::ParserBase );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 our $VERSION = '2.10'; # VERSION
17  
18 6 __PACKAGE__->mk_accessors( qw( locale mode pieces escape pf0 ) );
19  
20 use constant {
21 6 LITERAL_MODE => 'Locale::Babelfish::Phrase::Literal',
22 VARIABLE_MODE => 'Locale::Babelfish::Phrase::Variable',
23 PLURALS_MODE => 'Locale::Babelfish::Phrase::PluralForms',
24 VARIABLE_RE => qr/^[a-zA-Z0-9_\.]+$/,
25 6 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 sub new {
29 6 ▶ my ( $class, $phrase, $locale ) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
30 6 my $self = $class->SUPER::new( $phrase );
31 6 ▶ $self->locale( $locale ) if $locale;
Branch: 1 branch
BranchTrueFalseTotal
if $locale 0 6 6
32 6 return $self;
33 }
34  
35  
36 sub init {
37 95 ▶ my ( $self, $phrase ) = @_;
Subroutine init: called CC=1 SCAR=0.0
Pod documented
38 95 $self->SUPER::init( $phrase );
39 95 $self->mode( LITERAL_MODE );
40 95 $self->pieces( [] );
41 95 $self->pf0( undef ); # plural forms without name yet
42 95 return $self;
43 }
44  
45  
46 sub finalize_mode {
47 93 ▶ my ( $self ) = @_;
Subroutine finalize_mode: called CC=7 SCAR=20.2
Pod documented
48 93 ▶ if ( $self->mode eq LITERAL_MODE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->mode eq 'Locale::Babelfish::Phrase::Literal') { } 88 5 93
elsif ($self->mode eq 'Locale::Babelfish::Phrase::Variable') { } 0 5 5
elsif ($self->mode eq 'Locale::Babelfish::Phrase::PluralForms') { } 5 0 5
49 77 push @{ $self->pieces }, LITERAL_MODE->new( text => $self->piece )
50 88 ▶ if length($self->piece) || scalar(@{ $self->pieces }) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->piece or scalar @{$self->pieces;} == 0 77 11 88
Condition100%3/3
length $self->piece or scalar @{$self->pieces;} == 0
l!l&&r!l&&!r
76111
MC/DC100%2/2
length $self->piece or scalar @{$self->pieces;} == 0
length $self->piece scalar @{$self->pieces;} == 0
Truth table100%3/3
A or B
A= length $self->pieceB= scalar @{$self->pieces;} == 0
ABresult
1X1
011
000
51 }
52 elsif ( $self->mode eq VARIABLE_MODE ) {
53 0 $self->throw( "Variable definition not ended with \"}\": ". $self->piece );
54 }
55 elsif ( $self->mode eq PLURALS_MODE ) {
56 5 ▶ $self->throw( "Plural forms definition not ended with \"))\": ". $self->piece )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->pf0 0 5 5
57 unless defined $self->pf0;
58 5 push @{ $self->pieces }, PLURALS_MODE->new( forms => $self->pf0, name => $self->piece, locale => $self->locale, );
59 }
60 else {
61 0 $self->throw( "Logic broken, unknown parser mode: ". $self->mode );
62 }
63 }
64  
65  
66 sub parse {
67 96 ▶ my ( $self, $phrase, $locale ) = @_;
Subroutine parse: called CC=26 SCAR=32.7
Pod documented
68  
69 96 $self->SUPER::parse( $phrase );
70 95 ▶ $self->locale( $locale ) if $locale;
Branch: 1 branch
BranchTrueFalseTotal
if $locale 28 67 95
71  
72 95 my $plurals_parser = Locale::Babelfish::Phrase::PluralFormsParser->new();
73  
74 95 while ( 1 ) {
75 1500 my $char = $self->to_next_char;
76  
77 1500 ▶ unless ( length $char ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $char) 93 1407 1500
78 93 $self->finalize_mode;
79 93 return $self->pieces;
80 }
81  
82 1407 ▶ if ( $self->mode eq LITERAL_MODE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "Locale::Babelfish::Phrase::Literal") 820 587 1407
83 820 ▶ if ( $self->escape ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->escape) 8 812 820
84 8 $self->add_to_piece( $char );
85 8 $self->escape(0);
86 8 next;
87 }
88  
89 812 ▶ if ( $char eq "\\" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "\\") 8 804 812
90 8 $self->escape( 1 );
91 8 next;
92 }
93  
94 804 ▶ if ( $char eq '#' && $self->next_char eq '{' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "#" and $self->next_char eq "{") 28 776 804
Condition66%2/3
$char eq "#" and $self->next_char eq "{"
!ll&&!rl&&r
776028
MC/DC50%1/2
$char eq "#" and $self->next_char eq "{"
$char eq "#" $self->next_char eq "{"
Truth table66%2/3
A and B
A= $char eq "#"B= $self->next_char eq "{"
ABresult
0X0
100
111
95 28 ▶ if ( length $self->piece ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->piece) 24 4 28
96 24 push @{ $self->pieces }, LITERAL_MODE->new( text => $self->piece );
97 24 $self->piece('');
98 }
99 28 $self->to_next_char; # skip "{"
100 28 $self->mode( VARIABLE_MODE );
101 28 next;
102 }
103  
104 776 ▶ if ( $char eq '(' && $self->next_char eq '(' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "(" and $self->next_char eq "(") 17 759 776
Condition100%3/3
$char eq "(" and $self->next_char eq "("
!ll&&!rl&&r
758117
MC/DC100%2/2
$char eq "(" and $self->next_char eq "("
$char eq "(" $self->next_char eq "("
Truth table100%3/3
A and B
A= $char eq "("B= $self->next_char eq "("
ABresult
0X0
100
111
105 17 ▶ if ( length $self->piece ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->piece) 17 0 17
106 17 push @{ $self->pieces }, LITERAL_MODE->new( text => $self->piece );
107 17 $self->piece('');
108 }
109 17 $self->to_next_char; # skip second "("
110 17 $self->mode( PLURALS_MODE );
111 17 next;
112 }
113 }
114  
115 1346 ▶ if ( $self->mode eq VARIABLE_MODE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "Locale::Babelfish::Phrase::Variable") 171 1175 1346
116 171 ▶ if ( $self->escape ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->escape) 0 171 171
117 0 $self->add_to_piece( $char );
118 0 $self->escape(0);
119 0 next;
120 }
121  
122 171 ▶ if ( $char eq "\\" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "\\") 0 171 171
123 0 $self->escape( 1 );
124 0 next;
125 }
126  
127 171 ▶ if ( $char eq '}' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "}") 28 143 171
128 28 my $name = $self->trim( $self->piece );
129 28 ▶ unless ( length $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $name) 1 27 28
130 1 $self->throw( "No variable name given." );
131 }
132 27 ▶ if ( $name !~ VARIABLE_RE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /^[a-zA-Z0-9_\.]+$/) 1 26 27
133 1 $self->throw( "Variable name doesn't meet conditions: $name." );
134 }
135 26 push @{ $self->pieces }, VARIABLE_MODE->new( name => $name );
136 26 $self->piece('');
137 26 $self->mode( LITERAL_MODE );
138 26 next;
139 }
140 }
141  
142 1318 ▶ if ( $self->mode eq PLURALS_MODE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "Locale::Babelfish::Phrase::PluralForms") 416 902 1318
143 416 ▶ if ( defined $self->pf0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->pf0) 62 354 416
144 62 ▶ if ( $char =~ VARIABLE_RE && ($char ne '.' || $self->next_char =~ VARIABLE_RE) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /^[a-zA-Z0-9_\.]+$/ and $char ne '.' || $self->next_char =~ /^[a-zA-Z0-9_\.]+$/) { } 54 8 62
Condition83%5/6
$char =~ /^[a-zA-Z0-9_\.]+$/ and $char ne "." || $self->next_char =~ /^[a-zA-Z0-9_\.]+$/
!ll&&!rl&&r
7154
$char ne "." or $self->next_char =~ /^[a-zA-Z0-9_\.]+$/
l!l&&r!l&&!r
5401
MC/DC66%2/3
$char =~ /^[a-zA-Z0-9_\.]+$/ and $char ne "." || $self->next_char =~ /^[a-zA-Z0-9_\.]+$/
$char =~ /^[a-zA-Z0-9_\.]+$/ $char ne "." $self->next_char =~ /^[a-zA-Z0-9_\.]+$/
Truth table75%3/4
A and B or C
A= $char =~ /^[a-zA-Z0-9_\.]+$/B= $char ne "."C= $self->next_char =~ /^[a-zA-Z0-9_\.]+$/
ABCresult
0XX0
1000
11X1
1011
145 54 $self->add_to_piece( $char );
146 54 next;
147 }
148 else {
149 8 push @{ $self->pieces }, PLURALS_MODE->new( forms => $self->pf0, name => $self->piece, locale => $self->locale, );
150 8 $self->pf0( undef );
151 8 $self->mode( LITERAL_MODE );
152 8 $self->piece('');
153 8 $self->backward;
154 8 next;
155 }
156 }
157 354 ▶ if ( $char eq ')' && $self->next_char eq ')' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq ")" and $self->next_char eq ")") 17 337 354
Condition66%2/3
$char eq ")" and $self->next_char eq ")"
!ll&&!rl&&r
337017
MC/DC50%1/2
$char eq ")" and $self->next_char eq ")"
$char eq ")" $self->next_char eq ")"
Truth table66%2/3
A and B
A= $char eq ")"B= $self->next_char eq ")"
ABresult
0X0
100
111
158 17 $self->pf0( $plurals_parser->parse( $self->piece ) );
159 17 $self->piece('');
160 17 $self->to_next_char; # skip second ")"
161 17 ▶ if ( $self->next_char eq ':' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->next_char eq ":") 13 4 17
162 13 $self->to_next_char; # skip ":"
163 13 next;
164 }
165 4 push @{ $self->pieces }, PLURALS_MODE->new( forms => $self->pf0, name => 'count', locale => $self->locale, );
166 4 $self->pf0( undef );
167 4 $self->mode( LITERAL_MODE );
168 4 next;
169 }
170 }
171 1239 $self->add_to_piece( $char );
172 } # while ( 1 )
173 }
174  
175 6 1;
176  
177 __END__
178  
179 =pod
180  
181 =encoding UTF-8
182  
183 =head1 NAME
184  
185 Locale::Babelfish::Phrase::Parser - Babelfish syntax parser.
186  
187 =head1 VERSION
188  
189 version 2.10
190  
191 =head1 METHODS
192  
193 =head2 new
194  
195 $class->new()
196 $class->new( $phrase )
197  
198 Instantiates parser.
199  
200 =head2 init
201  
202 Initializes parser. Should not be called directly.
203  
204 =head2 finalize_mode
205  
206 Finalizes all operations after phrase end.
207  
208 =head2 parse
209  
210 $parser->parse()
211 $parser->parse( $phrase )
212  
213 Parses specified phrase.
214  
215 =head1 AUTHORS
216  
217 =over 4
218  
219 =item *
220  
221 Akzhan Abdulin <akzhan@cpan.org>
222  
223 =item *
224  
225 Igor Mironov <grif@cpan.org>
226  
227 =item *
228  
229 Victor Efimov <efimov@reg.ru>
230  
231 =item *
232  
233 REG.RU LLC
234  
235 =item *
236  
237 Kirill Sysoev <k.sysoev@me.com>
238  
239 =item *
240  
241 Alexandr Tkach <tkach@reg.ru>
242  
243 =back
244  
245 =head1 COPYRIGHT AND LICENSE
246  
247 This software is Copyright (c) 2014 by REG.RU LLC.
248  
249 This is free software, licensed under:
250  
251 The MIT (X11) License
252  
253 =cut
« Babelfish/Phrase/Pluralizer.pm ↑ Summary Babelfish.pm »