perl6 需要帮助以了解更多关于 proto regex/token/rule

perl6 Need help to understand more about proto regex/token/rule

以下代码摘自 the Perl 6 documentation,我想在更多实验之前了解更多信息:

proto token command {*}
      token command:sym<create>   { <sym> }
      token command:sym<retrieve> { <sym> }
      token command:sym<update>   { <sym> }
      token command:sym<delete>   { <sym> }
  1. 第一行的*是什么星?可以是别的东西吗,比如

    proto token command { /give me an apple/ }
    
  2. "sym"可以是别的东西吗,比如

    command:eat<apple> { <eat> } ?
    

{*} 告诉运行时调用正确的候选项。
编译器允许您将其缩短为 {*}

,而不是强制您为调用正确的常见情况编写 {{*}}

所有 proto 例程都是如此,例如 submethodregextokenrule

regex 原型例程的情况下,只允许裸 {*}
主要原因可能是因为 no-one 确实想出了一个很好的方法来使其在正则表达式 sub-language.

中合理地工作

所以这里有一个 proto sub 的例子,它做了一些对所有候选人都常见的事情。

#! /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 );
}

这回答了你的第二个问题。是的,已经晚了。 您必须区分两个不同的 sym(或 eat)。一个是关于令牌定义为 "adverb"(或扩展语法标识符,无论你想怎么称呼它),还有一个是在令牌本身上。

如果您在令牌正文中使用 <eat>,Perl 6 将根本找不到它。您将收到类似

的错误
No such method 'eat' for invocant of type 'Foo'

其中 Foo 是语法的名称。 <sym> is a predefined token,匹配token中副词的值(或对值)。

原则上,您可以使用扩展语法来定义多标记(或规则或正则表达式)。但是,如果您尝试以这种方式定义它,您将得到不同的错误:

Can only use <sym> token in a proto regex

那么,你第二个问题的答案是否定的,也不是。