ProtocolBuffers/Compiler.pm

filter: Statementstmt 92.5% 260 / 281 Branchbran 70.1% 94 / 134 Conditioncond 30.3% 10 / 33 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 91.4% 32 / 35 Podpod 0.0% 0 / 6 totaltotal 79.3% 400 / 504 CC 86 SCAR 29.4
CC
86
Cov
81%
_get_int_value
60.9
collect_fields
33.6
parse
14.2
CRAP 18.9
↑ Summary ProtocolBuffers/CodecIV32.pm »
1 package Google::ProtocolBuffers::Compiler;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Parse::RecDescent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Google::ProtocolBuffers::Constants qw/:types :labels/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Config qw/%Config/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 ##
12 ## Grammar is based on work by Alek Storm
13 ## http://groups.google.com/group/protobuf/browse_thread/thread/1cccfc624cd612da
14 ## http://groups.google.com/group/protobuf/attach/33102cfc0c57d449/proto2.ebnf?part=4
15 ##
16
17 12 my $grammar = <<'END_OF_GRAMMAR';
18
19 proto : <skip: qr! (?: //.*\n | \s+ )* !x>
20 ## list of top level declarations.
21 ## Skip empty declarations and ";".
22 (message | extend | enum | import | package | option | service | syntax | ";")(s) /\Z/
23 { $return = [ grep {ref $_} @{$item[2]} ]; }
24 | <error>
25
26 import : "import" <commit> strLit ";"
27 { $return = [ import => $item{strLit} ]; }
28 ## error? reject pair means:
29 ## if rule was commited (i.e. "import" was found), then fail the entire parse
30 ## otherwise, just skip this production (and try another one)
31 | <error?> <reject>
32
33 package : "package" <commit> qualifiedIdent ";"
34 { $return = [ package => $item{qualifiedIdent} ]; }
35 | <error?> <reject>
36
37 option : ## so far, options are ignored
38 "option" <commit> optionBody ";"
39 { $return = '' }
40 | <error?> <reject>
41
42 optionBody : qualifiedIdent "=" constant
43 { $return = '' }
44
45 message : "message" <commit> ident messageBody
46 { $return = [ message => $item{ident}, $item{messageBody} ]; }
47 | <error?> <reject>
48
49 extend : "extend" <commit> userType "{" ( field | group | ";" )(s?) "}"
50 { $return = [extend => $item{userType}, [ grep {ref $_} @{$item[5]}] ]}
51
52 enum : "enum" <commit> ident "{" (option | enumField | ";")(s) "}"
53 { $return = [ enum => $item{ident}, [grep {ref $_} @{$item[5]}] ] }
54 | <error?> <reject>
55
56 enumField : ident "=" intLit ";"
57 { $return = [ enumField => $item{ident}, $item{intLit} ] }
58
59 service : ## services are ignored
60 "service" <commit> ident "{" ( option | rpc | ";" )(s?) "}"
61 { $return = '' }
62 | <error?> <reject>
63
64 rpc : "rpc" <commit> ident "(" userType ")" "returns" "(" userType ")" rpcOptions(?) ";"
65 { $return = '' }
66 | <error?> <reject>
67
68 rpcOptions : "{" option(s?) "}"
69
70 messageBody : "{" <commit> ( field | enum | message | extend | extensions | group | option | oneof | ";" )(s?) "}"
71 { $return = [ grep {ref $_} @{$item[3]} ] }
72 | <error?> <reject>
73
74 group : label "group" <commit> ident "=" intLit messageBody
75 { $return = [group => $item{label}, $item{ident}, $item{intLit}, $item{messageBody} ] }
76 | <error?> <reject>
77
78 field : label type ident "=" intLit fOptList(?) ";"
79 { $return = [field => $item{label}, $item{type}, $item{ident}, $item{intLit}, $item[6][0] ] }
80
81 oneof : "oneof" <commit> ident "{" ( oneofField | ";" )(s?) "}"
82 { $return = [ oneof => $item{ident}, [grep {ref $_} @{$item[5]}] ] }
83 | <error?> <reject>
84  
85 oneofField : type ident "=" intLit fOptList(?) ";"
86 { $return = [field => "optional", $item{type}, $item{ident}, $item{intLit}, $item[5][0] ] }
87  
88 fOptList : "[" fieldOption(s? /,/) "]"
89 { $return = (grep {length($_)} @{$item[2]})[0] || '' }
90
91 fieldOption : "default" <commit> "=" constant
92 { $return = $item{constant} }
93 | optionBody
94 { $return = '' }
95 | <error?> <reject>
96
97 extensions : "extensions" <commit> extension(s /,/) ";"
98 { $return = '' }
99 | <error?> <reject>
100
101 extension : intLit ( "to" ( intLit | "max" ) )(s?)
102 { $return = '' }
103
104 label : "required" | "optional" | "repeated"
105
106
107 type : "double" | "float" | "int32" | "int64" | "uint32" | "uint64"
108 | "sint32" | "sint64" | "fixed32" | "fixed64" | "sfixed32" | "sfixed64"
109 | "bool" | "string" | "bytes" | userType
110
111 userType : (".")(?) qualifiedIdent
112 { $return = ($item[1] && @{$item[1]}) ? ".$item[2]" : $item[2] }
113
114 constant : ident
115 { $return = $item[1]; }
116 | (floatLit | intLit | strLit | boolLit)
117 { $return = { value => $item[1] } }
118
119 ident : /[a-z_]\w*/i
120
121 qualifiedIdent:
122 <leftop: ident "." ident>
123 { $return = join(".", @{ $item[1] })}
124
125 intLit : hexInt | octInt| decInt
126
127 decInt : /[-+]?[1-9]\d*/
128 { $return = Google::ProtocolBuffers::Compiler::get_dec_int($item[1]) }
129
130 hexInt : /[-+]?0[xX]([A-Fa-f0-9])+/
131 { $return = Google::ProtocolBuffers::Compiler::get_hex_int($item[1]) }
132
133 octInt : /[-+]?0[0-7]*/
134 { $return = Google::ProtocolBuffers::Compiler::get_oct_int($item[1]) }
135
136 floatLit : ## Make floatLit do not match integer literals,
137 ## so that it doesn't take off '0' from '0xFFF' or '012' (oct).
138 /[-+]?\d*\.\d+([Ee][\+-]?\d+)?/
139 | /[-+]?\d+[Ee][\+-]?\d+/
140
141
142 boolLit : "true"
143 { $return = 1 }
144 | "false"
145 { $return = 0 }
146
147 strLit : /['"]/ <skip:''> ( hexEscape | octEscape | charEscape | regularChar)(s?) /['"]/
148 { $return = join('', @{$item[3]}) }
149
150 regularChar : ## all chars exept chr(0) and "\n"
151 /[^\0\n'"]/
152
153 hexEscape : /\\[Xx]/ /[A-Fa-f0-9]{1,2}/
154 { $return = chr(hex($item[2])) }
155
156 octEscape : '\\' /^0?[0-7]{1,3}/
157 { $return = chr(oct("0$item[2]") & 0xFF); }
158
159 charEscape : /\\[abfnrtv\\'"]/
160 {
161 my $s = substr($item[1], 1, 1);
162 $return = ($s eq 'a') ? "\a" :
163 ($s eq 'b') ? "\b" :
164 ($s eq 'f') ? "\f" :
165 ($s eq 'n') ? "\n" :
166 ($s eq 'r') ? "\r" :
167 ($s eq 't') ? "\t" :
168 ($s eq 'v') ? "\x0b" : $s;
169 }
170
171
172 syntax : "syntax" "=" strLit ## syntax = "proto2";
173 {
174 die "Unknown syntax" unless $item{strLit} eq 'proto2';
175 $return = '';
176 }
177
178 END_OF_GRAMMAR
179
180 12 my %primitive_types = (
181 "double" => TYPE_DOUBLE,
182 "float" => TYPE_FLOAT,
183 "int32" => TYPE_INT32,
184 "int64" => TYPE_INT64,
185 "uint32" => TYPE_UINT32,
186 "uint64" => TYPE_UINT64,
187 "sint32" => TYPE_SINT32,
188 "sint64" => TYPE_SINT64,
189 "fixed32" => TYPE_FIXED32,
190 "fixed64" => TYPE_FIXED64,
191 "sfixed32" => TYPE_SFIXED32,
192 "sfixed64" => TYPE_SFIXED64,
193 "bool" => TYPE_BOOL,
194 "string" => TYPE_STRING,
195 "bytes" => TYPE_BYTES,
196 );
197
198 12 my %labels = (
199 'required' => LABEL_REQUIRED,
200 'optional' => LABEL_OPTIONAL,
201 'repeated' => LABEL_REPEATED,
202 );
203
204 12 my $has_64bit = $Config{ivsize}>=8;
205
206 2617 ▶sub _get_int_value {
Subroutine _get_int_value: called CC=12 SCAR=41.1
207 2617 my $str = shift;
208 2617 my $max_pos_str = shift;
209 2617 my $max_neg_str = shift;
210 2617 my $str_to_num = shift;
211 2617 my $str_to_bigint = shift;
212
213 2617 my $is_negative = ($str =~/^-/);
214 2617 $str =~ s/^[+-]//;
215
216 2617 ▶ if (!$has_64bit) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_64bit) 0 2617 2617
217 0 my $l = length($str);
218 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_negative and $l > length $max_pos_str || $l == length $max_pos_str && uc $str ge uc $max_pos_str or $is_negative and $l > length $max_neg_str || $l == length $max_neg_str && uc $str ge uc $max_neg_str) 0 0 0
Condition0%0/21
not $is_negative and $l > length $max_pos_str || $l == length $max_pos_str && uc $str ge uc $max_pos_str or $is_negative and $l > length $max_neg_str || $l == length $max_neg_str && uc $str ge uc $max_neg_str
l!l&&r!l&&!r
000
not $is_negative and $l > length $max_pos_str || $l == length $max_pos_str && uc $str ge uc $max_pos_str
!ll&&!rl&&r
000
$l > length $max_pos_str or $l == length $max_pos_str and uc $str ge uc $max_pos_str
l!l&&r!l&&!r
000
$l == length $max_pos_str and uc $str ge uc $max_pos_str
!ll&&!rl&&r
000
$is_negative and $l > length $max_neg_str || $l == length $max_neg_str && uc $str ge uc $max_neg_str
!ll&&!rl&&r
000
$l > length $max_neg_str or $l == length $max_neg_str and uc $str ge uc $max_neg_str
l!l&&r!l&&!r
000
$l == length $max_neg_str and uc $str ge uc $max_neg_str
!ll&&!rl&&r
000
MC/DC0%0/8
not $is_negative and $l > length $max_pos_str || $l == length $max_pos_str && uc $str ge uc $max_pos_str or $is_negative and $l > length $max_neg_str || $l == length $max_neg_str && uc $str ge uc $max_neg_str
not $is_negative $l > length $max_pos_str $l == length $max_pos_str uc $str ge uc $max_pos_str $is_negative $l > length $max_neg_str $l == length $max_neg_str uc $str ge uc $max_neg_str
Truth table0%0/17
not(A) and B or C and D or E and F or G and H
A= not $is_negativeB= $l > length $max_pos_strC= $l == length $max_pos_strD= uc $str ge uc $max_pos_strE= $is_negativeF= $l > length $max_neg_strG= $l == length $max_neg_strH= uc $str ge uc $max_neg_str
ABCDEFGHresult
11XXXXXX1
1011XXXX1
0XXX11XX1
0XXX10111
100X11XX1
100X10111
101011XX1
101010111
0XXX0XXX0
0XXX100X0
0XXX10100
100X0XXX0
100X100X0
100X10100
10100XXX0
1010100X0
101010100
219 !$is_negative &&
220 ($l>length($max_pos_str)
221 || ($l==length($max_pos_str) && uc($str) ge uc($max_pos_str)))
222 || $is_negative &&
223 ( $l>length($max_neg_str)
224 || ($l==length($max_neg_str) && uc($str) ge uc($max_neg_str)))
225 )
226 {
227 0 my $v = $str_to_bigint->($str);
228 0 ▶ return ($is_negative) ? -$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
$is_negative ? : 0 0 0
229 }
230 }
231
232 2617 my $v = $str_to_num->($str);
233 2617 ▶ return ($is_negative) ? -$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
$is_negative ? : 73 2544 2617
234 }
235
236 2570 ▶sub get_dec_int {
Subroutine get_dec_int: called CC=1 SCAR=0.0
Pod undocumented
237 2570 my $str = shift;
238
239 return _get_int_value(
240 $str, "2147483647", "2147483648",
241 2570 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
242 12 ▶ no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
243 2570 return $_[0]+0;
244 },
245 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
246 0 return Math::BigInt->new($_[0]);
247 }
248 2570 );
249 }
250
251 36 ▶sub get_hex_int {
Subroutine get_hex_int: called CC=1 SCAR=0.0
Pod undocumented
252 36 my $str = shift;
253
254 return _get_int_value(
255 $str, "0x7fffffff", "0x80000000",
256 36 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
257 12 ▶ no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
258 36 return hex($_[0]);
259 },
260 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
261 0 return Math::BigInt->new($_[0]);
262 }
263 36 );
264 }
265
266 11 ▶sub get_oct_int {
Subroutine get_oct_int: called CC=1 SCAR=0.6
Pod undocumented
267 11 my $str = shift;
268 return _get_int_value(
269 $str, "017777777777", "020000000000",
270 11 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
271 12 ▶ no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
272 11 return oct($_[0]);
273 },
274 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
275 ## oops, Math::BigInt doesn't accept strings of octal digits,
276 ## ... but accepts binary digits
277 0 my $v = shift;
278 0 my @oct_2_binary = qw(000 001 010 011 100 101 110 111);
279 0 $v =~ s/(.)/$oct_2_binary[$1]/g;
280 0 return Math::BigInt->new('0b' . $v);
281 }
282 11 );
283 }
284
285
286 22 ▶sub parse {
Subroutine parse: called CC=14 SCAR=26.5
Pod undocumented
287 22 my $class = shift;
288 22 my $source = shift;
289 22 my $opts = shift;
290
291 22 my $self = bless { opts => $opts };
292
293 22 $::RD_ERRORS = 1;
294 22 $::RD_WARN = 1;
295 22 ▶ my $parser = Parse::RecDescent->new($grammar) or die;
Branch: 1 branch
BranchTrueFalseTotal
my $parser = "Parse::RecDescent"->new($grammar) or die 22 0 22
296
297 ## all top level declarations from all files (files be included)
298 ## will be here
299 22 my @parse_tree;
300
301 22 my (@import_files, $text);
302 22 ▶ if ($source->{text}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($source->{'text'}) { } 12 10 22
elsif ($source->{'file'}) { } 10 0 10
303 12 $text = $source->{text};
304 } elsif ($source->{file}) {
305 10 @import_files = ('', $source->{file});
306 } else {
307 0 die;
308 }
309
310 22 my %already_included_files;
311
312 22 ▶ while ($text || @import_files) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text or @import_files) 31 22 53
Condition100%3/3
$text or @import_files
l!l&&r!l&&!r
121922
MC/DC100%2/2
$text or @import_files
$text @import_files
Truth table100%3/3
A or B
A= $textB= @import_files
ABresult
1X1
011
000
313 31 my ($content, $filename);
314
315 31 ▶ if ($text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) { } 12 19 31
316 12 $content = $text;
317 12 undef $text;
318 } else {
319 ## path may be relative to the path of the file, where
320 ## "import" directive. Also, root dir for proto files
321 ## may be specified in options
322 19 my ($root, $path) = splice(@import_files, 0, 2);
323 19 $filename = $self->_find_filename($root, $path);
324 19 ▶ next if $already_included_files{$filename}++;
Branch: 1 branch
BranchTrueFalseTotal
if $already_included_files{$filename}++ 0 19 19
325 {
326 19 my $fh;
327 19 ▶ open $fh, $filename or die "Can't read from $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $filename or die "Can't read from ${filename}: $!" 19 0 19
328 19 local $/;
329 19 $content = <$fh>;
330 19 close $fh;
331 }
332 }
333
334 31 my $res = $parser->proto($content);
335 31 ▶ die "" unless defined $res;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res 0 31 31
336
337 ## start each file from empty package
338 31 push @parse_tree, [package=>''];
339 31 foreach my $decl (@$res) {
340 311 ▶ if ($decl->[0] eq 'import') {
Branch: 1 branch
BranchTrueFalseTotal
if ($decl->[0] eq 'import') { } 9 302 311
341 9 push @import_files, ($filename, $decl->[1]);
342 } else {
343 302 push @parse_tree, $decl;
344 }
345 }
346 }
347
348 ##
349 ## Pass #1.
350 ## Find names of messages and enums, including nested ones.
351 ##
352 22 my $symbol_table = Google::ProtocolBuffers::Compiler::SymbolTable->new;
353 22 $self->{symbol_table} = $symbol_table;
354 22 $self->collect_names('', \@parse_tree);
355
356 ##
357 ## Pass #2.
358 ## Create complete descriptions of messages with extensions.
359 ## For each field of a user type a fully quilified type name must be found.
360 ## For each default value defined by a constant (enum), a f.q.n of enum value must be found
361 ##
362 22 foreach my $kind (qw/message group enum oneof/) {
363 88 foreach my $fqname ($symbol_table->lookup_names_of_kind($kind)) {
364 344 $self->{types}->{$fqname} = { kind => $kind, fields => [], extensions => [], oneofs => [] };
365 }
366 }
367 22 $self->collect_fields('', \@parse_tree);
368
369 22 return $self->{types};
370 }
371
372 19 ▶sub _find_filename {
Subroutine _find_filename: called CC=6 SCAR=18.5
373 19 my $self = shift;
374 19 my $base_filename = shift;
375 19 my $path = shift;
376
377 =comment
378 my $filename = File::Spec->rel2abs($path, $base_filename);
379 return $filename if -e $filename;
380
381 if ($self->{opts}->{include_dir}) {
382 $filename = File::Spec->rel2abs($path, $self->{opts}->{include_dir});
383 return $filename if -e $filename;
384 }
385 =cut
386 12 ▶ use Cwd; my $d = getcwd();
Subroutine BEGIN: called CC=1 SCAR=0.0
387
388 19 my $filename = $path;
389 19 ▶ return $filename if -e $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -e $filename 0 19 19
390
391 19 ▶ if (my $inc_dirs = $self->{opts}->{include_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $inc_dirs = $self->{"opts"}{"include_dir"}) 19 0 19
392 19 ▶ $inc_dirs = [ $inc_dirs ] unless(ref($inc_dirs) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $inc_dirs eq "ARRAY" 19 0 19
393 19 foreach my $d (@$inc_dirs){
394 19 $filename = File::Spec->catfile($d, $path);
395 19 ▶ return $filename if -e $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -e $filename 19 0 19
396 }
397 }
398 0 die "Can't find proto file: '$path'";
399 }
400
401
402 402 ▶sub collect_names {
Subroutine collect_names: called CC=11 SCAR=24.0
Pod undocumented
403 402 my $self = shift;
404 402 my $context = shift;
405 402 my $nodes = shift;
406
407 402 my $symbol_table = $self->{symbol_table};
408 402 foreach my $decl (@$nodes) {
409 2661 my $kind = $decl->[0]; ## 'message', 'extent', 'enum' etc...
410 2661 ▶ if ($kind eq 'package') {
Branch: 8 branches
BranchTrueFalseTotal
if ($kind eq 'package') { } 57 2604 2661
elsif ($kind eq 'message') { } 237 2367 2604
elsif ($kind eq 'enum') { } 49 2318 2367
elsif ($kind eq 'group') { } 57 2261 2318
elsif ($kind eq 'oneof') { } 1 2260 2261
elsif ($kind eq 'extend') { } 37 2223 2260
elsif ($kind eq 'field') { } 2022 201 2223
elsif ($kind eq 'enumField') { } 201 0 201
411 ## package directive just set new context,
412 ## not related to previous one
413 57 $context = $symbol_table->set_package($decl->[1]);
414 } elsif ($kind eq 'message') {
415 ## message may include nested messages/enums/groups/oneofs
416 237 my $child_context = $symbol_table->add('message' => $decl->[1], $context);
417 237 $self->collect_names($child_context, $decl->[2]);
418 } elsif ($kind eq 'enum') {
419 49 my $child_context = $symbol_table->add('enum' => $decl->[1], $context);
420 49 $self->collect_names($child_context, $decl->[2]);
421 } elsif ($kind eq 'group') {
422 ## there may be nested messages/enums/groups/oneofs etc. inside group
423 ## [group => $label, $ident, $intLit, $messageBody ]
424 57 my $child_context = $symbol_table->add('group' => $decl->[2], $context);
425 57 $self->collect_names($child_context, $decl->[4]);
426 } elsif ($kind eq 'oneof') {
427 ## OneOfs may only contain fields, we add them to both
428 ## the current and oneof context
429 1 my $child_context = $symbol_table->add('oneof' => $decl->[1], $context);
430 1 foreach my $oneof (@{$decl->[2]}) {
431 2 $symbol_table->add('field' => $oneof->[3], $context);
432 2 $symbol_table->add('field' => $oneof->[3], $child_context);
433 }
434 } elsif ($kind eq 'extend') {
435 ## extend blocks are tricky:
436 ## 1) they don't create a new scope
437 ## 2) there may be a group inside extend block, and there may be everything inside the group
438 37 $self->collect_names($context, $decl->[2]);
439 } elsif ($kind eq 'field') {
440 ## we add fields into symbol table just to check their uniqueness
441 ## in several extension blocks or oneofs. Example:
442 ## .proto:
443 ## extend A { required int32 foo = 100 };
444 ## extend B { required int32 foo = 200 };
445 ## // Invalid! foo is already declared!
446 ##
447 2022 $symbol_table->add('field' => $decl->[3], $context);
448 } elsif ($kind eq 'enumField') {
449 201 $symbol_table->add('enum_field' => $decl->[1], $context);
450 } else {
451 0 warn $kind;
452 }
453 }
454 }
455
456 403 ▶sub collect_fields {
Subroutine collect_fields: called CC=31 SCAR=35.1
Pod undocumented
457 403 my $self = shift;
458 403 my $context = shift;
459 403 my $nodes = shift;
460 403 my $destination_type_name = shift;
461 403 my $is_extension = shift;
462
463 403 my $symbol_table = $self->{symbol_table};
464 403 foreach my $decl (@$nodes) {
465 2663 my $kind = $decl->[0]; ## 'message', 'extent', 'enum' etc...
466 2663 ▶ if ($kind eq 'package') {
Branch: 8 branches
BranchTrueFalseTotal
if ($kind eq 'package') { } 57 2606 2663
elsif ($kind eq 'message') { } 237 2369 2606
elsif ($kind eq 'enum') { } 49 2320 2369
elsif ($kind eq 'group') { } 57 2263 2320
elsif ($kind eq 'oneof') { } 1 2262 2263
elsif ($kind eq 'extend') { } 37 2225 2262
elsif ($kind eq 'field') { } 2024 201 2225
elsif ($kind eq 'enumField') { } 201 0 201
467 57 $context = $decl->[1];
468 } elsif ($kind eq 'message') {
469 237 ▶ my $child_context = ($context) ? "$context.$decl->[1]" : $decl->[1];
Branch: 1 branch
BranchTrueFalseTotal
$context ? : 231 6 237
470 237 $self->collect_fields($child_context, $decl->[2], $child_context);
471 } elsif ($kind eq 'enum') {
472 49 ▶ my $child_context = ($context) ? "$context.$decl->[1]" : $decl->[1];
Branch: 1 branch
BranchTrueFalseTotal
$context ? : 49 0 49
473 49 $self->collect_fields($child_context, $decl->[2], $child_context);
474 } elsif ($kind eq 'group') {
475 ## groups are tricky: they are both definition of a field and type.
476 ## [group => $label, $ident, $intLit, $messageBody ]
477 ## first, collect fields inside the group
478 57 ▶ my $child_context = ($context) ? "$context.$decl->[2]" : $decl->[2];
Branch: 1 branch
BranchTrueFalseTotal
$context ? : 57 0 57
479 57 $self->collect_fields($child_context, $decl->[4], $child_context);
480 ## second, add the group as one field to parent (destination) type
481 57 ▶ confess unless $destination_type_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $destination_type_name 0 57 57
482 57 my $name;
483 my $fields_list;
484 57 ▶ if ($is_extension) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_extension) { } 18 39 57
485 ## for extensions, fully quilified names of fields are used,
486 ## because they may be declared anywhere - even in another package
487 18 $fields_list = $self->{types}->{$destination_type_name}->{extensions};
488 18 $name = $symbol_table->lookup('group' => $decl->[2], $context);
489 } else {
490 ## regualar fields are always immediate children of their type
491 39 $fields_list = $self->{types}->{$destination_type_name}->{fields};
492 39 $name = $decl->[2];
493 }
494 57 ▶ my $label = (exists $labels{$decl->[1]}) ? $labels{$decl->[1]} : die;
Branch: 1 branch
BranchTrueFalseTotal
exists $labels{$decl->[1]} ? : 57 0 57
495 57 my ($type_name, $kind) = $symbol_table->lookup_symbol($decl->[2], $context);
496 57 ▶ die unless $kind eq 'group';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "group" 0 57 57
497 57 my $field_number = $decl->[3];
498 57 push @$fields_list, [$label, $type_name, $name, $field_number];
499 } elsif ($kind eq 'oneof') {
500 1 ▶ my $child_context = ($context) ? "$context.$decl->[1]" : $decl->[1];
Branch: 1 branch
BranchTrueFalseTotal
$context ? : 1 0 1
501 1 $self->collect_fields($child_context, $decl->[2], $child_context);
502 1 push @{$self->{types}->{$destination_type_name}->{oneofs}}, $child_context;
503 } elsif ($kind eq 'extend') {
504 ## what is the fqn of the message to be extended?
505 37 my $destination_message = $symbol_table->lookup('message' => $decl->[1], $context);
506 37 $self->collect_fields($context, $decl->[2], $destination_message, 1);
507 } elsif ($kind eq 'field') {
508 2024 ▶ confess unless $destination_type_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $destination_type_name 0 2024 2024
509 # $decl = ['field' => $label, $type, $ident, $item{intLit}, $item{fOptList}] }
510 2024 my $name;
511 my $fields_list;
512 2024 ▶ if ($is_extension) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_extension) { } 640 1384 2024
513 ## for extensions, fully quilified names of fields are used,
514 ## because they may be declared anywhere - even in another package
515 640 $fields_list = $self->{types}->{$destination_type_name}->{extensions};
516 640 $name = $symbol_table->lookup('field' => $decl->[3], $context);
517 } else {
518 ## regualar fields are always immediate children of their type
519 1384 $fields_list = $self->{types}->{$destination_type_name}->{fields};
520 1384 $name = $decl->[3];
521 }
522
523 2024 ▶ my $label = (exists $labels{$decl->[1]}) ? $labels{$decl->[1]} : die;
Branch: 1 branch
BranchTrueFalseTotal
exists $labels{$decl->[1]} ? : 2024 0 2024
524
525 2024 my ($type_name, $kind);
526 2024 ▶ if (exists $primitive_types{$decl->[2]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $primitive_types{$decl->[2]}) { } 1619 405 2024
527 1619 $type_name = $primitive_types{$decl->[2]};
528 } else {
529 405 ($type_name, $kind) = $symbol_table->lookup_symbol($decl->[2], $context);
530 405 ▶ die unless $kind eq 'message' || $kind eq 'group' || $kind eq 'enum';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "message" or $kind eq "group" or $kind eq "enum" 0 405 405
Condition66%4/6
$kind eq "message" or $kind eq "group" or $kind eq "enum"
l!l&&r!l&&!r
2201850
$kind eq "message" or $kind eq "group"
l!l&&r!l&&!r
2200185
MC/DC0%0/3
$kind eq "message" or $kind eq "group" or $kind eq "enum"
$kind eq "message" $kind eq "group" $kind eq "enum"
Truth table50%2/4
A or B or C
A= $kind eq "message"B= $kind eq "group"C= $kind eq "enum"
ABCresult
1XX1
01X1
0011
0000
531 }
532
533 2024 my $field_number = $decl->[4];
534
535 2024 my $default_value = $decl->[5];
536 2024 ▶ if ($default_value && !ref $default_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default_value and not ref $default_value) 74 1950 2024
Condition100%3/3
$default_value and not ref $default_value
!ll&&!rl&&r
159735374
MC/DC100%2/2
$default_value and not ref $default_value
$default_value not ref $default_value
Truth table100%3/3
A and not(B)
A= $default_valueB= not ref $default_value
ABresult
0X0
100
111
537 74 ▶ if ($default_value eq 'true') {
Branch: 2 branches
BranchTrueFalseTotal
if ($default_value eq 'true') { } 18 56 74
elsif ($default_value eq 'false') { } 0 56 56
538 18 $default_value = { value => 1 };
539 } elsif ($default_value eq 'false') {
540 0 $default_value = { value => 0 };
541 } else {
542 ## this default is enum value
543 ## type name must be fqn of enum type
544 56 ▶ die unless $kind eq 'enum';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "enum" 0 56 56
545 56 $default_value = $symbol_table->lookup('enum_field' => $default_value, $type_name);
546 }
547 }
548 2024 push @$fields_list, [$label, $type_name, $name, $field_number, $default_value];
549 } elsif ($kind eq 'enumField') {
550 201 ▶ confess unless $destination_type_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $destination_type_name 0 201 201
551 201 my $fields_list = $self->{types}->{$destination_type_name}->{fields};
552 201 push @{$fields_list}, [$decl->[1], $decl->[2]];
553 } else {
554 0 warn $kind;
555 }
556 }
557 }
558
559 package Google::ProtocolBuffers::Compiler::SymbolTable;
560 ##
561 ## %$self - symbol name table, descriptions of fully qualified names like Foo.Bar:
562 ## $names{'foo'} = { kind => 'package' }
563 ## $names{'foo.Bar'} = { kind => 'message' }
564 ## $names{'foo.Bar.Baz'}={ kind => 'enum', }
565 ##
566 12 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
567 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
568
569 22 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
570 22 my $class = shift;
571 22 return bless {}, $class;
572 }
573
574 57 ▶sub set_package {
Subroutine set_package: called CC=5 SCAR=16.5
575 57 my $self = shift;
576 57 my $package = shift;
577
578 57 ▶ return '' unless $package;
Branch: 1 branch
BranchTrueFalseTotal
unless $package 31 26 57
579
580 26 my @idents = split qr/\./, $package;
581 26 my $name = shift @idents;
582 26 while (1) {
583 34 ▶ if (exists $self->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$name}) { } 0 34 34
584 0 ▶ die unless $self->{$name}->{kind} eq 'package';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$name}{"kind"} eq "package" 0 0 0
585 } else {
586 34 $self->{$name} = {kind => 'package'}
587 }
588 34 ▶ last unless @idents;
Branch: 1 branch
BranchTrueFalseTotal
unless @idents 26 8 34
589 8 $name .= '.' . shift(@idents);
590 }
591 26 return $name;
592 }
593
594 2772 ▶sub _add {
Subroutine _add: called CC=5 SCAR=16.4
595 2772 my $self = shift;
596 2772 my $kind = shift;
597 2772 my $name = shift;
598 2772 my $context = shift;
599
600 ## no fully quilified names are alowed to declare (so far)
601 2772 ▶ die if $name =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\./ 0 2772 2772
602 2772 my $fqn;
603 2772 ▶ if ($context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context) { } 2765 7 2772
604 2765 ▶ die "$name, $context" unless $self->{$context};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$context} 0 2765 2765
605 2765 $fqn = "$context.$name";
606 } else {
607 7 $fqn = $name;
608 }
609
610 2772 ▶ if (exists $self->{$fqn}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$fqn}) { } 0 2772 2772
611 0 die "Name '$fqn' is already defined";
612 } else {
613 2772 $self->{$fqn} = { kind=>$kind };
614 }
615
616 2772 return $fqn;
617 }
618
619 2571 ▶sub add {
Subroutine add: called CC=3 SCAR=11.0
620 2571 my $self = shift;
621 2571 my $kind = shift;
622 2571 my $name = shift;
623 2571 my $context = shift;
624
625 ## tricky: enum values are both children and siblings of enums
626 2571 ▶ if ($kind eq 'enum_field') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq 'enum_field') { } 201 2370 2571
627 201 ▶ die unless $self->{$context}->{kind} eq 'enum';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$context}{"kind"} eq "enum" 0 201 201
628 201 my $fqn = $self->_add($kind, $name, $context);
629 201 $context =~ s/(^|\.)\w+$//; ## parent context
630 201 $self->_add($kind, $name, $context);
631 201 return $fqn;
632 } else {
633 2370 return $self->_add($kind, $name, $context);
634 }
635 }
636
637 ## input: fully or partially qualified name
638 ## output: (fully qualified name, its kind - 'message', 'enum_field' etc.)
639 1213 ▶sub lookup_symbol {
Subroutine lookup_symbol: called CC=6 SCAR=19.2
640 1213 my $self = shift;
641 1213 my $n = shift;
642 1213 my $c = shift;
643
644 1213 my $context = $c;
645 1213 my $name = $n;
646 1213 ▶ if ($name =~ s/^\.//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/^\.//) { } 0 1213 1213
647 ## this is an fully quialified name
648 0 ▶ if (exists $self->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$name}) 0 0 0
649 0 return ($name, $self->{$name}->{kind});
650 }
651 } else {
652 ## relative name - look it up in the current context and up
653 1213 while (1) {
654 1533 ▶ my $fqn = ($context) ? "$context.$name" : $name;
Branch: 1 branch
BranchTrueFalseTotal
$context ? : 1441 92 1533
655 1533 ▶ if (exists $self->{$fqn}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$fqn}) 1213 320 1533
656 1213 return ($fqn, $self->{$fqn}->{kind});
657 }
658 ## one level up
659 320 ▶ last unless $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $context 0 320 320
660 320 $context =~ s/(^|\.)\w+$//;
661 }
662 }
663 0 die "Name '$name' ($c, $n) is not defined" . Data::Dumper::Dumper($self);
664 }
665
666 ## input: kind, fully or partially qualified name, context
667 ## ouptut: fully qualified name
668 ## if found kind of the name doesn't match given kind, an exception is raised
669 751 ▶sub lookup {
Subroutine lookup: called CC=2 SCAR=7.1
670 751 my $self = shift;
671 751 my $kind = shift;
672 751 my $name = shift;
673 751 my $context = shift;
674
675 751 my ($fqn, $k) = $self->lookup_symbol($name, $context);
676 751 ▶ unless ($kind eq $k) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($kind eq $k) 0 751 751
677 0 confess "Error: while looking for '$kind' named '$name' in '$context', a '$k' named '$fqn' was found";
678 }
679 751 return $fqn;
680 }
681
682 ## returns list of all fully qualified name of a given kind
683 88 ▶sub lookup_names_of_kind {
Subroutine lookup_names_of_kind: called CC=1 SCAR=0.0
684 88 my $self = shift;
685 88 my $kind = shift;
686
687 88 return grep { $self->{$_}->{kind} eq $kind } keys %$self;
688 }
689
690 12 1;
↑ Summary ProtocolBuffers/CodecIV32.pm »