Module Submission: Regexp::MultiLanguage



I'm working on a project where I need to use a set of common regular
expressions in more than one programming language. To facilitate this,
I've written Regexp::MultiLanguage which parses a simple language and
produces code that works in Perl, PHP, and JavaScript. (plus, adding
new languages is easy) Sound useful? Is this already implemented? Is
the name OK?

For example, here is a sample Regexp::MultiLanguage input:

number : integer || binary

integer : /\d+/
binary : /0b[01]+/i

I'd eventually like to extend this language to allow named regexp
captures and more, but for version 0.01 I'm keeping it simple.

I then use the following code (which assumes the above input is stored
in $snippet)

use Regexp::MultiLanguage qw(Perl JavaScript PHP);

print "Perl: \n";
print Regexp::MultiLanguage->compile( $snippet, 'Perl', 'isa_' );

print "\nJavaScript: \n";
print Regexp::MultiLanguage->compile( $snippet, 'JavaScript',
'isa_' );

print "\nPHP: \n";
print Regexp::MultiLanguage->compile( $snippet, 'PHP', 'isa_' );

This produces the following:

Perl:
sub isa_number { (isa_integer($_[0]) || isa_binary($_[0])) }

sub isa_integer { ($_[0] =~ m/\d+/) }

sub isa_binary { ($_[0] =~ m/0b[01]+/i) }

1;

JavaScript:
function isa_number(value) { return (isa_integer($_[0]) ||
isa_binary($_[0])) }

function isa_integer(value) { return (value.match(/\d+/) }

function isa_binary(value) { return (value.match(/0b[01]+/i) }

PHP:
<?php

function isa_number( $text ) { return (isa_integer( $text ) ||
isa_binary( $text )); }

function isa_integer( $text ) { return preg_match("/\\d+/", $text);
}

function isa_binary( $text ) { return preg_match("/0b[01]+/i",
$text); }


?>

I'd love to hear your comments.

Thanks,
- Robby

.



Relevant Pages

  • Re: Breaking backwards compatibility - good or bad?
    ... coming and are needed to anything really big and useful in PHP. ... command or statement in the language that is going to be made invalid ... The PHP team tries very hard not to break CORRECT scripts. ... "Introducing case-sensitivity for variables and functions names has ...
    (comp.lang.php)
  • Re: Similarities between PHP and Javascript in code?
    ... I just didnt need a server side language at ... One of the things that you need to be aware is that PHP and Javascript ... consistent, and appears to be the product of a single brain, which makes ...
    (comp.lang.php)
  • Re: Similarities between PHP and Javascript in code?
    ... interactivity on the site? ... I just didnt need a server side language at ... One of the things that you need to be aware is that PHP and Javascript ...
    (comp.lang.php)
  • Re: Case sensitivity in programming languages.
    ... the language. ... And these conventions take advantage of case sensitivity. ... In PHP it can be any of those things. ... group of programmers who want t force their stupid ideas onto others. ...
    (comp.lang.php)
  • Re: A Lot of Questions from a Noob
    ... OOP is a language paradigm that is not specific to ... I Google'd for an introduction to object-oriented programming, ... Watch out for the die-hard Perl fanbois: ... PHP 5 OOP has come a long way from PHP 4. ...
    (comp.lang.php)