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   964 use utf8;
  3         10  
  3         15  
6 3     3   84 use strict;
  3         4  
  3         54  
7 3     3   11 use warnings;
  3         6  
  3         98  
8              
9 3     3   13 use parent qw( Class::Accessor::Fast );
  3         6  
  3         12  
10              
11             our $VERSION = '2.003'; # VERSION
12              
13             __PACKAGE__->mk_accessors( qw( phrase index length prev piece escape ) );
14              
15              
16             sub new {
17 5     5 1 12 my ( $class, $phrase ) = @_;
18 5         14 my $parser = bless {}, $class;
19 5 50       18 $parser->init( $phrase ) if defined $phrase;
20 5         13 return $parser;
21             }
22              
23              
24             sub init {
25 79     79 1 140 my ( $self, $phrase ) = @_;
26 79         1397 $self->phrase( $phrase );
27 79         1540 $self->index( -1 );
28 79         1485 $self->prev( undef );
29 79         1520 $self->length( length( $phrase ) );
30 79         1461 $self->piece( '' );
31 79         1415 $self->escape( 0 );
32 79         437 return $self;
33             }
34              
35              
36             sub trim {
37 20     20 1 120 my ( $self, $str ) = @_;
38 20     1   63 $str =~ s/\A\p{PerlSpace}+//;
  1         198  
  1         1  
  1         19  
39 20         70 $str =~ s/\p{PerlSpace}+\z//;
40 20         51 return $str;
41             }
42              
43              
44             sub char {
45 2494     2494 1 3640 my ( $self ) = @_;
46 2494   50     34056 return substr( $self->phrase, $self->index, 1 ) // '';
47             }
48              
49              
50             sub next_char {
51 73     73 1 132 my ( $self ) = @_;
52 73 100       1959 return '' if $self->index >= $self->length - 1;
53 71   50     2203 return substr( $self->phrase, $self->index + 1, 1 ) // '';
54             }
55              
56              
57             sub to_next_char {
58 1325     1325 1 1939 my ( $self ) = @_;
59 1325 100       18463 if ( $self->index >= 0 ) {
60 1246         5384 $self->prev( $self->char );
61             }
62 1325         60261 $self->index( $self->index + 1 );
63 1325 100       25207 return '' if $self->index eq $self->length;
64 1248         23916 return $self->char();
65             }
66              
67              
68             sub throw {
69 3     3 1 23 my ( $self, $message ) = @_;
70 3   100     49 die "Cannot parse phrase \"". ( $self->phrase // 'undef' ). "\" at ". ( $self->index // '-1' ). " index: $message";
      100        
71             }
72              
73              
74             sub add_to_piece {
75 1095     1095 1 2112 my ( $self, @chars ) = @_;
76 1095         15413 $self->piece( join('', $self->piece, @chars ) );
77             }
78              
79              
80             sub backward {
81 8     8 1 20 my ( $self ) = @_;
82 8         115 $self->index( $self->index - 1 );
83 8 50       165 if ( $self->index > 0 ) {
84 8         145 $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 80     80 1 157 my ( $self, $phrase ) = @_;
94              
95 80 100       178 if ( defined $phrase ) {
96 79         217 $self->init( $phrase );
97             }
98              
99 80 100       1160 $self->throw( "No phrase given" ) unless defined $self->phrase;
100              
101 79         1339 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.003
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             =back
209              
210             =head1 COPYRIGHT AND LICENSE
211              
212             This software is Copyright (c) 2014 by REG.RU LLC.
213              
214             This is free software, licensed under:
215              
216             The MIT (X11) License
217              
218             =cut