Phonology/Segment/Rules.pm

filter: Statementstmt 90.1% 55 / 61 Branchbran 66.6% 16 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.8% 18 / 22 Podpod 0.0% 0 / 6 totaltotal 78.7% 89 / 113 CC 13 SCAR 9.2
CC
13
Cov
84%
__ANON__
6.0
_insert
5.0
DELETE
3.0
CRAP 2.5
« Phonology/Functions.pm ↑ Summary Phonology/Common.pm »
1 #!/usr/bin/perl
2  
3 package Lingua::Phonology::Segment::Rules;
4  
5 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Lingua::Phonology::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 our $VERSION = 0.2;
11  
12 # This class acts just like a Segment, but adds the INSERT_RIGHT, INSERT_LEFT,
13 # etc. methods. It is not a proper subclass, because there's no way to get the
14 # proper behavior, but the utility function _is_seg is designed to recognize
15 # this class as a segment also. We are named as if we were an actual subclass.
16  
17 # To properly mimic Segment.pm, we have to overload
18 use overload
19 # The fun stuff
20 0 ▶ '""' => sub { "$_[0]->{seg}" },
Subroutine BEGIN: called CC=1 SCAR=3.3
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 0 ▶ 'cmp' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
22 0 my ($l, $r, $swap) = @_;
23 0 ▶ if ($swap) { return "$r" cmp "$l" }
Branch: 1 branch
BranchTrueFalseTotal
if ($swap) { } 0 0 0
24 0 else { return "$l" cmp "$r" } },
25 0 ▶ '0+' => sub { int $_[0]->{seg} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 8 'fallback' => 1;
27  
28  
29 3 ▶sub err ($) { _err($_[0]) if warnings::enabled() };
Subroutine err: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 0 3 3
30  
31 2726 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
32 2726 my $proto = shift;
33  
34 # If new() was called as an object method, the child should take care of it
35 2726 ▶ return $proto->{seg}->new(@_) if ref $proto;
Branch: 1 branch
BranchTrueFalseTotal
if ref $proto 6 2720 2726
36  
37 # Don't carp here, so that the caller can make their own error message
38 2720 my ($word, $base) = @_;
39 2720 ▶ return unless _is_seg $base;
Branch: 1 branch
BranchTrueFalseTotal
unless &_is_seg($base) 0 2720 2720
40  
41 2720 return bless { seg => $base, word => $word, id => int $base }, $proto;
42 }
43  
44 17 ▶sub _insert {
Subroutine _insert: called CC=5 SCAR=16.2
45 17 my $self = shift;
46 17 my ($dir, $seg) = @_;
47 17 ▶ return err "Can't INSERT_$dir with a tier in effect" if _is_tier $self->{seg};
Branch: 1 branch
BranchTrueFalseTotal
if &_is_tier($self->{"seg"}) 2 15 17
48 15 ▶ return err "Argument to INSERT_$dir not a segment" unless _is_seg $seg;
Branch: 1 branch
BranchTrueFalseTotal
unless &_is_seg($seg) 0 15 15
49 15 ▶ my $pos = ($dir eq 'RIGHT') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$dir eq 'RIGHT' ? : 4 11 15
50  
51 # Always insert into your parent
52 15 $self->{word}->_insert($self->{id}, $pos, $seg);
53  
54 # Pass on _insert calls if possible
55 15 ▶ $self->{seg}->_insert(@_) if $self->{seg}->can('_insert');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seg"}->can("_insert") 0 15 15
56  
57 15 1;
58 }
59  
60 12 ▶sub INSERT_LEFT {
Subroutine INSERT_LEFT: called CC=1 SCAR=0.0
Pod undocumented
61 12 (shift)->_insert('LEFT', @_);
62 }
63  
64 5 ▶sub INSERT_RIGHT {
Subroutine INSERT_RIGHT: called CC=1 SCAR=0.0
Pod undocumented
65 5 (shift)->_insert('RIGHT', @_);
66 }
67  
68 7 ▶sub DELETE {
Subroutine DELETE: called CC=3 SCAR=11.0
69 7 my $self = shift;
70 7 ▶ return err "Can't DELETE with a tier in effect" if _is_tier $self->{seg};
Branch: 1 branch
BranchTrueFalseTotal
if &_is_tier($self->{"seg"}) 1 6 7
71 6 $self->{word}->_delete($self->{id});
72 6 ▶ $self->{seg}->DELETE if $self->{seg}->can('DELETE');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seg"}->can("DELETE") 0 6 6
73 }
74  
75 594 ▶sub _getid {
Subroutine _getid: called CC=1 SCAR=0.0
76 594 return $_[0]->{id};
77 }
78  
79 # Override clear() to do a DELETE as well
80 3 ▶sub clear {
Subroutine clear: called CC=2 SCAR=7.1
Pod undocumented
81 3 my $self = shift;
82 3 ▶ $self->DELETE unless _is_tier $self->{seg};
Branch: 1 branch
BranchTrueFalseTotal
unless &_is_tier($self->{"seg"}) 3 0 3
83 3 $self->{seg}->clear;
84 }
85  
86 1 ▶sub _RULE {
Subroutine _RULE: called CC=1 SCAR=0.0
87 1 return $_[0]->{word}->rule;
88 }
89  
90 # Don't be a boundary unless the seg you're holding has a method for deciding
91 159 ▶sub BOUNDARY {
Subroutine BOUNDARY: called CC=2 SCAR=6.9
Pod undocumented
92 159 my $self = shift;
93 159 ▶ return $self->{seg}->BOUNDARY if $self->{seg}->can('BOUNDARY');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seg"}->can("BOUNDARY") 37 122 159
94 122 return;
95 }
96  
97 # AUTOLOAD dispatches all other methods to the seg
98 8 our $AUTOLOAD;
99 57 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
100 57 my $self = shift;
101 57 my $method = $AUTOLOAD;
102 57 $method =~ s/.*:://;
103  
104 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
105 57 ▶ *$method = sub { (shift)->{seg}->$method(@_); };
Subroutine __ANON__: called CC=1 SCAR=0.0
106 57 $self->$method(@_);
107 }
108  
109 # Don't destroy your children!
110 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
111  
112 8 1;
« Phonology/Functions.pm ↑ Summary Phonology/Common.pm »