| 1 | package Locale::Babelfish::Phrase::ParserBase; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | # ABSTRACT: Babelfish abstract parser. | ||||||||||||||||||||||
| 4 | |||||||||||||||||||||||
| 5 | 7 | ▶ | use utf8; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 7 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 7 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | 7 | ▶ | use parent qw( Class::Accessor::Fast ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 7 | our $VERSION = '2.10'; # VERSION | |||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | 7 | __PACKAGE__->mk_accessors( qw( phrase index length prev piece escape ) ); | |||||||||||||||||||||
| 14 | |||||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | sub new { | ||||||||||||||||||||||
| 17 | 6 | ▶ | my ( $class, $phrase ) = @_; | ||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||
| 18 | 6 | my $parser = bless {}, $class; | |||||||||||||||||||||
| 19 | 6 | ▶ | $parser->init( $phrase ) if defined $phrase; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 20 | 6 | return $parser; | |||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | sub init { | ||||||||||||||||||||||
| 25 | 95 | ▶ | my ( $self, $phrase ) = @_; | ||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 26 | 95 | $self->phrase( $phrase ); | |||||||||||||||||||||
| 27 | 95 | $self->index( -1 ); | |||||||||||||||||||||
| 28 | 95 | $self->prev( undef ); | |||||||||||||||||||||
| 29 | 95 | $self->length( length( $phrase ) ); | |||||||||||||||||||||
| 30 | 95 | $self->piece( '' ); | |||||||||||||||||||||
| 31 | 95 | $self->escape( 0 ); | |||||||||||||||||||||
| 32 | 95 | return $self; | |||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | |||||||||||||||||||||||
| 36 | sub trim { | ||||||||||||||||||||||
| 37 | 28 | ▶ | my ( $self, $str ) = @_; | ||||||||||||||||||||
|
Subroutine
trim: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 38 | 28 | $str =~ s/\A\p{PerlSpace}+//; | |||||||||||||||||||||
| 39 | 28 | $str =~ s/\p{PerlSpace}+\z//; | |||||||||||||||||||||
| 40 | 28 | return $str; | |||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | |||||||||||||||||||||||
| 44 | sub char { | ||||||||||||||||||||||
| 45 | 2962 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||
|
Subroutine
char: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||
| 46 | 2962 | ▶ | return substr( $self->phrase, $self->index, 1 ) // ''; | ||||||||||||||||||||
|
Condition50%1/2
substr($self->phrase, $self->index, 1) // ""
MC/DC0%0/1
substr($self->phrase, $self->index, 1) // ""
substr($self->phrase, $self->index, 1)
Truth table50%1/2
A
A= substr($self->phrase, $self->index, 1)
| |||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | |||||||||||||||||||||||
| 50 | sub next_char { | ||||||||||||||||||||||
| 51 | 81 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||
|
Subroutine
next_char: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||
| 52 | 81 | ▶ | return '' if $self->index >= $self->length - 1; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 53 | 79 | ▶ | return substr( $self->phrase, $self->index + 1, 1 ) // ''; | ||||||||||||||||||||
|
Condition50%1/2
substr($self->phrase, $self->index + 1, 1) // ""
MC/DC0%0/1
substr($self->phrase, $self->index + 1, 1) // ""
substr($self->phrase, $self->index + 1, 1)
Truth table50%1/2
A
A= substr($self->phrase, $self->index + 1, 1)
| |||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||
| 55 | |||||||||||||||||||||||
| 56 | |||||||||||||||||||||||
| 57 | sub to_next_char { | ||||||||||||||||||||||
| 58 | 1575 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||
|
Subroutine
to_next_char: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||
| 59 | 1575 | ▶ | if ( $self->index >= 0 ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 60 | 1480 | $self->prev( $self->char ); | |||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||
| 62 | 1575 | $self->index( $self->index + 1 ); | |||||||||||||||||||||
| 63 | 1575 | ▶ | return '' if $self->index eq $self->length; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 64 | 1482 | return $self->char(); | |||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||
| 66 | |||||||||||||||||||||||
| 67 | |||||||||||||||||||||||
| 68 | sub throw { | ||||||||||||||||||||||
| 69 | 3 | ▶ | my ( $self, $message ) = @_; | ||||||||||||||||||||
|
Subroutine
throw: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||
| 70 | 3 | ▶ | die "Cannot parse phrase \"". ( $self->phrase // 'undef' ). "\" at ". ( $self->index // '-1' ). " index: $message"; | ||||||||||||||||||||
|
Condition100%4/4
$self->phrase // "undef"
$self->index // -1
MC/DC100%1/1
$self->phrase // "undef"
$self->phrase
MC/DC100%1/1
$self->index // -1
$self->index
Truth table100%2/2
A
A= $self->phrase
Truth table100%2/2
A
A= $self->index
| |||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||
| 72 | |||||||||||||||||||||||
| 73 | |||||||||||||||||||||||
| 74 | sub add_to_piece { | ||||||||||||||||||||||
| 75 | 1301 | ▶ | my ( $self, @chars ) = @_; | ||||||||||||||||||||
|
Subroutine
add_to_piece: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 76 | 1301 | $self->piece( join('', $self->piece, @chars ) ); | |||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||
| 78 | |||||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | sub backward { | ||||||||||||||||||||||
| 81 | 8 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||
|
Subroutine
backward: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||
| 82 | 8 | $self->index( $self->index - 1 ); | |||||||||||||||||||||
| 83 | 8 | ▶ | if ( $self->index > 0 ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 84 | 8 | $self->prev( substr( $self->phrase, $self->index - 1, 1 ) ); | |||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||||
| 87 | 0 | $self->prev( undef ); | |||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||
| 90 | |||||||||||||||||||||||
| 91 | |||||||||||||||||||||||
| 92 | sub parse { | ||||||||||||||||||||||
| 93 | 96 | ▶ | my ( $self, $phrase ) = @_; | ||||||||||||||||||||
|
Subroutine
parse: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||
| 94 | |||||||||||||||||||||||
| 95 | 96 | ▶ | if ( defined $phrase ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 96 | 95 | $self->init( $phrase ); | |||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||
| 98 | |||||||||||||||||||||||
| 99 | 96 | ▶ | $self->throw( "No phrase given" ) unless defined $self->phrase; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | 95 | return $self->phrase; | |||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||
| 103 | |||||||||||||||||||||||
| 104 | 7 | 1; | |||||||||||||||||||||
| 105 | |||||||||||||||||||||||
| 106 | __END__ | ||||||||||||||||||||||
| 107 | |||||||||||||||||||||||
| 108 | =pod | ||||||||||||||||||||||
| 109 | |||||||||||||||||||||||
| 110 | =encoding UTF-8 | ||||||||||||||||||||||
| 111 | |||||||||||||||||||||||
| 112 | =head1 NAME | ||||||||||||||||||||||
| 113 | |||||||||||||||||||||||
| 114 | Locale::Babelfish::Phrase::ParserBase - Babelfish abstract parser. | ||||||||||||||||||||||
| 115 | |||||||||||||||||||||||
| 116 | =head1 VERSION | ||||||||||||||||||||||
| 117 | |||||||||||||||||||||||
| 118 | version 2.10 | ||||||||||||||||||||||
| 119 | |||||||||||||||||||||||
| 120 | =head1 METHODS | ||||||||||||||||||||||
| 121 | |||||||||||||||||||||||
| 122 | =head2 new | ||||||||||||||||||||||
| 123 | |||||||||||||||||||||||
| 124 | $class->new() | ||||||||||||||||||||||
| 125 | $class->new( $phrase ) | ||||||||||||||||||||||
| 126 | |||||||||||||||||||||||
| 127 | Instantiates parser. | ||||||||||||||||||||||
| 128 | |||||||||||||||||||||||
| 129 | =head2 init | ||||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | Initializes parser. Should not be called directly. | ||||||||||||||||||||||
| 132 | |||||||||||||||||||||||
| 133 | =head2 trim | ||||||||||||||||||||||
| 134 | |||||||||||||||||||||||
| 135 | $self->trim( $str ) | ||||||||||||||||||||||
| 136 | |||||||||||||||||||||||
| 137 | Removes space characters from start and end of specified string. | ||||||||||||||||||||||
| 138 | |||||||||||||||||||||||
| 139 | =head2 char | ||||||||||||||||||||||
| 140 | |||||||||||||||||||||||
| 141 | $self->char | ||||||||||||||||||||||
| 142 | |||||||||||||||||||||||
| 143 | Gets character on current cursor position. | ||||||||||||||||||||||
| 144 | |||||||||||||||||||||||
| 145 | Will return empty string if no character. | ||||||||||||||||||||||
| 146 | |||||||||||||||||||||||
| 147 | =head2 next_char | ||||||||||||||||||||||
| 148 | |||||||||||||||||||||||
| 149 | $self->next_char | ||||||||||||||||||||||
| 150 | |||||||||||||||||||||||
| 151 | Gets character on next cursor position. | ||||||||||||||||||||||
| 152 | |||||||||||||||||||||||
| 153 | Will return empty string if no character. | ||||||||||||||||||||||
| 154 | |||||||||||||||||||||||
| 155 | =head2 to_next_char | ||||||||||||||||||||||
| 156 | |||||||||||||||||||||||
| 157 | $self->to_next_char | ||||||||||||||||||||||
| 158 | |||||||||||||||||||||||
| 159 | Moves cursor to next position. | ||||||||||||||||||||||
| 160 | |||||||||||||||||||||||
| 161 | Return new current character. | ||||||||||||||||||||||
| 162 | |||||||||||||||||||||||
| 163 | =head2 throw | ||||||||||||||||||||||
| 164 | |||||||||||||||||||||||
| 165 | $self->throw( $message ) | ||||||||||||||||||||||
| 166 | |||||||||||||||||||||||
| 167 | Throws given message in phrase context. | ||||||||||||||||||||||
| 168 | |||||||||||||||||||||||
| 169 | =head2 add_to_piece | ||||||||||||||||||||||
| 170 | |||||||||||||||||||||||
| 171 | $parser->add_to_piece( @chars ) | ||||||||||||||||||||||
| 172 | |||||||||||||||||||||||
| 173 | Adds given chars to current piece. | ||||||||||||||||||||||
| 174 | |||||||||||||||||||||||
| 175 | =head2 backward | ||||||||||||||||||||||
| 176 | |||||||||||||||||||||||
| 177 | $parser->backward | ||||||||||||||||||||||
| 178 | |||||||||||||||||||||||
| 179 | Moves cursor backward. | ||||||||||||||||||||||
| 180 | |||||||||||||||||||||||
| 181 | =head2 parse | ||||||||||||||||||||||
| 182 | |||||||||||||||||||||||
| 183 | $parser->parse() | ||||||||||||||||||||||
| 184 | $parser->parse( $phrase ) | ||||||||||||||||||||||
| 185 | |||||||||||||||||||||||
| 186 | Parses specified phrase. | ||||||||||||||||||||||
| 187 | |||||||||||||||||||||||
| 188 | =head1 AUTHORS | ||||||||||||||||||||||
| 189 | |||||||||||||||||||||||
| 190 | =over 4 | ||||||||||||||||||||||
| 191 | |||||||||||||||||||||||
| 192 | =item * | ||||||||||||||||||||||
| 193 | |||||||||||||||||||||||
| 194 | Akzhan Abdulin <akzhan@cpan.org> | ||||||||||||||||||||||
| 195 | |||||||||||||||||||||||
| 196 | =item * | ||||||||||||||||||||||
| 197 | |||||||||||||||||||||||
| 198 | Igor Mironov <grif@cpan.org> | ||||||||||||||||||||||
| 199 | |||||||||||||||||||||||
| 200 | =item * | ||||||||||||||||||||||
| 201 | |||||||||||||||||||||||
| 202 | Victor Efimov <efimov@reg.ru> | ||||||||||||||||||||||
| 203 | |||||||||||||||||||||||
| 204 | =item * | ||||||||||||||||||||||
| 205 | |||||||||||||||||||||||
| 206 | REG.RU LLC | ||||||||||||||||||||||
| 207 | |||||||||||||||||||||||
| 208 | =item * | ||||||||||||||||||||||
| 209 | |||||||||||||||||||||||
| 210 | Kirill Sysoev <k.sysoev@me.com> | ||||||||||||||||||||||
| 211 | |||||||||||||||||||||||
| 212 | =item * | ||||||||||||||||||||||
| 213 | |||||||||||||||||||||||
| 214 | Alexandr Tkach <tkach@reg.ru> | ||||||||||||||||||||||
| 215 | |||||||||||||||||||||||
| 216 | =back | ||||||||||||||||||||||
| 217 | |||||||||||||||||||||||
| 218 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||
| 219 | |||||||||||||||||||||||
| 220 | This software is Copyright (c) 2014 by REG.RU LLC. | ||||||||||||||||||||||
| 221 | |||||||||||||||||||||||
| 222 | This is free software, licensed under: | ||||||||||||||||||||||
| 223 | |||||||||||||||||||||||
| 224 | The MIT (X11) License | ||||||||||||||||||||||
| 225 | |||||||||||||||||||||||
| 226 | =cut | ||||||||||||||||||||||