File Coverage

blib/lib/Locale/Babelfish/Phrase/ParserBase.pm
Criterion Covered Total %
statement 55 56 98.2
branch 12 14 85.7
condition 6 8 75.0
subroutine 15 15 100.0
pod 10 10 100.0
total 98 103 95.1


line stmt bran cond sub pod time code
1             package Locale::Babelfish::Phrase::ParserBase;
2              
3             # ABSTRACT: Babelfish abstract parser.
4              
5 3     3   1313 use utf8;
  3         8  
  3         16  
6 3     3   94 use strict;
  3         6  
  3         55  
7 3     3   14 use warnings;
  3         6  
  3         147  
8              
9 3     3   19 use parent qw( Class::Accessor::Fast );
  3         6  
  3         18  
10              
11             our $VERSION = '2.10'; # VERSION
12              
13             __PACKAGE__->mk_accessors( qw( phrase index length prev piece escape ) );
14              
15              
16             sub new {
17 5     5 1 16 my ( $class, $phrase ) = @_;
18 5         14 my $parser = bless {}, $class;
19 5 50       24 $parser->init( $phrase ) if defined $phrase;
20 5         14 return $parser;
21             }
22              
23              
24             sub init {
25 95     95 1 172 my ( $self, $phrase ) = @_;
26 95         2032 $self->phrase( $phrase );
27 95         2216 $self->index( -1 );
28 95         2116 $self->prev( undef );
29 95         2134 $self->length( length( $phrase ) );
30 95         2023 $self->piece( '' );
31 95         2016 $self->escape( 0 );
32 95         611 return $self;
33             }
34              
35              
36             sub trim {
37 28     28 1 181 my ( $self, $str ) = @_;
38 28     1   86 $str =~ s/\A\p{PerlSpace}+//;
  1         319  
  1         3  
  1         16  
39 28         91 $str =~ s/\p{PerlSpace}+\z//;
40 28         67 return $str;
41             }
42              
43              
44             sub char {
45 2962     2962 1 5123 my ( $self ) = @_;
46 2962   50     48252 return substr( $self->phrase, $self->index, 1 ) // '';
47             }
48              
49              
50             sub next_char {
51 81     81 1 170 my ( $self ) = @_;
52 81 100       1328 return '' if $self->index >= $self->length - 1;
53 79   50     2912 return substr( $self->phrase, $self->index + 1, 1 ) // '';
54             }
55              
56              
57             sub to_next_char {
58 1575     1575 1 2769 my ( $self ) = @_;
59 1575 100       26202 if ( $self->index >= 0 ) {
60 1480         7739 $self->prev( $self->char );
61             }
62 1575         86554 $self->index( $self->index + 1 );
63 1575 100       34622 return '' if $self->index eq $self->length;
64 1482         33219 return $self->char();
65             }
66              
67              
68             sub throw {
69 3     3 1 35 my ( $self, $message ) = @_;
70 3   100     55 die "Cannot parse phrase \"". ( $self->phrase // 'undef' ). "\" at ". ( $self->index // '-1' ). " index: $message";
      100        
71             }
72              
73              
74             sub add_to_piece {
75 1301     1301 1 2920 my ( $self, @chars ) = @_;
76 1301         21602 $self->piece( join('', $self->piece, @chars ) );
77             }
78              
79              
80             sub backward {
81 8     8 1 27 my ( $self ) = @_;
82 8         140 $self->index( $self->index - 1 );
83 8 50       200 if ( $self->index > 0 ) {
84 8         182 $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 96     96 1 182 my ( $self, $phrase ) = @_;
94              
95 96 100       221 if ( defined $phrase ) {
96 95         257 $self->init( $phrase );
97             }
98              
99 96 100       1600 $self->throw( "No phrase given" ) unless defined $self->phrase;
100              
101 95         1955 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.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