Here are two grammars. One uses proto token
, and the other does not. They both do the same. These are mainly examples in S05 in the section "Variable (non) interpolation" . In this simple example, both of them can do the same.
What situations justify all the extra typing? proto
tokens have different methods in the action class, and perhaps there is a slight advantage. However, you need to recruit some additional materials to get this benefit.
Is there some proto
feature that facilitates other parts of the grammar?
grammar NoProto { token variable { <sigil> <identifier> } token identifier { <ident>+ } token sigil { < $ @ % & :: > } } grammar YesProto { token variable { <sigil> <identifier> } token identifier { <ident>+ } proto token sigil { * } token sigil:sym<$> { <sym> } token sigil:sym<@> { <sym> } token sigil:sym<%> { <sym> } token sigil:sym<&> { <sym> } token sigil:sym<::> { <sym> } } class Proto::Actions { method variable ($/) { say "found variable: " ~ $/; } method identifier ($/) { say "found identifier: " ~ $/; } method sigil ($/) { say "found sigil: " ~ $/; } method sigil:sym<$> ($/) { say "found sym sigil: " ~ $/; } } my $variable = '$butterfuly'; say "------No proto parsing"; my $no_proto_match = NoProto.parse( $variable, :rule<variable>, :actions(Proto::Actions), ); say "------Yes proto parsing"; my $yes_proto_match = YesProto.parse( $variable, :rule<variable>, :actions(Proto::Actions), );
The result shows that proto
calls another method in the action class:
------No proto parsing found sigil: $ found identifier: butterfuly found variable: $butterfuly ------Yes proto parsing found sym sigil: $ found identifier: butterfuly found variable: $butterfuly
source share