Babelfish/Phrase/Compiler.pm

filter: Statementstmt 96.9% 63 / 65 Branchbran 68.7% 11 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 92.8% 13 / 14 Podpod 100% 4 / 4 totaltotal 92.3% 96 / 104 CC 11 SCAR 15.9
CC
11
Cov
92%
compile
10.1
new
2.0
throw
2.0
CRAP 4.9
« Babelfish/Phrase/PluralForms.pm ↑ Summary Babelfish/Phrase/PluralFormsParser.pm »
1 package Locale::Babelfish::Phrase::Compiler;
2  
3 # ABSTRACT: Babelfish AST Compiler
4  
5  
6 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use List::Util 1.33 qw( none );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use Locale::Babelfish::Phrase::Literal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Locale::Babelfish::Phrase::Variable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Locale::Babelfish::Phrase::PluralForms ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 ▶use parent qw( Class::Accessor::Fast );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 10 our $VERSION = '2.13'; # VERSION
19  
20 10 __PACKAGE__->mk_accessors( qw( ast ) );
21  
22 10 my $sub_index = 0;
23  
24  
25 6 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
26 6 my ( $class, $ast ) = @_;
27 6 my $parser = bless {}, $class;
28 6 ▶ $parser->init( $ast ) if $ast;
Branch: 1 branch
BranchTrueFalseTotal
if $ast 0 6 6
29 6 return $parser;
30 }
31  
32  
33 265 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
34 265 my ( $self, $ast ) = @_;
35 265 $self->ast( $ast );
36 265 return $self;
37 }
38  
39  
40 0 ▶sub throw {
Subroutine throw: not called CC=1 SCAR=6.9
Pod documented
41 0 my ( $self, $message ) = @_;
42 0 die "Cannot compile: $message";
43 }
44  
45  
46  
47 265 ▶sub compile {
Subroutine compile: called CC=10 SCAR=23.1
Pod documented
48 265 my ( $self, $ast ) = @_;
49  
50 265 ▶ $self->init( $ast ) if $ast;
Branch: 1 branch
BranchTrueFalseTotal
if $ast 265 0 265
51  
52 265 ▶ $self->throw("No AST given") unless $self->ast;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ast 0 265 265
53 265 ▶ $self->throw("Empty AST given") if scalar( @{ $self->ast } ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->ast;} == 0 0 265 265
54  
55 265 ▶ if ( scalar( @{ $self->ast } ) == 1 && ref($self->ast->[0]) eq 'Locale::Babelfish::Phrase::Literal' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->ast;} == 1 and ref $self->ast->[0] eq "Locale::Babelfish::Phrase::Literal") 201 64 265
Condition100%3/3
scalar @{$self->ast;} == 1 and ref $self->ast->[0] eq "Locale::Babelfish::Phrase::Literal"
!ll&&!rl&&r
3232201
MC/DC100%2/2
scalar @{$self->ast;} == 1 and ref $self->ast->[0] eq "Locale::Babelfish::Phrase::Literal"
scalar @{$self->ast;} == 1 ref $self->ast->[0] eq "Locale::Babelfish::Phrase::Literal"
Truth table100%3/3
A and B
A= scalar @{$self->ast;} == 1B= ref $self->ast->[0] eq "Locale::Babelfish::Phrase::Literal"
ABresult
0X0
100
111
56 # просто строка
57 201 return $self->ast->[0]->text;
58 }
59  
60 64 my $text = 'sub { my ( $params ) = @_; return join \'\',';
61 64 for my $node ( @{ $self->ast } ) {
62 163 ▶ if ( ref($node) eq 'Locale::Babelfish::Phrase::Literal' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $node eq 'Locale::Babelfish::Phrase::Literal') { } 70 93 163
elsif (ref $node eq 'Locale::Babelfish::Phrase::Variable') { } 66 27 93
elsif (ref $node eq 'Locale::Babelfish::Phrase::PluralForms') { } 27 0 27
63 70 $text .= $node->to_perl_escaped_str. ',';
64 }
65 elsif ( ref($node) eq 'Locale::Babelfish::Phrase::Variable' ) {
66 66 $text .= "(\$params->{". $node->to_perl_escaped_str. "} // ''),";
67 }
68 elsif ( ref($node) eq 'Locale::Babelfish::Phrase::PluralForms' ) {
69 27 my $sub = $node->to_perl_sub();
70 27 my $index = ++$sub_index;
71 27 my $name = "Locale::Babelfish::Phrase::Compiler::COMPILED_SUB_$index";
72 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
73 27 *{$name} = $sub;
74 10 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
75 27 $text .= "$name(\$params),"
76 }
77 }
78 64 $text .= '\'\'; }';
79 64 return eval $text;
80 }
81  
82 10 1;
83  
84 __END__
« Babelfish/Phrase/PluralForms.pm ↑ Summary Babelfish/Phrase/PluralFormsParser.pm »