Dave Whipp writes:
> grammar Letter {
>     rule greet :w { <greet_word> $to:=(\S+?) , $$}
>     rule greet_word { [Hi|Hey|Yo] }
>     ...
> }
> 
> grammar FormalLetter is Letter {
>     rule greet_word{ Dear }
>     ...
> }
> 
> Will the :w do the right thing here?

In the new S5 revision, :w changed from static to dynamic.  :w is about
the text now, not the rules.  So, yes, it will.

> My second though was that inheritance might be overused here. 

Perhaps, perhaps not.  In the multimethod paper that's coming soon (it's
turned out to be a lot more than just a multimethod paper; more of an
object model paper), inheritance may become much more useful than it
once was, at least in an abstract sense.  In particular, subtypes can be
a form of inheritance in the system.  And the best part is that we don't
have to change anything from S12 to apply the model.

But that's beside the point.   I just had to give all y'all a little
preview.

It's my suspicion that the template method pattern is often going to be
the right thing to do in rules.

> My refactoring was the "template method" pattern. How would I use a
> strategy instead. I know I can define a rule that accepts a parameter,
> but if I did that then I'd need to pass that parameter all the way
> down the tree. Is it possible to write something like
> 
>   $letter =~ /<(Letter but greet_word(rule :w { Guten Tag })).text>;

Hmm... this pattern seems a lot kludgier for this situation.  You could
use a parameterized grammar, I suppose:

    grammar Letter[?$greeter = / [Hi|Hey|Yo] /] {
        rule greet :w { <$greeter> $Âto := (\S+?) , $$ }
    }

Which may be a better solution if you're expecting a lot of different
types.

By the way, you can't say:

    $letter ~~ /<( ...

Because <( starts a conditional assertion.  The usage becomes:

    $letter ~~ /<Letter[/:w Guten Tag /].greet>/

Luke

Reply via email to