Babelfish/Phrase/Compiler.pm

filter: Statementstmt 96.9% 63 / 65 Branchbran 68.7% 11 / 16 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 92.8% 13 / 14 Podpod 100% 4 / 4 totaltotal 90.3% 94 / 104 CC 11 SCAR 16.0
CC
11
Cov
90%
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 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use List::Util 1.33 qw( none );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use Locale::Babelfish::Phrase::Literal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Locale::Babelfish::Phrase::Variable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Locale::Babelfish::Phrase::PluralForms ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 ▶use parent qw( Class::Accessor::Fast );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 our $VERSION = '2.10'; # VERSION
19  
20 9 __PACKAGE__->mk_accessors( qw( ast ) );
21  
22 9 my $sub_index = 0;
23  
24  
25 sub new {
26 5 ▶ my ( $class, $ast ) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
27 5 my $parser = bless {}, $class;
28 5 ▶ $parser->init( $ast ) if $ast;
Branch: 1 branch
BranchTrueFalseTotal
if $ast 0 5 5
29 5 return $parser;
30 }
31  
32  
33 sub init {
34 69 ▶ my ( $self, $ast ) = @_;
Subroutine init: called CC=1 SCAR=0.0
Pod documented
35 69 $self->ast( $ast );
36 69 return $self;
37 }
38  
39  
40 sub throw {
41 0 ▶ my ( $self, $message ) = @_;
Subroutine throw: not called CC=1 SCAR=6.9
Pod documented
42 0 die "Cannot compile: $message";
43 }
44  
45  
46  
47 sub compile {
48 69 ▶ my ( $self, $ast ) = @_;
Subroutine compile: called CC=10 SCAR=23.2
Pod documented
49  
50 69 ▶ $self->init( $ast ) if $ast;
Branch: 1 branch
BranchTrueFalseTotal
if $ast 69 0 69
51  
52 69 ▶ $self->throw("No AST given") unless $self->ast;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ast 0 69 69
53 69 ▶ $self->throw("Empty AST given") if scalar( @{ $self->ast } ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->ast;} == 0 0 69 69
54  
55 69 ▶ 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") 47 22 69
Condition66%2/3
scalar @{$self->ast;} == 1 and ref $self->ast->[0] eq "Locale::Babelfish::Phrase::Literal"
!ll&&!rl&&r
22047
MC/DC50%1/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 table66%2/3
A and B
A= scalar @{$self->ast;} == 1B= ref $self->ast->[0] eq "Locale::Babelfish::Phrase::Literal"
ABresult
0X0
100
111
56 # просто строка
57 47 return $self->ast->[0]->text;
58 }
59  
60 22 my $text = 'sub { my ( $params ) = @_; return join \'\',';
61 22 for my $node ( @{ $self->ast } ) {
62 81 ▶ if ( ref($node) eq 'Locale::Babelfish::Phrase::Literal' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $node eq 'Locale::Babelfish::Phrase::Literal') { } 42 39 81
elsif (ref $node eq 'Locale::Babelfish::Phrase::Variable') { } 24 15 39
elsif (ref $node eq 'Locale::Babelfish::Phrase::PluralForms') { } 15 0 15
63 42 $text .= $node->to_perl_escaped_str. ',';
64 }
65 elsif ( ref($node) eq 'Locale::Babelfish::Phrase::Variable' ) {
66 24 $text .= "(\$params->{". $node->to_perl_escaped_str. "} // ''),";
67 }
68 elsif ( ref($node) eq 'Locale::Babelfish::Phrase::PluralForms' ) {
69 15 my $sub = $node->to_perl_sub();
70 15 my $index = ++$sub_index;
71 15 my $name = "Locale::Babelfish::Phrase::Compiler::COMPILED_SUB_$index";
72 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
73 15 *{$name} = $sub;
74 9 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
75 15 $text .= "$name(\$params),"
76 }
77 }
78 22 $text .= '\'\'; }';
79 22 return eval $text;
80 }
81  
82 9 1;
83  
84 __END__
85  
86 =pod
87  
88 =encoding UTF-8
89  
90 =head1 NAME
91  
92 Locale::Babelfish::Phrase::Compiler - Babelfish AST Compiler
93  
94 =head1 VERSION
95  
96 version 2.10
97  
98 =head1 DESCRIPTION
99  
100 Compiles AST to string or to coderef.
101  
102 =head1 METHODS
103  
104 =head2 new
105  
106 $class->new()
107 $class->new( $ast )
108  
109 Instantiates AST compiler.
110  
111 =head2 init
112  
113 Initializes compiler. Should not be called directly.
114  
115 =head2 throw
116  
117 $self->throw( $message )
118  
119 Throws given message in compiler context.
120  
121 =head2 compile
122  
123 $self->compile()
124 $self->compile( $ast )
125  
126 Compiles AST.
127  
128 Result is string when possible; coderef otherwise.
129  
130 =head1 AUTHORS
131  
132 =over 4
133  
134 =item *
135  
136 Akzhan Abdulin <akzhan@cpan.org>
137  
138 =item *
139  
140 Igor Mironov <grif@cpan.org>
141  
142 =item *
143  
144 Victor Efimov <efimov@reg.ru>
145  
146 =item *
147  
148 REG.RU LLC
149  
150 =item *
151  
152 Kirill Sysoev <k.sysoev@me.com>
153  
154 =item *
155  
156 Alexandr Tkach <tkach@reg.ru>
157  
158 =back
159  
160 =head1 COPYRIGHT AND LICENSE
161  
162 This software is Copyright (c) 2014 by REG.RU LLC.
163  
164 This is free software, licensed under:
165  
166 The MIT (X11) License
167  
168 =cut
« Babelfish/Phrase/PluralForms.pm ↑ Summary Babelfish/Phrase/PluralFormsParser.pm »