| 1 | package Serge::Engine::Plugin::parse_js; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use parent Serge::Engine::Plugin::Base::Parser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = 2; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | sub name { | ||||||||||||||||||||||||||||
| 9 | 0 | ▶ | return 'Generic JavaScript object parser plugin'; | ||||||||||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | } | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub parse { | ||||||||||||||||||||||||||||
| 13 | 0 | ▶ | my ($self, $textref, $callbackref, $lang) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 0 | ▶ | die 'callbackref not specified' unless $callbackref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | my $translated_text = $$textref; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Parse strings | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Example: | ||||||||||||||||||||||||||||
| 22 | # "key" : "value", | ||||||||||||||||||||||||||||
| 23 | # "key":"value", | ||||||||||||||||||||||||||||
| 24 | # "key": "value", // comment | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | $translated_text =~ s!^(\h*")(.+?)("\h*:\h*")((?:\\\\|\\"|[^"])+?)("\h*(?:,\h*)?(?://\h*(.*)\h*)?)$!$1.$2.$3.$self->parse_callback($callbackref, $4, undef, $2, $6, $lang).$5!mgie; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | return $translated_text; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub parse_callback { | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my ($self, $callbackref, $string, $context, $key, $comment, $lang) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_callback: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | my @hint; | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | push @hint, $key if $key ne '' && $key ne $string; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$key ne "" and $key ne $string
MC/DC0%0/2
$key ne "" and $key ne $string
$key ne ""
$key ne $string
Truth table0%0/3
A and B
A= $key ne ""B= $key ne $string
| |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | push @hint, $comment if $comment ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | $string =~ s/(\\\d{1,3}|\\x[0-9a-f]{2}|\\u[0-9a-f]{4}|\\u\{[0-9a-f]{1,6}\}|\\[bfnrtv"'\\])/_unescape($1)/sgie; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | my $translated_string = &$callbackref($string, $context, join("\n", @hint), undef, $lang, $key); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | $translated_string =~ s/(\\[bfrv]|[\\"'\n\t])/_escape($1)/sge; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | return $translated_string; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub _unescape { | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my $s = shift; | ||||||||||||||||||||||||||
|
Subroutine
_unescape: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $s eq '\\n' && return "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $s eq '\\t' && return "\t"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if ($s =~ m/^\\(\d{1,3})$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | return chr oct $1; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ($s =~ m/^\\x([0-9a-f]{2})$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | return chr hex $1; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ($s =~ m/^\\u([0-9a-f]{4})$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | return chr hex $1; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if ($s =~ m/^\\u\{([0-9a-f]{1,6})\}$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | return chr hex $1; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ($s =~ m/^\\[bfrv]$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | return $s; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | $s =~ s/^\\//; | |||||||||||||||||||||||||||
| 73 | 0 | return $s; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub _escape { | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my $s = shift; | ||||||||||||||||||||||||||
|
Subroutine
_escape: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $s eq "\n" && return '\n'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $s eq "\t" && return '\t'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($s =~ m/^\\[bfrv]$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | return $s; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 0 | return '\\'.$s; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | 1; | |||||||||||||||||||||||||||