File Coverage

blib/lib/Regexp/MultiLanguage.pm
Criterion Covered Total %
statement 15 36 41.6
branch 0 4 0.0
condition 0 5 0.0
subroutine 5 6 83.3
pod 1 1 100.0
total 21 52 40.3


line stmt bran cond sub pod time code
1             package Regexp::MultiLanguage;
2              
3 1     1   36947 use Parse::RecDescent;
  1         65067  
  1         8  
4              
5 1     1   48 use warnings;
  1         2  
  1         37  
6 1     1   5 use strict;
  1         6  
  1         152  
7              
8             our $parser;
9              
10             =head1 NAME
11              
12             Regexp::MultiLanguage - Convert common regular expressions checks
13             in to Perl, PHP, and JavaScript code.
14              
15             =head1 VERSION
16              
17             Version 0.03
18              
19             =cut
20              
21             our $VERSION = '0.03';
22              
23             =head1 SYNOPSIS
24              
25             Given a set of regular expressions in a simple format, this module writes
26             code for Perl, PHP, and JavaScript that uses those regular expressions.
27              
28             use Regexp::MultiLanguage qw(Perl JavaScript PHP);
29              
30             my $snippet = <<'END';
31             number : integer || binary
32            
33             integer : /\d+/
34             binary : /0b[01]+/i
35             END
36            
37             print "Perl: \n";
38             print Regexp::MultiLanguage->compile( $snippet, 'Perl', 'isa_' );
39            
40             print "\nJavaScript: \n";
41             print Regexp::MultiLanguage->compile( $snippet, 'JavaScript', 'isa_' );
42            
43             print "\nPHP: \n";
44             print Regexp::MultiLanguage->compile( $snippet, 'PHP', 'isa_' );
45            
46             =head1 FORMAT
47              
48             The format used is similar to L:
49              
50             name : expr
51            
52             where C is a boolean expression where each term is either another C or
53             a regular expression.
54              
55             =head1 FUNCTIONS
56              
57             =head2 compile
58              
59             Usage: Regexp::MultiLanguage->compile( $code, $language, [$function_prefix] );
60              
61             For each C in the L, generates one function whose name is
62             C<[$function_prefix]name>. These functions will compile in the language specified
63             (must be C, C, or C).
64              
65             =cut
66              
67             sub compile {
68 0     0 1 0 my $class = shift;
69 0         0 my $script = shift;
70 0         0 my $dialect = shift;
71 0   0     0 my $prefix = shift || '';
72            
73 0         0 my $di_obj = ('Regexp::MultiLanguage::'.$dialect)->new( 'prefix' => $prefix );
74            
75 0 0       0 unless ( $parser ) {
76              
77 0         0 $::RD_AUTOACTION = q
78             | my $d = $thisparser->{'local'}->{'dialect'};
79             #print $item[0], "\n";
80             if ( my $f = $d->can( $item[0] ) ) { $return = $d->$f( \%item ); }
81             else { $return = $item[ $#item ]; }
82             1; |;
83              
84             # see the __DATA__ section below for the grammar definition
85 0         0 my $fh;
86             {
87 1     1   5 no strict "refs";
  1         2  
  1         278  
  0         0  
88 0         0 $fh = \*{"Regexp::MultiLanguage::DATA"};
  0         0  
89             }
90 0         0 my $grammar = ''; my $line;
  0         0  
91 0   0     0 while ( defined( $line = <$fh> ) and $line !~ m/^__END__/ ) {
92 0         0 $grammar .= $line;
93             }
94 0         0 close Regexp::MultiLanguage::DATA;
95              
96 0         0 $parser = Parse::RecDescent->new( $grammar );
97             }
98            
99 0         0 $parser->{'local'}->{'dialect'} = $di_obj;
100            
101 0         0 return $parser->regex_file( $script );
102             }
103              
104             # import the following languages
105             sub import {
106 1     1   12 my $class = shift;
107 1         2 my $prefix = 'Regexp::MultiLanguage::';
108            
109 1         16 foreach ( @_ ) {
110 0           eval "require ${prefix}$_";
111 0 0         die $@ if $@;
112             }
113             }
114              
115             =head1 AUTHOR
116              
117             Robby Walker, robwalker@cpan.org
118              
119             =head1 BUGS
120              
121             Please report any bugs or feature requests to
122             C, or through the web interface at
123             L.
124             I will be notified, and then you'll automatically be notified of progress on
125             your bug as I make changes.
126              
127             =head1 TODO
128              
129             =over
130              
131             =item More tests.
132              
133             =item Allow named captures
134              
135             =item Allow matching against captures
136              
137             =back
138              
139             =head1 SUPPORT
140              
141             You can find documentation for this module with the perldoc command.
142              
143             perldoc Regexp::MultiLanguage
144              
145             You can also look for information at:
146              
147             =over 4
148              
149             =item * AnnoCPAN: Annotated CPAN documentation
150              
151             L
152              
153             =item * CPAN Ratings
154              
155             L
156              
157             =item * RT: CPAN's request tracker
158              
159             L
160              
161             =item * Search CPAN
162              
163             L
164              
165             =back
166              
167             =head1 ACKNOWLEDGEMENTS
168              
169             The development of this module was supported by L.
170              
171             =head1 SEE ALSO
172              
173             This module was developed for use in L - the multi-language validation solution.
174              
175             =head1 COPYRIGHT & LICENSE
176              
177             Copyright 2006 Robby Walker, all rights reserved.
178              
179             This program is free software; you can redistribute it and/or modify it
180             under the same terms as Perl itself.
181              
182             =cut
183              
184             1; # End of Regexp::MultiLanguage
185              
186             __DATA__