| 1 | package Pegex::Input; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use Pegex::Base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 18 | has string => (); | |||||||||||||||||||||||||||
| 6 | 18 | has stringref => (); | |||||||||||||||||||||||||||
| 7 | 18 | has file => (); | |||||||||||||||||||||||||||
| 8 | 18 | has handle => (); | |||||||||||||||||||||||||||
| 9 | 18 | has _buffer => (); | |||||||||||||||||||||||||||
| 10 | 18 | has _is_eof => 0; | |||||||||||||||||||||||||||
| 11 | 18 | has _is_open => 0; | |||||||||||||||||||||||||||
| 12 | 18 | has _is_close => 0; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # NOTE: Current implementation reads entire input into _buffer on open(). | ||||||||||||||||||||||||||||
| 15 | 287 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 287 | my ($self) = @_; | |||||||||||||||||||||||||||
| 17 | 287 | ▶ | die "Attempted Pegex::Input::read before open" if not $self->{_is_open}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 287 | ▶ | die "Attempted Pegex::Input::read after EOF" if $self->{_is_eof}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 287 | my $buffer = $self->{_buffer}; | |||||||||||||||||||||||||||
| 21 | 287 | $self->{_buffer} = undef; | |||||||||||||||||||||||||||
| 22 | 287 | $self->{_is_eof} = 1; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 287 | return $buffer; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 287 | ▶ | sub open { | ||||||||||||||||||||||||||
|
Subroutine
open: called CC=8 SCAR=24.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 287 | my ($self) = @_; | |||||||||||||||||||||||||||
| 29 | die "Attempted to reopen Pegex::Input object" | ||||||||||||||||||||||||||||
| 30 | 287 | ▶ | if $self->{_is_open} or $self->{_is_close}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"_is_open"} or $self->{"_is_close"}
MC/DC0%0/2
$self->{"_is_open"} or $self->{"_is_close"}
$self->{"_is_open"}
$self->{"_is_close"}
Truth table33%1/3
A or B
A= $self->{"_is_open"}B= $self->{"_is_close"}
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 287 | ▶ | if (my $ref = $self->{stringref}) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 33 | 0 | $self->{_buffer} = $ref; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | elsif (my $handle = $self->{handle}) { | ||||||||||||||||||||||||||||
| 36 | 0 | $self->{_buffer} = \ do { local $/; <$handle> }; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | elsif (my $path = $self->{file}) { | ||||||||||||||||||||||||||||
| 39 | 1 | ▶ | open my $handle, "<", $path | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | or die "Pegex::Input can't open $path for input:\n$!"; | ||||||||||||||||||||||||||||
| 41 | 1 | $self->{_buffer} = \ do { local $/; <$handle> }; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | elsif (exists $self->{string}) { | ||||||||||||||||||||||||||||
| 44 | 286 | $self->{_buffer} = \$self->{string}; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | else { | ||||||||||||||||||||||||||||
| 47 | 0 | die "Pegex::Input::open failed. No source to open"; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 287 | $self->{_is_open} = 1; | |||||||||||||||||||||||||||
| 50 | 287 | return $self; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 259 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 259 | my ($self) = @_; | |||||||||||||||||||||||||||
| 55 | die "Attempted to close an unopen Pegex::Input object" | ||||||||||||||||||||||||||||
| 56 | 259 | ▶ | if $self->{_is_close}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 259 | ▶ | close $self->{handle} if $self->{handle}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 259 | $self->{_is_open} = 0; | |||||||||||||||||||||||||||
| 59 | 259 | $self->{_is_close} = 1; | |||||||||||||||||||||||||||
| 60 | 259 | $self->{_buffer} = undef; | |||||||||||||||||||||||||||
| 61 | 259 | return $self; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 18 | 1; | |||||||||||||||||||||||||||