perl-regexp-utils

Regexp::Parser::JavaScript

A Regexp::Parser Subclass for JavaScript Regular Expressions

SYNOPSIS

  use Regexp::Parser::JavaScript;
  
  my $parser = Regexp::Parser::JavaScript->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::JavaScript modules provides a subclass of the Regexp::Parser, a regular expression parser module. The Regexp::Parser::JavaScript module provides an implementation of the parser for the regular expression language as defined by ECMA 376 Third Edition (the ECMAScript specification), modified for compatibility with Web browser implementations.

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

METHODS

The Regexp::Parser::JavaScript module provides same methods as Regexp::Parser::JavaScript. See Regexp::Parser::Perl58.

ERROR TYPES

The Regexp::Parser::JavaScript module reports same kinds of errors as the Regexp::Parser module does to the callback function specified by the onerror method, if any. In addition, it might also have an additional error type: RPJSe_OCTESC.

RPJSe_OCTESC

This error is reported when an octal escape sequence is used in the regular expression. The octal escape sequence is not part of the ECMAScript specification though it is supported by Web browsers for backward compatibility.

DEPENDENCY

This module requires Perl 5.10.0 or later.

This module depends on Regexp::Parser and Regexp::Parser::Perl58 modules.

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 - A superclass, available from CPAN.

Regexp::Parser::Perl58 - A superclass.

DEVELOPMENT

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

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

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.