Perl6 Need help to learn more about proto regex / token / rule

The following code is taken from the Perl 6 documentation , and I'm trying to learn more about it before experimenting:

proto token command {*} token command:sym<create> { <sym> } token command:sym<retrieve> { <sym> } token command:sym<update> { <sym> } token command:sym<delete> { <sym> } 
  1. Is * on the first line a star? Could it be something else, for example,

     proto token command { /give me an apple/ } 
  2. Could sym be something else, like

     command:eat<apple> { <eat> } ? 
+7
source share
2 answers

{*} reports that the correct candidate will call the runtime.
Instead of forcing you to write {{*}} for the normal case, simply invoking the correct one, the compiler allows you to shorten it to {*}

This is true for all proto routines, such as sub , method , regex , token and rule .

For prototype regex , only bare {*} allowed.
The main reason is probably because someone really didn’t have a good way to make him work reasonably in a regular expression sublanguage.

So here is a proto sub example that does some things that are common to all candidates.

 #! /usr/bin/env perl6 use v6.c; for @*ARGS { $_ = '--stdin' when '-' } # find out the number of bytes proto sub MAIN (|) { try { # {*} calls the correct multi # then we get the number of elems from its result # and try to say it say {*}.elems # <------------- } # if {*} returns a Failure note the error message to $*ERR or note $!.message; } #| the number of bytes on the clipboard multi sub MAIN () { xclip } #| the number of bytes in a file multi sub MAIN ( Str $filename ){ $filename.IO.slurp(:!chomp,:bin) } #| the number of bytes from stdin multi sub MAIN ( Bool :stdin($)! ){ $*IN.slurp-rest(:bin) } sub xclip () { run( «xclip -o», :out ) .out.slurp-rest( :bin, :close ); } 
+9
source

This answers your second question. It's too late. You must distinguish between two different sym (or eat ). The one that by the definition of the token as an "adverb" (or the extended syntactic identifier, as you want to call it), and the one that belongs to the token itself.

If you use <eat> in the body of the token, Perl 6 simply will not find it. You will get an error like

 No such method 'eat' for invocant of type 'Foo' 

Where Foo is the name of the grammar. <sym> is a predefined token that matches the value of the adverb (or value of a pair) in the token.

Basically, you can use the extended syntax to define a multi-token (or rule, or regular expression). However, if you try to define it this way, you will get another error:

 Can only use <sym> token in a proto regex 

So, the answer to your second question is no, and no.

+1
source

Source: https://habr.com/ru/post/1264332/


All Articles