Abhijit A. Mahabal writes:
> As I try writing P6 programs I do find myself needing required named
> params, and I thought I'd like something like ++$named_req for myself as a
> shorthand for "+$named_req is required" or whatever is decided.
>
> Now, larry often says (only partly jokingly) "go write your own grammar".
> But I do not know how I'd change/modify/append the grammar to accomodate
> that cosmetic change above. Needing to know "the entire P6 grammar"
> isn't mouth watering. I am sure this has been discussed before, so can
> somebody please point me to some place where I can start reading about
> this?
Well, there's no official grammar for Perl 6 anywhere. The most
definitive source you can find is in A6. In that document we find:
rule parameter :w {
[ <type>? <zone>? <variable> <trait>* <defval>?
| \[ <signature> \] # treat a single array ref as an arg list
]
}
rule zone {
[ \? # optional positional
| \* # slurpy array or hash (or scalar)
| \+ # optional named-only
]
}
It would seem that this is the place to create your ++ zone. Here we
go:
grammar Grammar::ReqNamed {
is Grammar::Perl;
rule zone {
\+ <before: \+> {
.substr(.pos, 1) = '';
s/<after: <variable>>/is required/;
fail;
}
| <next>
}
}
That probably doesn't work. But it gets close. I'd much prefer to
parse it and then change the syntax tree in the walking phase, but I
don't know how to do that yet.
Luke