I have two questions. Is the behavior that I'm showing right, and if so, is it documented somewhere?
I played with the TOP grammar method. Declared as rule , it implies bindings of the beginning and end of the string along with :sigspace :
grammar Number { rule TOP { \d+ } } my @strings = '137', '137 ', ' 137 '; for @strings -> $string { my $result = Number.parse( $string ); given $result { when Match { put "<$string> worked!" } when Any { put "<$string> failed!" } } }
Without whitespace or trailing spaces, the string parses. With leading spaces, it fails:
<137> worked! <137 > worked! < 137 > failed!
I suppose this means that rule first applies :sigspace , and then binds:
grammar Foo { regex TOP { ^ :sigspace \d+ $ } }
I was expecting the rule allow spaces, which will happen if you reorder:
grammar Foo { regex TOP { :sigspace ^ \d+ $ } }
I could add an explicit token to the rule to start the line:
grammar Number { rule TOP { ^ \d+ } }
Now everything works:
<137> worked! <137 > worked! < 137 > worked!
I have no reason to think that it should be anyway. Grammars docs say two things happen, but the docs don't say what order these effects apply:
Please note: if you deal with the .parse method, the TOP token is automatically bound
and
When a rule is used instead of a token, any spaces after the atom turn into an unclaimed ws call.
I think the answer is that the rule is not actually fixed in the sense of the template. This works .parse . The cursor should start at position 0 and end at the last position of the line. Something outside the template.