| 1 | package PYX::Parser; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Class::Utils qw(set_params); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Encode qw(decode); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Error::Pure qw(err); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||
| 11 | # Constants. | ||||||||||||||||||||||||||||||||||||
| 12 | 8 | Readonly::Scalar my $EMPTY_STR => q{}; | |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | 8 | our $VERSION = 0.09; | |||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||
| 16 | # Constructor. | ||||||||||||||||||||||||||||||||||||
| 17 | sub new { | ||||||||||||||||||||||||||||||||||||
| 18 | 8 | ▶ | my ($class, @params) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 19 | 8 | 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 | 8 | $self->{'input_encoding'} = 'utf-8'; | |||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||
| 38 | # Non parser options. | ||||||||||||||||||||||||||||||||||||
| 39 | 8 | $self->{'non_parser_options'} = {}; | |||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||
| 41 | # Output encoding. | ||||||||||||||||||||||||||||||||||||
| 42 | 8 | $self->{'output_encoding'} = 'utf-8'; | |||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||
| 44 | # Output handler. | ||||||||||||||||||||||||||||||||||||
| 45 | 8 | $self->{'output_handler'} = \*STDOUT; | |||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||
| 47 | # Output rewrite. | ||||||||||||||||||||||||||||||||||||
| 48 | 8 | $self->{'output_rewrite'} = 0; | |||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||
| 50 | # Process params. | ||||||||||||||||||||||||||||||||||||
| 51 | 8 | set_params($self, @params); | |||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||
| 53 | # Check output handler. | ||||||||||||||||||||||||||||||||||||
| 54 | 6 | ▶ | if (defined $self->{'output_handler'} | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{"output_handler"} and ref $self->{"output_handler"} ne "GLOB"
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"
| |||||||||||||||||||||||||||||||||||||
| 55 | && ref $self->{'output_handler'} ne 'GLOB') { | ||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||
| 57 | 0 | err 'Bad output handler.'; | |||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||
| 60 | # Processing line. | ||||||||||||||||||||||||||||||||||||
| 61 | 6 | $self->{'_line'} = $EMPTY_STR; | |||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||
| 63 | # Object. | ||||||||||||||||||||||||||||||||||||
| 64 | 6 | 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 | 1 | ▶ | my ($self, $pyx, $out) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=6 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||
| 78 | 1 | ▶ | if (! defined $out) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 79 | 1 | $out = $self->{'output_handler'}; | |||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||
| 82 | # Input data. | ||||||||||||||||||||||||||||||||||||
| 83 | 1 | my @text; | |||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | if (ref $pyx eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 85 | 0 | @text = @{$pyx}; | |||||||||||||||||||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||||||||||||||||||
| 87 | 1 | @text = split /\n/ms, $pyx; | |||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||
| 90 | # Parse. | ||||||||||||||||||||||||||||||||||||
| 91 | 1 | ▶ | if ($self->{'callbacks'}->{'init'}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 92 | 0 | &{$self->{'callbacks'}->{'init'}}($self); | |||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||
| 94 | 1 | foreach my $line (@text) { | |||||||||||||||||||||||||||||||||||
| 95 | 7 | $self->_parse($line, $out); | |||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | if ($self->{'callbacks'}->{'final'}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 98 | 0 | &{$self->{'callbacks'}->{'final'}}($self); | |||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||
| 101 | 1 | 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
Condition33%1/3
not $input_file_handler or ref $input_file_handler ne "GLOB"
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"
| |||||||||||||||||||||||||||||||||||||
| 120 | 0 | err 'No input handler.'; | |||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||
| 122 | 16 | ▶ | if (! defined $out) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 123 | 16 | $out = $self->{'output_handler'}; | |||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||
| 125 | 16 | ▶ | if ($self->{'callbacks'}->{'init'}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 126 | 0 | &{$self->{'callbacks'}->{'init'}}($self); | |||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||
| 128 | 16 | ▶ | while (my $line = <$input_file_handler>) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 134 | 0 | &{$self->{'callbacks'}->{'final'}}($self); | |||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||
| 137 | 16 | return; | |||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||
| 140 | # Parse text string. | ||||||||||||||||||||||||||||||||||||
| 141 | sub _parse { | ||||||||||||||||||||||||||||||||||||
| 142 | 46 | ▶ | my ($self, $line, $out) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||
| 144 | 46 | $self->{'_line'} = $line; | |||||||||||||||||||||||||||||||||||
| 145 | 46 | my ($type, $value) = $line =~ m/\A([A()\?\-_])(.*)\Z/; | |||||||||||||||||||||||||||||||||||
| 146 | 46 | ▶ | if (! $type) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 147 | 4 | $type = 'X'; | |||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||
| 150 | # Attribute. | ||||||||||||||||||||||||||||||||||||
| 151 | 46 | ▶ | if ($type eq 'A') { | ||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||
| 152 | 7 | my ($att, $attval) = $line =~ m/\AA([^\s]+)\s*(.*)\Z/; | |||||||||||||||||||||||||||||||||||
| 153 | 7 | $self->_is_sub('attribute', $out, $att, $attval); | |||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||
| 155 | # Start of element. | ||||||||||||||||||||||||||||||||||||
| 156 | } elsif ($type eq '(') { | ||||||||||||||||||||||||||||||||||||
| 157 | 10 | $self->_is_sub('start_element', $out, $value); | |||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||
| 159 | # End of element. | ||||||||||||||||||||||||||||||||||||
| 160 | } elsif ($type eq ')') { | ||||||||||||||||||||||||||||||||||||
| 161 | 6 | $self->_is_sub('end_element', $out, $value); | |||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||
| 163 | # Data. | ||||||||||||||||||||||||||||||||||||
| 164 | } elsif ($type eq '-') { | ||||||||||||||||||||||||||||||||||||
| 165 | 7 | $self->_is_sub('data', $out, $value); | |||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||
| 167 | # Instruction. | ||||||||||||||||||||||||||||||||||||
| 168 | } elsif ($type eq '?') { | ||||||||||||||||||||||||||||||||||||
| 169 | 6 | my ($target, $data) = $line =~ m/\A\?([^\s]+)\s*(.*)\Z/; | |||||||||||||||||||||||||||||||||||
| 170 | 6 | $self->_is_sub('instruction', $out, $target, $data); | |||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||
| 172 | # Comment. | ||||||||||||||||||||||||||||||||||||
| 173 | } elsif ($type eq '_') { | ||||||||||||||||||||||||||||||||||||
| 174 | 6 | $self->_is_sub('comment', $out, $value); | |||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||
| 176 | # Others. | ||||||||||||||||||||||||||||||||||||
| 177 | } else { | ||||||||||||||||||||||||||||||||||||
| 178 | 4 | ▶ | if ($self->{'callbacks'}->{'other'}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 179 | 4 | &{$self->{'callbacks'}->{'other'}}($self, $line); | |||||||||||||||||||||||||||||||||||
| 180 | } else { | ||||||||||||||||||||||||||||||||||||
| 181 | 0 | err "Bad PYX line '$line'."; | |||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||
| 185 | 46 | return; | |||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||
| 188 | # Helper to defined callbacks. | ||||||||||||||||||||||||||||||||||||
| 189 | sub _is_sub { | ||||||||||||||||||||||||||||||||||||
| 190 | 42 | ▶ | my ($self, $key, $out, @values) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_is_sub: called CC=6 SCAR=19.7
| |||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||
| 192 | # Callback with name '$key'. | ||||||||||||||||||||||||||||||||||||
| 193 | 42 | ▶ | if (exists $self->{'callbacks'}->{$key} | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
exists $self->{"callbacks"}{$key} and ref $self->{"callbacks"}{$key} eq "CODE"
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"
| |||||||||||||||||||||||||||||||||||||
| 194 | && ref $self->{'callbacks'}->{$key} eq 'CODE') { | ||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||
| 196 | 24 | &{$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"
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"
| |||||||||||||||||||||||||||||||||||||
| 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 | 42 | return; | |||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||
| 216 | 8 | 1; | |||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||
| 218 | __END__ | ||||||||||||||||||||||||||||||||||||