Perl 5 version 24.3 documentation

TAP::Parser::Grammar

NAME

TAP::Parser::Grammar - A grammar for the Test Anything Protocol.

VERSION

Version 3.36

SYNOPSIS

  1. use TAP::Parser::Grammar;
  2. my $grammar = $self->make_grammar({
  3. iterator => $tap_parser_iterator,
  4. parser => $tap_parser,
  5. version => 12,
  6. });
  7. my $result = $grammar->tokenize;

DESCRIPTION

TAP::Parser::Grammar tokenizes lines from a TAP::Parser::Iterator and constructs TAP::Parser::Result subclasses to represent the tokens.

Do not attempt to use this class directly. It won't make sense. It's mainly here to ensure that we will be able to have pluggable grammars when TAP is expanded at some future date (plus, this stuff was really cluttering the parser).

METHODS

Class Methods

new

  1. my $grammar = TAP::Parser::Grammar->new({
  2. iterator => $iterator,
  3. parser => $parser,
  4. version => $version,
  5. });

Returns TAP::Parser grammar object that will parse the TAP stream from the specified iterator. Both iterator and parser are required arguments. If version is not set it defaults to 12 (see set_version for more details).

Instance Methods

set_version

  1. $grammar->set_version(13);

Tell the grammar which TAP syntax version to support. The lowest supported version is 12. Although 'TAP version' isn't valid version 12 syntax it is accepted so that higher version numbers may be parsed.

tokenize

  1. my $token = $grammar->tokenize;

This method will return a TAP::Parser::Result object representing the current line of TAP.

token_types

  1. my @types = $grammar->token_types;

Returns the different types of tokens which this grammar can parse.

syntax_for

  1. my $syntax = $grammar->syntax_for($token_type);

Returns a pre-compiled regular expression which will match a chunk of TAP corresponding to the token type. For example (not that you should really pay attention to this, $grammar->syntax_for('comment') will return qr/^#(.*)/.

handler_for

  1. my $handler = $grammar->handler_for($token_type);

Returns a code reference which, when passed an appropriate line of TAP, returns the lexed token corresponding to that line. As a result, the basic TAP parsing loop looks similar to the following:

  1. my @tokens;
  2. my $grammar = TAP::Grammar->new;
  3. LINE: while ( defined( my $line = $parser->_next_chunk_of_tap ) ) {
  4. for my $type ( $grammar->token_types ) {
  5. my $syntax = $grammar->syntax_for($type);
  6. if ( $line =~ $syntax ) {
  7. my $handler = $grammar->handler_for($type);
  8. push @tokens => $grammar->$handler($line);
  9. next LINE;
  10. }
  11. }
  12. push @tokens => $grammar->_make_unknown_token($line);
  13. }

TAP GRAMMAR

NOTE: This grammar is slightly out of date. There's still some discussion about it and a new one will be provided when we have things better defined.

The TAP::Parser does not use a formal grammar because TAP is essentially a stream-based protocol. In fact, it's quite legal to have an infinite stream. For the same reason that we don't apply regexes to streams, we're not using a formal grammar here. Instead, we parse the TAP in lines.

For purposes for forward compatibility, any result which does not match the following grammar is currently referred to as TAP::Parser::Result::Unknown. It is not a parse error.

A formal grammar would look similar to the following:

  1. (*
  2. For the time being, I'm cheating on the EBNF by allowing
  3. certain terms to be defined by POSIX character classes by
  4. using the following syntax:
  5. digit ::= [:digit:]
  6. As far as I am aware, that's not valid EBNF. Sue me. I
  7. didn't know how to write "char" otherwise (Unicode issues).
  8. Suggestions welcome.
  9. *)
  10. tap ::= version? { comment | unknown } leading_plan lines
  11. |
  12. lines trailing_plan {comment}
  13. version ::= 'TAP version ' positiveInteger {positiveInteger} "\n"
  14. leading_plan ::= plan skip_directive? "\n"
  15. trailing_plan ::= plan "\n"
  16. plan ::= '1..' nonNegativeInteger
  17. lines ::= line {line}
  18. line ::= (comment | test | unknown | bailout ) "\n"
  19. test ::= status positiveInteger? description? directive?
  20. status ::= 'not '? 'ok '
  21. description ::= (character - (digit | '#')) {character - '#'}
  22. directive ::= todo_directive | skip_directive
  23. todo_directive ::= hash_mark 'TODO' ' ' {character}
  24. skip_directive ::= hash_mark 'SKIP' ' ' {character}
  25. comment ::= hash_mark {character}
  26. hash_mark ::= '#' {' '}
  27. bailout ::= 'Bail out!' {character}
  28. unknown ::= { (character - "\n") }
  29. (* POSIX character classes and other terminals *)
  30. digit ::= [:digit:]
  31. character ::= ([:print:] - "\n")
  32. positiveInteger ::= ( digit - '0' ) {digit}
  33. nonNegativeInteger ::= digit {digit}

SUBCLASSING

Please see SUBCLASSING in TAP::Parser for a subclassing overview.

If you really want to subclass TAP::Parser's grammar the best thing to do is read through the code. There's no easy way of summarizing it here.

SEE ALSO

TAP::Object, TAP::Parser, TAP::Parser::Iterator, TAP::Parser::Result,