The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Parse::Keyword - write syntax extensions in perl

VERSION

version 0.01

SYNOPSIS

  use Parse::Keyword { try => \&try_parser };

  sub try {
      my ($try, $catch) = @_;
      &Try::Tiny::try($try, ($catch ? (&Try::Tiny::catch($catch)) : ()));
  }

  sub try_parser {
      lex_read_space;
      die "syntax error" unless lex_peek eq '{';
      my $try = parse_block;
      lex_read_space;

      my $catch;
      if (lex_peek(6) =~ /^catch\b/) {
          lex_read(5);
          lex_read_space;
          die "syntax error" unless lex_peek eq '{';
          $catch = parse_block;
      }

      return (sub { ($try, $catch) }, 1);
  }

DESCRIPTION

This module allows you to write keyword-based syntax extensions without requiring you to write any C code yourself. It is similar to Devel::Declare, except that it uses the Perl parser API introduced in Perl 5.14 in order to allow you to parse parts of things using perl's own parser, rather than having to fake it with balanced brace matching or other fragile things.

To use this module, you should pass a hashref to the use statement. The keys of this hashref are subroutines in the current package which should have special parsing behavior attached to them, and the values are coderefs which should be used to implement the custom parsing behavior.

The parsing coderefs will be called when perl encounters a call to the keyword that you attached custom parsing to. The current parser state will be directly after parsing the keyword. Your parser function should return a coderef which, when called at runtime, will produce the arguments to the function. In addition, if your keyword should be parsed as a statement (for instance, if you don't want to require a trailing semicolon), you can return a second, true value.

In order to actually handle the parsing itself, this module also exports various parsing functions, which you can call. See below for details.

FUNCTIONS

lex_peek($n)

Returns a string consisting of the next $n characters in the input (or next one character, if $n isn't given). This string may be shorter than $n characters if there are fewer than $n characters remaining to read. The current position in the buffer to be parsed is not moved. See "PL_parser->linestr" in perlapi and "lex_next_chunk" in perlapi for more information.

lex_read($n)

Moves the current position in the parsing buffer forward by $n characters (or one character, if $n isn't given). See "lex_read_to" in perlapi for more details.

lex_read_space

Moves the current position in the parsing buffer forward past any whitespace or comments. See "lex_read_space" in perlapi for more details.

lex_stuff($str)

Inserts $str into the current parsing buffer at the current location, so that future calls to lex_peek and such will see it. Note that this does not move the current position in the parsing buffer, so multiple calls to lex_stuff at the same location will end up inserted into the buffer in reverse order. See "lex_stuff_sv" in perlapi for more information.

parse_block, parse_stmtseq, parse_fullstmt, parse_barestmt, parse_fullexpr, parse_listexpr, parse_termexpr, parse_arithexpr

These functions parse the specified amount of Perl code, and return a coderef which will evaluate that code when executed. See "parse_block" in perlapi, "parse_stmtseq" in perlapi, "parse_fullstmt" in perlapi, "parse_barestmt" in perlapi, "parse_fullexpr" in perlapi, parse_listexpr, parse_termexpr, and "parse_arithexpr" in perlapi for more details.

compiling_package

Returns the name of the package that the keyword which is currently being parsed was called in. This should be used instead of caller if you want to do something like install a subroutine in the calling package.

BUGS

This module inherits the limitation from Devel::CallParser that custom parsing is only triggered if the keyword is called by its unqualified name (try, not Try::try, for instance).

Please report any bugs to GitHub Issues at https://github.com/doy/parse-keyword/issues.

SEE ALSO

Devel::CallParser

Devel::Declare

SUPPORT

You can find this documentation for this module with the perldoc command.

    perldoc Parse::Keyword

You can also look for information at:

AUTHOR

Jesse Luehrs <doy@tozt.net>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2013 by Jesse Luehrs.

This is free software, licensed under:

  The MIT (X11) License