How does perl6 decide which proto
token
to match against first?
The below code works as expected, it matches the string 1234
, and Grammar::Tracer
shows that the first token matched against is s:sym<d>
, which makes sense since it's the longest token.
However, if I changed a literal to token, for example, changing token
three
form '3'
to <digit>
, it fails to match, and Grammar::Tracer
shows that s:sym<b>
is being matched against first.
Moving s:sym<d>
to the top, matches the string in both cases, but what is the explanation for that behavior?
#!/usr/bin/env perl6
no precompilation;
use Grammar::Tracer;
grammar G {
token TOP { <s> }
proto token s { * }
token s:sym<a> { <one> }
token s:sym<b> { <one> <two> }
token s:sym<c> { <one> <two> <three> }
token s:sym<d> { <one> <two> <three> <four> }
token one { '1' }
token two { '2' }
token three { '3' }
token four { '4' }
}
my $g = G.new;
say $g.parse: '1234';
# Output: Match
# token three { '3' }
TOP
| s
| | s:sym<d>
| | | one
# Output No Match
# token three { <digit> }
TOP
| s
| | s:sym<b>
| | | one