Austin Hastings wrote:

Another example. Let's say there's a class that deals with colors.
It has an operator that returns true if two colors look about the same. Given a list of color objects, is there a regexp to find a
rainbow? Even if the color class doesn't support stringification?



Yes.


grammar Rainbow;

rule color {...}; # this one's on you.

rule same_color($color is Colorific)
{
 <color> ::: { fail unless $1.looks_like($color); }
}

rule band($color is Colorific)
{
 <same_color($color)>+
}

rule Rainbow
{
<band(new Color("red"))>
<band(new Color("orange"))>
<band(new Color("yellow"))>
<band(new Color("green"))>
<band(new Color("blue"))>
<band(new Color("indigo"))>
<band(new Color("violet"))>
<pot_o_gold>?
}




I'm a bit confused by the C<same_color> rule; specifically, this line:

$1.looks_like($color)

Shouldn't this be: C<< $color.looks_like($1) >> ?  Otherwise, it
suggests that you're redefining the match object class, which
probably isn't a good idea.


Joseph F. Ryan [EMAIL PROTECTED]



Reply via email to