perl-regexp-utils

Regexp::Parser::Perl58

A Regexp::Parser Subclass for Perl 5.8 Regular Expressions

SYNOPSIS

  use Regexp::Parser::Perl58;
  
  my $parser = Regexp::Parser::Perl58->new;
  
  my @error;
  
  ## Prepare a callback function invoked whenever an error or warning
  ## is raised by the parser.
  $parser->onerror (sub {
    my %args = @_;
    
    my $error_message = '';
    if ($args{level} eq 'w') {
      $error_message .= 'Warning: ';
    } else {
      $error_message .= 'Error: ';
    }
    
    $error_message .= sprintf $args{type}, @{$args{args}};
    
    $error_message .= ' (';
    $error_message .= substr ${$args{valueref}},
        $args{pos_start}, $args{pos_end} - $args{pos_start};
    $error_message .= ')';
    
    $error_message .= "\n";
    
    push @error, $error_message;
  });
  
  ## Parse the regular expression given as a string.
  ## Use |eval| to catch an exception that would be thrown if the
  ## regular expression contains a fatal error.
  eval {
    $parser->parse ($regexp_as_string);
  };
  
  if ($parser->errnum) {
    ## @error contains at least one fatal error.
    warn @error;
  } else {
    ## @error contains no fatal error.
    warn @error;
    
    ## Now, $parser->root contains the root node of the parsed regular
    ## expression tree.  See |perldoc Regexp::Parser| for more information.
  }

DESCRIPTION

The Regexp::Parser::Perl58 modules provides a subclass of the Regexp::Parser, a regular expression parser module. The Regexp::Parser::Perl58 module provides an implementation of the parser for the regular expression language as supported by Perl 5.8 (with some minor restrictions).

Although the Regexp::Parser itself provides the facility to parse Perl 5.8 regular expression, this module in addition enables the caller to specify a callback function that is invoked whenever the parser detects an error or wants to issue a warning message.

Apart from this additional function, this module provides the same interface as the one applied by the Regexp::Parser module. For more information, see Regexp::Parser.

METHODS

The Regexp::Parser::Perl58 module provides same methods as Regexp::Parser. In addition, the following method is defined:

[$onerror =] $parser->onerror ([$onerror]);

Gets or sets the callback function that is invoked when an error or warning is issued by the parser.

On setting, i.e. an argument is specified to the method, the argument is set as the error handler function. The argument must be a CODE reference or undef, which represents no-op.

On getting, i.e, no argument is specified to the method, the method returns the current error handler function, if it has been set, or returns undef.

The callback function, if specified, is invoked whenever the parser wants to report an error or warning to the application. When invoked, the callback function is given a set of key-value pairs describing the error or warning.

level => "m" / "w"

The level of the error. String m represents that it is a fatal error which prevents the regular expression from being effective. String w represents that it is a warning.

code => integer

The code that identifies the type of the error. See Regexp::Parser for the list of error codes.

type => string

The error type. The error type is a string that describes the type of the error as a simple English text. It can be used as the first argument of an sprintf call to obtain an error message, with the args argument, like:

  sprintf $args{type}, @{$args{args}}
args => ARRAY-ref

The optional values that describes the error or warning. The number and contents of the values depends on the type of the error.

valueref => SCALAR-ref-to-string

A reference to the string being parsed.

pos_start => integer

The index in the string referenced by valueref, from which the substring that causes the error or warning starts.

pos_end => integer

The index in the string referenced by valueref, at which the substring that causes the error or warning ends.

DEPENDENCY

This module requires Perl 5.10.0 or later.

This module depends on the Regexp::Parser module.

SEE ALSO

The latest version of this module is available at https://suika.suikawiki.org/regexp/.

Regular expression visualizer https://suika.suikawiki.org/regexp/visualizer/input. It uses this module to parse Perl 5.8 regular expressions.

Regexp::Parser - The superclass, available from CPAN.

Regexp::Parser::JavaScript - A parser for JavaScript regular expresssions.

DEVELOPMENT

CVS log: https://suika.suikawiki.org/regexp/lib/Regexp/Parser/Perl58.pm,cvslog.

Bug tracking system: http://manakai.g.hatena.ne.jp/task/7/.

Note that this module does not support Perl 5.10 regular expression extensions, as the module name suggests. They are expected to be supported by another module.

AUTHOR

Wakaba <wakaba@suikawiki.org>.

LICENSE

Copyright 2008-2009 Wakaba <wakaba@suikawiki.org>.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.