lib/Locale/Simple/Scraper/Parser.pm

filter: Statementstmt 97.1% 273 / 281 Branchbran 85.9% 67 / 78 Conditioncond 87.5% 14 / 16 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 91.4% 85 / 93 Podpod 0.0% 0 / 42 totaltotal 85.7% 446 / 520 CC 47 SCAR 8.5
CC
47
Cov
94%
unescape
8.0
constant_string
5.0
quoted_string_contents
4.0
CRAP 2.3
« lib/Locale/Simple/CLI.pm ↑ Summary lib/Locale/Simple/Scraper/ParserShortcuts.pm »
1 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Locale::Simple::Scraper::Parser;
5 22 our $VERSION = '0.200';
6 # ABSTRACT: parser to finds translation tokens in a code file
7  
8 22 ▶use base qw( Parser::MGC );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 22 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use curry;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use charnames ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 112 ▶has func_qr => ( is => 'ro', lazy => 1, default => sub { shift->default_func_qr } );
Subroutine __ANON__: called CC=1 SCAR=0.0
16 112 ▶has found => ( is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
17 22 has type => ( is => 'ro', required => 1 );
18  
19 22 with "Locale::Simple::Scraper::ParserShortcuts";
20  
21 # The helper names are exported from a Go package, so they start with an
22 # upper case letter there. A package prefix before the dot needs no pattern of
23 # its own: the word boundary already matches after the dot.
24 112 ▶sub default_func_qr {
Subroutine default_func_qr: called CC=3 SCAR=11.0
Pod undocumented
25 112 my ( $self ) = @_;
26 112 ▶ return qr/\b(?:L(?:|n|p|np|d|dp|dn|dnp)\b|N(?:|n|p|np|d|dp|dn|dnp)_)/ if $self->type eq "go";
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "go" 7 105 112
27  
28 # Rust has macros: a bang follows the name, and the deferred markers are
29 # lower case there. The path before a macro needs no pattern either.
30 105 ▶ return qr/\b(?:l(?:|n|p|np|d|dp|dn|dnp)!|n(?:|n|p|np|d|dp|dn|dnp)_!)/ if $self->type eq "rs";
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "rs" 8 97 105
31 97 return qr/\b(?:l(?:|n|p|np|d|dp|dn|dnp)\b|N(?:|n|p|np|d|dp|dn|dnp)_)/;
32 }
33  
34 # Maps a name as written in the source to the one the argument tables use.
35 994 ▶sub canonical_func {
Subroutine canonical_func: called CC=4 SCAR=13.9
Pod undocumented
36 994 my ( $self, $func ) = @_;
37 994 ▶ $func =~ s/^L/l/ if $self->type eq "go";
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "go" 93 901 994
38 994 ▶ if ( $self->type eq "rs" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "rs") 68 926 994
39 68 $func =~ s/!$//;
40 68 ▶ $func = ucfirst $func if $func =~ /_$/;
Branch: 1 branch
BranchTrueFalseTotal
if $func =~ /_$/ 16 52 68
41 }
42 994 return $func;
43 }
44  
45 112 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
46 112 my ( $self ) = @_;
47 112 $self->sequence_of( $self->c_any_of( $self->curry::noise, $self->curry::call ) );
48 112 return $self->found;
49 }
50  
51 1944 ▶sub noise {
Subroutine noise: called CC=2 SCAR=6.9
Pod undocumented
52 1944 my ( $self ) = @_;
53 1944 my $noise = $self->substring_before( $self->func_qr );
54 1944 ▶ $self->fail( "no noise found" ) if !length $noise;
Branch: 1 branch
BranchTrueFalseTotal
unless length $noise 939 1005 1944
55 1005 $self->debug( "discarded %d characters of noise", length $noise );
56 1005 return $noise;
57 }
58  
59 # A call whose arguments do not parse is reported and skipped; reading goes
60 # on where they failed, at the top level and inside another call's argument
61 # alike. Nothing is read twice, so nothing is recorded twice.
62 2354 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod undocumented
63 2354 my ( $self ) = @_;
64  
65 2354 my $func = $self->canonical_func( $self->expect( $self->func_qr ) );
66 994 my $line = ( $self->where )[0];
67 994 $self->debug( "found func $func at line %d", $line );
68  
69 994 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
70 994 my $arguments = $self->arguments( $func );
71 693 push @{ $self->found }, { func => $func, args => $arguments, line => $line };
72 }
73 301 ▶ catch {
Subroutine __ANON__: called CC=2 SCAR=7.1
74 301 ▶ die $_ if !eval { $_->isa( "Parser::MGC::Failure" ) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $_->isa("Parser::MGC::Failure") } } 0 301 301
75 301 $self->warn_failure( $_ );
76 994 };
77  
78 994 return;
79 }
80  
81 994 ▶sub arguments {
Subroutine arguments: called CC=1 SCAR=0.0
Pod undocumented
82 994 my ( $self, $func ) = @_;
83  
84 994 my @arguments = ( $self->op( "(" ), $self->required_args( $func ), $self->extra_arguments, $self->op( ")" ) );
85 693 $self->debug( "found %d arguments", scalar @arguments );
86  
87 693 return \@arguments;
88 }
89  
90 2524 ▶sub op {
Subroutine op: called CC=2 SCAR=6.9
Pod undocumented
91 2524 my ( $self, $op ) = @_;
92 2524 ▶ return if $self->with_ws( maybe_expect => qr/\s*\Q$op\E/ );
Branch: 1 branch
BranchTrueFalseTotal
if $self->with_ws("maybe_expect", qr/\s*\Q$op\E/) 2364 160 2524
93 160 $self->fail( "Expected \"$op\"" );
94 }
95  
96 738 ▶sub extra_arguments {
Subroutine extra_arguments: called CC=2 SCAR=6.9
Pod undocumented
97 738 my ( $self ) = @_;
98 738 ▶ return if !$self->maybe_expect( "," );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->maybe_expect(",") 609 129 738
99  
100 129 ▶ my $extra_args = $self->list_of( ",", sub { $self->argument( "argument" ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
101 129 return @{$extra_args};
102 }
103  
104 # An argument the catalogue does not need, the count or one after it: any
105 # code up to the comma or closing bracket that ends it. Returns its source
106 # text, or nothing when it is a translation call alone, as that call is
107 # recorded on its own.
108 294 ▶sub argument {
Subroutine argument: called CC=3 SCAR=11.0
Pod undocumented
109 294 my ( $self, $name ) = @_;
110 294 $self->skip_ws;
111 294 my $start = $self->pos;
112 294 my $parts = $self->sequence_of( $self->c_any_of( $self->code_parts ) );
113 294 ▶ $self->fail( "Expected $name" ) if $self->pos == $start;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pos == $start 5 289 294
114 289 ▶ return if !@{$parts};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$parts;} 7 282 289
115 282 return substr $self->{str}, $start, $self->pos - $start;
116 }
117  
118 # The pieces such code is made of. Translation calls in it are recorded.
119 # String literals and bracket pairs are read as a whole, so a comma or a
120 # bracket inside them does not end the argument.
121 424 ▶sub code_parts {
Subroutine code_parts: called CC=1 SCAR=0.0
Pod undocumented
122 424 my ( $self ) = @_;
123 return (
124 424 $self->curry::call,
125 $self->skipped_literals,
126 $self->curry::bracketed,
127 $self->c_expect( qr/\w+|[^\w\s,()\[\]{}]/ ),
128 );
129 }
130  
131 # A bracket pair with what it holds, commas included. A pair left open (in
132 # broken code, or opened inside a literal form the scraper does not know)
133 # ends where its contents do. It never fails once opened, so what is read
134 # inside it is never read again.
135 1248 ▶sub bracketed {
Subroutine bracketed: called CC=1 SCAR=0.0
Pod undocumented
136 1248 my ( $self ) = @_;
137 1248 my $open = $self->expect( qr/[(\[{]/ );
138 130 ( my $close = $open ) =~ tr/([{/)]}/;
139 130 $self->sequence_of( $self->c_any_of( $self->code_parts, $self->c_expect( "," ) ) );
140 130 $self->maybe_expect( $close );
141 130 return $open;
142 }
143  
144 # The language's string literals as they appear in skipped code. The quoted
145 # forms are read leniently, any escape and empty included, as their text is
146 # not kept; the others are read as for a msgid.
147 424 ▶sub skipped_literals {
Subroutine skipped_literals: called CC=3 SCAR=11.0
Pod undocumented
148 424 my ( $self ) = @_;
149 my @literals = map {
150 424 ▶ ref $_ && $_->[0] =~ /^["']\z/
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && $_->[0] =~ /^["']\z/ ? : 670 321 991
Condition100%3/3
ref $_ and $_->[0] =~ /^["']\z/
!ll&&!rl&&r
120201670
MC/DC100%2/2
ref $_ and $_->[0] =~ /^["']\z/
ref $_ $_->[0] =~ /^["']\z/
Truth table100%3/3
A and B
A= ref $_B= $_->[0] =~ /^["']\z/
ABresult
0X0
100
111
151 ? $self->c_expect( $self->quoted_qr( $_->[0] ) )
152 : $self->literal_scope( $_ )
153 } $self->literal_forms;
154 424 return @literals, map { $self->c_expect( $_ ) } $self->code_only_literals;
155 }
156  
157 670 ▶sub quoted_qr {
Subroutine quoted_qr: called CC=1 SCAR=0.0
Pod undocumented
158 670 my ( $self, $quote ) = @_;
159 670 my $q = quotemeta $quote;
160 670 return qr/$q(?:[^\\$q]|\\.)*$q/s;
161 }
162  
163 # Literals that never hold a msgid but must not be read as code: JavaScript
164 # template literals, Python strings with any prefix (f-strings, bytes), Go
165 # runes and Rust chars (one character or one escape; a Rust lifetime has no
166 # closing quote and stays code).
167 424 ▶sub code_only_literals {
Subroutine code_only_literals: called CC=2 SCAR=6.9
Pod undocumented
168 424 my $char = qr/'(?:\\.[^'\\\n]*|[^'\\\n])'/;
169 424 my $python = qr/(?<!\w)[rRbBfFuUtT]{0,2}(?:"""(?:[^\\]|\\.)*?"""|'''(?:[^\\]|\\.)*?'''|"(?:[^\\"\n]|\\.)*"|'(?:[^\\'\n]|\\.)*')/s;
170 424 my %literals = ( js => [qr/`(?:[^`\\]|\\.)*`/s], py => [$python], go => [$char], rs => [$char] );
171 424 ▶ return @{ $literals{ shift->type } || [] };
Condition100%2/2
$literals{(shift())->type} || []
l!l
285139
MC/DC100%1/1
$literals{(shift())->type} || []
$literals{(shift())->type}
Truth table100%2/2
A
A= $literals{(shift())->type}
Aresult
11
00
172 }
173  
174 880 ▶sub required_args {
Subroutine required_args: called CC=1 SCAR=0.0
Pod undocumented
175 880 my ( $self, $func ) = @_;
176 880 my %arg_lists = (
177 l => [qw( tr_token )],
178 ln => [qw( tr_token comma plural_token comma plural_count )],
179 lp => [qw( context_id comma tr_token )],
180 lnp => [qw( context_id comma tr_token comma plural_token comma plural_count )],
181 ld => [qw( domain_id comma tr_token )],
182 ldn => [qw( domain_id comma tr_token comma plural_token comma plural_count )],
183 ldp => [qw( domain_id comma context_id comma tr_token )],
184 ldnp => [qw( domain_id comma context_id comma tr_token comma plural_token comma plural_count )],
185  
186 # Deferred-translation markers — runtime no-ops, scraper treats
187 # them identically to their l* twins.
188 N_ => [qw( tr_token )],
189 Nn_ => [qw( tr_token comma plural_token )],
190 Np_ => [qw( context_id comma tr_token )],
191 Nnp_ => [qw( context_id comma tr_token comma plural_token )],
192 Nd_ => [qw( domain_id comma tr_token )],
193 Ndn_ => [qw( domain_id comma tr_token comma plural_token )],
194 Ndp_ => [qw( domain_id comma context_id comma tr_token )],
195 Ndnp_ => [qw( domain_id comma context_id comma tr_token comma plural_token )],
196 );
197 880 return $self->collect_from( $arg_lists{$func} );
198 }
199  
200 822 ▶sub tr_token { shift->named_token( "translation token" ) }
Subroutine tr_token: called CC=1 SCAR=0.0
Pod undocumented
201 255 ▶sub plural_token { shift->named_token( "plural translation token" ) }
Subroutine plural_token: called CC=1 SCAR=0.0
Pod undocumented
202 132 ▶sub plural_count { shift->argument( "count of plural entity" ) }
Subroutine plural_count: called CC=1 SCAR=0.0
Pod undocumented
203 223 ▶sub context_id { shift->named_token( "context id" ) }
Subroutine context_id: called CC=1 SCAR=0.0
Pod undocumented
204 239 ▶sub domain_id { shift->named_token( "domain id" ) }
Subroutine domain_id: called CC=1 SCAR=0.0
Pod undocumented
205 792 ▶sub comma { shift->op( "," ) }
Subroutine comma: called CC=1 SCAR=0.0
Pod undocumented
206  
207 1539 ▶sub constant_string {
Subroutine constant_string: called CC=5 SCAR=16.1
Pod undocumented
208 1539 my ( $self ) = @_;
209  
210 1539 my @components = $self->literal_scopes;
211  
212 1539 my $string;
213 1539 ▶ if ( defined $self->concat_op ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->concat_op) { } 1412 127 1539
214 1412 $string = $self->list_of( $self->concat_op, $self->c_any_of( @components ) );
215 }
216 else {
217 # No concatenation operator: a string argument is a single literal.
218 127 ▶ my $single = $self->maybe( sub { $self->any_of( @components ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
219 127 ▶ $string = defined $single ? [$single] : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $single ? : 125 2 127
220 }
221  
222 1539 ▶ return join "", map { defined $_ ? $_ : "" } @{$string} if @{$string};
Branch: 2 branches
BranchTrueFalseTotal
if @{$string;} 1398 141 1539
defined $_ ? : 1417 0 1417
223  
224 141 $self->fail;
225 }
226  
227 # undef means the language has no concatenation operator.
228 2951 ▶sub concat_op {
Subroutine concat_op: called CC=1 SCAR=0.0
Pod undocumented
229 2951 my %ops = ( js => "+", pl => ".", tx => qr/(_|~)/, py => "+", php => ".", go => "+", rs => undef );
230 2951 return $ops{ shift->type };
231 }
232  
233 # String literal forms per language type. An entry is either
234 # [ open, content method, close, arguments ] or the name of a method that
235 # parses a whole literal by itself (for forms whose terminator depends on the
236 # opening). Quoted forms name the escape table their contents are read with.
237 1963 ▶sub literal_forms {
Subroutine literal_forms: called CC=1 SCAR=0.0
Pod undocumented
238 1963 my %forms = (
239 js => [ quoted( q["], "js" ), quoted( q['], "js" ), [ q[`], "js_template_contents", q[`] ] ],
240 pl => [ quoted( q["], "perl" ), quoted( q['], "single" ) ],
241 py => ["python_strings"],
242 tx => [ quoted( q["], "xslate" ), quoted( q['], "single" ) ],
243 # No single quotes: in Go they delimit a rune, not a string.
244 go => [ quoted( q["], "go" ), [ q[`], "go_backtick_string_contents", q[`] ] ],
245 # No single quotes either (chars and lifetimes), and no concatenation.
246 rs => [ quoted( q["], "rust" ), "rust_raw_string" ],
247 php => [ quoted( q["], "php" ), quoted( q['], "single" ), "php_heredoc_or_nowdoc" ],
248 );
249 1963 return @{ $forms{ shift->type } };
250 }
251  
252 19630 ▶sub quoted {
Subroutine quoted: called CC=1 SCAR=0.0
Pod undocumented
253 19630 my ( $quote, $escapes ) = @_;
254 19630 return [ $quote, "quoted_string_contents", $quote, $quote, $escapes ];
255 }
256  
257 1539 ▶sub literal_scopes {
Subroutine literal_scopes: called CC=1 SCAR=0.0
Pod undocumented
258 1539 my ( $self ) = @_;
259 1539 return map { $self->literal_scope( $_ ) } $self->literal_forms;
260 }
261  
262 # Whitespace before a literal is skipped; inside it, it is text.
263 3539 ▶sub literal_scope {
Subroutine literal_scope: called CC=2 SCAR=6.9
Pod undocumented
264 3539 my ( $self, $form ) = @_;
265 3539 ▶ if ( !ref $form ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $form) 547 2992 3539
266 547 my $read = $self->c_with_ws( $form );
267 547 ▶ return sub { $self->skip_ws; $self->$read };
Subroutine __ANON__: called CC=1 SCAR=0.0
268 }
269 2992 my ( $open, $contents, $close, @args ) = @{$form};
270 2992 return $self->curry::scope_of( $open, $self->c_with_ws( $contents, @args ), $close );
271 }
272  
273 # The text up to the closing quote, with its escapes read as the language
274 # reads them.
275 1289 ▶sub quoted_string_contents {
Subroutine quoted_string_contents: called CC=4 SCAR=13.9
Pod undocumented
276 1289 my ( $self, $quote, $escapes ) = @_;
277 1289 my $q = quotemeta $quote;
278 1289 my $text = $self->maybe_expect( qr/(?:[^\\$q]++|\\.)+/s );
279 1289 ▶ $text = $self->unescape( $text, $escapes ) if defined $text;
Branch: 1 branch
BranchTrueFalseTotal
if defined $text 1283 6 1289
280 1250 ▶ $self->fail( "no string contents found" ) if !defined $text or !length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 6 1244 1250
Condition66%2/3
defined $text and length $text
!ll&&!rl&&r
601244
MC/DC50%1/2
defined $text and length $text
defined $text length $text
Truth table66%2/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
281 1244 return $text;
282 }
283  
284 # Python: adjacent literals join, also over lines and comments. Each is plain,
285 # raw (r) or unicode (u), in single or triple quotes; raw ones keep their
286 # backslashes. Other prefixes (f, b, t and their raw forms) give no constant
287 # str: not read.
288 280 ▶sub python_strings {
Subroutine python_strings: called CC=3 SCAR=11.0
Pod undocumented
289 280 my ( $self ) = @_;
290 280 my @parts = ( $self->python_string );
291 160 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $part = $self->maybe(sub { $self->expect(qr/(?:\s|#[^\n]*|\\\r?\n)*/); $self->python_string; } ))) 5 160 165
292 defined(
293 my $part = $self->maybe(
294 165 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
295 165 $self->expect( qr/(?:\s|#[^\n]*|\\\r?\n)*/ );
296 8 $self->python_string;
297 }
298 )
299 )
300 )
301 {
302 5 push @parts, $part;
303 }
304 160 my $text = join "", @parts;
305 160 ▶ $self->fail( "no string contents found" ) if !length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 160 160
306 160 return $text;
307 }
308  
309 288 ▶sub python_string {
Subroutine python_string: called CC=3 SCAR=11.0
Pod undocumented
310 288 my ( $self ) = @_;
311 288 my ( undef, $prefix, $quote ) = $self->expect( qr/([rRuU]?)("""|'''|"|')/ );
312 169 my $q = quotemeta substr( $quote, 0, 1 );
313 169 ▶ my $body =
Branch: 1 branch
BranchTrueFalseTotal
length $quote == 3 ? : 4 165 169
314 length $quote == 3
315 ? qr/(?:[^\\$q]++|\\.|$q(?!$q$q))*/s
316 : qr/(?:[^\\$q\n]++|\\.)*/s;
317 169 my $text = $self->expect( $body );
318 166 $self->expect( qr/\Q$quote\E/ );
319 166 ▶ return lc $prefix eq "r" ? $text : $self->unescape( $text, "python" );
Branch: 1 branch
BranchTrueFalseTotal
lc $prefix eq 'r' ? : 6 160 166
320 }
321  
322 # A JavaScript template literal is a msgid while it holds no ${ substitution.
323 # Its line breaks are LF.
324 9 ▶sub js_template_contents {
Subroutine js_template_contents: called CC=3 SCAR=11.2
Pod undocumented
325 9 my ( $self ) = @_;
326 9 my $text = $self->maybe_expect( qr/(?:[^\\`\$]++|\\.|\$(?!\{))+/s );
327 9 ▶ $self->fail( "no string contents found" ) if !defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 9 9
328 9 $text =~ s/\r\n?/\n/g;
329 9 $text = $self->unescape( $text, "js_template" );
330 9 ▶ $self->fail( "no string contents found" ) if !length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 9 9
331 9 return $text;
332 }
333  
334 # Escapes inside string literals, per table: the patterns tried after a
335 # backslash, each with the text it stands for, and what becomes of an escape
336 # no pattern takes: the character alone (drop), backslash and character
337 # (keep), or no msgid at all (fail: the language rejects it). A table may
338 # name the variables the literal interpolates: such a literal is no constant
339 # and gives no msgid either; escaped sigils stay text. The scraper
340 # reads and writes bytes, so a character named by its code point is stored
341 # as its UTF-8 bytes; Go and PHP byte escapes are stored as the byte.
342 22 my %c_escape = ( a => "\a", b => "\b", e => "\e", f => "\f", n => "\n", r => "\r", t => "\t", v => "\x0b" );
343  
344 76 ▶sub c_escape { exists $c_escape{ $_[0] } ? $c_escape{ $_[0] } : $_[0] }
Subroutine c_escape: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
exists $c_escape{$_[0]} ? : 56 20 76
345  
346 12 ▶sub digits {
Subroutine digits: called CC=2 SCAR=7.2
Pod undocumented
347 12 ( my $digits = shift ) =~ tr/_//d;
348 12 ▶ return length $digits ? $digits : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $digits ? : 12 0 12
349 }
350  
351 24 ▶sub code_point {
Subroutine code_point: called CC=2 SCAR=6.9
Pod undocumented
352 24 my ( $number ) = @_;
353 24 ▶ die "code point out of range\n" if $number > 0x10FFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $number > 1114111 0 24 24
354 22 ▶ my $char = do { no warnings; chr $number };
Subroutine BEGIN: called CC=1 SCAR=0.0
355 24 utf8::encode( $char );
356 24 return $char;
357 }
358  
359 # Go and Rust take no surrogate halves.
360 3 ▶sub scalar_value {
Subroutine scalar_value: called CC=3 SCAR=11.4
Pod undocumented
361 3 my ( $number ) = @_;
362 3 ▶ die "surrogate half\n" if $number >= 0xD800 and $number <= 0xDFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $number >= 55296 and $number <= 57343 0 3 3
Condition66%2/3
$number >= 55296 and $number <= 57343
!ll&&!rl&&r
120
MC/DC0%0/2
$number >= 55296 and $number <= 57343
$number >= 55296 $number <= 57343
Truth table66%2/3
A and B
A= $number >= 55296B= $number <= 57343
ABresult
0X0
100
111
363 3 return code_point( $number );
364 }
365  
366 3 ▶sub named_char {
Subroutine named_char: called CC=2 SCAR=7.0
Pod undocumented
367 3 my ( $name ) = @_;
368 3 my $char = charnames::string_vianame( $name );
369 3 ▶ die "unknown character name\n" if !defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 3 3
370 3 utf8::encode( $char );
371 3 return $char;
372 }
373  
374 4 ▶sub reject { die "escape the language rejects\n" }
Subroutine reject: called CC=1 SCAR=0.0
Pod undocumented
375  
376 my @js_tail = (
377 [ qr/([bfnrtv])/ => \&c_escape ],
378 1 ▶ [ qr/x([0-9A-Fa-f]{2})/ => sub { code_point( hex $_[0] ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
379 0 ▶ [ qr/u([Dd][89ABab][0-9A-Fa-f]{2})\\u([Dd][C-Fc-f][0-9A-Fa-f]{2})/ => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
380 0 code_point( 0x10000 + ( hex( $_[0] ) - 0xD800 ) * 0x400 + hex( $_[1] ) - 0xDC00 );
381 }
382 ],
383 0 ▶ [ qr/u([0-9A-Fa-f]{4})/ => sub { code_point( hex $_[0] ) } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
384 2 ▶ [ qr/u\{([0-9A-Fa-f]+)\}/ => sub { code_point( hex $_[0] ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
385 [ qr/[xu]/ => \&reject ],
386 );
387  
388 my %escapes = (
389 perl => {
390 rules => [
391 [ qr/([abefnrt])/ => \&c_escape ],
392 4 ▶ [ qr/x\{\s*([0-9A-Fa-f_]*)\s*\}/ => sub { code_point( hex digits( $_[0] ) ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
393 2 ▶ [ qr/x([0-9A-Fa-f]{0,2})/ => sub { code_point( hex digits( $_[0] ) ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
394 2 ▶ [ qr/N\{U\+([0-9A-Fa-f_]+)\}/ => sub { code_point( hex digits( $_[0] ) ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
395 2 ▶ [ qr/N\{([^}]+)\}/ => sub { named_char( $_[0] ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
396 2 ▶ [ qr/o\{\s*([0-7_]+)\s*\}/ => sub { code_point( oct digits( $_[0] ) ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
397 2 ▶ [ qr/([0-7]{1,3})/ => sub { code_point( oct $_[0] ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
398 2 ▶ [ qr/c(.)/s => sub { chr( ord( uc $_[0] ) ^ 64 ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
399 # Case changes and \Q act on the text that follows: not read.
400 [ qr/[lLuUQEF]/ => \&reject ],
401 ],
402 other => "drop",
403 # Every $ names a variable (a lone one does not compile); an @ does
404 # before a name, ::, ', $, +, -, a digit or {. Under use utf8 the
405 # name may start with any letter.
406 interpolates => qr/\$|\@[0-9:'{\$+\-_\p{XIDS}]/,
407 },
408 js => {
409 rules => [
410 1 ▶ [ qr/(?:\r\n|\n|\r|\xE2\x80[\xA8\xA9])/ => sub { "" } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
411 0 ▶ [ qr/0(?![0-9])/ => sub { "\0" } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
412 1 ▶ [ qr/([0-3][0-7]{0,2}|[4-7][0-7]?)/ => sub { code_point( oct $_[0] ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
413 @js_tail,
414 ],
415 other => "drop",
416 },
417 # Templates take no octal escapes, nor \8 and \9.
418 js_template => {
419 rules => [
420 0 ▶ [ qr/(?:\n|\xE2\x80[\xA8\xA9])/ => sub { "" } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
421 0 ▶ [ qr/0(?![0-9])/ => sub { "\0" } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
422 [ qr/[0-9]/ => \&reject ],
423 @js_tail,
424 ],
425 other => "drop",
426 },
427 python => {
428 rules => [
429 1 ▶ [ qr/(?:\r\n|\n|\r)/ => sub { "" } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
430 [ qr/([\\'"abfnrtv])/ => \&c_escape ],
431 1 ▶ [ qr/([0-7]{1,3})/ => sub { code_point( oct $_[0] ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
432 1 ▶ [ qr/x([0-9A-Fa-f]{2})/ => sub { code_point( hex $_[0] ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
433 0 ▶ [ qr/u([0-9A-Fa-f]{4})/ => sub { code_point( hex $_[0] ) } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
434 1 ▶ [ qr/U([0-9A-Fa-f]{8})/ => sub { code_point( hex $_[0] ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
435 1 ▶ [ qr/N\{([^}]+)\}/ => sub { named_char( uc $_[0] ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
436 [ qr/[xuUN]/ => \&reject ],
437 ],
438 other => "keep",
439 },
440 go => {
441 rules => [
442 [ qr/([\\"abfnrtv])/ => \&c_escape ],
443 2 ▶ [ qr/([0-3][0-7]{2})/ => sub { chr oct $_[0] } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
444 2 ▶ [ qr/x([0-9A-Fa-f]{2})/ => sub { chr hex $_[0] } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
445 0 ▶ [ qr/u([0-9A-Fa-f]{4})/ => sub { scalar_value( hex $_[0] ) } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
446 1 ▶ [ qr/U([0-9A-Fa-f]{8})/ => sub { scalar_value( hex $_[0] ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
447 ],
448 other => "fail",
449 },
450 rust => {
451 rules => [
452 1 ▶ [ qr/\r?\n[ \t\r\n]*/ => sub { "" } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
453 0 ▶ [ qr/0/ => sub { "\0" } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
454 [ qr/([\\"'nrt])/ => \&c_escape ],
455 1 ▶ [ qr/x([0-7][0-9A-Fa-f])/ => sub { code_point( hex $_[0] ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
456 2 ▶ [ qr/u\{((?:[0-9A-Fa-f]_*){1,6})\}/ => sub { scalar_value( hex digits( $_[0] ) ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
457 ],
458 other => "fail",
459 },
460 xslate => {
461 rules => [ [ qr/([nrt])/ => \&c_escape ] ],
462 other => "drop",
463 },
464 # Perl, Xslate and PHP single quotes.
465 single => {
466 10 ▶ rules => [ [ qr/([\\'])/ => sub { $_[0] } ] ],
Subroutine __ANON__: called CC=1 SCAR=0.0
467 other => "keep",
468 },
469 php => {
470 rules => [
471 [ qr/([nrtvef\\\$"])/ => \&c_escape ],
472 1 ▶ [ qr/([0-7]{1,3})/ => sub { chr( oct( $_[0] ) & 0xFF ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
473 3 ▶ [ qr/x([0-9A-Fa-f]{1,2})/ => sub { chr hex $_[0] } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
474 1 ▶ [ qr/u\{([0-9A-Fa-f]+)\}/ => sub { code_point( hex $_[0] ) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
475 [ qr/u\{/ => \&reject ],
476 ],
477 other => "keep",
478 interpolates => qr/\$[A-Za-z_{[:^ascii:]]|\{\$/,
479 },
480 );
481  
482 # A heredoc reads the escapes of double quotes, except the quote itself.
483 $escapes{php_heredoc} = {
484 22 %{ $escapes{php} },
485 22 rules => [ [ qr/([nrtvef\\\$])/ => \&c_escape ], @{ $escapes{php}{rules} }[ 1 .. $#{ $escapes{php}{rules} } ] ],
486 };
487  
488 1459 ▶sub unescape {
Subroutine unescape: called CC=8 SCAR=20.8
Pod undocumented
489 1459 my ( $self, $text, $table ) = @_;
490 1459 my ( $rules, $other, $interpolates ) = @{ $escapes{$table} }{qw( rules other interpolates )};
491 1459 my $result = "";
492 1629 ▶ my $plain = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
493 1629 my ( $bytes ) = @_;
494 1629 utf8::decode( my $chars = $bytes ); # UTF-8 text is matched as characters
495 1629 ▶ $self->fail( "interpolated variable" ) if $interpolates and $chars =~ $interpolates;
Branch: 1 branch
BranchTrueFalseTotal
if $interpolates and $chars =~ /$interpolates/ 35 1594 1629
Condition100%3/3
$interpolates and $chars =~ /$interpolates/
!ll&&!rl&&r
92467035
MC/DC100%2/2
$interpolates and $chars =~ /$interpolates/
$interpolates $chars =~ /$interpolates/
Truth table100%3/3
A and B
A= $interpolatesB= $chars =~ /$interpolates/
ABresult
0X0
100
111
496 1594 return $bytes;
497 1459 };
498 1459 ▶ ESCAPE: while ( $text =~ /\G([^\\]*)\\/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G([^\\]*)\\/cg) 177 1452 1629
499 177 $result .= $plain->( $1 );
500 176 for my $rule ( @{$rules} ) {
501 623 my ( $pattern, $meaning ) = @{$rule};
502 623 ▶ next if $text !~ /\G$pattern/gc;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\G$pattern/cg 493 130 623
503 130 my $char = eval { $meaning->( $1, $2 ) };
504 130 ▶ $self->fail( "escape not read: $@" ) if !defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 4 126 130
505 126 $result .= $char;
506 126 next ESCAPE;
507 }
508 46 $text =~ /\G(.)/sgc;
509 46 ▶ $self->fail( "escape not read: \\$1" ) if $other eq "fail";
Branch: 1 branch
BranchTrueFalseTotal
if $other eq "fail" 2 44 46
510 44 ▶ $result .= $other eq "keep" ? "\\$1" : $1;
Branch: 1 branch
BranchTrueFalseTotal
$other eq 'keep' ? : 14 30 44
511 }
512 1452 ▶ return $result . $plain->( substr( $text, pos( $text ) || 0 ) );
Condition100%2/2
pos $text || 0
l!l
861366
MC/DC100%1/1
pos $text || 0
pos $text
Truth table100%2/2
A
A= pos $text
Aresult
11
00
513 }
514  
515 # Heredoc and nowdoc: the closing identifier is the one that was opened, so
516 # the whole literal is parsed here. A heredoc reads its escapes, a nowdoc
517 # keeps the text.
518 114 ▶sub php_heredoc_or_nowdoc {
Subroutine php_heredoc_or_nowdoc: called CC=3 SCAR=11.0
Pod undocumented
519 114 my ( $self ) = @_;
520 114 my @open = $self->expect( qr/<<<[ \t]*(?:'(\w+)'|"(\w+)"|(\w+))[ \t]*\r?\n/ );
521 10 shift @open; # the whole match comes first, then the captures
522 10 my $nowdoc = defined $open[0];
523 10 my ( $id ) = grep { defined } @open;
524 10 my $close = qr/\r?\n[ \t]*\Q$id\E\b/;
525 10 my $text = $self->raw_string_contents( $close );
526 10 $self->expect( $close );
527 10 ▶ $text = $self->unescape( $text, "php_heredoc" ) if !$nowdoc;
Branch: 1 branch
BranchTrueFalseTotal
unless $nowdoc 7 3 10
528 9 ▶ $self->fail( "no string contents found" ) if !length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 9 9
529 9 return $text;
530 }
531  
532 # A raw string ends at a quote followed by as many hashes as it was opened
533 # with, so it is parsed as a whole.
534 89 ▶sub rust_raw_string {
Subroutine rust_raw_string: called CC=1 SCAR=0.0
Pod undocumented
535 89 my ( $self ) = @_;
536 89 my ( undef, $hashes ) = $self->expect( qr/r(#*)"/ );
537 8 my $close = qr/"\Q$hashes\E/;
538 8 my $text = $self->raw_string_contents( $close );
539 8 $self->expect( $close );
540 8 return $text;
541 }
542  
543 4 ▶sub go_backtick_string_contents { shift->raw_string_contents( q[`] ) }
Subroutine go_backtick_string_contents: called CC=1 SCAR=0.0
Pod undocumented
544  
545 # Contents up to the given terminator pattern, as they are (PHP heredoc and
546 # nowdoc, Go backticks, Rust raw strings).
547 22 ▶sub raw_string_contents {
Subroutine raw_string_contents: called CC=2 SCAR=7.0
Pod undocumented
548 22 my ( $self, $stop ) = @_;
549 22 my $text = $self->substring_before( $stop );
550 22 ▶ $self->fail( "no string contents found" ) if !length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 22 22
551 22 return $text;
552 }
553  
554 22 1;
555  
556 __END__
« lib/Locale/Simple/CLI.pm ↑ Summary lib/Locale/Simple/Scraper/ParserShortcuts.pm »