Gherkin/Lexer.pm

filter: Statementstmt 100% 98 / 98 Branchbran 92.8% 13 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 26 / 26 Podpod 50.0% 1 / 2 totaltotal 98.5% 138 / 140 CC 13 SCAR 9.3
CC
13
Cov
99%
_tokenize
7.0
_scan_line
3.0
_process_pragmas
3.0
CRAP 2.5
« Gherkin/Node/Table.pm ↑ Summary Gherkin/Node/Background.pm »
1 package Test::Pcuke::Gherkin::Lexer;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Test::Pcuke::Gherkin::I18n;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 =head1 NAME
9  
10 Test::Pcuke::Gherkin::Lexer - roll your own cucumber
11  
12 =head1 VERSION
13  
14 Version 0.000001
15  
16 =cut
17  
18 2 our $VERSION = '0.000001';
19  
20  
21 =head1 SYNOPSIS
22  
23 TODO SYNOPSIS
24  
25 use Test::Pcuke::Gherkin::Lexer;
26 ...
27 my $lexens = Test::Pcuke::Gherkin::Lexer->scan( $input );
28 ...
29  
30 =cut
31  
32 # TODO i18n!!!
33 2 my $REGEXP = {
34 empty => [ qr{^\s*$}ix ],
35 pragma => [ qr{^\s*#(?:\s*(\w+\s*:\s*\w+))+}i ],
36 comment => [ qr{^\s*#}i ],
37 tag => [ qr{@\w+}ix ],
38 feature => [ qr{^\s*(feature: .*?)\s*$}ix ],
39 any => [ qr{^\s*(.*?)\s*$}ix ],
40 background => [ qr{^\s*(background:.*?)\s*$}ix ],
41 scenario => [ qr{^\s*(scenario:.*?)\s*$}ix ],
42 outline => [ qr{^\s*(scenario outline:.*?)\s*$}i, qr{^\s*(scenario template:.*?)\s*$}ix ],
43 given => [ qr{^\s*(\*)\s*(.*?)\s*$}i, qr{^\s*(given)\s*(.*?)\s*$}ix ],
44 when => [ qr{^\s*(\*)\s*(.*?)\s*$}i, qr{^\s*(when)\s*(.*?)\s*$}ix ],
45 then => [ qr{^\s*(\*)\s*(.*?)\s*$}i, qr{^\s*(then)\s*(.*?)\s*$}ix ],
46 and => [ qr{^\s*(\*)\s*(.*?)\s*$}i, qr{^\s*(and)\s*(.*?)\s*$}ix ],
47 but => [ qr{^\s*(\*)\s*(.*?)\s*$}i, qr{^\s*(but)\s*(.*?)\s*$}ix ],
48 examples => [ qr{^\s*(examples:.*?)\s*$}i, qr{^\s*(scenarios:.*?)\s*$}ix ],
49 trow => [ qr{^ \s* \| (.*?) \| \s* $}ix ],
50 text_quote => [ qr{^ \s* (""") \s* $}ix ],
51 };
52  
53 2 my @LEVELS = qw{root};
54  
55 my $TOKENIZERS = {
56 root => [
57 {
58 regexps => ['empty'],
59 mktoken => _skip()
60 },
61
62 {
63 regexps => ['pragma'],
64 mktoken => _mktoken_split('PRAG', qr{\s*:\s*}),
65 },
66
67 {
68 regexps => ['comment'],
69 mktoken => _skip(),
70 },
71
72 {
73 regexps => ['tag'],
74 mktoken => _mktoken_perchunk('TAG'),
75 },
76
77 {
78 regexps => ['feature'],
79 mktoken => _mktoken_join('FEAT'),
80 level => 'feature',
81 },
82
83 {
84 regexps => ['any'],
85 mktoken => _mktoken_join('ERR'),
86 },
87 ],
88
89 feature => [
90 {
91 regexps => ['empty', 'comment'],
92 mktoken => _skip()
93 },
94
95 {
96 regexps => ['tag'],
97 mktoken => _mktoken_perchunk('TAG'),
98 },
99
100 {
101 regexps => ['background'],
102 mktoken => _mktoken_join('BGR'),
103 level => 'steps',
104 },
105
106 {
107 regexps => ['scenario'],
108 mktoken => _mktoken_join('SCEN'),
109 level => 'steps',
110 },
111
112 {
113 regexps => ['outline'],
114 mktoken => _mktoken_join('OUTL'),
115 level => ['outline', 'steps'],
116 },
117
118 {
119 regexps => ['any'],
120 mktoken => _mktoken_join('NARR'),
121 },
122 ],
123
124 steps => [
125 {
126 regexps => ['empty', 'comment'],
127 mktoken => _skip()
128 },
129
130 {
131 regexps => [qw{given when then and but}],
132 9 ▶ mktoken => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
133 9 my ($self, $line, $type, $title) = @_;
134 9 ['STEP', uc $type, $title ]
135 },
136 },
137
138 {
139 regexps => ['text_quote'],
140 mktoken => _skip(),
141 level => 'text',
142 },
143
144 {
145 regexps => ['trow'],
146 5 ▶ mktoken => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
147 5 my ($self, $line, @chunks) = @_;
148 5 my @cols = map { s/^\s*|\s*$//g; $_ } split /\s*\|\s*/, $chunks[0];
149 5 ['TROW', @cols];
150 }
151 },
152
153 {
154 2 regexps => ['any'],
155 mktoken => _mktoken_uplevel(),
156 },
157 ],
158
159 text => [
160 {
161 regexps => ['text_quote'],
162 mktoken => _skip(), # maybe _mktoken_skip_uplevel() ?
163 level => 'steps', # 'cause it is not very beautiful
164 },
165 {
166 regexps => ['any'],
167 mktoken => _mktoken_join('TEXT'),
168 },
169 ],
170
171 outline => [
172 {
173 regexps => ['empty', 'comment'],
174 mktoken => _skip()
175 },
176
177 {
178 regexps => ['examples'],
179 mktoken => _mktoken_join('SCENS'),
180 level => 'scenarios',
181 },
182
183 {
184 regexps => ['any'],
185 mktoken => _mktoken_uplevel(),
186 },
187 ],
188
189 scenarios => [
190 {
191 regexps => ['empty', 'comment'],
192 mktoken => _skip()
193 },
194
195 {
196 regexps => ['trow'],
197 mktoken => _mktoken_split('TROW', qr{\s*\|\s*}),
198 },
199
200 {
201 regexps => ['any'],
202 mktoken => _mktoken_uplevel(),
203 },
204 ],
205 };
206  
207 =head1 METHODS
208  
209 =head2 scan $input
210  
211 =cut
212  
213 17 ▶sub scan {
Subroutine scan: called CC=2 SCAR=6.9
Pod documented
214 17 my ($self, $input) = @_;
215 17 my $result;
216 17 my @lines = split /[\r\n]+/, $input;
217
218 17 foreach my $line ( @lines ) {
219 64 push @$result, @{ $self->_scan_line($line) };
220 }
221
222 17 return $result;
223 }
224  
225 66 ▶sub _scan_line {
Subroutine _scan_line: called CC=3 SCAR=11.0
226 66 my ($self, $line) = @_;
227 66 my @chunks;
228 my @result;
229
230 66 foreach my $tokenizer ( @{$TOKENIZERS->{ $self->_get_level } } ) {
231 197 push @result, $self->_tokenize($tokenizer, $line);
232 197 ▶ last if @result;
Branch: 1 branch
BranchTrueFalseTotal
if @result 66 131 197
233 }
234
235 66 @result = grep { $_->[0] ne 'SKIP' } @result;
236
237 66 $self->_process_pragmas( grep { $_->[0] eq 'PRAG' } @result);
238
239 66 return \@result;
240 }
241  
242 197 ▶sub _tokenize {
Subroutine _tokenize: called CC=7 SCAR=19.5
243 197 my ($self, $conf, $line) = @_;
244 197 my @tokens;
245 my @chunks;
246
247 197 foreach my $re_name ( @{$conf->{regexps}} ) {
248 274 foreach my $re ( @{ $REGEXP->{$re_name} } ) {
249 343 @chunks = ($line =~ /$re/ig );
250 343 ▶ if ( @chunks ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chunks) 66 277 343
251 66 push @tokens, $conf->{mktoken}->($self, $line, @chunks);
252
253 66 ▶ if ( $conf->{level} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"level"}) 15 51 66
254 15 ▶ $conf->{level} = [$conf->{level}] unless ref $conf->{level};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conf->{"level"} 6 9 15
255 15 $self->_append_levels( @{ $conf->{level} } )
256 }
257 66 last;
258 }
259 }
260 274 ▶ last if @tokens;
Branch: 1 branch
BranchTrueFalseTotal
if @tokens 66 208 274
261 }
262 197 return @tokens;
263 }
264  
265 66 ▶sub _process_pragmas {
Subroutine _process_pragmas: called CC=3 SCAR=11.0
266 66 my $self = shift;
267 66 foreach (@_) {
268 5 my ($prag, $name, $value) = @{$_};
269 5 ▶ $self->_set_language($value) if $name eq 'language';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "language" 3 2 5
270 }
271 }
272  
273 3 ▶sub _set_language {
Subroutine _set_language: called CC=1 SCAR=0.0
274 3 my ($self, $language) = @_;
275 3 $REGEXP = Test::Pcuke::Gherkin::I18n->patterns( $language );
276 }
277  
278 12 ▶sub _skip { return sub {['SKIP']} }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _skip: called CC=1 SCAR=0.0
279  
280 16 ▶sub _mktoken_join {
Subroutine _mktoken_join: called CC=1 SCAR=0.0
281 16 my ($label) = @_;
282 22 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
283 22 my ($self, $line, @chunks) = @_;
284 22 [$label, join(' ', @chunks) ]
285 }
286 16 }
287  
288 6 ▶sub _mktoken_uplevel {
Subroutine _mktoken_uplevel: called CC=1 SCAR=0.0
289 2 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
290 2 my ($self, $line, @chunks) = @_;
291 2 $self->_level_up;
292 2 return @ { $self->_scan_line($line) };
293 }
294 6 }
295  
296 4 ▶sub _mktoken_perchunk {
Subroutine _mktoken_perchunk: called CC=1 SCAR=0.0
297 4 my ($label) = @_;
298 6 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
299 6 my ($self, $line, @chunks) = @_;
300 6 map {[$label,$_]} @chunks;
301 }
302 4 }
303  
304 4 ▶sub _mktoken_split {
Subroutine _mktoken_split: called CC=1 SCAR=0.0
305 4 my ($label, $re) = @_;
306 10 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
307 10 my ($self, $line, @chunks) = @_;
308 10 map {[$label, map { s/^\s*|\s*$//g; $_ } split /$re/i, $_]} @chunks;
309 }
310 4 }
311  
312 17 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod undocumented
313 17 my ($self) = @_;
314 17 $self->_set_levels( 'root' );
315 }
316  
317 17 ▶sub _set_levels {
Subroutine _set_levels: called CC=1 SCAR=0.0
318 17 my $self = shift;
319 17 @LEVELS = @_;
320 }
321  
322 20 ▶sub _append_levels {
Subroutine _append_levels: called CC=1 SCAR=0.0
323 20 my ($self, @levels) = @_;
324 20 push @LEVELS, @levels;
325 }
326  
327 2 ▶sub _level_up {
Subroutine _level_up: called CC=2 SCAR=7.1
328 2 my ($self) = @_;
329 2 ▶ if ( @LEVELS > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@LEVELS > 1) { } 2 0 2
330 2 pop @LEVELS;
331 }
332 else {
333 # confess can't go nowhere
334 }
335 }
336  
337 66 ▶sub _get_level { $LEVELS[-1] }
Subroutine _get_level: called CC=1 SCAR=0.0
338  
339 =head1 AUTHOR
340  
341 Andrei V. Toutoukine, C<< <tut at isuct.ru> >>
342  
343 =head1 BUGS
344  
345 Please report any bugs or feature requests to C<bug-/home/tut/bin/src/test-pcuke-gherkin at rt.cpan.org>, or through
346 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=/home/tut/bin/src/Test-Pcuke-Gherkin>. I will be notified, and then you'll
347 automatically be notified of progress on your bug as I make changes.
348  
349  
350  
351  
352 =head1 SUPPORT
353  
354 You can find documentation for this module with the perldoc command.
355  
356 perldoc Test::Pcuke::Gherkin::Lexer
357  
358  
359 You can also look for information at:
360  
361 =over 4
362  
363 =item * RT: CPAN's request tracker
364  
365 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=/home/tut/bin/src/Test-Pcuke-Gherkin>
366  
367 =item * AnnoCPAN: Annotated CPAN documentation
368  
369 L<http://annocpan.org/dist//home/tut/bin/src/Test-Pcuke-Gherkin>
370  
371 =item * CPAN Ratings
372  
373 L<http://cpanratings.perl.org/d//home/tut/bin/src/Test-Pcuke-Gherkin>
374  
375 =item * Search CPAN
376  
377 L<http://search.cpan.org/dist//home/tut/bin/src/Test-Pcuke-Gherkin/>
378  
379 =back
380  
381  
382 =head1 ACKNOWLEDGEMENTS
383  
384  
385 =head1 LICENSE AND COPYRIGHT
386  
387 Copyright 2011 Andrei V. Toutoukine.
388  
389 This program is released under the following license: artistic
390  
391  
392 =cut
393  
394 2 1; # End of Test::Pcuke::Gherkin::Lexer
« Gherkin/Node/Table.pm ↑ Summary Gherkin/Node/Background.pm »