ASP/Parser.pm

filter: Statementstmt 97.1% 134 / 138 Branchbran 75.8% 44 / 58 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 90.0% 198 / 220 CC 33 SCAR 19.5
CC
33
Cov
90%
_parse_asp
12.0
_parse_ssi
9.6
_parse_xml_subs
7.0
CRAP 7.0
« ASP.pm ↑ Summary ASP/Response.pm »
1 package CatalystX::ASP::Parser;
2  
3 9 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 9 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 with 'CatalystX::ASP::Compiler';
9  
10 9 requires 'compile_include';
11  
12 =head1 NAME
13  
14 CatalystX::ASP::Parser - Role for CatalystX::ASP providing code parsing
15  
16 =head1 SYNOPSIS
17  
18 use CatalystX::ASP;
19 with 'CatalystX::ASP::Compiler', 'CatalystX::ASP::Parser';
20  
21 sub execute {
22 my ($self, $c, $scriptref) = @_;
23 my $parsed = $self->parse($c, $scriptref);
24 my $subid = $self->compile($c, $parsed->{data});
25 eval { &$subid };
26 }
27  
28 =head1 DESCRIPTION
29  
30 This class implements the ability to parse ASP code into readable format for
31 C<CatalystX::ASP::Compiler>
32  
33 =cut
34  
35 25 ▶sub _build_parsed_object {
Subroutine _build_parsed_object: called CC=3 SCAR=11.0
36 25 my ( $self, $scriptref, %opts ) = @_;
37  
38 $$scriptref = join( ';;',
39 'no strict;',
40 115 ▶ 'use vars qw(' . join( ' ', map {"\$$_"} @CatalystX::ASP::Objects ) . ');',
Branch: 1 branch
BranchTrueFalseTotal
$opts{'file'} ? : 0 23 23
41 $opts{file} ? "\n#line 1 $opts{file}\n" : '',
42 $$scriptref,
43 25 ▶ ) unless $opts{is_raw};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"is_raw"} 23 2 25
44  
45 25 return { %opts, data => $scriptref };
46 }
47  
48 =head1 METHODS
49  
50 =over
51  
52 =item $self->parse($c, $scriptref)
53  
54 Take a C<$scriptref> and returns a hash including parsed data
55  
56 =cut
57  
58 25 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.1
Pod documented
59 25 my ( $self, $c, $scriptref ) = @_;
60  
61 25 my $parsed_object = eval {
62 25 $self->GlobalASA->Script_OnParse;
63 25 $scriptref = $self->_parse_ssi( $c, $scriptref );
64 25 my $parsed_scriptref = $self->_parse_asp( $c, $scriptref );
65 25 ▶ if ( $parsed_scriptref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed_scriptref) { } 23 2 25
66 23 return $self->_build_parsed_object( $parsed_scriptref, is_perl => 1 );
67 } else {
68 2 return $self->_build_parsed_object( $scriptref, is_raw => 1 );
69 }
70 };
71 25 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 25 25
72 0 $c->error( "Error in parsing: $@" );
73 }
74 25 return $parsed_object;
75 }
76  
77 =item $self->parse_file($c, $file)
78  
79 Take a C<$file> and returns a hash including parsed data
80  
81 =cut
82  
83 15 ▶sub parse_file {
Subroutine parse_file: called CC=2 SCAR=6.9
Pod documented
84 15 my ( $self, $c, $file ) = @_;
85  
86 15 my $scriptref = eval { read_file( $file, scalar_ref => 1 ); };
87  
88 # Asssume $@ =~ /sysopen: No such file or directory/
89 # Don't want to parse error because of possibly different locale than en_US.UTF-8
90 15 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 14 15
91  
92 # To get to this point would mean that some call to $Response->Include()
93 # is for a non-existent file
94 1 $c->error( "Could not read_file: $file in parse_file: $@" );
95 1 $c->detach;
96 }
97  
98 14 my $parsed_object = $self->parse( $c, $scriptref );
99  
100 14 $parsed_object->{file} = $file;
101  
102 14 return $parsed_object;
103 }
104  
105 # This parser processes and converts are SSI to call $Response->Include()
106 25 ▶sub _parse_ssi {
Subroutine _parse_ssi: called CC=9 SCAR=22.6
107 25 my ( $self, $c, $scriptref ) = @_;
108  
109 25 my $data = '';
110 25 my $file_line_number;
111 my $is_code_block;
112  
113 25 ▶ return \$data unless $$scriptref;
Branch: 1 branch
BranchTrueFalseTotal
unless $$scriptref 0 25 25
114  
115 25 ▶ while ( $$scriptref =~ s/^(.*?)\<!--\#include\s+file\s*=\s*\"?([^\s\"]*?)\"?(\s+args\s*=\s*\"?.*?)?\"?\s*--\>//so ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$scriptref =~ s/^(.*?)\<!--\#include\s+file\s*=\s*\"?([^\s\"]*?)\"?(\s+args\s*=\s*\"?.*?)?\"?\s*--\>//os) 2 25 27
116 2 $data .= $1; # append the head
117 2 my $include = $2;
118 2 my $args;
119 2 ▶ if ( $3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($3) 1 1 2
120 1 $args = $3;
121 1 $args =~ s/^\s+args\s*\=\s*\"?//sgo;
122 }
123  
124 2 my $head_data = $1;
125 2 ▶ if ( $head_data =~ s/.*\n\#line (\d+) [^\n]+\n(\%\>)?//s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head_data =~ s/.*\n\#line (\d+) [^\n]+\n(\%\>)?//s) 0 2 2
126 0 $file_line_number = $1;
127 0 ▶ $is_code_block = $2 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 0 0 0
128 }
129 2 $file_line_number += $head_data =~ s/\n//sg;
130 2 $head_data =~ s/\<\%.*?\%\>//sg;
131 2 $is_code_block += $head_data =~ s/\<\%//sg;
132 2 $is_code_block -= $head_data =~ s/\%\>//sg;
133 2 $is_code_block = $is_code_block > 0; # stray percents like height=100%> kinds of tags
134  
135 # global directory, as well as includes dirs
136 2 ▶ $c->error( "Could not find $include in IncludesDir" )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->search_includes_dir($include) 0 2 2
137 unless $self->search_includes_dir( $include );
138  
139 # because the script is literally different whether there
140 # are includes or not, whether we are compiling includes
141 # need to be part of the script identifier, so the global
142 # caching does not return a script with different preferences.
143 2 ▶ $args ||= '';
Condition100%2/2
$args ||= ""
l!l
11
MC/DC100%1/1
$args ||= ""
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
144 2 $data .= "<% \$Response->Include('$include', '$args'); %>";
145  
146 # compile include now, so Loading() works for dynamic includes too
147 2 ▶ $c->error( "Failed to compile $include" )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->compile_include($c, $include) 0 2 2
148 unless $self->compile_include( $c, $include );
149 }
150 25 $data .= $$scriptref; # append what's left
151  
152 25 return \$data;
153 }
154  
155 # Where the real ASP parsing happens. It's actually decently simple, just don't
156 # look at the Parse() from the original author.
157 27 ▶sub _parse_asp {
Subroutine _parse_asp: called CC=12 SCAR=24.9
158 27 my ( $self, $c, $scriptref ) = @_;
159  
160 27 ▶ $$scriptref = $self->_parse_xml_subs( $c, $$scriptref ) if $self->XMLSubsMatch;
Branch: 1 branch
BranchTrueFalseTotal
if $self->XMLSubsMatch 23 4 27
161  
162 # This is where we start to throw data back that lets the system render a
163 # static file as is instead of executing it as a per subroutine.
164 27 ▶ return unless $$scriptref =~ /\<\%.*?\%\>/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $$scriptref =~ /\<\%.*?\%\>/s 2 25 27
165  
166 25 $scriptref = \join( '', $$scriptref, '<%;;;%>' ); # always end with some perl code for parsing.
167  
168 25 my ( $script, @out, $perl_block, $last_perl_block );
169 25 ▶ while ( $$scriptref =~ s/^(.*?)\<\%(.*?)\%\>//so ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$scriptref =~ s/^(.*?)\<\%(.*?)\%\>//os) 63 25 88
170 63 my ( $text, $perl ) = ( $1, $2 );
171 63 my $is_perl_block = $perl !~ /^\s*\=(.*)$/so;
172  
173 # with some extra text parsing, we remove asp formatting from
174 # influencing the generated html formatting, in particular
175 # dealing with perl blocks and new lines
176 63 ▶ if ( $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) 27 36 63
177  
178 # don't touch the white space, to preserve line numbers
179 27 $text =~ s/\\/\\\\/gso;
180 27 $text =~ s/\'/\\\'/gso;
181  
182 27 ▶ $last_perl_block = 0 if $last_perl_block;
Branch: 1 branch
BranchTrueFalseTotal
if $last_perl_block 12 15 27
183  
184 27 push @out, "\'$text\'";
185 }
186  
187 63 ▶ if ( $perl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perl) 63 0 63
188 63 ▶ unless ( $is_perl_block ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_perl_block) { } 49 14 63
189  
190 # we have a scalar assignment here
191 14 push( @out, "($1)" );
192 } else {
193 49 $last_perl_block = 1;
194 49 ▶ if ( @out ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out) 19 30 49
195  
196 # we pass by reference here with the idea that we are not
197 # copying the HTML twice this way. This might be large
198 # saving on a typical site with rich HTML headers & footers
199 19 $script .= '$Response->WriteRef( \(' . join( '.', @out ) . ') );';
200 19 @out = ();
201 }
202  
203 # allow old <% #comment %> style to still work, but we
204 # need to insert a newline at the end of the comment for
205 # it to still exist, with the lines now being sync'd up
206 # if these old comments still exist, the perl script
207 # will be off by one line from the asp script
208 49 ▶ if ( $perl !~ /\n\s*$/so ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($perl =~ /\n\s*$/so) 43 6 49
209 43 ▶ if ( $perl =~ /\#[^\n]*$/so ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perl =~ /\#[^\n]*$/so) 0 43 43
210 0 $perl .= "\n";
211 }
212 }
213  
214 # skip if the perl code is just a placeholder
215 49 ▶ unless ( $perl eq ';;;' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($perl eq ";;;") 24 25 49
216 24 $script .= $perl . '; ';
217 }
218 }
219 }
220 }
221  
222 25 \$script;
223 }
224  
225 # Helper method to process all the XML substitions in the script. Essentially
226 # translates xmlsubs to perl method calls, passing in arguments and html within
227 # each xmlsub tag
228 23 ▶sub _parse_xml_subs {
Subroutine _parse_xml_subs: called CC=7 SCAR=19.5
229 23 my ( $self, $c, $script ) = @_;
230  
231 23 $script = $self->_code_tag_encode( $script );
232  
233 23 my $xml_subs_match = $self->XMLSubsMatch;
234  
235 # Does a first pass to process xmlsubs with no content block within.
236 # Eg. <xmlsub:method />
237 # Why need to do first pass? I have no clue, need to ask JCHAMAS.
238 23 $script =~ s@\<\s*($xml_subs_match)(\s+[^\>]*)?/\>
239 @ {
240 3 my ( $func, $args ) = ( $1, $2 );
241 3 $args = $self->_code_tag_decode( $args );
242 3 $func =~ s/\:+/\:\:/g;
243 3 $func =~ s/\-/\_/g;
244 3 ▶ if ( $args ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 3 0 3
245 3 $args =~ s/(\s*)([^\s]+?)(\s*)\=(\s*[^\s]+)/,$1'$2'$3\=\>$4/sg;
246 3 $args =~ s/^(\s*),/$1/s;
247 }
248 3 ▶ $args ||= '';
Condition50%1/2
$args ||= ""
l!l
30
MC/DC0%0/1
$args ||= ""
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
249 3 "<% $func({ $args }, ''); %>"
250 } @sgex;
251  
252 23 while ( 1 ) {
253 27 ▶ last unless $script =~ s@
Branch: 1 branch
BranchTrueFalseTotal
unless $script =~ s[ \<\s*($xml_subs_match)(\s+[^\>]*)?\>(?!.*?\<\s*\1[^\>]*\>)(.*?)\<\/\1\s*> ][{ () = ($1, $2, $3); $args = $self->_code_tag_decode($args); $func =~ s/\:+/::/g; if ($args) { $args =~ s/(\s*)([^\s]+?)(\s*)\=(\s*[^\s]+)/,$1'$2'$3=>$4/gs; $args =~ s/^(\s*),/$1/s; } ; $args ||= ""; $text = $self->_code_tag_decode($text); if ($text =~ /\<\%|\<($xml_subs_match)/) { my $sub_scriptref = $self->_parse_asp($c, \$text); $text = join(" ", "&{sub {", "my \$saved = \$Response->Body;", "\$Response->Clear;", "local \$Response->{out} = local \$Response->{BinaryRef} = \\( \$Response->{Body} );", "local *CatalystX::ASP::Response::Flush = sub {};", $$sub_scriptref, ";", "my \$trapped = \$Response->Body;", "\$Response->Body( \$saved );", "\$trapped;", "} }"); } else { $text =~ s/\\/\\\\/sog; $text =~ s/\'/\\'/sog; $text = "'${text}'"; } ; "<% $func({ $args }, $text); %>"; } ;]egsx 23 4 27
254 \<\s*($xml_subs_match)(\s+[^\>]*)?\>(?!.*?\<\s*\1[^\>]*\>)(.*?)\<\/\1\s*>
255 @ {
256 4 my( $func, $args, $text ) = ( $1, $2, $3 );
257 4 $args = $self->_code_tag_decode( $args );
258 4 $func =~ s/\:+/\:\:/g;
259 # Parse and process args to convert into perl hash
260 4 ▶ if ( $args ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 4 0 4
261 4 $args =~ s/(\s*)([^\s]+?)(\s*)\=(\s*[^\s]+)/,$1'$2'$3\=\>$4/sg;
262 4 $args =~ s/^(\s*),/$1/s;
263 }
264 4 ▶ $args ||= '';
Condition50%1/2
$args ||= ""
l!l
40
MC/DC0%0/1
$args ||= ""
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
265 4 $text = $self->_code_tag_decode( $text );
266  
267 4 ▶ if ( $text =~ m/\<\%|\<($xml_subs_match)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\<\%|\<($xml_subs_match)/) { } 2 2 4
268 # parse again, and control output buffer for this level
269 2 my $sub_scriptref = $self->_parse_asp( $c, \$text );
270 # Place the script inside a sub for compilation later
271 2 $text = join( ' ',
272 '&{sub {',
273 'my $saved = $Response->Body;',
274 '$Response->Clear;',
275 'local $Response->{out} = local $Response->{BinaryRef} = \( $Response->{Body} );',
276 'local *CatalystX::ASP::Response::Flush = sub {};',
277 $$sub_scriptref,
278 ';',
279 'my $trapped = $Response->Body;',
280 '$Response->Body( $saved );',
281 '$trapped;',
282 '} }'
283 );
284 } else {
285 # raw text
286 2 $text =~ s/\\/\\\\/gso;
287 2 $text =~ s/\'/\\\'/gso;
288 2 $text = "'$text'";
289 }
290  
291 4 "<% $func({ $args }, $text); %>"
292 } @sgex;
293 }
294  
295 23 return $self->_code_tag_decode( $script );
296 }
297  
298 # This simply encodes any ASP tags into something that won't be processed anywhere
299 23 ▶sub _code_tag_encode {
Subroutine _code_tag_encode: called CC=2 SCAR=7.0
300 23 my ( $self, $data ) = @_;
301  
302 23 ▶ if ( defined $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 23 0 23
303 23 $data =~ s@\<\%(.*?)\%\>
304 30 @ { '[-AsP-[' . encode_entities( $1 ) . ']-AsP-]'; } @esgx;
305 }
306 23 return $data;
307 }
308  
309 # This simply decodes what's been encoded above
310 34 ▶sub _code_tag_decode {
Subroutine _code_tag_decode: called CC=2 SCAR=7.0
311 34 my ( $self, $data ) = @_;
312  
313 34 ▶ if ( defined $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 34 0 34
314 34 $data =~ s@\[\-AsP\-\[(.*?)\]\-AsP\-\]
315 30 @ { '<%' . decode_entities( $1 ) . '%>'; } @esgx;
316 }
317 34 return $data;
318 }
319  
320 # Searches the script for and subroutines, returns 1 or 0
321 13 ▶sub _parse_for_subs {
Subroutine _parse_for_subs: called CC=1 SCAR=0.0
322 13 my ( $self, $scriptref ) = @_;
323  
324 13 return $$scriptref =~ /(^|\n)\s*sub\s+([^\s\{]+)\s*\{/;
325 }
326  
327 9 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
328  
329 9 1;
330  
331 =back
332  
333 =head1 SEE ALSO
334  
335 =over
336  
337 =item * L<CatalystX::ASP>
338  
339 =item * L<CatalystX::ASP::Compiler>
340  
341 =back
« ASP.pm ↑ Summary ASP/Response.pm »