Template/Parser/Context.pm

filter: Statementstmt 94.9% 112 / 118 Branchbran 94.6% 53 / 56 Conditioncond 76.4% 13 / 17 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 93.7% 15 / 16 Podpod 100% 3 / 3 totaltotal 91.8% 202 / 220 CC 36 SCAR 18.0
CC
36
Cov
93%
_in_script
10.0
_in_init
9.4
read_line
7.0
CRAP 6.0
« Template/JSAdapter.pm ↑ Summary Template/Test.pm »
1 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 EJS::Template::Parser::Context - Implementation of EJS::Template::Parser
7  
8 =cut
9  
10 package EJS::Template::Parser::Context;
11 8 ▶use base 'EJS::Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use EJS::Template::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 my $states = {
16 'INIT' => {
17 key => 'INIT', method => '_in_init',
18 js_open => '', js_close => '',
19 },
20 'TEXT' => {
21 key => 'TEXT', method => '_in_text',
22 js_open => 'print("', js_close => '");',
23 },
24 '<%' => {
25 key => '<%' , method => '_in_script',
26 js_open => '', js_close => '',
27 },
28 '<%=' => {
29 key => '<%=' , method => '_in_script',
30 js_open => 'print(' , js_close => ');',
31 },
32 };
33  
34 =head1 Methods
35  
36 =head2 new
37  
38 Creates a new parser context.
39  
40 =cut
41  
42 83 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
43 83 my ($class, $config) = @_;
44 83 my $self = $class->SUPER::new($config);
45
46 83 $self->{stack} = [];
47 83 $self->{result} = [];
48
49 83 $self->{default_escape} = do {
50 83 ▶ my $name = $self->config('escape') || '';
Condition100%2/2
$self->config('escape') || ''
l!l
677
MC/DC100%1/1
$self->config('escape') || ''
$self->config('escape')
Truth table100%2/2
A
A= $self->config('escape')
Aresult
11
00
51
52 83 ▶ if ($name eq '' || $name eq 'raw') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq '' or $name eq 'raw') { } 78 5 83
Condition100%3/3
$name eq "" or $name eq "raw"
l!l&&r!l&&!r
7715
MC/DC100%2/2
$name eq "" or $name eq "raw"
$name eq "" $name eq "raw"
Truth table100%3/3
A or B
A= $name eq ""B= $name eq "raw"
ABresult
1X1
011
000
53 78 '';
54 } else {
55 5 ▶ $EJS::Template::Runtime::ESCAPES{$name} || '';
Condition50%1/2
$EJS::Template::Runtime::ESCAPES{$name} || ''
l!l
50
MC/DC0%0/1
$EJS::Template::Runtime::ESCAPES{$name} || ''
$EJS::Template::Runtime::ESCAPES{$name}
Truth table50%1/2
A
A= $EJS::Template::Runtime::ESCAPES{$name}
Aresult
11
00
56 }
57 };
58
59 83 $self->_push_state('INIT');
60 83 return $self;
61 }
62  
63 =head2 read_line
64  
65 Parses a line.
66  
67 =cut
68  
69 143 ▶sub read_line {
Subroutine read_line: called CC=7 SCAR=19.5
Pod documented
70 143 my ($self, $line) = @_;
71
72 143 ▶ while ($line =~ m{(.*?)((^\s*)?<%(?:(?:\s*:\s*\w+\s*)?=)?|%>(\s*?(?:\n|$))?|\\?["']|/\*|\*/|//|\n|$)}g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[(.*?)((^\s*)?<%(?:(?:\s*:\s*\w+\s*)?=)?|%>(\s*?(?:\n|$))?|\\?["']|/\*|\*/|//|\n|$)]g) 468 143 611
73 468 my ($text, $mark, $left, $right) = ($1, $2, $3, $4);
74 468 my $escape;
75
76 468 ▶ if ($mark =~ s/<%\s*:\s*(\w+)\s*=/<%=/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mark =~ s/<%\s*:\s*(\w+)\s*=/<%=/) { } 3 465 468
elsif ($mark eq '<%=') { } 45 420 465
77 3 my $name = $1;
78 3 ▶ $escape = $EJS::Template::Runtime::ESCAPES{$name} || '';
Condition50%1/2
$EJS::Template::Runtime::ESCAPES{$name} || ''
l!l
30
MC/DC0%0/1
$EJS::Template::Runtime::ESCAPES{$name} || ''
$EJS::Template::Runtime::ESCAPES{$name}
Truth table50%1/2
A
A= $EJS::Template::Runtime::ESCAPES{$name}
Aresult
11
00
79 } elsif ($mark eq '<%=') {
80 45 $escape = $self->{default_escape};
81 }
82
83 468 $mark =~ s/\s+(<%=?)/$1/;
84 468 $mark =~ s/(%>)\s+/$1/;
85
86 468 my $opt = {
87 left => $left ,
88 right => $right ,
89 escape => $escape,
90 };
91
92 468 ▶ $self->_handle_token($text) if $text ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $text ne "" 231 237 468
93 468 ▶ $self->_handle_token($mark, $opt) if $mark ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $mark ne "" 319 149 468
94 }
95 }
96  
97 =head2 result
98  
99 Retrieves the result (an array ref of the generated JavaScript texts).
100  
101 =cut
102  
103 83 ▶sub result {
Subroutine result: called CC=2 SCAR=6.9
Pod documented
104 83 my ($self) = @_;
105 83 ▶ $self->_pop_state() until @{$self->{stack}} <= 1;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{"stack"};} <= 1 or do { $self->_pop_state } 83 10 93
106 83 return $self->{result};
107 }
108  
109 550 ▶sub _handle_token {
Subroutine _handle_token: called CC=1 SCAR=0.0
110 550 my ($self, $token, $opt) = @_;
111 550 my $method = $self->{stack}[-1]{state}{method};
112 550 return $self->$method($token, $opt);
113 }
114  
115 0 ▶sub _top_key {
Subroutine _top_key: not called CC=1 SCAR=6.9
116 0 my ($self) = @_;
117 0 return $self->{stack}[-1]{state}{key};
118 }
119  
120 135 ▶sub _top_opt {
Subroutine _top_opt: called CC=2 SCAR=7.2
121 135 my ($self) = @_;
122 135 ▶ return $self->{stack}[-1]{opt} || {};
Condition50%1/2
$self->{'stack'}[-1]{'opt'} || {}
l!l
1350
MC/DC0%0/1
$self->{'stack'}[-1]{'opt'} || {}
$self->{'stack'}[-1]{'opt'}
Truth table50%1/2
A
A= $self->{'stack'}[-1]{'opt'}
Aresult
11
00
123 }
124  
125 732 ▶sub _append_result {
Subroutine _append_result: called CC=1 SCAR=0.0
126 732 my ($self, $text) = @_;
127 732 push @{$self->{result}}, $text;
128 }
129  
130 240 ▶sub _push_state {
Subroutine _push_state: called CC=2 SCAR=6.9
131 240 my ($self, $state_key, $opt) = @_;
132 240 my $state = $states->{$state_key};
133 240 ▶ $self->_append_result($state->{js_open}) if $state->{js_open} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"js_open"} ne "" 123 117 240
134 240 my $entry = {state => $state, opt => $opt};
135 240 push @{$self->{stack}}, $entry;
136 }
137  
138 157 ▶sub _pop_state {
Subroutine _pop_state: called CC=2 SCAR=6.9
139 157 my ($self) = @_;
140 157 my $entry = pop @{$self->{stack}};
141 157 my $state = $entry->{state};
142 157 ▶ $self->_append_result($state->{js_close}) if $state->{js_close} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"js_close"} ne "" 123 34 157
143 }
144  
145 131 ▶sub _in_init {
Subroutine _in_init: called CC=9 SCAR=22.4
146 131 my ($self, $token, $opt) = @_;
147
148 131 ▶ if ($token eq '<%') {
Branch: 3 branches
BranchTrueFalseTotal
if ($token eq '<%') { } 31 100 131
elsif ($token eq '<%=') { } 40 60 100
elsif ($token eq "\n") { } 0 60 60
149 31 ▶ if (defined $opt->{left}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{"left"}) 31 0 31
150 31 $opt->{ltrim} = {};
151
152 31 ▶ if ($opt->{left} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"left"} ne "") 16 15 31
153 16 $self->_append_result($opt->{left});
154 16 $opt->{ltrim}{index} = $#{$self->{result}};
155 }
156 }
157
158 31 $self->_push_state($token, $opt);
159 } elsif ($token eq "<%=") {
160 40 ▶ if (defined $opt->{left} && $opt->{left} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{"left"} and $opt->{"left"} ne "") 8 32 40
Condition66%2/3
defined $opt->{"left"} and $opt->{"left"} ne ""
!ll&&!rl&&r
0328
MC/DC50%1/2
defined $opt->{"left"} and $opt->{"left"} ne ""
defined $opt->{"left"} $opt->{"left"} ne ""
Truth table66%2/3
A and B
A= defined $opt->{"left"}B= $opt->{"left"} ne ""
ABresult
0X0
100
111
161 8 $self->_push_state('TEXT');
162 8 $self->_append_result($opt->{left});
163 8 $self->_pop_state();
164 }
165
166 40 $self->_push_state($token, $opt);
167
168 40 ▶ if (my $escape = $opt->{escape}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $escape = $opt->{"escape"}) 1 39 40
169 1 $self->_append_result(qq{EJS.$escape(});
170 }
171 } elsif ($token eq "\n") {
172 0 $self->_push_state('TEXT');
173 0 $self->_append_result("\\n");
174 0 $self->_pop_state();
175 0 $self->_append_result("\n");
176 } else {
177 60 $token =~ s/([\\"])/\\$1/g;
178 60 $self->_push_state('TEXT');
179 60 $self->_append_result($token);
180 }
181 }
182  
183 83 ▶sub _in_text {
Subroutine _in_text: called CC=5 SCAR=16.1
184 83 my ($self, $token, $opt) = @_;
185
186 83 ▶ if ($token eq '<%') {
Branch: 3 branches
BranchTrueFalseTotal
if ($token eq '<%') { } 3 80 83
elsif ($token eq '<%=') { } 15 65 80
elsif ($token eq "\n") { } 40 25 65
187 3 $self->_pop_state();
188 3 $self->_push_state($token, $opt);
189 } elsif ($token eq '<%=') {
190 15 $self->_pop_state();
191 15 $self->_push_state($token, $opt);
192
193 15 ▶ if (my $escape = $opt->{escape}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $escape = $opt->{"escape"}) 4 11 15
194 4 $self->_append_result(qq{EJS.$escape(});
195 }
196 } elsif ($token eq "\n") {
197 40 $self->_append_result("\\n");
198 40 $self->_pop_state();
199 40 $self->_append_result("\n");
200 } else {
201 25 $token =~ s/([\\"])/\\$1/g;
202 25 $self->_append_result($token);
203 }
204 }
205  
206 336 ▶sub _in_script {
Subroutine _in_script: called CC=10 SCAR=23.0
207 336 my ($self, $token, $opt) = @_;
208
209 336 ▶ if ($token eq '%>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq '%>') { } 81 255 336
210 81 ▶ if (my $ltrim = $self->_top_opt->{ltrim}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ltrim = $self->_top_opt->{'ltrim'}) { } 27 54 81
211 27 ▶ if (defined $opt->{right}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{'right'}) { } 25 2 27
212 25 ▶ if ($opt->{right} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"right"} ne "") 15 10 25
213 15 $self->_append_result($opt->{right});
214 }
215 } else {
216 2 ▶ if (defined(my $idx = $ltrim->{index})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $idx = $ltrim->{"index"})) 2 0 2
217 2 my $left = $self->{result}[$idx];
218 2 $self->{result}[$idx] = qq{print("$left");};
219 }
220 }
221
222 27 $self->_pop_state();
223 } else {
224 54 ▶ if ($self->_top_opt->{escape}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_top_opt->{"escape"}) 5 49 54
225 5 $self->_append_result(qq{)});
226 }
227
228 54 $self->_pop_state();
229
230 54 my $right = $opt->{right};
231
232 54 ▶ if (defined $right && $right ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $right and $right ne "") 17 37 54
Condition100%3/3
defined $right and $right ne ""
!ll&&!rl&&r
73017
MC/DC100%2/2
defined $right and $right ne ""
defined $right $right ne ""
Truth table100%3/3
A and B
A= defined $rightB= $right ne ""
ABresult
0X0
100
111
233 17 ▶ if ($right =~ s/\n$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($right =~ s/\n$//) { } 15 2 17
234 15 $self->_append_result(qq{print("$right\\n");\n});
235 } else {
236 2 $self->_append_result(qq{print("$right");});
237 }
238 }
239 }
240 } else {
241 255 $self->_append_result($token);
242 }
243 }
244  
245 =head1 SEE ALSO
246  
247 =over 4
248  
249 =item * L<EJS::Template>
250  
251 =item * L<EJS::Template::Parser>
252  
253 =back
254  
255 =cut
256  
257 8 1;
« Template/JSAdapter.pm ↑ Summary Template/Test.pm »