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


Version 3.23


  use TAP::Parser::Grammar;
  my $grammar = $self−>make_grammar({
    iterator => $tap_parser_iterator,
    parser   => $tap_parser,
    version  => 12,
  my $result = $grammar−>tokenize;


"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).


Class Methods

  my $grammar = TAP::Parser::Grammar−>new({
      iterator => $iterator,
      parser   => $parser,
      version  => $version,

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


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.


  my $token = $grammar−>tokenize;

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


  my @types = $grammar−>token_types;

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


  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/^#(.*)/".


  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:

 my @tokens;
 my $grammar = TAP::Grammar−>new;
 LINE: while ( defined( my $line = $parser−>_next_chunk_of_tap ) ) {
     for my $type ( $grammar−>token_types ) {
         my $syntax  = $grammar−>syntax_for($type);
         if ( $line =~ $syntax ) {
             my $handler = $grammar−>handler_for($type);
             push @tokens => $grammar−>$handler($line);
             next LINE;
     push @tokens => $grammar−>_make_unknown_token($line);


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:

     For the time being, I'm cheating on the EBNF by allowing
     certain terms to be defined by POSIX character classes by
     using the following syntax:
       digit ::= [:digit:]
     As far as I am aware, that's not valid EBNF.  Sue me.  I
     didn't know how to write "char" otherwise (Unicode issues).
     Suggestions welcome.
 tap            ::= version? { comment | unknown } leading_plan lines
                    lines trailing_plan {comment}
 version        ::= 'TAP version ' positiveInteger {positiveInteger} "\n"
 leading_plan   ::= plan skip_directive? "\n"
 trailing_plan  ::= plan "\n"
 plan           ::= '1..' nonNegativeInteger
 lines          ::= line {line}
 line           ::= (comment | test | unknown | bailout ) "\n"
 test           ::= status positiveInteger? description? directive?
 status         ::= 'not '? 'ok '
 description    ::= (character − (digit | '#')) {character − '#'}
 directive      ::= todo_directive | skip_directive
 todo_directive ::= hash_mark 'TODO' ' ' {character}
 skip_directive ::= hash_mark 'SKIP' ' ' {character}
 comment        ::= hash_mark {character}
 hash_mark      ::= '#' {' '}
 bailout        ::= 'Bail out!' {character}
 unknown        ::= { (character − "\n") }
 (* POSIX character classes and other terminals *)
 digit              ::= [:digit:]
 character          ::= ([:print:] − "\n")
 positiveInteger    ::= ( digit − '0' ) {digit}
 nonNegativeInteger ::= digit {digit}


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.


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


Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.

Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.

Free Software

Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.

Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.

Free Books

The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.

Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.


Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.

Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.