PYX/Parser.pm

filter: Statementstmt 72.5% 74 / 102 Branchbran 61.9% 26 / 42 Conditioncond 41.6% 5 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 84.6% 11 / 13 Podpod 100% 5 / 5 totaltotal 67.0% 122 / 182 SCAR 39.6
CC
27
Cov
67%
parse
42.0
parse_handler
9.7
_parse
9.0
CRAP 52.5
↑ Summary PYX.pm »
1 package PYX::Parser;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Constants.
12 7 Readonly::Scalar my $EMPTY_STR => q{};
13  
14 7 our $VERSION = 0.08;
15  
16 # Constructor.
17 sub new {
18 7 ▶ my ($class, @params) = @_;
Subroutine new: called CC=3 SCAR=11.4
Pod documented
19 7 my $self = bless {}, $class;
20  
21 # Parse callbacks.
22 $self->{'callbacks'} = {
23 'attribute' => undef,
24 'comment' => undef,
25 'data' => undef,
26 'end_element' => undef,
27 'final' => undef,
28 'init' => undef,
29 'instruction' => undef,
30 'rewrite' => undef,
31 'start_element' => undef,
32 'other' => undef,
33 },
34  
35 # Input encoding.
36 7 $self->{'input_encoding'} = 'utf-8';
37  
38 # Non parser options.
39 7 $self->{'non_parser_options'} = {};
40  
41 # Output encoding.
42 7 $self->{'output_encoding'} = 'utf-8';
43  
44 # Output handler.
45 7 $self->{'output_handler'} = \*STDOUT;
46  
47 # Output rewrite.
48 7 $self->{'output_rewrite'} = 0;
49  
50 # Process params.
51 7 set_params($self, @params);
52  
53 # Check output handler.
54 5 ▶ if (defined $self->{'output_handler'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"output_handler"} and ref $self->{"output_handler"} ne "GLOB") 0 5 5
Condition33%1/3
defined $self->{"output_handler"} and ref $self->{"output_handler"} ne "GLOB"
!ll&&!rl&&r
050
MC/DC0%0/2
defined $self->{"output_handler"} and ref $self->{"output_handler"} ne "GLOB"
defined $self->{"output_handler"} ref $self->{"output_handler"} ne "GLOB"
Truth table33%1/3
A and B
A= defined $self->{"output_handler"}B= ref $self->{"output_handler"} ne "GLOB"
ABresult
0X0
100
111
55 && ref $self->{'output_handler'} ne 'GLOB') {
56  
57 0 err 'Bad output handler.';
58 }
59  
60 # Processing line.
61 5 $self->{'_line'} = $EMPTY_STR;
62  
63 # Object.
64 5 return $self;
65 }
66  
67 # Get actual parsing line.
68 sub line {
69 0 ▶ my $self = shift;
Subroutine line: not called CC=1 SCAR=6.9
Pod documented
70  
71 0 return $self->{'_line'};
72 }
73  
74 # Parse PYX text or array of PYX text.
75 sub parse {
76 0 ▶ my ($self, $pyx, $out) = @_;
Subroutine parse: not called CC=6 SCAR=37.4
Pod documented
77  
78 0 ▶ if (! defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $out) 0 0 0
79 0 $out = $self->{'output_handler'};
80 }
81  
82 # Input data.
83 0 my @text;
84 0 ▶ if (ref $pyx eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pyx eq 'ARRAY') { } 0 0 0
85 0 @text = @{$pyx};
86 } else {
87 0 @text = split /\n/ms, $pyx;
88 }
89  
90 # Parse.
91 0 ▶ if ($self->{'callbacks'}->{'init'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"callbacks"}{"init"}) 0 0 0
92 0 &{$self->{'callbacks'}->{'init'}}($self);
93 }
94 0 foreach my $line (@text) {
95 0 $self->_parse($line, $out);
96 }
97 0 ▶ if ($self->{'callbacks'}->{'final'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"callbacks"}{"final"}) 0 0 0
98 0 &{$self->{'callbacks'}->{'final'}}($self);
99 }
100  
101 0 return;
102 }
103  
104 # Parse file with PYX data.
105 sub parse_file {
106 16 ▶ my ($self, $input_file, $out) = @_;
Subroutine parse_file: called CC=1 SCAR=0.0
Pod documented
107  
108 16 open my $inf, '<', $input_file;
109 16 $self->parse_handler($inf, $out);
110 16 close $inf;
111  
112 16 return;
113 }
114  
115 # Parse PYX handler.
116 sub parse_handler {
117 16 ▶ my ($self, $input_file_handler, $out) = @_;
Subroutine parse_handler: called CC=7 SCAR=22.7
Pod documented
118  
119 16 ▶ if (! $input_file_handler || ref $input_file_handler ne 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $input_file_handler or ref $input_file_handler ne "GLOB") 0 16 16
Condition33%1/3
not $input_file_handler or ref $input_file_handler ne "GLOB"
l!l&&r!l&&!r
0016
MC/DC0%0/2
not $input_file_handler or ref $input_file_handler ne "GLOB"
not $input_file_handler ref $input_file_handler ne "GLOB"
Truth table33%1/3
not(A) or B
A= not $input_file_handlerB= ref $input_file_handler ne "GLOB"
ABresult
1X1
011
000
120 0 err 'No input handler.';
121 }
122 16 ▶ if (! defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $out) 16 0 16
123 16 $out = $self->{'output_handler'};
124 }
125 16 ▶ if ($self->{'callbacks'}->{'init'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"callbacks"}{"init"}) 0 16 16
126 0 &{$self->{'callbacks'}->{'init'}}($self);
127 }
128 16 ▶ while (my $line = <$input_file_handler>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $input_file_handler)) 39 16 55
129 39 chomp $line;
130 39 $line = decode($self->{'input_encoding'}, $line);
131 39 $self->_parse($line, $out);
132 }
133 16 ▶ if ($self->{'callbacks'}->{'final'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"callbacks"}{"final"}) 0 16 16
134 0 &{$self->{'callbacks'}->{'final'}}($self);
135 }
136  
137 16 return;
138 }
139  
140 # Parse text string.
141 sub _parse {
142 39 ▶ my ($self, $line, $out) = @_;
Subroutine _parse: called CC=9 SCAR=22.0
143  
144 39 $self->{'_line'} = $line;
145 39 my ($type, $value) = $line =~ m/\A([A()\?\-_])(.*)\Z/;
146 39 ▶ if (! $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 3 36 39
147 3 $type = 'X';
148 }
149  
150 # Attribute.
151 39 ▶ if ($type eq 'A') {
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'A') { } 6 33 39
elsif ($type eq '(') { } 9 24 33
elsif ($type eq ')') { } 5 19 24
elsif ($type eq '-') { } 6 13 19
elsif ($type eq '?') { } 5 8 13
elsif ($type eq '_') { } 5 3 8
152 6 my ($att, $attval) = $line =~ m/\AA([^\s]+)\s*(.*)\Z/;
153 6 $self->_is_sub('attribute', $out, $att, $attval);
154  
155 # Start of element.
156 } elsif ($type eq '(') {
157 9 $self->_is_sub('start_element', $out, $value);
158  
159 # End of element.
160 } elsif ($type eq ')') {
161 5 $self->_is_sub('end_element', $out, $value);
162  
163 # Data.
164 } elsif ($type eq '-') {
165 6 $self->_is_sub('data', $out, $value);
166  
167 # Instruction.
168 } elsif ($type eq '?') {
169 5 my ($target, $data) = $line =~ m/\A\?([^\s]+)\s*(.*)\Z/;
170 5 $self->_is_sub('instruction', $out, $target, $data);
171  
172 # Comment.
173 } elsif ($type eq '_') {
174 5 $self->_is_sub('comment', $out, $value);
175  
176 # Others.
177 } else {
178 3 ▶ if ($self->{'callbacks'}->{'other'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'callbacks'}{'other'}) { } 3 0 3
179 3 &{$self->{'callbacks'}->{'other'}}($self, $line);
180 } else {
181 0 err "Bad PYX line '$line'.";
182 }
183 }
184  
185 39 return;
186 }
187  
188 # Helper to defined callbacks.
189 sub _is_sub {
190 36 ▶ my ($self, $key, $out, @values) = @_;
Subroutine _is_sub: called CC=6 SCAR=19.7
191  
192 # Callback with name '$key'.
193 36 ▶ if (exists $self->{'callbacks'}->{$key}
Branch: 3 branches
BranchTrueFalseTotal
if (exists $self->{'callbacks'}{$key} and ref $self->{'callbacks'}{$key} eq 'CODE') { } 18 18 36
elsif (exists $self->{'callbacks'}{'rewrite'} and ref $self->{'callbacks'}{'rewrite'} eq 'CODE') { } 0 18 18
elsif ($self->{'output_rewrite'}) { } 18 0 18
Condition66%2/3
exists $self->{"callbacks"}{$key} and ref $self->{"callbacks"}{$key} eq "CODE"
!ll&&!rl&&r
01818
MC/DC50%1/2
exists $self->{"callbacks"}{$key} and ref $self->{"callbacks"}{$key} eq "CODE"
exists $self->{"callbacks"}{$key} ref $self->{"callbacks"}{$key} eq "CODE"
Truth table66%2/3
A and B
A= exists $self->{"callbacks"}{$key}B= ref $self->{"callbacks"}{$key} eq "CODE"
ABresult
0X0
100
111
194 && ref $self->{'callbacks'}->{$key} eq 'CODE') {
195  
196 18 &{$self->{'callbacks'}->{$key}}($self, @values);
197  
198 # Rewrite callback.
199 ▶ } elsif (exists $self->{'callbacks'}->{'rewrite'}
Condition33%1/3
exists $self->{"callbacks"}{"rewrite"} and ref $self->{"callbacks"}{"rewrite"} eq "CODE"
!ll&&!rl&&r
0180
MC/DC0%0/2
exists $self->{"callbacks"}{"rewrite"} and ref $self->{"callbacks"}{"rewrite"} eq "CODE"
exists $self->{"callbacks"}{"rewrite"} ref $self->{"callbacks"}{"rewrite"} eq "CODE"
Truth table33%1/3
A and B
A= exists $self->{"callbacks"}{"rewrite"}B= ref $self->{"callbacks"}{"rewrite"} eq "CODE"
ABresult
0X0
100
111
200 && ref $self->{'callbacks'}->{'rewrite'} eq 'CODE') {
201  
202 0 &{$self->{'callbacks'}->{'rewrite'}}($self, $self->{'_line'});
203  
204 # Raw output to output file handler.
205 } elsif ($self->{'output_rewrite'}) {
206 my $encoded_line = Encode::encode(
207 $self->{'output_encoding'},
208 18 $self->{'_line'},
209 );
210 18 print {$out} $encoded_line, "\n";
211 }
212  
213 36 return;
214 }
215  
216 7 1;
217  
218 __END__
↑ Summary PYX.pm »