blib/lib/Text/KDL/XS/Parser.pm

filter: Statementstmt 100% 69 / 69 Branchbran 96.8% 31 / 32 Conditioncond 65.2% 15 / 23 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 100% 15 / 15 Podpod 100% 2 / 2 totaltotal 89.6% 138 / 154 CC 25 SCAR 15.7
CC
25
Cov
93%
_reader_for
10.5
new
5.0
_max_depth
4.0
CRAP 4.8
« blib/lib/Text/KDL/XS/Document.pm ↑ Summary blib/lib/Text/KDL/XS/Node.pm »
1 package Text::KDL::XS::Parser;
2  
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 24 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use Text::KDL::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 24 our @CARP_NOT = qw(
11 Text::KDL::XS Text::KDL::XS::Document Text::KDL::XS::Emitter
12 Text::KDL::XS::Node Text::KDL::XS::Value
13 );
14  
15 24 my $DEFAULT_MAX_DEPTH = 512;
16 24 my $LARGEST_MAX_DEPTH = ~0 >> 1;
17 24 my %IS_OPTION = map { $_ => 1 } qw(version emit_comments max_depth);
18  
19 174 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
20 174 my ($class, $source, @options) = @_;
21  
22 174 ▶ Carp::croak(__PACKAGE__ . ": source is required") unless defined $source;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 1 173 174
23 173 my $options = Text::KDL::XS::_parse_named_arguments(__PACKAGE__, 'option', \%IS_OPTION, @options);
24 173 my $flags = _option_flags($options);
25 172 my $max_depth = _max_depth($options->{max_depth});
26 169 ▶ my $reader = _is_text_source($source) ? undef : _reader_for($source);
Branch: 1 branch
BranchTrueFalseTotal
_is_text_source($source) ? : 129 40 169
27  
28 167 my $parser;
29 167 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $parser = $reader ? $class->_new_stream_parser($reader, $flags, $max_depth) : $class->_new_string_parser($source, $flags, $max_depth); 1 } } or Text::KDL::XS::_rethrow($@, __FILE__) 163 4 167
30 167 ▶ $parser = $reader
Branch: 1 branch
BranchTrueFalseTotal
$reader ? : 38 129 167
31 ? $class->_new_stream_parser($reader, $flags, $max_depth)
32 : $class->_new_string_parser($source, $flags, $max_depth);
33 163 1;
34 } or Text::KDL::XS::_rethrow($@, __FILE__);
35 163 return $parser;
36 }
37  
38 1564 ▶sub next_event {
Subroutine next_event: called CC=2 SCAR=6.9
Pod documented
39 1564 my ($self) = @_;
40 1564 my $event;
41 1564 ▶ eval { $event = $self->_next_event; 1 } or Text::KDL::XS::_rethrow($@, __FILE__);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $event = $self->_next_event; 1 } } or Text::KDL::XS::_rethrow($@, __FILE__) 1557 7 1564
42 1557 return $event;
43 }
44  
45 173 ▶sub _option_flags {
Subroutine _option_flags: called CC=4 SCAR=13.9
46 173 my ($options) = @_;
47 173 my $version = Text::KDL::XS::_normalize_version(__PACKAGE__, $options->{version});
48 172 ▶ my $flags
Branch: 2 branches
BranchTrueFalseTotal
$version eq '1' ? : 18 154 172
$version eq '2' ? : 34 120 154
49 = $version eq '1' ? Text::KDL::XS::_OPT_V1()
50 : $version eq '2' ? Text::KDL::XS::_OPT_V2()
51 : Text::KDL::XS::_OPT_DETECT();
52 172 ▶ $flags |= Text::KDL::XS::_OPT_EMIT_COMMENTS() if $options->{emit_comments};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"emit_comments"} 9 163 172
53 172 return $flags;
54 }
55  
56 172 ▶sub _max_depth {
Subroutine _max_depth: called CC=4 SCAR=13.9
57 172 my ($max_depth) = @_;
58 172 ▶ return $DEFAULT_MAX_DEPTH unless defined $max_depth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_depth 166 6 172
59 6 ▶ Carp::croak(__PACKAGE__ . ": max_depth must be a non-negative integer, got '$max_depth'")
Branch: 1 branch
BranchTrueFalseTotal
unless $max_depth =~ /\A[0-9]+\z/ and $max_depth <= $LARGEST_MAX_DEPTH 3 3 6
Condition66%2/3
$max_depth =~ /\A[0-9]+\z/ and $max_depth <= $LARGEST_MAX_DEPTH
!ll&&!rl&&r
303
MC/DC50%1/2
$max_depth =~ /\A[0-9]+\z/ and $max_depth <= $LARGEST_MAX_DEPTH
$max_depth =~ /\A[0-9]+\z/ $max_depth <= $LARGEST_MAX_DEPTH
Truth table66%2/3
A and B
A= $max_depth =~ /\A[0-9]+\z/B= $max_depth <= $LARGEST_MAX_DEPTH
ABresult
0X0
100
111
60 unless $max_depth =~ /\A[0-9]+\z/ && $max_depth <= $LARGEST_MAX_DEPTH;
61 3 return $max_depth;
62 }
63  
64 # A plain string, as opposed to a reference or a bare glob such as *STDIN.
65 169 ▶sub _is_text_source {
Subroutine _is_text_source: called CC=2 SCAR=6.9
66 169 my ($source) = @_;
67 169 ▶ return !ref $source && ref \$source ne 'GLOB';
Condition100%3/3
!ref($source) && ref \$source ne 'GLOB'
!ll&&!rl&&r
391129
MC/DC100%2/2
!ref($source) && ref \$source ne 'GLOB'
!ref($source) ref \$source ne 'GLOB'
Truth table100%3/3
not(A) && B
A= !ref($source)B= ref \$source ne 'GLOB'
ABresult
0X0
100
111
68 }
69  
70 # The code reference that delivers the chunks of a callback, filehandle or IO
71 # object source to the XS layer.
72 40 ▶sub _reader_for {
Subroutine _reader_for: called CC=10 SCAR=23.5
73 40 my ($source) = @_;
74 40 ▶ my $reftype = Scalar::Util::reftype($source) // '';
Condition100%2/2
builtin::reftype($source) // ""
l!l
391
MC/DC100%1/1
builtin::reftype($source) // ""
builtin::reftype($source)
Truth table100%2/2
A
A= builtin::reftype($source)
Aresult
11
00
75  
76 40 ▶ return $source if $reftype eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $reftype eq "CODE" 27 13 40
77 13 ▶ return _reader_for_handle($source) if Scalar::Util::openhandle($source);
Branch: 1 branch
BranchTrueFalseTotal
if &Scalar::Util::openhandle($source) 10 3 13
78 3 ▶ Carp::croak(__PACKAGE__ . ": filehandle is not open")
Branch: 1 branch
BranchTrueFalseTotal
if $reftype eq "GLOB" or $reftype eq "IO" or ref \$source eq "GLOB" 1 2 3
Condition66%4/6
$reftype eq "GLOB" or $reftype eq "IO" or ref \$source eq "GLOB"
l!l&&r!l&&!r
102
$reftype eq "GLOB" or $reftype eq "IO"
l!l&&r!l&&!r
102
MC/DC33%1/3
$reftype eq "GLOB" or $reftype eq "IO" or ref \$source eq "GLOB"
$reftype eq "GLOB" $reftype eq "IO" ref \$source eq "GLOB"
Truth table50%2/4
A or B or C
A= $reftype eq "GLOB"B= $reftype eq "IO"C= ref \$source eq "GLOB"
ABCresult
1XX1
01X1
0011
0000
79 if $reftype eq 'GLOB' || $reftype eq 'IO' || ref \$source eq 'GLOB';
80 2 ▶ return _reader_for_object($source)
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($source) and $source->can("read") || $source->can("sysread") 1 1 2
Condition50%3/6
builtin::blessed($source) and $source->can("read") || $source->can("sysread")
!ll&&!rl&&r
101
$source->can("read") or $source->can("sysread")
l!l&&r!l&&!r
100
MC/DC33%1/3
builtin::blessed($source) and $source->can("read") || $source->can("sysread")
builtin::blessed($source) $source->can("read") $source->can("sysread")
Truth table50%2/4
A and B or C
A= builtin::blessed($source)B= $source->can("read")C= $source->can("sysread")
ABCresult
0XX0
1000
11X1
1011
81 if Scalar::Util::blessed($source) && ($source->can('read') || $source->can('sysread'));
82 1 Carp::croak(__PACKAGE__ . ": unsupported source ref type '" . ref($source) . "'");
83 }
84  
85 # Reads an open filehandle with the PerlIO-aware read, so that layers such as
86 # :encoding(UTF-8), data buffered by earlier reads and in-memory handles work.
87 10 ▶sub _reader_for_handle {
Subroutine _reader_for_handle: called CC=1 SCAR=0.0
88 10 my ($handle) = @_;
89 19 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
90 19 my ($wanted_bytes) = @_;
91 19 my $count = read($handle, my $chunk, $wanted_bytes);
92 19 ▶ Carp::croak(__PACKAGE__ . ": read failed: $!") unless defined $count;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 1 18 19
93 18 return $chunk;
94 10 };
95 }
96  
97 1 ▶sub _reader_for_object {
Subroutine _reader_for_object: called CC=2 SCAR=7.2
98 1 my ($object) = @_;
99 1 ▶ my $read = $object->can('read') || $object->can('sysread');
Condition33%1/3
$object->can('read') || $object->can('sysread')
l!l&&r!l&&!r
100
MC/DC0%0/2
$object->can('read') || $object->can('sysread')
$object->can('read') $object->can('sysread')
Truth table33%1/3
A || B
A= $object->can('read')B= $object->can('sysread')
ABresult
1X1
011
000
100 2 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
101 2 my ($wanted_bytes) = @_;
102 2 my $count = $object->$read(my $chunk, $wanted_bytes);
103 2 ▶ Carp::croak(__PACKAGE__ . ": read failed: $!") unless defined $count;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 0 2 2
104 2 return $chunk;
105 1 };
106 }
107  
108 24 1;
109  
110 __END__
« blib/lib/Text/KDL/XS/Document.pm ↑ Summary blib/lib/Text/KDL/XS/Node.pm »