blib/lib/TAP/Parser/Grammar.pm

filter: Statementstmt 98.6% 146 / 148 Branchbran 94.4% 34 / 36 Conditioncond 85.7% 12 / 14 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 33 / 33 Podpod 100% 5 / 5 totaltotal 96.7% 236 / 244 CC 25 SCAR 9.6
CC
25
Cov
97%
__ANON__
5.0
tokenize
5.0
_make_plan_token
4.0
CRAP 2.6
« blib/lib/TAP/Base.pm ↑ Summary blib/lib/TAP/Parser/SourceHandler/Handle.pm »
1 package TAP::Parser::Grammar;
2  
3 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 31 ▶use TAP::Parser::ResultFactory ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 31 ▶use TAP::Parser::YAMLish::Reader ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 31 ▶use base 'TAP::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 TAP::Parser::Grammar - A grammar for the Test Anything Protocol.
14  
15 =head1 VERSION
16  
17 Version 3.52
18  
19 =cut
20  
21 31 our $VERSION = '3.52';
22  
23 =head1 SYNOPSIS
24  
25 use TAP::Parser::Grammar;
26 my $grammar = $self->make_grammar({
27 iterator => $tap_parser_iterator,
28 parser => $tap_parser,
29 version => 12,
30 });
31  
32 my $result = $grammar->tokenize;
33  
34 =head1 DESCRIPTION
35  
36 C<TAP::Parser::Grammar> tokenizes lines from a L<TAP::Parser::Iterator> and
37 constructs L<TAP::Parser::Result> subclasses to represent the tokens.
38  
39 Do not attempt to use this class directly. It won't make sense. It's mainly
40 here to ensure that we will be able to have pluggable grammars when TAP is
41 expanded at some future date (plus, this stuff was really cluttering the
42 parser).
43  
44 =head1 METHODS
45  
46 =head2 Class Methods
47  
48 =head3 C<new>
49  
50 my $grammar = TAP::Parser::Grammar->new({
51 iterator => $iterator,
52 parser => $parser,
53 version => $version,
54 });
55  
56 Returns L<TAP::Parser> grammar object that will parse the TAP stream from the
57 specified iterator. Both C<iterator> and C<parser> are required arguments.
58 If C<version> is not set it defaults to C<12> (see L</set_version> for more
59 details).
60  
61 =cut
62  
63 # new() implementation supplied by TAP::Object
64 340 ▶sub _initialize {
Subroutine _initialize: called CC=3 SCAR=11.0
65 340 my ( $self, $args ) = @_;
66 340 $self->{iterator} = $args->{iterator}; # TODO: accessor
67 340 ▶ $self->{iterator} ||= $args->{stream}; # deprecated
Condition66%2/3
$self->{"iterator"} ||= $args->{"stream"}
l!l&&r!l&&!r
33802
MC/DC50%1/2
$self->{"iterator"} ||= $args->{"stream"}
$self->{"iterator"} $args->{"stream"}
Truth table66%2/3
A ||= B
A= $self->{"iterator"}B= $args->{"stream"}
ABresult
1X1
011
000
68 340 $self->{parser} = $args->{parser}; # TODO: accessor
69 340 ▶ $self->set_version( $args->{version} || 12 );
Condition100%2/2
$args->{'version'} || 12
l!l
3346
MC/DC100%1/1
$args->{'version'} || 12
$args->{'version'}
Truth table100%2/2
A
A= $args->{'version'}
Aresult
11
00
70 340 return $self;
71 }
72  
73 31 my %language_for;
74  
75 {
76  
77 # XXX the 'not' and 'ok' might be on separate lines in VMS ...
78 31 my $ok = qr/(?:not )?ok\b/;
79 31 my $num = qr/\d+/;
80  
81 my %v12 = (
82 version => {
83 syntax => qr/^TAP\s+version\s+(\d+)\s*\z/i,
84 30 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
85 30 my ( $self, $line ) = @_;
86 30 my $version = $1;
87 30 return $self->_make_version_token( $line, $version, );
88 },
89 },
90 plan => {
91 syntax => qr/^1\.\.(\d+)\s*(.*)\z/,
92 293 ▶ handler => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
93 293 my ( $self, $line ) = @_;
94 293 my ( $tests_planned, $tail ) = ( $1, $2 );
95 293 my $explanation = undef;
96 293 my $skip = '';
97  
98 293 ▶ if ( $tail =~ /^todo((?:\s+\d+)+)/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tail =~ /^todo((?:\s+\d+)+)/) { } 2 291 293
elsif (0 == $tests_planned) { } 9 282 291
elsif (not $tail =~ /^\s*$/) { } 0 282 282
99 2 my @todo = split /\s+/, _trim($1);
100 2 return $self->_make_plan_token(
101 $line, $tests_planned, 'TODO',
102 '', \@todo
103 );
104 }
105 elsif ( 0 == $tests_planned ) {
106 9 $skip = 'SKIP';
107  
108 # If we can't match # SKIP the directive should be undef.
109 9 ($explanation) = $tail =~ /^#\s*SKIP\S*\s+(.*)/i;
110 }
111 elsif ( $tail !~ /^\s*$/ ) {
112 0 return $self->_make_unknown_token($line);
113 }
114  
115 291 ▶ $explanation = '' unless defined $explanation;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $explanation 286 5 291
116  
117 291 return $self->_make_plan_token(
118 $line, $tests_planned, $skip,
119 $explanation, []
120 );
121  
122 },
123 },
124  
125 # An optimization to handle the most common test lines without
126 # directives.
127 simple_test => {
128 syntax => qr/^($ok) \ ($num) (?:\ ([^#]+))? \z/x,
129 12846 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
130 12846 my ( $self, $line ) = @_;
131 12846 my ( $ok, $num, $desc ) = ( $1, $2, $3 );
132  
133 12846 return $self->_make_test_token(
134 $line, $ok, $num,
135 $desc
136 );
137 },
138 },
139 test => {
140 syntax => qr/^($ok) \s* ($num)? \s* (.*) \z/x,
141 184 ▶ handler => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
142 184 my ( $self, $line ) = @_;
143 184 my ( $ok, $num, $desc ) = ( $1, $2, $3 );
144 184 my ( $dir, $explanation ) = ( '', '' );
145 184 ▶ if ($desc =~ m/^ ( [^\\\#]* (?: \\. [^\\\#]* )* )
Branch: 1 branch
BranchTrueFalseTotal
if ($desc =~ /^ ( [^\\\#]* (?: \\. [^\\\#]* )* ) \# \s* (SKIP|TODO) \b \s* (.*) $/xi) 107 77 184
146 \# \s* (SKIP|TODO) \b \s* (.*) $/ix
147 )
148 {
149 107 ( $desc, $dir, $explanation ) = ( $1, $2, $3 );
150 }
151 184 return $self->_make_test_token(
152 $line, $ok, $num, $desc,
153 $dir, $explanation
154 );
155 },
156 },
157 comment => {
158 syntax => qr/^#(.*)/,
159 111 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
160 111 my ( $self, $line ) = @_;
161 111 my $comment = $1;
162 111 return $self->_make_comment_token( $line, $comment );
163 },
164 },
165 bailout => {
166 syntax => qr/^\s*Bail out!\s*(.*)/,
167 7 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
168 7 my ( $self, $line ) = @_;
169 7 my $explanation = $1;
170 7 return $self->_make_bailout_token(
171 $line,
172 $explanation
173 );
174 },
175 },
176 31 );
177  
178 my %v13 = (
179 %v12,
180 plan => {
181 syntax => qr/^1\.\.(\d+)\s*(?:\s*#\s*SKIP\b(.*))?\z/i,
182 29 ▶ handler => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
183 29 my ( $self, $line ) = @_;
184 29 my ( $tests_planned, $explanation ) = ( $1, $2 );
185 29 ▶ my $skip
Branch: 1 branch
BranchTrueFalseTotal
0 == $tests_planned || defined $explanation ? : 5 24 29
Condition100%3/3
0 == $tests_planned or defined $explanation
l!l&&r!l&&!r
4124
MC/DC100%2/2
0 == $tests_planned or defined $explanation
0 == $tests_planned defined $explanation
Truth table100%3/3
A or B
A= 0 == $tests_plannedB= defined $explanation
ABresult
1X1
011
000
186 = ( 0 == $tests_planned || defined $explanation )
187 ? 'SKIP'
188 : '';
189 29 ▶ $explanation = '' unless defined $explanation;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $explanation 26 3 29
190 29 return $self->_make_plan_token(
191 $line, $tests_planned, $skip,
192 $explanation, []
193 );
194 },
195 },
196 yaml => {
197 syntax => qr/^ (\s+) (---.*) $/x,
198 10 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
199 10 my ( $self, $line ) = @_;
200 10 my ( $pad, $marker ) = ( $1, $2 );
201 10 return $self->_make_yaml_token( $pad, $marker );
202 },
203 },
204 pragma => {
205 syntax =>
206 qr/^ pragma \s+ ( [-+] \w+ \s* (?: , \s* [-+] \w+ \s* )* ) $/x,
207 7 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
208 7 my ( $self, $line ) = @_;
209 7 my $pragmas = $1;
210 7 return $self->_make_pragma_token( $line, $pragmas );
211 },
212 },
213 31 );
214  
215 %language_for = (
216 '12' => {
217 tokens => \%v12,
218 },
219 '13' => {
220 tokens => \%v13,
221 24 ▶ setup => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
222 24 shift->{iterator}->handle_unicode;
223 },
224 },
225 '14' => {
226 tokens => \%v13,
227 1 ▶ setup => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
228 1 shift->{iterator}->handle_unicode;
229 },
230 },
231 31 );
232 }
233  
234 ##############################################################################
235  
236 =head2 Instance Methods
237  
238 =head3 C<set_version>
239  
240 $grammar->set_version(13);
241  
242 Tell the grammar which TAP syntax version to support. The lowest
243 supported version is 12. Although 'TAP version' isn't valid version 12
244 syntax it is accepted so that higher version numbers may be parsed.
245  
246 =cut
247  
248 369 ▶sub set_version {
Subroutine set_version: called CC=3 SCAR=11.0
Pod documented
249 369 my $self = shift;
250 369 my $version = shift;
251  
252 369 ▶ if ( my $language = $language_for{$version} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $language = $language_for{$version}) { } 368 1 369
253 368 $self->{version} = $version;
254 368 $self->{tokens} = $language->{tokens};
255  
256 368 ▶ if ( my $setup = $language->{setup} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $setup = $language->{"setup"}) 25 343 368
257 25 $self->$setup();
258 }
259  
260 368 $self->_order_tokens;
261 }
262 else {
263 1 require Carp;
264 1 Carp::croak("Unsupported syntax version: $version");
265 }
266 }
267  
268 # Optimization to put the most frequent tokens first.
269 368 ▶sub _order_tokens {
Subroutine _order_tokens: called CC=1 SCAR=0.0
270 368 my $self = shift;
271  
272 368 my %copy = %{ $self->{tokens} };
273 1472 my @ordered_tokens = grep {defined}
274 368 map { delete $copy{$_} } qw( simple_test test comment plan );
275 368 push @ordered_tokens, values %copy;
276  
277 368 $self->{ordered_tokens} = \@ordered_tokens;
278 }
279  
280 ##############################################################################
281  
282 =head3 C<tokenize>
283  
284 my $token = $grammar->tokenize;
285  
286 This method will return a L<TAP::Parser::Result> object representing the
287 current line of TAP.
288  
289 =cut
290  
291 13891 ▶sub tokenize {
Subroutine tokenize: called CC=5 SCAR=16.1
Pod documented
292 13891 my $self = shift;
293  
294 13891 my $line = $self->{iterator}->next;
295 13889 ▶ unless ( defined $line ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 325 13564 13889
296 325 delete $self->{parser}; # break circular ref
297 325 return;
298 }
299  
300 13564 my $token;
301  
302 13564 for my $token_data ( @{ $self->{ordered_tokens} } ) {
303 15460 ▶ if ( $line =~ $token_data->{syntax} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$token_data->{"syntax"}/) 13513 1947 15460
304 13513 my $handler = $token_data->{handler};
305 13513 $token = $self->$handler($line);
306 13513 last;
307 }
308 }
309  
310 13564 ▶ $token = $self->_make_unknown_token($line) unless $token;
Branch: 1 branch
BranchTrueFalseTotal
unless $token 51 13513 13564
311  
312 13564 return $self->{parser}->make_result($token);
313 }
314  
315 ##############################################################################
316  
317 =head3 C<token_types>
318  
319 my @types = $grammar->token_types;
320  
321 Returns the different types of tokens which this grammar can parse.
322  
323 =cut
324  
325 2 ▶sub token_types {
Subroutine token_types: called CC=1 SCAR=0.0
Pod documented
326 2 my $self = shift;
327 2 return keys %{ $self->{tokens} };
328 }
329  
330 ##############################################################################
331  
332 =head3 C<syntax_for>
333  
334 my $syntax = $grammar->syntax_for($token_type);
335  
336 Returns a pre-compiled regular expression which will match a chunk of TAP
337 corresponding to the token type. For example (not that you should really pay
338 attention to this, C<< $grammar->syntax_for('comment') >> will return
339 C<< qr/^#(.*)/ >>.
340  
341 =cut
342  
343 8 ▶sub syntax_for {
Subroutine syntax_for: called CC=1 SCAR=0.0
Pod documented
344 8 my ( $self, $type ) = @_;
345 8 return $self->{tokens}->{$type}->{syntax};
346 }
347  
348 ##############################################################################
349  
350 =head3 C<handler_for>
351  
352 my $handler = $grammar->handler_for($token_type);
353  
354 Returns a code reference which, when passed an appropriate line of TAP,
355 returns the lexed token corresponding to that line. As a result, the basic
356 TAP parsing loop looks similar to the following:
357  
358 my @tokens;
359 my $grammar = TAP::Grammar->new;
360 LINE: while ( defined( my $line = $parser->_next_chunk_of_tap ) ) {
361 for my $type ( $grammar->token_types ) {
362 my $syntax = $grammar->syntax_for($type);
363 if ( $line =~ $syntax ) {
364 my $handler = $grammar->handler_for($type);
365 push @tokens => $grammar->$handler($line);
366 next LINE;
367 }
368 }
369 push @tokens => $grammar->_make_unknown_token($line);
370 }
371  
372 =cut
373  
374 8 ▶sub handler_for {
Subroutine handler_for: called CC=1 SCAR=0.0
Pod documented
375 8 my ( $self, $type ) = @_;
376 8 return $self->{tokens}->{$type}->{handler};
377 }
378  
379 30 ▶sub _make_version_token {
Subroutine _make_version_token: called CC=1 SCAR=0.0
380 30 my ( $self, $line, $version ) = @_;
381 return {
382 30 type => 'version',
383 raw => $line,
384 version => $version,
385 };
386 }
387  
388 322 ▶sub _make_plan_token {
Subroutine _make_plan_token: called CC=4 SCAR=13.9
389 322 my ( $self, $line, $tests_planned, $directive, $explanation, $todo ) = @_;
390  
391 322 ▶ if ( $directive eq 'SKIP'
Branch: 1 branch
BranchTrueFalseTotal
if ($directive eq "SKIP" and 0 != $tests_planned and $self->{"version"} < 13) 1 321 322
Condition83%5/6
$directive eq "SKIP" and 0 != $tests_planned and $self->{"version"} < 13
!ll&&!rl&&r
32101
$directive eq "SKIP" and 0 != $tests_planned
!ll&&!rl&&r
308131
MC/DC66%2/3
$directive eq "SKIP" and 0 != $tests_planned and $self->{"version"} < 13
$directive eq "SKIP" 0 != $tests_planned $self->{"version"} < 13
Truth table75%3/4
A and B and C
A= $directive eq "SKIP"B= 0 != $tests_plannedC= $self->{"version"} < 13
ABCresult
0XX0
10X0
1100
1111
392 && 0 != $tests_planned
393 && $self->{version} < 13 )
394 {
395 1 warn
396 "Specified SKIP directive in plan but more than 0 tests ($line)\n";
397 }
398  
399 return {
400 322 type => 'plan',
401 raw => $line,
402 tests_planned => $tests_planned,
403 directive => $directive,
404 explanation => _trim($explanation),
405 todo_list => $todo,
406 };
407 }
408  
409 13030 ▶sub _make_test_token {
Subroutine _make_test_token: called CC=3 SCAR=11.0
410 13030 my ( $self, $line, $ok, $num, $desc, $dir, $explanation ) = @_;
411 return {
412 13030 ▶ ok => $ok,
Branch: 2 branches
BranchTrueFalseTotal
defined $num ? : 12978 52 13030
defined $dir ? : 184 12846 13030
413  
414 # forcing this to be an integer (and not a string) reduces memory
415 # consumption. RT #84939
416 test_num => ( defined $num ? 0 + $num : undef ),
417 description => _trim($desc),
418 directive => ( defined $dir ? uc $dir : '' ),
419 explanation => _trim($explanation),
420 raw => $line,
421 type => 'test',
422 };
423 }
424  
425 51 ▶sub _make_unknown_token {
Subroutine _make_unknown_token: called CC=1 SCAR=0.0
426 51 my ( $self, $line ) = @_;
427 return {
428 51 raw => $line,
429 type => 'unknown',
430 };
431 }
432  
433 111 ▶sub _make_comment_token {
Subroutine _make_comment_token: called CC=1 SCAR=0.0
434 111 my ( $self, $line, $comment ) = @_;
435 return {
436 111 type => 'comment',
437 raw => $line,
438 comment => _trim($comment)
439 };
440 }
441  
442 7 ▶sub _make_bailout_token {
Subroutine _make_bailout_token: called CC=1 SCAR=0.0
443 7 my ( $self, $line, $explanation ) = @_;
444 return {
445 7 type => 'bailout',
446 raw => $line,
447 bailout => _trim($explanation)
448 };
449 }
450  
451 10 ▶sub _make_yaml_token {
Subroutine _make_yaml_token: called CC=1 SCAR=0.0
452 10 my ( $self, $pad, $marker ) = @_;
453  
454 10 my $yaml = TAP::Parser::YAMLish::Reader->new;
455  
456 10 my $iterator = $self->{iterator};
457  
458 # Construct a reader that reads from our input stripping leading
459 # spaces from each line.
460 10 my $leader = length($pad);
461 10 my $strip = qr{ ^ (\s{$leader}) (.*) $ }x;
462 10 my @extra = ($marker);
463 50 ▶ my $reader = sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
464 50 ▶ return shift @extra if @extra;
Branch: 1 branch
BranchTrueFalseTotal
if @extra 10 40 50
465 40 my $line = $iterator->next;
466 40 ▶ return $2 if $line =~ $strip;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$strip/ 40 0 40
467 0 return;
468 10 };
469  
470 10 my $data = $yaml->read($reader);
471  
472 # Reconstitute input. This is convoluted. Maybe we should just
473 # record it on the way in...
474 10 chomp( my $raw = $yaml->get_raw );
475 10 $raw =~ s/^/$pad/mg;
476  
477 return {
478 10 type => 'yaml',
479 raw => $raw,
480 data => $data
481 };
482 }
483  
484 7 ▶sub _make_pragma_token {
Subroutine _make_pragma_token: called CC=1 SCAR=0.0
485 7 my ( $self, $line, $pragmas ) = @_;
486 return {
487 7 type => 'pragma',
488 raw => $line,
489 pragmas => [ split /\s*,\s*/, _trim($pragmas) ],
490 };
491 }
492  
493 26509 ▶sub _trim {
Subroutine _trim: called CC=2 SCAR=6.9
494 26509 my $data = shift;
495  
496 26509 ▶ return '' unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 13316 13193 26509
497  
498 13193 $data =~ s/^\s+//;
499 13193 $data =~ s/\s+$//;
500 13193 return $data;
501 }
502  
503 31 1;
504  
505 =head1 TAP GRAMMAR
506  
507 B<NOTE:> This grammar is slightly out of date. There's still some discussion
508 about it and a new one will be provided when we have things better defined.
509  
510 The L<TAP::Parser> does not use a formal grammar because TAP is essentially a
511 stream-based protocol. In fact, it's quite legal to have an infinite stream.
512 For the same reason that we don't apply regexes to streams, we're not using a
513 formal grammar here. Instead, we parse the TAP in lines.
514  
515 For purposes for forward compatibility, any result which does not match the
516 following grammar is currently referred to as
517 L<TAP::Parser::Result::Unknown>. It is I<not> a parse error.
518  
519 A formal grammar would look similar to the following:
520  
521 (*
522 For the time being, I'm cheating on the EBNF by allowing
523 certain terms to be defined by POSIX character classes by
524 using the following syntax:
525  
526 digit ::= [:digit:]
527  
528 As far as I am aware, that's not valid EBNF. Sue me. I
529 didn't know how to write "char" otherwise (Unicode issues).
530 Suggestions welcome.
531 *)
532  
533 tap ::= version? { comment | unknown } leading_plan lines
534 |
535 lines trailing_plan {comment}
536  
537 version ::= 'TAP version ' positiveInteger {positiveInteger} "\n"
538  
539 leading_plan ::= plan skip_directive? "\n"
540  
541 trailing_plan ::= plan "\n"
542  
543 plan ::= '1..' nonNegativeInteger
544  
545 lines ::= line {line}
546  
547 line ::= (comment | test | unknown | bailout ) "\n"
548  
549 test ::= status positiveInteger? description? directive?
550  
551 status ::= 'not '? 'ok '
552  
553 description ::= (character - (digit | '#')) {character - '#'}
554  
555 directive ::= todo_directive | skip_directive
556  
557 todo_directive ::= hash_mark 'TODO' ' ' {character}
558  
559 skip_directive ::= hash_mark 'SKIP' ' ' {character}
560  
561 comment ::= hash_mark {character}
562  
563 hash_mark ::= '#' {' '}
564  
565 bailout ::= 'Bail out!' {character}
566  
567 unknown ::= { (character - "\n") }
568  
569 (* POSIX character classes and other terminals *)
570  
571 digit ::= [:digit:]
572 character ::= ([:print:] - "\n")
573 positiveInteger ::= ( digit - '0' ) {digit}
574 nonNegativeInteger ::= digit {digit}
575  
576 =head1 SUBCLASSING
577  
578 Please see L<TAP::Parser/SUBCLASSING> for a subclassing overview.
579  
580 If you I<really> want to subclass L<TAP::Parser>'s grammar the best thing to
581 do is read through the code. There's no easy way of summarizing it here.
582  
583 =head1 SEE ALSO
584  
585 L<TAP::Object>,
586 L<TAP::Parser>,
587 L<TAP::Parser::Iterator>,
588 L<TAP::Parser::Result>,
589  
590 =cut
« blib/lib/TAP/Base.pm ↑ Summary blib/lib/TAP/Parser/SourceHandler/Handle.pm »