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 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  
9 7 ▶use Locale::Babelfish::Phrase::Literal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Locale::Babelfish::Phrase::Variable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Locale::Babelfish::Phrase::PluralForms ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Locale::Babelfish::Phrase::PluralFormsParser ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 ▶use parent qw( Locale::Babelfish::Phrase::ParserBase );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 7 our $VERSION = '2.13'; # VERSION
17  
18 7 __PACKAGE__->mk_accessors( qw( locale mode pieces escape pf0 ) );
19  
20 7 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 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 7 };
26  
27  
28 7 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
29 7 my ( $class, $phrase, $locale ) = @_;
30 7 my $self = $class->SUPER::new( $phrase );
31 7 ▶ $self->locale( $locale ) if $locale;
Branch: 1 branch
BranchTrueFalseTotal
if $locale 0 7 7
32 7 return $self;
33 }
34  
35  
36 291 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
37 291 my ( $self, $phrase ) = @_;
38 291 $self->SUPER::init( $phrase );
39 291 $self->mode( LITERAL_MODE );
40 291 $self->pieces( [] );
41 291 $self->pf0( undef ); # plural forms without name yet
42 291 return $self;
43 }
44  
45  
46 289 ▶sub finalize_mode {
Subroutine finalize_mode: called CC=7 SCAR=20.2
Pod documented
47 289 my ( $self ) = @_;
48 289 ▶ if ( $self->mode eq LITERAL_MODE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->mode eq 'Locale::Babelfish::Phrase::Literal') { } 282 7 289
elsif ($self->mode eq 'Locale::Babelfish::Phrase::Variable') { } 0 7 7
elsif ($self->mode eq 'Locale::Babelfish::Phrase::PluralForms') { } 7 0 7
49 237 push @{ $self->pieces }, LITERAL_MODE->new( text => $self->piece )
50 282 ▶ if length($self->piece) || scalar(@{ $self->pieces }) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->piece or scalar @{$self->pieces;} == 0 237 45 282
Condition100%3/3
length $self->piece or scalar @{$self->pieces;} == 0
l!l&&r!l&&!r
236145
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 7 ▶ $self->throw( "Plural forms definition not ended with \"))\": ". $self->piece )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->pf0 0 7 7
57 unless defined $self->pf0;
58 7 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 292 ▶sub parse {
Subroutine parse: called CC=26 SCAR=32.7
Pod documented
67 292 my ( $self, $phrase, $locale ) = @_;
68  
69 292 $self->SUPER::parse( $phrase );
70 291 ▶ $self->locale( $locale ) if $locale;
Branch: 1 branch
BranchTrueFalseTotal
if $locale 190 101 291
71  
72 291 my $plurals_parser = Locale::Babelfish::Phrase::PluralFormsParser->new();
73  
74 291 while ( 1 ) {
75 3570 my $char = $self->to_next_char;
76  
77 3570 ▶ unless ( length $char ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $char) 289 3281 3570
78 289 $self->finalize_mode;
79 289 return $self->pieces;
80 }
81  
82 3281 ▶ if ( $self->mode eq LITERAL_MODE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "Locale::Babelfish::Phrase::Literal") 2014 1267 3281
83 2014 ▶ if ( $self->escape ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->escape) 10 2004 2014
84 10 $self->add_to_piece( $char );
85 10 $self->escape(0);
86 10 next;
87 }
88  
89 2004 ▶ if ( $char eq "\\" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "\\") 10 1994 2004
90 10 $self->escape( 1 );
91 10 next;
92 }
93  
94 1994 ▶ if ( $char eq '#' && $self->next_char eq '{' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "#" and $self->next_char eq "{") 70 1924 1994
Condition66%2/3
$char eq "#" and $self->next_char eq "{"
!ll&&!rl&&r
1924070
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 70 ▶ if ( length $self->piece ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->piece) 34 36 70
96 34 push @{ $self->pieces }, LITERAL_MODE->new( text => $self->piece );
97 34 $self->piece('');
98 }
99 70 $self->to_next_char; # skip "{"
100 70 $self->mode( VARIABLE_MODE );
101 70 next;
102 }
103  
104 1924 ▶ if ( $char eq '(' && $self->next_char eq '(' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "(" and $self->next_char eq "(") 29 1895 1924
Condition100%3/3
$char eq "(" and $self->next_char eq "("
!ll&&!rl&&r
1894129
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 29 ▶ if ( length $self->piece ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->piece) 29 0 29
106 29 push @{ $self->pieces }, LITERAL_MODE->new( text => $self->piece );
107 29 $self->piece('');
108 }
109 29 $self->to_next_char; # skip second "("
110 29 $self->mode( PLURALS_MODE );
111 29 next;
112 }
113 }
114  
115 3162 ▶ if ( $self->mode eq VARIABLE_MODE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "Locale::Babelfish::Phrase::Variable") 569 2593 3162
116 569 ▶ if ( $self->escape ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->escape) 0 569 569
117 0 $self->add_to_piece( $char );
118 0 $self->escape(0);
119 0 next;
120 }
121  
122 569 ▶ if ( $char eq "\\" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "\\") 0 569 569
123 0 $self->escape( 1 );
124 0 next;
125 }
126  
127 569 ▶ if ( $char eq '}' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "}") 70 499 569
128 70 my $name = $self->trim( $self->piece );
129 70 ▶ unless ( length $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $name) 1 69 70
130 1 $self->throw( "No variable name given." );
131 }
132 69 ▶ if ( $name !~ VARIABLE_RE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /^[a-zA-Z0-9_\.]+$/) 1 68 69
133 1 $self->throw( "Variable name doesn't meet conditions: $name." );
134 }
135 68 push @{ $self->pieces }, VARIABLE_MODE->new( name => $name );
136 68 $self->piece('');
137 68 $self->mode( LITERAL_MODE );
138 68 next;
139 }
140 }
141  
142 3092 ▶ if ( $self->mode eq PLURALS_MODE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "Locale::Babelfish::Phrase::PluralForms") 698 2394 3092
143 698 ▶ if ( defined $self->pf0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->pf0) 120 578 698
144 120 ▶ 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_\.]+$/) { } 102 18 120
Condition83%5/6
$char =~ /^[a-zA-Z0-9_\.]+$/ and $char ne "." || $self->next_char =~ /^[a-zA-Z0-9_\.]+$/
!ll&&!rl&&r
171102
$char ne "." or $self->next_char =~ /^[a-zA-Z0-9_\.]+$/
l!l&&r!l&&!r
10201
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 102 $self->add_to_piece( $char );
146 102 next;
147 }
148 else {
149 18 push @{ $self->pieces }, PLURALS_MODE->new( forms => $self->pf0, name => $self->piece, locale => $self->locale, );
150 18 $self->pf0( undef );
151 18 $self->mode( LITERAL_MODE );
152 18 $self->piece('');
153 18 $self->backward;
154 18 next;
155 }
156 }
157 578 ▶ if ( $char eq ')' && $self->next_char eq ')' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq ")" and $self->next_char eq ")") 29 549 578
Condition66%2/3
$char eq ")" and $self->next_char eq ")"
!ll&&!rl&&r
549029
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 29 $self->pf0( $plurals_parser->parse( $self->piece ) );
159 29 $self->piece('');
160 29 $self->to_next_char; # skip second ")"
161 29 ▶ if ( $self->next_char eq ':' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->next_char eq ":") 25 4 29
162 25 $self->to_next_char; # skip ":"
163 25 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 2943 $self->add_to_piece( $char );
172 } # while ( 1 )
173 }
174  
175 7 1;
176  
177 __END__
« Babelfish/Phrase/Pluralizer.pm ↑ Summary Babelfish.pm »