| 1 | package Catalyst::View::ByCode::Declare; | ||||||||||||
| 2 | 7 | $Catalyst::View::ByCode::Declare::VERSION = '0.28'; | |||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 7 | ▶ | use Devel::Declare(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 7 | ▶ | use B::Hooks::EndOfScope; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | ###### Thanks ##################################################### | ||||||||||||
| 10 | # # | ||||||||||||
| 11 | # Thanks to Kang-min Liu for doing 'Markapl.pm' # | ||||||||||||
| 12 | # most of the concepts here are 'borrowed' from this great module # | ||||||||||||
| 13 | # sorry for copying instead of thinking. # | ||||||||||||
| 14 | # # | ||||||||||||
| 15 | #################################################### /Thanks ###### | ||||||||||||
| 16 | |||||||||||||
| 17 | # these variables will get local()'ized during a parser run | ||||||||||||
| 18 | 7 | our ($Declarator, $Offset); | |||||||||||
| 19 | |||||||||||||
| 20 | ####################################### SCANNERs | ||||||||||||
| 21 | # | ||||||||||||
| 22 | # skip space symbols (if any) | ||||||||||||
| 23 | # | ||||||||||||
| 24 | 139 | ▶ | sub skip_space { | ||||||||||
|
Subroutine
skip_space: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 25 | 139 | $Offset += Devel::Declare::toke_skipspace($Offset); | |||||||||||
| 26 | } | ||||||||||||
| 27 | |||||||||||||
| 28 | # | ||||||||||||
| 29 | # skip the sub_name just parsed (is still in $Declarator) | ||||||||||||
| 30 | # | ||||||||||||
| 31 | 33 | ▶ | sub skip_declarator { | ||||||||||
|
Subroutine
skip_declarator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 32 | 33 | $Offset += Devel::Declare::toke_move_past_token($Offset); | |||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | # | ||||||||||||
| 36 | # skip a word and return it -- or undef if no word found | ||||||||||||
| 37 | # | ||||||||||||
| 38 | 4 | ▶ | sub skip_word { | ||||||||||
|
Subroutine
skip_word: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||
| 39 | 4 | skip_space; | |||||||||||
| 40 | |||||||||||||
| 41 | 4 | ▶ | if (my $length = Devel::Declare::toke_scan_word($Offset, 1)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | 4 | my $linestr = Devel::Declare::get_linestr; | |||||||||||
| 43 | 4 | $Offset += $length; | |||||||||||
| 44 | 4 | return substr($linestr,$Offset-$length, $length); | |||||||||||
| 45 | } | ||||||||||||
| 46 | 0 | return; | |||||||||||
| 47 | } | ||||||||||||
| 48 | |||||||||||||
| 49 | # | ||||||||||||
| 50 | # non-destructively read next character | ||||||||||||
| 51 | # | ||||||||||||
| 52 | 120 | ▶ | sub next_char { | ||||||||||
|
Subroutine
next_char: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 53 | 120 | skip_space; | |||||||||||
| 54 | 120 | my $linestr = Devel::Declare::get_linestr; | |||||||||||
| 55 | 120 | return substr($linestr, $Offset, 1); | |||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | # | ||||||||||||
| 59 | # non-destructively read next word (=token) | ||||||||||||
| 60 | # | ||||||||||||
| 61 | 5 | ▶ | sub next_word { | ||||||||||
|
Subroutine
next_word: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||
| 62 | 5 | skip_space; | |||||||||||
| 63 | |||||||||||||
| 64 | 5 | ▶ | if (my $length = Devel::Declare::toke_scan_word($Offset, 1)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 65 | 5 | my $linestr = Devel::Declare::get_linestr; | |||||||||||
| 66 | 5 | return substr($linestr, $Offset, $length); | |||||||||||
| 67 | } | ||||||||||||
| 68 | 0 | return ''; | |||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | # | ||||||||||||
| 72 | # destructively read a valid name if possible | ||||||||||||
| 73 | # | ||||||||||||
| 74 | 10 | ▶ | sub strip_name { | ||||||||||
|
Subroutine
strip_name: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||
| 75 | 10 | skip_space; | |||||||||||
| 76 | |||||||||||||
| 77 | 10 | ▶ | if (my $length = Devel::Declare::toke_scan_word($Offset, 1)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | 10 | return inject('', $length); | |||||||||||
| 79 | } | ||||||||||||
| 80 | 0 | return; | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | # | ||||||||||||
| 84 | # destructively read a possibly dash-separated name | ||||||||||||
| 85 | # | ||||||||||||
| 86 | 6 | ▶ | sub strip_css_name { | ||||||||||
|
Subroutine
strip_css_name: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 87 | 6 | my $name = strip_name; | |||||||||||
| 88 | 6 | ▶ | while (next_char eq '-') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | 4 | inject('', 1); | |||||||||||
| 90 | 4 | $name .= '-'; | |||||||||||
| 91 | 4 | ▶ | if (next_char =~ m{\A[a-zA-Z0-9_]}xms) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | 4 | $name .= strip_name; | |||||||||||
| 93 | } | ||||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 6 | return $name; | |||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | # | ||||||||||||
| 100 | # read a prototype-like definition (looks like '(...)') | ||||||||||||
| 101 | # | ||||||||||||
| 102 | 26 | ▶ | sub strip_proto { | ||||||||||
|
Subroutine
strip_proto: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 103 | 26 | ▶ | if (next_char eq '(') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | 7 | my $length = Devel::Declare::toke_scan_str($Offset); | |||||||||||
| 105 | 7 | my $proto = Devel::Declare::get_lex_stuff(); | |||||||||||
| 106 | 7 | Devel::Declare::clear_lex_stuff(); | |||||||||||
| 107 | 7 | inject('', $length); | |||||||||||
| 108 | 7 | return $proto; | |||||||||||
| 109 | } | ||||||||||||
| 110 | 19 | return; | |||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | # | ||||||||||||
| 114 | # helper: check if a declarator is in a hash key | ||||||||||||
| 115 | # | ||||||||||||
| 116 | 33 | ▶ | sub declarator_is_hash_key { | ||||||||||
|
Subroutine
declarator_is_hash_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 117 | 33 | my $offset_before = $Offset; | |||||||||||
| 118 | 33 | skip_declarator; | |||||||||||
| 119 | |||||||||||||
| 120 | # This means that current declarator is in a hash key. | ||||||||||||
| 121 | # Don't shadow sub in this case | ||||||||||||
| 122 | 33 | return ($Offset == $offset_before); | |||||||||||
| 123 | } | ||||||||||||
| 124 | |||||||||||||
| 125 | # | ||||||||||||
| 126 | # parse: id? ('.' class)* ( '(' .* ')' )? | ||||||||||||
| 127 | # | ||||||||||||
| 128 | 26 | ▶ | sub parse_tag_declaration { | ||||||||||
|
Subroutine
parse_tag_declaration: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||
| 129 | # collect ID, class and (...) staff here... | ||||||||||||
| 130 | # for later injection into top of block | ||||||||||||
| 131 | 26 | my $extras = ''; | |||||||||||
| 132 | |||||||||||||
| 133 | # check for an indentifier (ID) | ||||||||||||
| 134 | 26 | ▶ | if (next_char =~ m{\A[a-zA-Z0-9_]}xms) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 135 | # looks like an ID | ||||||||||||
| 136 | 4 | my $name = strip_css_name; | |||||||||||
| 137 | 4 | $extras .= " id => '$name',"; | |||||||||||
| 138 | } | ||||||||||||
| 139 | |||||||||||||
| 140 | # check for '.class' as often as possible | ||||||||||||
| 141 | 26 | my @class; | |||||||||||
| 142 | 26 | ▶ | while (next_char eq '.') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | # found '.' -- eliminate it and read name | ||||||||||||
| 144 | 2 | inject('',1); | |||||||||||
| 145 | 2 | push @class, strip_css_name; | |||||||||||
| 146 | } | ||||||||||||
| 147 | 26 | ▶ | if (scalar(@class)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | 2 | $extras .= " class => '" . join(' ', @class) . "',"; | |||||||||||
| 149 | } | ||||||||||||
| 150 | |||||||||||||
| 151 | # | ||||||||||||
| 152 | # see if we have (...) stuff | ||||||||||||
| 153 | # | ||||||||||||
| 154 | 26 | my $proto = strip_proto; | |||||||||||
| 155 | 26 | ▶ | if ($proto) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 156 | ### | ||||||||||||
| 157 | ### BAD HACK: multiline (...) things will otherwise fail | ||||||||||||
| 158 | ### must be very tolerant! | ||||||||||||
| 159 | ### | ||||||||||||
| 160 | 7 | $proto =~ s{\s*[\r\n]\s*}{}xmsg; | |||||||||||
| 161 | |||||||||||||
| 162 | 7 | $extras .= " $proto,"; | |||||||||||
| 163 | } | ||||||||||||
| 164 | |||||||||||||
| 165 | 26 | ▶ | if ($extras) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 166 | 11 | ▶ | if (next_char eq '{') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 167 | # block present -- add after block | ||||||||||||
| 168 | 11 | inject_after_block($extras); | |||||||||||
| 169 | } else { | ||||||||||||
| 170 | # no block present -- fake a block and add after it | ||||||||||||
| 171 | 0 | inject(" {} $extras"); | |||||||||||
| 172 | } | ||||||||||||
| 173 | } | ||||||||||||
| 174 | } | ||||||||||||
| 175 | |||||||||||||
| 176 | ####################################### INJECTORs | ||||||||||||
| 177 | # | ||||||||||||
| 178 | # inject something at current position | ||||||||||||
| 179 | # - with optional length | ||||||||||||
| 180 | # - at optional offset | ||||||||||||
| 181 | # returns thing at inserted position before | ||||||||||||
| 182 | # | ||||||||||||
| 183 | 38 | ▶ | sub inject { | ||||||||||
|
Subroutine
inject: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 184 | 38 | my $inject = shift; | |||||||||||
| 185 | 38 | ▶ | my $length = shift || 0; | ||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 186 | 38 | ▶ | my $offset = shift || 0; | ||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 187 | |||||||||||||
| 188 | 38 | my $linestr = Devel::Declare::get_linestr; | |||||||||||
| 189 | 38 | my $previous = substr($linestr, $Offset+$offset, $length); | |||||||||||
| 190 | 38 | substr($linestr, $Offset+$offset, $length) = $inject; | |||||||||||
| 191 | 38 | Devel::Declare::set_linestr($linestr); | |||||||||||
| 192 | |||||||||||||
| 193 | 38 | return $previous; | |||||||||||
| 194 | } | ||||||||||||
| 195 | |||||||||||||
| 196 | # | ||||||||||||
| 197 | # inject something at top of a '{ ...}' block | ||||||||||||
| 198 | # returns: boolean depending on success | ||||||||||||
| 199 | # | ||||||||||||
| 200 | 11 | ▶ | sub inject_into_block { | ||||||||||
|
Subroutine
inject_into_block: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||
| 201 | 11 | my $inject = shift; | |||||||||||
| 202 | |||||||||||||
| 203 | 11 | ▶ | if (next_char eq '{') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 204 | 11 | inject($inject,0,1); | |||||||||||
| 205 | 11 | return 1; | |||||||||||
| 206 | } | ||||||||||||
| 207 | 0 | return 0; | |||||||||||
| 208 | } | ||||||||||||
| 209 | |||||||||||||
| 210 | # | ||||||||||||
| 211 | # inject something before a '{ ...}' block | ||||||||||||
| 212 | # returns: boolean depending on success | ||||||||||||
| 213 | # | ||||||||||||
| 214 | 0 | ▶ | sub inject_before_block { | ||||||||||
|
Subroutine
inject_before_block: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 215 | 0 | my $inject = shift; | |||||||||||
| 216 | |||||||||||||
| 217 | 0 | ▶ | if (next_char eq '{') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 218 | 0 | inject($inject); | |||||||||||
| 219 | 0 | return 1; | |||||||||||
| 220 | } | ||||||||||||
| 221 | |||||||||||||
| 222 | 0 | return 0; | |||||||||||
| 223 | } | ||||||||||||
| 224 | |||||||||||||
| 225 | # | ||||||||||||
| 226 | # inject something after scope as soon as '}' is reached | ||||||||||||
| 227 | # | ||||||||||||
| 228 | 7 | our @thing_to_inject; | |||||||||||
| 229 | |||||||||||||
| 230 | 11 | ▶ | sub inject_after_block { # called from a parser | ||||||||||
|
Subroutine
inject_after_block: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 231 | 11 | my $inject = shift; | |||||||||||
| 232 | 11 | push @thing_to_inject, $inject; | |||||||||||
| 233 | |||||||||||||
| 234 | # force calling the sub below as soon as block's scope is done. | ||||||||||||
| 235 | 11 | inject_into_block(qq{ BEGIN { Catalyst::View::ByCode::Declare::post_block_inject }; }); | |||||||||||
| 236 | } | ||||||||||||
| 237 | |||||||||||||
| 238 | 8 | ▶ | sub post_block_inject { # called from a BEGIN {} block at scope start | ||||||||||
|
Subroutine
post_block_inject: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 239 | 8 | my $inject = pop @thing_to_inject; | |||||||||||
| 240 | |||||||||||||
| 241 | 8 | ▶ | on_scope_end { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 242 | 8 | my $linestr = Devel::Declare::get_linestr; | |||||||||||
| 243 | 8 | my $offset = Devel::Declare::get_linestr_offset; | |||||||||||
| 244 | |||||||||||||
| 245 | 8 | substr($linestr, $offset, 0) = $inject; | |||||||||||
| 246 | 8 | Devel::Declare::set_linestr($linestr); | |||||||||||
| 247 | 8 | }; | |||||||||||
| 248 | } | ||||||||||||
| 249 | |||||||||||||
| 250 | ####################################### ADD SUBs | ||||||||||||
| 251 | # | ||||||||||||
| 252 | # put modified sub into requested package | ||||||||||||
| 253 | # | ||||||||||||
| 254 | 5 | ▶ | sub install_sub { | ||||||||||
|
Subroutine
install_sub: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 255 | 5 | my $sub_name = shift; | |||||||||||
| 256 | 5 | my $code = shift; | |||||||||||
| 257 | 5 | my $add_to_array = shift; | |||||||||||
| 258 | |||||||||||||
| 259 | 5 | my $package = Devel::Declare::get_curstash_name; | |||||||||||
| 260 | |||||||||||||
| 261 | 7 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 262 | 7 | ▶ | no warnings 'redefine'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 263 | ### deleting does not warn, but aliassing is still in action | ||||||||||||
| 264 | # http://www252.pair.com/comdog/mastering_perl/Chapters/08.symbol_tables.html | ||||||||||||
| 265 | # delete ${"$package\::"}{$sub_name}; | ||||||||||||
| 266 | 5 | *{"$package\::$sub_name"} = $code; | |||||||||||
| 267 | # cannot modify: *{"$package\::$sub_name"}{CODE} = $code; | ||||||||||||
| 268 | 5 | push @{"$package\::EXPORT"}, $sub_name; | |||||||||||
| 269 | 5 | ▶ | push @{"$package\::$add_to_array"}, $sub_name if ($add_to_array); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 270 | ### right?? push @{"$package\::$EXPORT_TAGS\{default\}"}, $sub_name; | ||||||||||||
| 271 | } | ||||||||||||
| 272 | |||||||||||||
| 273 | ####################################### PARSERs | ||||||||||||
| 274 | # | ||||||||||||
| 275 | # generate a tag-parser | ||||||||||||
| 276 | # initiated after compiling a tag subroutine | ||||||||||||
| 277 | # parses: tag id? ('.' class)* ( '(' .* ')' )? | ||||||||||||
| 278 | # injects some magic after the block following the declaration | ||||||||||||
| 279 | # | ||||||||||||
| 280 | 1666 | ▶ | sub tag_parser { | ||||||||||
|
Subroutine
tag_parser: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 281 | 28 | ▶ | return sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 282 | 28 | local ($Declarator, $Offset) = @_; | |||||||||||
| 283 | 28 | ▶ | return if (declarator_is_hash_key); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 284 | |||||||||||||
| 285 | # parse the id.class() {} declaration | ||||||||||||
| 286 | 26 | parse_tag_declaration; | |||||||||||
| 287 | 1666 | }; | |||||||||||
| 288 | } | ||||||||||||
| 289 | |||||||||||||
| 290 | # | ||||||||||||
| 291 | # add a tag parser | ||||||||||||
| 292 | # | ||||||||||||
| 293 | 5 | ▶ | sub add_tag_parser { | ||||||||||
|
Subroutine
add_tag_parser: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 294 | 5 | my $sub_name = shift; | |||||||||||
| 295 | |||||||||||||
| 296 | 5 | my $package = Devel::Declare::get_curstash_name; | |||||||||||
| 297 | |||||||||||||
| 298 | 5 | Devel::Declare->setup_for($package, | |||||||||||
| 299 | { | ||||||||||||
| 300 | $sub_name => { | ||||||||||||
| 301 | const => tag_parser | ||||||||||||
| 302 | } | ||||||||||||
| 303 | }); | ||||||||||||
| 304 | } | ||||||||||||
| 305 | |||||||||||||
| 306 | # | ||||||||||||
| 307 | # generate a block-parser | ||||||||||||
| 308 | # initiated after compiling 'block' | ||||||||||||
| 309 | # parses: 'block' name '{' | ||||||||||||
| 310 | # injects ' => sub' after name | ||||||||||||
| 311 | # always installs a parser for block() calls. | ||||||||||||
| 312 | # | ||||||||||||
| 313 | 12 | ▶ | sub block_parser { | ||||||||||
|
Subroutine
block_parser: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 314 | 5 | ▶ | return sub { | ||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||
| 315 | 5 | local ($Declarator, $Offset) = @_; | |||||||||||
| 316 | 5 | ▶ | return if (declarator_is_hash_key); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 317 | |||||||||||||
| 318 | 5 | my $sub_name; | |||||||||||
| 319 | 5 | ▶ | if (next_word =~ m{\A [a-zA-Z_]\w* \z}xms) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 320 | # skip the block_name to append a '=> sub' afterwards | ||||||||||||
| 321 | 4 | $sub_name = skip_word; | |||||||||||
| 322 | 4 | ▶ | if (next_char eq '{') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 323 | 4 | inject(' => sub '); | |||||||||||
| 324 | 4 | $Offset += 8; | |||||||||||
| 325 | } | ||||||||||||
| 326 | } else { | ||||||||||||
| 327 | # take the next string we find as sub_name | ||||||||||||
| 328 | # silently assume correct perl-syntax | ||||||||||||
| 329 | 1 | Devel::Declare::toke_scan_str($Offset); | |||||||||||
| 330 | 1 | $sub_name = Devel::Declare::get_lex_stuff(); | |||||||||||
| 331 | 1 | Devel::Declare::clear_lex_stuff(); | |||||||||||
| 332 | } | ||||||||||||
| 333 | |||||||||||||
| 334 | # insert a preliminary sub named $sub_name | ||||||||||||
| 335 | # into the caller's namespace to make compiler happy | ||||||||||||
| 336 | # and to allow calling the sub without ()'s | ||||||||||||
| 337 | 5 | ▶ | install_sub($sub_name => sub(;&@) {}, 'EXPORT_BLOCK'); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||
| 338 | 5 | add_tag_parser($sub_name); | |||||||||||
| 339 | 12 | }; | |||||||||||
| 340 | } | ||||||||||||
| 341 | |||||||||||||
| 342 | 7 | 1; | |||||||||||