| 1 | # -*- Perl -*- | ||||||||||
| 2 | # | ||||||||||
| 3 | # Parser::MGC subclass that parses awkword patterns into a data structure | ||||||||||
| 4 | |||||||||||
| 5 | package Lingua::Awkwords::Parser; | ||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 3 | ▶ | use parent qw( Parser::MGC ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 3 | ▶ | use Lingua::Awkwords::ListOf; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 3 | ▶ | use Lingua::Awkwords::OneOf; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 3 | ▶ | use Lingua::Awkwords::String; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 3 | ▶ | use Lingua::Awkwords::Subpattern; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 3 | our $VERSION = '0.10'; | |||||||||
| 18 | |||||||||||
| 19 | 31 | ▶ | sub parse { | ||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 20 | 31 | my $self = shift; | |||||||||
| 21 | 31 | my $unit = $self->_parse_unit; | |||||||||
| 22 | |||||||||||
| 23 | my $filters = | ||||||||||
| 24 | 31 | ▶ | $self->sequence_of(sub { $self->expect('^'); $self->_parse_filter }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 31 | ▶ | $unit->add_filters(@$filters) if @$filters; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | |||||||||||
| 27 | 31 | return $unit; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | # filters exclude strings from previous units | ||||||||||
| 31 | 10 | ▶ | sub _parse_filter { | ||||||||
|
Subroutine
_parse_filter: called CC=2 SCAR=6.9
| |||||||||||
| 32 | 10 | my $self = shift; | |||||||||
| 33 | |||||||||||
| 34 | 10 | my $filter = ''; | |||||||||
| 35 | |||||||||||
| 36 | 1 while $self->any_of( | ||||||||||
| 37 | # NOTE code for these duplicted from the unit parse | ||||||||||
| 38 | 20 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
| |||||||||||
| 39 | 20 | $filter .= ($self->expect(qr/"([^"]*)"/))[-1]; | |||||||||
| 40 | 0 | 1; | |||||||||
| 41 | }, | ||||||||||
| 42 | 20 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 20 | $filter .= $self->generic_token('other', qr{[^ "A-Z\(\)\[\]/\*\^]+}); | |||||||||
| 44 | 10 | 1; | |||||||||
| 45 | }, | ||||||||||
| 46 | 10 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 10 | 0; | |||||||||
| 48 | } | ||||||||||
| 49 | 10 | ▶ | ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | |||||||||||
| 51 | 10 | return $filter; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | # units, which might be a ::ListOf choices [VV] or ::OneOf [a/b] or both | ||||||||||
| 55 | # [VV/CV] or neither [asdf]. units can also contain other units | ||||||||||
| 56 | 46 | ▶ | sub _parse_unit { | ||||||||
|
Subroutine
_parse_unit: called CC=5 SCAR=16.1
| |||||||||||
| 57 | 46 | my $self = shift; | |||||||||
| 58 | |||||||||||
| 59 | 46 | my ($oneof, $weight); | |||||||||
| 60 | 46 | my @terms = ''; | |||||||||
| 61 | |||||||||||
| 62 | 1 while $self->any_of( | ||||||||||
| 63 | 107 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 64 | 107 | $self->expect('*'); | |||||||||
| 65 | |||||||||||
| 66 | # NOTE original version instead treats [a*10*20/b] as a | ||||||||||
| 67 | # weight of 1020 then reduces that to 128 (with a warning) | ||||||||||
| 68 | # as of 0.06 upper limit removed from this implementation | ||||||||||
| 69 | 3 | ▶ | $self->fail("weight already set") if $weight; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | |||||||||||
| 71 | 2 | my $num = $self->token_int; | |||||||||
| 72 | 2 | ▶ | $self->fail("weight must be positive integer") if $num < 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 1 | $weight = $num; | |||||||||
| 74 | 1 | 1; | |||||||||
| 75 | }, | ||||||||||
| 76 | 106 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 77 | 106 | $self->expect('/'); | |||||||||
| 78 | |||||||||||
| 79 | 8 | ▶ | $oneof = Lingua::Awkwords::OneOf->new if !defined $oneof; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | |||||||||||
| 81 | 8 | for my $term (@terms) { | |||||||||
| 82 | # TODO cache these strings so only one obj instance per str? | ||||||||||
| 83 | 8 | ▶ | $term = Lingua::Awkwords::String->new(string => $term) if !ref $term; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | } | ||||||||||
| 85 | 8 | $oneof->add_choice(Lingua::Awkwords::ListOf->new(terms => [@terms]), $weight); | |||||||||
| 86 | |||||||||||
| 87 | # empty string here is so [a/] parses correctly as a choice | ||||||||||
| 88 | # between a and nothing instead of dropping out of the unit | ||||||||||
| 89 | # upon ] | ||||||||||
| 90 | 8 | undef $weight; | |||||||||
| 91 | 8 | @terms = ''; | |||||||||
| 92 | 8 | 1; | |||||||||
| 93 | }, | ||||||||||
| 94 | 98 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 95 | # recurse into sub-units [...] or (...) | ||||||||||
| 96 | 98 | my $delim = $self->expect(qr/[ \[\( ]/x); | |||||||||
| 97 | 15 | $delim =~ tr/[(/])/; | |||||||||
| 98 | |||||||||||
| 99 | 15 | my $ret = $self->scope_of(undef, \&_parse_unit, $delim); | |||||||||
| 100 | 10 | ▶ | if ($terms[-1] eq '') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 8 | $terms[-1] = $ret; | |||||||||
| 102 | } else { | ||||||||||
| 103 | 2 | push @terms, $ret; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | # () needs additional code as (a) is equivalent to [a/] so | ||||||||||
| 107 | # we must add an empty string to what must become a oneof | ||||||||||
| 108 | 10 | ▶ | if ($delim eq ')') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 1 | my $newof; | |||||||||
| 110 | 1 | ▶ | unless ($terms[-1]->can('add_choice')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 1 | $newof = Lingua::Awkwords::OneOf->new; | |||||||||
| 112 | 1 | $newof->add_choice($terms[-1]); | |||||||||
| 113 | 1 | $terms[-1] = $newof; | |||||||||
| 114 | } else { | ||||||||||
| 115 | 0 | $newof = $terms[-1]; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | # TODO cache this string in a hash so only one obj? | ||||||||||
| 119 | 1 | $newof->add_choice(Lingua::Awkwords::String->new(string => '')); | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | # filters in [VV]^aa form (as opposed to the top-level | ||||||||||
| 123 | # parse() VV^aa form which lack the trailing ] or ) of this | ||||||||||
| 124 | # code path | ||||||||||
| 125 | $self->maybe( | ||||||||||
| 126 | 10 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 127 | my $filters = | ||||||||||
| 128 | 10 | ▶ | $self->sequence_of(sub { $self->expect('^'); $self->_parse_filter }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 129 | 10 | ▶ | $terms[-1]->add_filters(@$filters) if @$filters; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | } | ||||||||||
| 131 | 10 | ); | |||||||||
| 132 | 10 | 1; | |||||||||
| 133 | }, | ||||||||||
| 134 | 88 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 135 | 88 | my $pat = $self->generic_token('subpattern', qr{[A-Z]}); | |||||||||
| 136 | 12 | ▶ | $self->fail("not a defined pattern") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | if !Lingua::Awkwords::Subpattern->is_pattern($pat); | ||||||||||
| 138 | |||||||||||
| 139 | 11 | my $ret = Lingua::Awkwords::Subpattern->new(pattern => $pat); | |||||||||
| 140 | 11 | ▶ | if ($terms[-1] eq '') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 5 | $terms[-1] = $ret; | |||||||||
| 142 | } else { | ||||||||||
| 143 | 6 | push @terms, $ret; | |||||||||
| 144 | } | ||||||||||
| 145 | 11 | 1; | |||||||||
| 146 | }, | ||||||||||
| 147 | # NOTE code from these two also used in _parse_filter | ||||||||||
| 148 | 77 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 149 | 77 | my $ret = ($self->expect(qr/"([^"]*)"/))[-1]; | |||||||||
| 150 | 5 | ▶ | if (ref $terms[-1]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | 1 | push @terms, $ret; | |||||||||
| 152 | } else { | ||||||||||
| 153 | 4 | $terms[-1] .= $ret; | |||||||||
| 154 | } | ||||||||||
| 155 | 5 | 1; | |||||||||
| 156 | }, | ||||||||||
| 157 | 72 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 158 | 72 | my $ret = $self->generic_token('other', qr{[^ "A-Z\(\)\[\]/\*\^]+}); | |||||||||
| 159 | 26 | ▶ | if (ref $terms[-1]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | 1 | push @terms, $ret; | |||||||||
| 161 | } else { | ||||||||||
| 162 | 25 | $terms[-1] .= $ret; | |||||||||
| 163 | } | ||||||||||
| 164 | 26 | 1; | |||||||||
| 165 | }, | ||||||||||
| 166 | 46 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 167 | 46 | 0; | |||||||||
| 168 | } | ||||||||||
| 169 | 46 | ▶ | ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | |||||||||||
| 171 | 46 | for my $term (@terms) { | |||||||||
| 172 | # TODO cache these strings so only one obj instance per str? | ||||||||||
| 173 | 56 | ▶ | $term = Lingua::Awkwords::String->new(string => $term) if !ref $term; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | } | ||||||||||
| 175 | |||||||||||
| 176 | 46 | ▶ | if (defined $oneof) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 177 | 5 | $oneof->add_choice(Lingua::Awkwords::ListOf->new(terms => [@terms]), $weight); | |||||||||
| 178 | 5 | return $oneof; | |||||||||
| 179 | } else { | ||||||||||
| 180 | 41 | return Lingua::Awkwords::ListOf->new(terms => \@terms); | |||||||||
| 181 | } | ||||||||||
| 182 | } | ||||||||||
| 183 | |||||||||||
| 184 | 3 | 1; | |||||||||
| 185 | __END__ | ||||||||||