| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | package Lingua::Phonology::Common; | ||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||
| 5 | # This module is used for functions needed at least in part by all other | ||||||||||||||||||||||||||||||||||
| 6 | # packages. | ||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | # We export everything. Since this is only for internal use, we know what we're | ||||||||||||||||||||||||||||||||||
| 9 | # getting, and the funcs all begin with _, so are unlikely to clash anyway | ||||||||||||||||||||||||||||||||||
| 10 | 10 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||
| 11 | 10 | @EXPORT = qw( | |||||||||||||||||||||||||||||||||
| 12 | _err | ||||||||||||||||||||||||||||||||||
| 13 | _is | ||||||||||||||||||||||||||||||||||
| 14 | _is_features | ||||||||||||||||||||||||||||||||||
| 15 | _is_symbols | ||||||||||||||||||||||||||||||||||
| 16 | _is_syllable | ||||||||||||||||||||||||||||||||||
| 17 | _is_seg | ||||||||||||||||||||||||||||||||||
| 18 | _is_boundary | ||||||||||||||||||||||||||||||||||
| 19 | _is_ruleseg | ||||||||||||||||||||||||||||||||||
| 20 | _is_tier | ||||||||||||||||||||||||||||||||||
| 21 | _to_handle | ||||||||||||||||||||||||||||||||||
| 22 | _parse_from_file | ||||||||||||||||||||||||||||||||||
| 23 | _parse_from_string | ||||||||||||||||||||||||||||||||||
| 24 | _string_from_struct | ||||||||||||||||||||||||||||||||||
| 25 | _parse_ext | ||||||||||||||||||||||||||||||||||
| 26 | _parse_plain | ||||||||||||||||||||||||||||||||||
| 27 | _deparse_ext | ||||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | 10 | $VERSION = 0.1; | |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||
| 32 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 33 | 10 | ▶ | use warnings::register; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | 10 | ▶ | use Carp qw/carp croak/; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 36 | 10 | our @CARP_NOT = qw/ | |||||||||||||||||||||||||||||||||
| 37 | Lingua::Phonology | ||||||||||||||||||||||||||||||||||
| 38 | Lingua::Phonology::Features | ||||||||||||||||||||||||||||||||||
| 39 | Lingua::Phonology::Symbols | ||||||||||||||||||||||||||||||||||
| 40 | Lingua::Phonology::Segment | ||||||||||||||||||||||||||||||||||
| 41 | Lingua::Phonology::Segment::Rules | ||||||||||||||||||||||||||||||||||
| 42 | Lingua::Phonology::Segment::Tier | ||||||||||||||||||||||||||||||||||
| 43 | Lingua::Phonology::Segment::Boundary | ||||||||||||||||||||||||||||||||||
| 44 | Lingua::Phonology::Rules | ||||||||||||||||||||||||||||||||||
| 45 | Lingua::Phonology::Syllable | ||||||||||||||||||||||||||||||||||
| 46 | Lingua::Phonology::Word | ||||||||||||||||||||||||||||||||||
| 47 | /; | ||||||||||||||||||||||||||||||||||
| 48 | 10 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 49 | 10 | ▶ | use XML::Simple; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | # Global variables. In principle, modules using this module can change these if | ||||||||||||||||||||||||||||||||||
| 52 | # they want, but they probably shouldn't lest evil things transpire. | ||||||||||||||||||||||||||||||||||
| 53 | 10 | our %xmlin_opts = ( | |||||||||||||||||||||||||||||||||
| 54 | KeyAttr => { feature => 'name', child => 'name', parent => 'name', symbol => 'label' }, | ||||||||||||||||||||||||||||||||||
| 55 | ForceArray => [qw/child parent feature symbol rule/], | ||||||||||||||||||||||||||||||||||
| 56 | GroupTags => { features => 'feature', symbols => 'symbol', order => 'block', persist => 'rule', block => 'rule' } | ||||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||||
| 58 | 10 | our %xmlout_opts = ( | |||||||||||||||||||||||||||||||||
| 59 | KeepRoot => 1, | ||||||||||||||||||||||||||||||||||
| 60 | KeyAttr => { feature => 'name', child => 'name', parent => 'name', symbol => 'label', rule => 'name' } | ||||||||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||
| 63 | # Concise synonym for UNIVERSAL::isa() with automatic error-writing | ||||||||||||||||||||||||||||||||||
| 64 | 22223 | ▶ | sub _is($$) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_is: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 65 | 22223 | UNIVERSAL::isa(@_); | |||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | # Extensions of _is for our own classes | ||||||||||||||||||||||||||||||||||
| 69 | 871 | ▶ | sub _is_features ($) { _is(shift, 'Lingua::Phonology::Features') } | ||||||||||||||||||||||||||||||||
|
Subroutine
_is_features: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 70 | 685 | ▶ | sub _is_symbols ($) { _is(shift, 'Lingua::Phonology::Symbols') } | ||||||||||||||||||||||||||||||||
|
Subroutine
_is_symbols: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub _is_syllable ($) { _is(shift, 'Lingua::Phonology::Syllable') } | ||||||||||||||||||||||||||||||||
|
Subroutine
_is_syllable: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub _is_boundary ($) { _is(shift, 'Lingua::Phonology::Segment::Boundary') } | ||||||||||||||||||||||||||||||||
|
Subroutine
_is_boundary: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 73 | 21 | ▶ | sub _is_ruleseg ($) { _is(shift, 'Lingua::Phonology::Segment::Rules') } | ||||||||||||||||||||||||||||||||
|
Subroutine
_is_ruleseg: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 74 | 27 | ▶ | sub _is_tier ($) { _is(shift, 'Lingua::Phonology::Segment::Tier') } | ||||||||||||||||||||||||||||||||
|
Subroutine
_is_tier: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | # _is_seg is hacked to allow various segment lookalikes | ||||||||||||||||||||||||||||||||||
| 77 | 5862 | ▶ | sub _is_seg ($) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_is_seg: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 78 | 5862 | my $seg = shift; | |||||||||||||||||||||||||||||||||
| 79 | 5862 | ▶ | return _is($seg, 'Lingua::Phonology::Segment') | ||||||||||||||||||||||||||||||||
|
Condition100%6/6
&_is($seg, 'Lingua::Phonology::Segment') || &_is($seg, 'Lingua::Phonology::Segment::Rules') || &_is($seg, 'Lingua::Phonology::Segment::Tier')
&_is($seg, "Lingua::Phonology::Segment") or &_is($seg, "Lingua::Phonology::Segment::Rules")
MC/DC100%3/3
&_is($seg, 'Lingua::Phonology::Segment') || &_is($seg, 'Lingua::Phonology::Segment::Rules') || &_is($seg, 'Lingua::Phonology::Segment::Tier')
&_is($seg, "Lingua::Phonology::Segment")
&_is($seg, "Lingua::Phonology::Segment::Rules")
&_is($seg, 'Lingua::Phonology::Segment::Tier')
Truth table100%4/4
A or B || C
A= &_is($seg, "Lingua::Phonology::Segment")B= &_is($seg, "Lingua::Phonology::Segment::Rules")C= &_is($seg, 'Lingua::Phonology::Segment::Tier')
| |||||||||||||||||||||||||||||||||||
| 80 | || _is($seg, 'Lingua::Phonology::Segment::Rules') | ||||||||||||||||||||||||||||||||||
| 81 | || _is($seg, 'Lingua::Phonology::Segment::Tier'); | ||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | # Make a handle from a filename; don't touch existing handles | ||||||||||||||||||||||||||||||||||
| 85 | 18 | ▶ | sub _to_handle($$) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_to_handle: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 86 | 18 | my ($file, $mode) = @_; | |||||||||||||||||||||||||||||||||
| 87 | 18 | ▶ | return $file if _is($file, 'GLOB'); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | 18 | my $handle = IO::Handle->new(); | |||||||||||||||||||||||||||||||||
| 90 | 18 | ▶ | open $handle, $mode, $file or croak "Couldn't open $file: $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 91 | 12 | return $handle; | |||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | # Get the parsed XML structure from a filename. Optional second arg specifies | ||||||||||||||||||||||||||||||||||
| 95 | # which key of the parse to return. You'd better specify a key that's present | ||||||||||||||||||||||||||||||||||
| 96 | # on the topmost level of the parse--this method won't look through the whole | ||||||||||||||||||||||||||||||||||
| 97 | # structure for you, like the previous version did. | ||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||
| 99 | 14 | ▶ | sub _parse_from_file ($;$) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_parse_from_file: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||
| 100 | 14 | my $file = shift; | |||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | # Open, slurp, close | ||||||||||||||||||||||||||||||||||
| 103 | 14 | ▶ | $file = _to_handle($file, '<') or return; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 104 | 10 | my $string = join '', <$file>; | |||||||||||||||||||||||||||||||||
| 105 | 10 | close $file; | |||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||
| 107 | 10 | return _parse_from_string($string, @_); | |||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | 27 | ▶ | sub _parse_from_string ($;$) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_parse_from_string: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||
| 111 | 27 | my ($string, $element) = @_; | |||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||
| 113 | # Parse the string, check for errors | ||||||||||||||||||||||||||||||||||
| 114 | 27 | my $parse; | |||||||||||||||||||||||||||||||||
| 115 | 27 | eval { $parse = XMLin($string, %xmlin_opts) }; | |||||||||||||||||||||||||||||||||
| 116 | 27 | ▶ | croak "XML parsing error: $@" if ($@); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | 27 | ▶ | if (defined $element) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 119 | 25 | ▶ | return $parse->{$element} if exists $parse->{$element}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 120 | 0 | croak "<$element> element not found"; | |||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||
| 122 | 2 | return $parse; | |||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | # Turn a data structure into an XML string | ||||||||||||||||||||||||||||||||||
| 126 | 8 | ▶ | sub _string_from_struct ($) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_string_from_struct: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||
| 127 | 8 | my $struct = shift; | |||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||
| 129 | 8 | my $string; | |||||||||||||||||||||||||||||||||
| 130 | 8 | eval { $string = XMLout($struct, %xmlout_opts) }; | |||||||||||||||||||||||||||||||||
| 131 | 8 | ▶ | croak "Error creating XML: $@" if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||
| 133 | 8 | return $string; | |||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||
| 136 | 57 | ▶ | sub _parse_ext ($) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_parse_ext: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 137 | 57 | my $string = shift; | |||||||||||||||||||||||||||||||||
| 138 | 57 | $string =~ s/(-?\d+):/\$_[$1]->/g; | |||||||||||||||||||||||||||||||||
| 139 | 57 | return eval "return sub { package main; $string }"; | |||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | 8 | ▶ | sub _parse_plain ($) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_parse_plain: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 143 | 8 | return eval "return sub { package main; $_[0] }"; | |||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | 46 | ▶ | sub _deparse_ext ($$) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_deparse_ext: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 147 | 46 | my ($code, $deparser) = @_; | |||||||||||||||||||||||||||||||||
| 148 | 46 | my $string = $deparser->coderef2text($code); | |||||||||||||||||||||||||||||||||
| 149 | 46 | $string =~ s/\{(.*)\}/$1/s; # Strip opening/closing brackets | |||||||||||||||||||||||||||||||||
| 150 | #$string =~ s/^\s*(.*?)\s*$/$1/s; # String leading/trailing whitespace | ||||||||||||||||||||||||||||||||||
| 151 | 46 | $string =~ s/\$_\[(-?\d+)\]->/$1:/gs; # Do ext conversion | |||||||||||||||||||||||||||||||||
| 152 | 46 | return $string; | |||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | sub _err ($) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_err: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 156 | 0 | carp shift; | |||||||||||||||||||||||||||||||||
| 157 | 0 | return; | |||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | 10 | 1; | |||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||