lib/Serge/Engine/Plugin/parse_go.pm

filter: Statementstmt 9.5% 7 / 73 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 25.0% 2 / 8 Podpod 0.0% 0 / 2 totaltotal 7.5% 9 / 119 SCAR 55.3
CC
17
Cov
7%
parse
306.0
name
2.0
_replace_newlines
2.0
CRAP 252.0
« lib/Serge/Sync/Plugin/VCS/svn.pm ↑ Summary lib/Serge/Engine/Plugin/parse_strings.pm »
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
BranchTrueFalseTotal
unless $callbackref 0 0 0
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
BranchTrueFalseTotal
if ($line =~ /^\s*$/) { } 0 0 0
elsif ($line =~ m[^\s*//\s*(.*?)\s*$]) { } 0 0 0
elsif ($line =~ /^\s*("(.*?)"|([\w\d]+))\s*:\s*"(.*)"\s*,\s*$/) { } 0 0 0
elsif ($line =~ /^\s*("(.*?)"|([\w\d]+))\s*:\s*`(.*)`\s*,\s*$/) { } 0 0 0
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
l!l&&r!l&&!r
000
MC/DC0%0/2
$2 || $3
$2 $3
Truth table0%0/3
A || B
A= $2B= $3
ABresult
1X1
011
000
71 0 ($hint, $context) = split(/##/, $key, 2);
72 0 ▶ push(@all_hints, $hint) if $hint;
Branch: 1 branch
BranchTrueFalseTotal
if $hint 0 0 0
73  
74 0 ▶ if ($str ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($str ne "") 0 0 0
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
BranchTrueFalseTotal
if ($translated_str ne "") 0 0 0
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
l!l&&r!l&&!r
000
MC/DC0%0/2
$2 || $3
$2 $3
Truth table0%0/3
A || B
A= $2B= $3
ABresult
1X1
011
000
88 0 ($hint, $context) = split(/##/, $key, 2);
89 0 ▶ push(@all_hints, $hint) if $hint;
Branch: 1 branch
BranchTrueFalseTotal
if $hint 0 0 0
90  
91 0 ▶ if ($str ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($str ne "") 0 0 0
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
BranchTrueFalseTotal
if ($translated_str ne "") 0 0 0
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
BranchTrueFalseTotal
if ($translated_str ne "") 0 0 0
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
BranchTrueFalseTotal
if ($lang) 0 0 0
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;
« lib/Serge/Sync/Plugin/VCS/svn.pm ↑ Summary lib/Serge/Engine/Plugin/parse_strings.pm »