| 1 | package Regexp::MultiLanguage; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Parse::RecDescent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | 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 | 2 | 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<Parse::RecDescent>: | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | name : expr | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | where C<expr> is a boolean expression where each term is either another C<name> 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<name> in the L<code/"FORMAT">, generates one function whose name is | ||||||||||||||||||||||||||||
| 62 | C<[$function_prefix]name>. These functions will compile in the language specified | ||||||||||||||||||||||||||||
| 63 | (must be C<Perl>, C<PHP>, or C<JavaScript>). | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub compile { | ||||||||||||||||||||||||||
|
Subroutine
compile: not called CC=5 SCAR=31.5
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 69 | 0 | my $script = shift; | |||||||||||||||||||||||||||
| 70 | 0 | my $dialect = shift; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | my $prefix = shift || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | my $di_obj = ('Regexp::MultiLanguage::'.$dialect)->new( 'prefix' => $prefix ); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | unless ( $parser ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 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 | my $fh; | |||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | no strict "refs"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 88 | 0 | $fh = \*{"Regexp::MultiLanguage::DATA"}; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 0 | my $grammar = ''; my $line; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | while ( defined( $line = <$fh> ) and $line !~ m/^__END__/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined($line = readline $fh) and not $line =~ /^__END__/
MC/DC0%0/2
defined($line = readline $fh) and not $line =~ /^__END__/
defined($line = readline $fh)
not $line =~ /^__END__/
Truth table0%0/3
A and not(B)
A= defined($line = readline $fh)B= not $line =~ /^__END__/
| |||||||||||||||||||||||||||||
| 92 | 0 | $grammar .= $line; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 0 | close Regexp::MultiLanguage::DATA; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | $parser = Parse::RecDescent->new( $grammar ); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | $parser->{'local'}->{'dialect'} = $di_obj; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | return $parser->regex_file( $script ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # import the following languages | ||||||||||||||||||||||||||||
| 105 | 1 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=15.4
| |||||||||||||||||||||||||||||
| 106 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 107 | 1 | my $prefix = 'Regexp::MultiLanguage::'; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1 | foreach ( @_ ) { | |||||||||||||||||||||||||||
| 110 | 0 | eval "require ${prefix}$_"; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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<bug-regexp-multilanguage at rt.cpan.org>, or through the web interface at | ||||||||||||||||||||||||||||
| 123 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Regexp-MultiLanguage>. | ||||||||||||||||||||||||||||
| 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<http://annocpan.org/dist/Regexp-MultiLanguage> | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | L<http://cpanratings.perl.org/d/Regexp-MultiLanguage> | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Regexp-MultiLanguage> | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | L<http://search.cpan.org/dist/Regexp-MultiLanguage> | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =back | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | The development of this module was supported by L<http://www.e-tutor.com>. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | This module was developed for use in L<REV> - 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 | 2 | 1; # End of Regexp::MultiLanguage | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | __DATA__ | ||||||||||||||||||||||||||||