| 1 | package Parse::EBNF::Token; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 61 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 4 | 61 | my ($class) = @_; | |||||||||||||||||||||||||||
| 5 | 61 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 6 | 61 | $self->{error} = 0; | |||||||||||||||||||||||||||
| 7 | 61 | return $self; | |||||||||||||||||||||||||||
| 8 | } | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 51 | ▶ | sub reduce_alternations { | ||||||||||||||||||||||||||
|
Subroutine
reduce_alternations: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | 51 | my ($self) = @_; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 51 | ▶ | return 1 unless $self->{type} eq 'list'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # | ||||||||||||||||||||||||||||
| 17 | # reduce our own children first | ||||||||||||||||||||||||||||
| 18 | # | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 21 | for my $token(@{$self->{tokens}}){ | |||||||||||||||||||||||||||
| 21 | 31 | $token->reduce_alternations(); | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # | ||||||||||||||||||||||||||||
| 26 | # now check if we have any alts | ||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 21 | my $alts = 0; | |||||||||||||||||||||||||||
| 30 | 21 | for my $token(@{$self->{tokens}}){ | |||||||||||||||||||||||||||
| 31 | 31 | ▶ | $alts++ if $token->{type} eq 'alt'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 21 | ▶ | return 1 unless $alts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # | ||||||||||||||||||||||||||||
| 38 | # we have alts - change our base type and create new alt children | ||||||||||||||||||||||||||||
| 39 | # | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | my $our_tokens = $self->{tokens}; | |||||||||||||||||||||||||||
| 42 | 2 | $self->{tokens} = []; | |||||||||||||||||||||||||||
| 43 | 2 | $self->{type} = 'alternation'; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | my $current = Parse::EBNF::Token->new(); | |||||||||||||||||||||||||||
| 46 | 2 | $current->{type} = 'list'; | |||||||||||||||||||||||||||
| 47 | 2 | $current->{tokens} = []; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | for my $token(@{$our_tokens}){ | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 6 | ▶ | if ($token->{type} eq 'alt'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | push @{$self->{tokens}}, $current; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 2 | $current = Parse::EBNF::Token->new(); | |||||||||||||||||||||||||||
| 56 | 2 | $current->{type} = 'list'; | |||||||||||||||||||||||||||
| 57 | 2 | $current->{tokens} = []; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | }else{ | ||||||||||||||||||||||||||||
| 60 | 4 | push @{$current->{tokens}}, $token; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | push @{$self->{tokens}}, $current; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | return 1; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 53 | ▶ | sub reduce_repetition { | ||||||||||||||||||||||||||
|
Subroutine
reduce_repetition: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 53 | my ($self) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 53 | ▶ | return 1 unless (($self->{type} eq 'list') || ($self->{type} eq 'alternation')); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"type"} eq "list" or $self->{"type"} eq "alternation"
MC/DC100%2/2
$self->{"type"} eq "list" or $self->{"type"} eq "alternation"
$self->{"type"} eq "list"
$self->{"type"} eq "alternation"
Truth table100%3/3
A or B
A= $self->{"type"} eq "list"B= $self->{"type"} eq "alternation"
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # | ||||||||||||||||||||||||||||
| 75 | # reduce our own children first | ||||||||||||||||||||||||||||
| 76 | # | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 25 | for my $token(@{$self->{tokens}}){ | |||||||||||||||||||||||||||
| 79 | 33 | $token->reduce_repetition(); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # | ||||||||||||||||||||||||||||
| 84 | # do it | ||||||||||||||||||||||||||||
| 85 | # | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 25 | my $old_tokens = $self->{tokens}; | |||||||||||||||||||||||||||
| 88 | 25 | $self->{tokens} = []; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 25 | for my $token(@{$old_tokens}){ | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 33 | ▶ | if ($token->{type} =~ m!^rep (.*)$!){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 3 | my $new = Parse::EBNF::Token->new(); | |||||||||||||||||||||||||||
| 95 | 3 | $new->{type} = 'repeat '.$1; | |||||||||||||||||||||||||||
| 96 | 3 | $new->{tokens} = []; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 3 | my $subject = pop @{$self->{tokens}}; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 3 | ▶ | unless (defined $subject){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | $self->{error} = "repetition operator without suject"; | |||||||||||||||||||||||||||
| 102 | 0 | return 0; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | push @{$new->{tokens}}, $subject; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 3 | push @{$self->{tokens}}, $new; | |||||||||||||||||||||||||||
| 108 | }else{ | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 30 | push @{$self->{tokens}}, $token; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 25 | return 1; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 53 | ▶ | sub reduce_empty { | ||||||||||||||||||||||||||
|
Subroutine
reduce_empty: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | 53 | my ($self) = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # | ||||||||||||||||||||||||||||
| 122 | # reduce our own children first | ||||||||||||||||||||||||||||
| 123 | # | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 53 | ▶ | if (defined($self->{tokens})){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 28 | for my $token(@{$self->{tokens}}){ | |||||||||||||||||||||||||||
| 127 | 33 | $token->reduce_empty(); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # | ||||||||||||||||||||||||||||
| 133 | # reduce self? | ||||||||||||||||||||||||||||
| 134 | # | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 53 | ▶ | if ($self->{type} eq 'list'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 23 | ▶ | if (scalar(@{$self->{tokens}}) == 1){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 21 | my $child = $self->{tokens}->[0]; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 21 | for my $key(keys %{$self}){ delete $self->{$key}; } | |||||||||||||||||||||||||||
| 141 | 21 | for my $key(keys %{$child}){ $self->{$key} = $child->{$key}; } | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 53 | return 1; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 32 | ▶ | sub reduce_rx { | ||||||||||||||||||||||||||
|
Subroutine
reduce_rx: called CC=13 SCAR=26.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 149 | 32 | my ($self) = @_; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # | ||||||||||||||||||||||||||||
| 153 | # reduce our own children first | ||||||||||||||||||||||||||||
| 154 | # | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 32 | ▶ | if (defined($self->{tokens})){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 7 | for my $token(@{$self->{tokens}}){ | |||||||||||||||||||||||||||
| 158 | 12 | $token->reduce_rx(); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 32 | ▶ | return 1 unless (($self->{type} eq 'alternation') || ($self->{type} eq 'list')); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"type"} eq "alternation" or $self->{"type"} eq "list"
MC/DC100%2/2
$self->{"type"} eq "alternation" or $self->{"type"} eq "list"
$self->{"type"} eq "alternation"
$self->{"type"} eq "list"
Truth table100%3/3
A or B
A= $self->{"type"} eq "alternation"B= $self->{"type"} eq "list"
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # | ||||||||||||||||||||||||||||
| 166 | # see if we're in a position to reduce self... | ||||||||||||||||||||||||||||
| 167 | # | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 4 | for my $token(@{$self->{tokens}}){ | |||||||||||||||||||||||||||
| 170 | 5 | ▶ | next if $token->{type} eq 'literal'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 3 | ▶ | next if $token->{type} eq 'rx'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 3 | return 1; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # | ||||||||||||||||||||||||||||
| 177 | # we can reduce all of our children into a single rx | ||||||||||||||||||||||||||||
| 178 | # | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 1 | my @rx; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1 | for my $token(@{$self->{tokens}}){ | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 2 | ▶ | if ($token->{type} eq 'literal'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 2 | push @rx, '('.quotemeta($token->{content}).')'; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 2 | ▶ | if ($token->{type} eq 'rx'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 0 | push @rx, $token->{content}; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 1 | my $rx = ''; | |||||||||||||||||||||||||||
| 194 | 1 | ▶ | $rx = join('', @rx) if $self->{type} eq 'list'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 1 | ▶ | $rx = join('|', @rx) if $self->{type} eq 'alternation'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 1 | $self->{type} = 'rx'; | |||||||||||||||||||||||||||
| 198 | 1 | $self->{content} = $rx; | |||||||||||||||||||||||||||
| 199 | 1 | $self->{tokens} = []; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 1 | return 1; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 2 | 1; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||||||||||||