On Mon, 05 Dec 2016 22:58:27 -0800, rongshe...@gmail.com wrote: > ------------------------------------------------------------ > --------------------- > > Okay, I am still having trouble with perl6 grammar and action. I want to > find a pattern in a string, and as soon as it is is found, change the > pattern according to action, and return the modified string. > > my $test = "xx, 1-March-23, 23.feb.21, yy foo 12/january/2099 , > zzz";# want this result: xx, 010323, 230221, yy foo 120199 , zzz"; # > 2 digits for day, month, year > > grammar month { > regex TOP { <unit>+ } > regex unit { <before> <form1> <after> } > regex before { .*? } > regex after { .*? }
Both `before` and `after` happen to be the names of a couple of the standard rules (for doing lookahead and lookbehind), inherited from the base Grammar class. In theory overriding these as you have is fine enough (until you try to use the built-in ones and then get confused because yours took precedence, at least ;-)). In practice, part of the regex compiler treated the name `before` specially and then got very upset when it was not something of the form `<before some regex here>`. I've now made it more robust in https://github.com/perl6/nqp/commit/768b20d9c756c636f58bc4d131e12e3ec60eebfa and with that patch your grammar seems to work nicely. What needs a bit more discussion is whether we'd like to simply leave it at that and add a spectest to codify that overriding before/after this way is fine (I'm good with this), or treat those two names a bit more specially (but then where do we draw the line on which built-ins get such treatment?) Thanks, /jnthn