Module Submission: Regexp::MultiLanguage

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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:

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

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

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


    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) }


    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.

- Robby

Re: Module Submission: Regexp::MultiLanguage writes:

Quoted text here. Click to load it

It sounds like it might be useful, and I've no issues with the name, but
it does sound rather incomplete. What about sed, Java, PCRE, etc?

More to the point I suppose, do you plan to provide a mechanism for users
to add new language support modules? Regexp::MultiLanguage::Java for
instance. If you do provide such a mechanism, then the languages that
happen to be supported by the "core" module becomes much less critical.


Cocoa programming in Perl:
Hire me! My resume:

Re: Module Submission: Regexp::MultiLanguage

Yes - it's easily extendable - there's 5 functions you have to write
for your Regexp::MultiLanguage::YourLanguage module.  Each 'dialect' is
generally only 20 lines long - at this point the three languages that
are there were just the top priorities.

- Robby

Sherm Pendley wrote:
Quoted text here. Click to load it

Site Timeline