Awkwords/Subpattern.pm

filter: Statementstmt 100% 53 / 53 Branchbran 92.8% 13 / 14 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 12 / 12 Podpod 100% 6 / 6 totaltotal 96.7% 90 / 93 CC 10 SCAR 9.3
CC
10
Cov
97%
update_pattern
4.0
render
4.0
walk
3.0
CRAP 2.5
« Awkwords/Parser.pm ↑ Summary Awkwords/ListOf.pm »
1 # -*- Perl -*-
2 #
3 # subpatterns are named A-Z and offer short-hand notation for e.g. V =>
4 # a/e/i/o/u (or instead a reference to some other parse tree)
5  
6 package Lingua::Awkwords::Subpattern;
7  
8 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Carp qw(confess croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 our $VERSION = '0.10';
15  
16 # these defaults set from what the online version does at
17 # http://akana.conlang.org/tools/awkwords/
18 4 my %patterns = (
19 C => [qw/p t k s m n/],
20 N => [qw/m n/],
21 V => [qw/a i u/],
22 );
23  
24 has pattern => (
25 is => 'rw',
26 17 ▶ trigger => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
27 17 my ($self, $pat) = @_;
28 17 ▶ die "subpattern $pat does not exist" unless exists $patterns{$pat};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $patterns{$pat} 1 16 17
29 16 $self->_set_target($patterns{$pat});
30 },
31 4 );
32 4 has target => (is => 'rwp',);
33  
34 ########################################################################
35 #
36 # METHODS
37  
38 1 ▶sub get_patterns {
Subroutine get_patterns: called CC=1 SCAR=0.0
Pod documented
39 1 return %patterns;
40 }
41  
42 14 ▶sub is_pattern {
Subroutine is_pattern: called CC=1 SCAR=0.0
Pod documented
43 14 my (undef, $pat) = @_;
44 14 return exists $patterns{$pat};
45 }
46  
47 47 ▶sub render {
Subroutine render: called CC=4 SCAR=13.9
Pod documented
48 47 my ($self) = @_;
49  
50 47 my $ret;
51 47 my $target = $self->target;
52 47 my $type = ref $target;
53  
54 # this complication allows for subpatterns to point at other parse
55 # trees instead of just simple terminal strings (yes, you could
56 # create loops where a ->render points to itself (don't do that))
57 #
58 # NOTE walk sub must be kept in sync with this logic
59 47 ▶ if (!$type) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $type) { } 5 42 47
60 5 $ret = $target;
61 } else {
62 42 ▶ if ($type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 39 3 42
63 # do not need Math::Random::Discrete here as the weights are
64 # always equal; for weighted instead write that unit out
65 # manually via [a*2/e/i/o/u] or such
66 39 ▶ $ret = @{$target}[ rand @$target ] // '';
Condition50%1/2
@{$target;}[rand @$target] // ""
l!l
390
MC/DC0%0/1
@{$target;}[rand @$target] // ""
@{$target;}[rand @$target]
Truth table50%1/2
A
A= @{$target;}[rand @$target]
Aresult
11
00
67 } else {
68 3 $ret = $target->render;
69 }
70 }
71 47 return $ret;
72 }
73  
74 2 ▶sub set_patterns {
Subroutine set_patterns: called CC=1 SCAR=0.0
Pod documented
75 2 my $class_or_self = shift;
76 # TODO error checking here may be beneficial if callers are in the
77 # habit of passing in data that blows up on ->render or ->walk
78 2 %patterns = (%patterns, @_);
79 2 return $class_or_self;
80 }
81  
82 8 ▶sub update_pattern {
Subroutine update_pattern: called CC=4 SCAR=13.9
Pod documented
83 8 my $class_or_self = shift;
84 8 my $pattern = shift;
85  
86 # TODO more error checking here may be beneficial if callers are in
87 # the habit of passing in data that blows up on ->render
88 8 ▶ croak "update needs a pattern and a list of values\n" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 7 8
89 7 ▶ croak "value must be defined" if !defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 6 7
90  
91 # NOTE arrayref as single argument is saved without making a copy of
92 # the contents; this will allow the caller to potentially change
93 # that ref and thus influence what is stored in patterns after this
94 # update_pattern call
95 6 ▶ $patterns{$pattern} = @_ == 1 ? $_[0] : [@_];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 6 0 6
96  
97 6 return $class_or_self;
98 }
99  
100 8 ▶sub walk {
Subroutine walk: called CC=3 SCAR=11.0
Pod documented
101 8 my ($self, $callback) = @_;
102  
103 8 $callback->($self);
104  
105 8 my $target = $self->target;
106 8 my $type = ref $target;
107  
108 # NOTE this logic must be kept in sync with render sub
109 8 ▶ if ($type and $type ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type and $type ne "ARRAY") 2 6 8
Condition100%3/3
$type and $type ne "ARRAY"
!ll&&!rl&&r
422
MC/DC100%2/2
$type and $type ne "ARRAY"
$type $type ne "ARRAY"
Truth table100%3/3
A and B
A= $typeB= $type ne "ARRAY"
ABresult
0X0
100
111
110 2 $target->walk($callback);
111 }
112 8 return;
113 }
114  
115 4 1;
116 __END__
« Awkwords/Parser.pm ↑ Summary Awkwords/ListOf.pm »