| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 167 | my $parser; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 167 | ▶ | eval { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 167 | ▶ | $parser = $reader | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 6 | ▶ | Carp::croak(__PACKAGE__ . ": max_depth must be a non-negative integer, got '$max_depth'") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$max_depth =~ /\A[0-9]+\z/ and $max_depth <= $LARGEST_MAX_DEPTH
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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'
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'
| |||||||||||||||||||||||||||||||||||||||||||
| 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) // ""
MC/DC100%1/1
builtin::reftype($source) // ""
builtin::reftype($source)
Truth table100%2/2
A
A= builtin::reftype($source)
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 40 | ▶ | return $source if $reftype eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 13 | ▶ | return _reader_for_handle($source) if Scalar::Util::openhandle($source); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | Carp::croak(__PACKAGE__ . ": filehandle is not open") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$reftype eq "GLOB" or $reftype eq "IO" or ref \$source eq "GLOB"
$reftype eq "GLOB" or $reftype eq "IO"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | if $reftype eq 'GLOB' || $reftype eq 'IO' || ref \$source eq 'GLOB'; | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | ▶ | return _reader_for_object($source) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
builtin::blessed($source) and $source->can("read") || $source->can("sysread")
$source->can("read") or $source->can("sysread")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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')
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')
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 2 | return $chunk; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 24 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||||||||||||||||||||||||