| 1 | package Text::APL::Parser; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use base 'Text::APL::Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | sub _BUILD { | ||||||||||||||||||||||||||||
| 9 | 37 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_BUILD: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 37 | ▶ | $self->{start_token} ||= '<%'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"start_token"} ||= "<%"
MC/DC0%0/1
$self->{"start_token"} ||= "<%"
$self->{"start_token"}
Truth table50%1/2
A
A= $self->{"start_token"}
| |||||||||||||||||||||||||||||
| 12 | 37 | ▶ | $self->{end_token} ||= '%>'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"end_token"} ||= "%>"
MC/DC0%0/1
$self->{"end_token"} ||= "%>"
$self->{"end_token"}
Truth table50%1/2
A
A= $self->{"end_token"}
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 37 | ▶ | $self->{line_token} ||= '%'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"line_token"} ||= "%"
MC/DC0%0/1
$self->{"line_token"} ||= "%"
$self->{"line_token"}
Truth table50%1/2
A
A= $self->{"line_token"}
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 37 | $self->{leftover_token} = $self->_build_leftover_pattern; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 37 | return $self; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub parse { | ||||||||||||||||||||||||||||
| 22 | 63 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 63 | my ($input) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 63 | my $TOKEN_START = qr/$self->{start_token}/; | |||||||||||||||||||||||||||
| 26 | 63 | my $TOKEN_END = qr/$self->{end_token}/; | |||||||||||||||||||||||||||
| 27 | 63 | my $TOKEN = qr/$TOKEN_START(==?)? [ ] (.*?) \s* $TOKEN_END/xms; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 63 | my $LINE_TOKEN_START = qr/^ \s* $self->{line_token} /xms; | |||||||||||||||||||||||||||
| 30 | 63 | my $LINE_TOKEN = qr/$LINE_TOKEN_START(==?)? \s* ([^\n]*)/xms; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 63 | my $LEFTOVER_TOKEN = $self->{leftover_token}; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 63 | ▶ | if (!defined $input) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 24 | ▶ | return [] unless defined $self->{buffer}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 12 | my $buffer = delete $self->{buffer}; | |||||||||||||||||||||||||||
| 38 | 12 | ▶ | return [$buffer =~ m/$LINE_TOKEN/xms | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | ? $self->_build_line_token($1, $2) | ||||||||||||||||||||||||||||
| 40 | : $self->_build_text($buffer) | ||||||||||||||||||||||||||||
| 41 | ]; | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 39 | ▶ | if (defined $self->{buffer}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 2 | $input = delete($self->{buffer}) . $input; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 39 | my $tape = []; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 39 | pos $input = 0; | |||||||||||||||||||||||||||
| 51 | 39 | ▶ | while (pos $input < length $input) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 62 | ▶ | if ($input =~ m/\G $TOKEN/gcxms) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 53 | 16 | push @$tape, $self->_build_token($1, $2); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | elsif ($input =~ m/\G $LINE_TOKEN \n/gcxms) { | ||||||||||||||||||||||||||||
| 56 | 14 | push @$tape, $self->_build_line_token($1, $2); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | elsif ($input =~ m/\G (.+?) (?=$TOKEN_START | $LINE_TOKEN_START)/gcxms) { | ||||||||||||||||||||||||||||
| 59 | 11 | push @$tape, $self->_build_text($1); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||||||||||
| 62 | 21 | ▶ | if ($input =~ m/( (?:$TOKEN_START | $LINE_TOKEN_START) .* )/gcxms) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 63 | 13 | $self->{buffer} = $1; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | elsif ($input =~ m/( $LEFTOVER_TOKEN ) $/gcxms) { | ||||||||||||||||||||||||||||
| 66 | 1 | $self->{buffer} = $1; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 21 | my $value = substr($input, pos($input)); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 21 | ▶ | if (defined $value && $value ne '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $value and $value ne ""
MC/DC50%1/2
defined $value and $value ne ""
defined $value
$value ne ""
Truth table66%2/3
A and B
A= defined $valueB= $value ne ""
| |||||||||||||||||||||||||||||
| 72 | 7 | push @$tape, $self->_build_text($value); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 21 | last; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 39 | $tape; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub _build_token { | ||||||||||||||||||||||||||||
| 83 | 16 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_token: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 84 | 16 | my ($modifier, $value) = @_; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 16 | ▶ | my $token = {type => defined $modifier ? 'expr' : 'exec', value => $value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 16 | ▶ | $token->{as_is} = 1 if defined $modifier && length $modifier == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $modifier and length $modifier == 2
MC/DC100%2/2
defined $modifier and length $modifier == 2
defined $modifier
length $modifier == 2
Truth table100%3/3
A and B
A= defined $modifierB= length $modifier == 2
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 16 | return $token; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub _build_line_token { | ||||||||||||||||||||||||||||
| 93 | 23 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_line_token: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 94 | 23 | my ($modifier, $value) = @_; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 23 | ▶ | my $token = {type => defined $modifier ? 'expr' : 'exec', value => $value, line => 1}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 23 | ▶ | $token->{as_is} = 1 if defined $modifier && length $modifier == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $modifier and length $modifier == 2
MC/DC100%2/2
defined $modifier and length $modifier == 2
defined $modifier
length $modifier == 2
Truth table100%3/3
A and B
A= defined $modifierB= length $modifier == 2
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 23 | return $token; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub _build_text { | ||||||||||||||||||||||||||||
| 103 | 21 | ▶ | my $self =shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_text: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 104 | 21 | my ($value) = @_; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 21 | return {type => 'text', value => $value}; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub _build_leftover_pattern { | ||||||||||||||||||||||||||||
| 110 | 37 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_leftover_pattern: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 37 | my @token = split //, $self->{start_token}; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 37 | my $pattern = ''; | |||||||||||||||||||||||||||
| 115 | 37 | $pattern .= '(?:' . $_ for @token; | |||||||||||||||||||||||||||
| 116 | 37 | $pattern .= ')?' for @token; | |||||||||||||||||||||||||||
| 117 | 37 | $pattern =~ s{\?$}{}; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 37 | return qr/$pattern/; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 3 | 1; | |||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||