Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Perl 6 regexes
#1
This review of Perl 6 was posted yesterday: http://www.evanmiller.org/a-review-of-perl-6.html

I just wanted to highlight a few things, and how incredibly rad they look...
Quote:Character classes use <> instead of , and ranges use .. instead of [icode]-. So:
[a-zA-Z0-9]

is now:
<[ a..z A..Z 0..9 ]>

Capturing groups still use parentheses, but non-capturing groups now use plain square brackets. Spaces must now be made explicit. So this:

(Male|Female) (?:Cat|Dog)

is now:

(Male || Female) ' ' [Cat || Dog]

Modifiers (i for case-insenstive is the most important one) can appear inside a regex when preceded by a colon. For example, this Perl 5 regex:

(Male|Female) (?:[Cc][Aa][Tt]|[Dd][Oo][Gg])

would be translated to Perl 6 as:

(Male || Female) ' ' [:i cat || dog]

..snip..
Quote:The Perl 6 feature I was most excited to read about — in fact the initial reason I was drawn to Perl 6, aside from morbid curiosity — is the inclusion of grammars in the language. A good way to ease into grammars is to refactor a regular expression, for example like this:
my regex sex { "Male" || "Female" };
my regex species { :i "cat" || "dog" };

if "Male CAT" ~~ /<sex> \s <species>/ {
    say "Sex: " ~ $/<sex>;
    say "Species: " ~ $/<species>.lc;
}
(Tilde is Perl 6’s string concatenation operator; double-tilde is “smart match”, which in this context applies a regular expression.)

Next you can group regexes into a grammar block, providing a special regex named TOP, indicating where the parsing should begin:
grammar Animal {
    regex sex { "Male" || "Female" }
    regex species { :i "cat" || "dog" }

    regex TOP {
        <sex> \s <species>
    }
}

if my $result = Animal.parse("Male CAT") {
    say "Sex: " ~ $result<sex>;
    say "Species: " ~ $result<species>;
}
Because named regexes (or rules) can refer to each other, and to themselves, the Perl 6 grammar engine can be used to build recursive-descent parsers. I use Ragel for a number of parsing tasks, so I liked the idea of having a language that has something Ragel-ish built in — but I’m afraid I won’t be abandoning Ragel anytime soon, and won’t be migrating to Perl 6, at least not on account of its grammar engine. 

That grammar block is super cool. Using it as a way to have regular expressions that don't look like trash is very, very cool. They're powerful, too... further in the article, they explain that perl's compiler is written in these grammar blocks.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020