| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 48 | 15 | ▶ | return err "Argument to INSERT_$dir not a segment" unless _is_seg $seg; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 15 | ▶ | my $pos = ($dir eq 'RIGHT') ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 71 | 6 | $self->{word}->_delete($self->{id}); | |||||||||
| 72 | 6 | ▶ | $self->{seg}->DELETE if $self->{seg}->can('DELETE'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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; | |||||||||