File Coverage

blib/lib/Locale/Babelfish/Phrase/ParserBase.pm
Criterion Covered Total %
statement 52 53 98.1
branch 12 14 85.7
condition 6 8 75.0
subroutine 14 14 100.0
pod 10 10 100.0
total 94 99 94.9


line stmt bran cond sub pod time code
1             package Locale::Babelfish::Phrase::ParserBase;
2              
3             # ABSTRACT: Babelfish abstract parser.
4              
5 8     8   326856 use utf8;
  8         422  
  8         54  
6 8     8   385 use strict;
  8         15  
  8         204  
7 8     8   37 use warnings;
  8         16  
  8         436  
8              
9 8     8   60 use parent qw( Class::Accessor::Fast );
  8         19  
  8         92  
10              
11             our $VERSION = '2.13'; # VERSION
12              
13             __PACKAGE__->mk_accessors( qw( phrase index length prev piece escape ) );
14              
15              
16             sub new {
17 7     7 1 23 my ( $class, $phrase ) = @_;
18 7         20 my $parser = bless {}, $class;
19 7 50       32 $parser->init( $phrase ) if defined $phrase;
20 7         22 return $parser;
21             }
22              
23              
24             sub init {
25 291     291 1 590 my ( $self, $phrase ) = @_;
26 291         8420 $self->phrase( $phrase );
27 291         8277 $self->index( -1 );
28 291         7959 $self->prev( undef );
29 291         8016 $self->length( length( $phrase ) );
30 291         7513 $self->piece( '' );
31 291         7992 $self->escape( 0 );
32 291         3529 return $self;
33             }
34              
35              
36             sub trim {
37 70     70 1 508 my ( $self, $str ) = @_;
38 70         222 $str =~ s/\A\p{PerlSpace}+//;
39 70         242 $str =~ s/\p{PerlSpace}+\z//;
40 70         227 return $str;
41             }
42              
43              
44             sub char {
45 6866     6866 1 12864 my ( $self ) = @_;
46 6866   50     150770 return substr( $self->phrase, $self->index, 1 ) // '';
47             }
48              
49              
50             sub next_char {
51 159     159 1 338 my ( $self ) = @_;
52 159 100       3607 return '' if $self->index >= $self->length - 1;
53 157   50     7181 return substr( $self->phrase, $self->index + 1, 1 ) // '';
54             }
55              
56              
57             sub to_next_char {
58 3723     3723 1 8110 my ( $self ) = @_;
59 3723 100       84486 if ( $self->index >= 0 ) {
60 3432         20399 $self->prev( $self->char );
61             }
62 3723         264640 $self->index( $self->index + 1 );
63 3723 100       104500 return '' if $self->index eq $self->length;
64 3434         100650 return $self->char();
65             }
66              
67              
68             sub throw {
69 3     3 1 28 my ( $self, $message ) = @_;
70 3   100     73 die "Cannot parse phrase \"". ( $self->phrase // 'undef' ). "\" at ". ( $self->index // '-1' ). " index: $message";
      100        
71             }
72              
73              
74             sub add_to_piece {
75 3055     3055 1 8509 my ( $self, @chars ) = @_;
76 3055         72832 $self->piece( join('', $self->piece, @chars ) );
77             }
78              
79              
80             sub backward {
81 18     18 1 56 my ( $self ) = @_;
82 18         418 $self->index( $self->index - 1 );
83 18 50       531 if ( $self->index > 0 ) {
84 18         482 $self->prev( substr( $self->phrase, $self->index - 1, 1 ) );
85             }
86             else {
87 0         0 $self->prev( undef );
88             }
89             }
90              
91              
92             sub parse {
93 292     292 1 606 my ( $self, $phrase ) = @_;
94              
95 292 100       803 if ( defined $phrase ) {
96 291         946 $self->init( $phrase );
97             }
98              
99 292 100       8324 $self->throw( "No phrase given" ) unless defined $self->phrase;
100              
101 291         7430 return $self->phrase;
102             }
103              
104             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.13
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