| 1 | package Serge::Engine::Plugin::parse_go; | ||||||||||||||||||||||
| 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 | # TODO: | ||||||||||||||||||||||
| 7 | # - Decode \\Uxxxx in strings | ||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | sub name { | ||||||||||||||||||||||
| 10 | 0 | ▶ | return '.go string map parser plugin'; | ||||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 11 | } | ||||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | sub parse { | ||||||||||||||||||||||
| 14 | 0 | ▶ | my ($self, $textref, $callbackref, $lang) = @_; | ||||||||||||||||||||
|
Subroutine
parse: not called CC=17 SCAR=57.2
Pod
undocumented
| |||||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | 0 | ▶ | die 'callbackref not specified' unless $callbackref; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | 0 | my $translated_text; | |||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | # Finding translatable strings in file | ||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | # Format is: | ||||||||||||||||||||||
| 23 | # =================================================== | ||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||
| 25 | # // optional single-line comment that is skipped, as it is followed by a blank line | ||||||||||||||||||||||
| 26 | # | ||||||||||||||||||||||
| 27 | # // optional comment that is treated as hint for the line below; | ||||||||||||||||||||||
| 28 | # // multiple-line comments are supported | ||||||||||||||||||||||
| 29 | # "some key": "value", | ||||||||||||||||||||||
| 30 | # | ||||||||||||||||||||||
| 31 | # // or | ||||||||||||||||||||||
| 32 | # some_key: "value", | ||||||||||||||||||||||
| 33 | # | ||||||||||||||||||||||
| 34 | # // or | ||||||||||||||||||||||
| 35 | # "some_key": `raw multi-line | ||||||||||||||||||||||
| 36 | # string`, | ||||||||||||||||||||||
| 37 | # | ||||||||||||||||||||||
| 38 | # =================================================== | ||||||||||||||||||||||
| 39 | # The key name is also appended to a hint message. | ||||||||||||||||||||||
| 40 | # If the key contains '##suffix', the 'suffix' is used as a context string | ||||||||||||||||||||||
| 41 | # | ||||||||||||||||||||||
| 42 | # Limitations of a regexp-based parsing approach: | ||||||||||||||||||||||
| 43 | # there must be no unpaired backticks inside strings. I.e. this string will break | ||||||||||||||||||||||
| 44 | # the parsing: | ||||||||||||||||||||||
| 45 | # | ||||||||||||||||||||||
| 46 | # "`" | ||||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | my @all_hints; | ||||||||||||||||||||||
| 49 | |||||||||||||||||||||||
| 50 | 0 | $$textref =~ s/`(.*?)`/'`'._replace_newlines($1).'`'/sge; | |||||||||||||||||||||
| 51 | |||||||||||||||||||||||
| 52 | 0 | foreach my $line (split(/\n/, $$textref)) { | |||||||||||||||||||||
| 53 | 0 | my $key; | |||||||||||||||||||||
| 54 | my $hint; | ||||||||||||||||||||||
| 55 | 0 | my $context; | |||||||||||||||||||||
| 56 | 0 | my $orig_str; | |||||||||||||||||||||
| 57 | 0 | my $str; | |||||||||||||||||||||
| 58 | 0 | my $orig_str_for_subst; | |||||||||||||||||||||
| 59 | 0 | my $translated_str; | |||||||||||||||||||||
| 60 | 0 | my $wrapper_sym; | |||||||||||||||||||||
| 61 | |||||||||||||||||||||||
| 62 | 0 | ▶ | if ($line =~ m/^\s*$/) { # blank line | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 63 | 0 | @all_hints = (); # reset accumulator array | |||||||||||||||||||||
| 64 | } elsif ($line =~ m/^\s*\/\/\s*(.*?)\s*$/) { # a [// comment] line | ||||||||||||||||||||||
| 65 | 0 | push(@all_hints, $1); | |||||||||||||||||||||
| 66 | } elsif ($line =~ m/^\s*("(.*?)"|([\w\d]+))\s*:\s*"(.*)"\s*,\s*$/) { # a ["key": "value",] line | ||||||||||||||||||||||
| 67 | 0 | $wrapper_sym = '"'; | |||||||||||||||||||||
| 68 | 0 | $orig_str = $4; | |||||||||||||||||||||
| 69 | 0 | $str = _unescape($orig_str); | |||||||||||||||||||||
| 70 | 0 | ▶ | $key = $2 || $3; | ||||||||||||||||||||
|
Condition0%0/3
$2 || $3
MC/DC0%0/2
$2 || $3
$2
$3
Truth table0%0/3
A || B
A= $2B= $3
| |||||||||||||||||||||||
| 71 | 0 | ($hint, $context) = split(/##/, $key, 2); | |||||||||||||||||||||
| 72 | 0 | ▶ | push(@all_hints, $hint) if $hint; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 73 | |||||||||||||||||||||||
| 74 | 0 | ▶ | if ($str ne '') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 75 | 0 | $translated_str = &$callbackref($str, $context, join("\n", @all_hints), undef, $lang, $key); | |||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||
| 77 | |||||||||||||||||||||||
| 78 | 0 | ▶ | if ($translated_str ne '') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 79 | 0 | $translated_str = $wrapper_sym._escape($translated_str).$wrapper_sym; | |||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||
| 81 | |||||||||||||||||||||||
| 82 | 0 | @all_hints = (); | |||||||||||||||||||||
| 83 | } elsif ($line =~ m/^\s*("(.*?)"|([\w\d]+))\s*:\s*`(.*)`\s*,\s*$/) { # a ["key": `value`,] line | ||||||||||||||||||||||
| 84 | 0 | $wrapper_sym = "`"; | |||||||||||||||||||||
| 85 | 0 | $orig_str = $4; | |||||||||||||||||||||
| 86 | 0 | $str = _restore_newlines($orig_str); | |||||||||||||||||||||
| 87 | 0 | ▶ | $key = $2 || $3; | ||||||||||||||||||||
|
Condition0%0/3
$2 || $3
MC/DC0%0/2
$2 || $3
$2
$3
Truth table0%0/3
A || B
A= $2B= $3
| |||||||||||||||||||||||
| 88 | 0 | ($hint, $context) = split(/##/, $key, 2); | |||||||||||||||||||||
| 89 | 0 | ▶ | push(@all_hints, $hint) if $hint; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 90 | |||||||||||||||||||||||
| 91 | 0 | ▶ | if ($str ne '') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 92 | 0 | $translated_str = &$callbackref($str, $context, join("\n", @all_hints), undef, $lang, $key); | |||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||
| 94 | |||||||||||||||||||||||
| 95 | 0 | ▶ | if ($translated_str ne '') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 96 | 0 | $translated_str = $wrapper_sym.$translated_str.$wrapper_sym; | |||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||
| 98 | |||||||||||||||||||||||
| 99 | 0 | @all_hints = (); | |||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||
| 101 | |||||||||||||||||||||||
| 102 | 0 | ▶ | if ($translated_str ne "") { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 103 | 0 | $orig_str = $wrapper_sym.$orig_str.$wrapper_sym; | |||||||||||||||||||||
| 104 | 0 | $line =~ s/\Q$orig_str\E\s*,\s*$/$translated_str.','/e; | |||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | 0 | ▶ | if ($lang) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 108 | 0 | $translated_text .= $line."\n"; | |||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||
| 111 | |||||||||||||||||||||||
| 112 | 0 | return $translated_text; | |||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | sub _replace_newlines { | ||||||||||||||||||||||
| 116 | 0 | ▶ | my $s = shift; | ||||||||||||||||||||
|
Subroutine
_replace_newlines: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 117 | 0 | $s =~ s/\n/\x01/g; | |||||||||||||||||||||
| 118 | 0 | return $s; | |||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||
| 120 | |||||||||||||||||||||||
| 121 | sub _restore_newlines { | ||||||||||||||||||||||
| 122 | 0 | ▶ | my $s = shift; | ||||||||||||||||||||
|
Subroutine
_restore_newlines: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 123 | 0 | $s =~ s/\x01/\n/g; | |||||||||||||||||||||
| 124 | 0 | return $s; | |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||
| 126 | |||||||||||||||||||||||
| 127 | sub _unescape { | ||||||||||||||||||||||
| 128 | 0 | ▶ | my $s = shift; | ||||||||||||||||||||
|
Subroutine
_unescape: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 129 | 0 | $s =~ s/\\u([0-9A-Fa-f]{4})/chr(hex($1))/ge; # decode \uXXXX | |||||||||||||||||||||
| 130 | 0 | $s =~ s/\\x([0-9A-Fa-f]{2})/chr(hex($1))/ge; # decode \xXX | |||||||||||||||||||||
| 131 | 0 | $s =~ s/\\"/"/g; | |||||||||||||||||||||
| 132 | 0 | $s =~ s/\\n/\n/g; | |||||||||||||||||||||
| 133 | 0 | $s =~ s/\\\\/\\/g; | |||||||||||||||||||||
| 134 | 0 | return $s; | |||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||
| 136 | |||||||||||||||||||||||
| 137 | sub _escape { | ||||||||||||||||||||||
| 138 | 0 | ▶ | my $s = shift; | ||||||||||||||||||||
|
Subroutine
_escape: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 139 | 0 | $s =~ s/\\/\\\\/g; | |||||||||||||||||||||
| 140 | 0 | $s =~ s/\n/\\n/g; | |||||||||||||||||||||
| 141 | 0 | $s =~ s/"/\\"/g; | |||||||||||||||||||||
| 142 | 0 | return $s; | |||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||
| 144 | |||||||||||||||||||||||
| 145 | 1 | 1; | |||||||||||||||||||||