Perl 6/Raku中捕获和不捕获正则表达式作用域的差异 [英] Difference in capturing and non-capturing regex scope in Perl 6 / Raku

查看:77
本文介绍了Perl 6/Raku中捕获和不捕获正则表达式作用域的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管文档指出将令牌/规则/正则表达式称为<.foo>而不是<foo>使它们无法捕获,但似乎范围有所不同,但我不确定是否是预期的.

Although the docs state that calling a token/rule/regex as <.foo> instead of <foo> makes them non-capturing, it seems there is a difference in scope, but I'm not sure if it's intended.

这是一个简化的测试.在模块文件中:

Here is a simplified test. In a module file:

unit module Foo;
my token y           {     y  }
my token a is export { x  <y> }
my token b is export { x <.y> }

在另一个脚本文件中:

grammar A {
  use Foo;
  token TOP { <a> }
}

grammar B {
  use Foo;
  token TOP { <b> }
}

如果我们调用A.parse("xy"),一切将按预期运行.但是,调用B.parse("xy")会导致错误No such method 'y' for invocant of type 'B'.这是预期的行为还是潜在的错误?

If we calling A.parse("xy") everything runs as expected. However, calling B.parse("xy") results in the error No such method 'y' for invocant of type 'B'. Is this expected behavior or a potential bug?

推荐答案

每个S05的意图

根据相关的推测/设计文档包括:

<foo ...>

这种形式总是优先考虑一个词法范围的regex声明,就像它是一个函数一样直接分派给它.如果范围内没有这样的词法正则表达式(或词法方法),则假定存在一个正则表达式,则将调用分配给当前语法.

This form always gives preference to a lexically scoped regex declaration, dispatching directly to it as if it were a function. If there is no such lexical regex (or lexical method) in scope, the call is dispatched to the current grammar, assuming there is one.

...

前导.显式地将方法作为子规则调用;初始字符不是字母数字这一事实也导致命名的断言无法捕获其匹配的内容.

A leading . explicitly calls a method as a subrule; the fact that the initial character is not alphanumeric also causes the named assertion to not capture what it matches.

...

如果无法调用该名称的任何词法范围的例程,也无法通过方法分派访问该名称的任何方法,则对<foo>的调用将失败. (使用哪个调度程序的决定是在编译时决定的,而不是在运行时决定的;方法调用不是回退机制.)

A call to <foo> will fail if there is neither any lexically scoped routine of that name it can call, nor any method of that name that be reached via method dispatch. (The decision of which dispatcher to use is made at compile time, not at run time; the method call is not a fallback mechanism.)

表格示例

  • <bar>如上所述.它优先解析为名为&bar的早期绑定 lexical (my/our)例程/规则.否则,它解析为后期尝试调用名为bar has (has)方法/规则的尝试.如果成功,则将匹配项存储在名为bar的捕获下.

    Examples of forms

    • <bar> is as explained above. It preferentially resolves to an early bound lexical (my/our) routine/rule named &bar. Otherwise it resolves to a late bound attempt to call a has (has) method/rule named bar. If it succeeds it stores the match under a capture named bar.

      <.bar>调用名为bar has (has)方法/规则.它不会捕获.

      <.bar> calls a has (has) method/rule named bar if it finds one. It does not capture.

      <bar=.bar>,则调用一个名为bar has (has)方法/规则.如果成功,则将匹配项存储在名为bar的捕获下.换句话说,它与<bar>相同,只是它仅尝试调用名为.bar has 方法;它不会首先尝试解析为词法&bar.

      <bar=.bar> calls a has (has) method/rule named bar if it finds one. If it succeeds it stores the match under a capture named bar. In other words, it's the same as <bar> except it only attempts to call a has method named .bar; it doesn't first attempt to resolve to a lexical &bar.

      <&bar><.&bar>的含义相同.他们调用名为&bar的词法例程,并进行 not 捕获.要执行相同的操作但要捕获,请使用<bar=&bar><bar=.&bar>.

      <&bar> and <.&bar> mean the same thing. They call a lexical routine named &bar and do not capture. To do the same thing, but capture, use <bar=&bar> or <bar=.&bar>.

      (如果您阅读了上面链接的推测/设计文档并进行了尝试,您会发现Rakudo已经实现了文档提及的大多数设计细节,即使它们没有得到官方支持/烘焙/记录也是如此.)

      (If you read the speculation/design doc linked above and try things, you'll find most of the design details that doc mentions have already been implemented in Rakudo even if they're not officially supported/roasted/documented.)

      首先是常见情况:

      grammar c {
        has rule TOP { <bar> }
        has rule bar { . { say 'has rule' } }
      }
      say c.parse: 'a';
      

      显示:

      has rule
      「a」
       bar => 「a」
      

      (has声明符是可选的,习惯上将它们排除.)

      (The has declarators are optional and it's idiomatic to exclude them.)

      现在引入一个词法范围为语法块的规则:

      Now introducing a rule lexically scoped to the grammar block:

      grammar c {
        my  rule bar { . { say 'inner my rule' } }
        has rule TOP { <bar> }
        has rule bar { . { say 'has rule' } }
      }
      say c.parse: 'a';
      

      显示:

      inner my rule
      「a」
       bar => 「a」
      

      即使声明为 outside 以外的词汇规则,语法块也优先于 has 规则:

      Even a lexical rule declared outside the grammar block has precedence over has rules:

      my rule bar { . { say 'outer my rule' } }
      grammar c {
        has rule TOP { <bar> }
        has rule bar { . { say 'has rule' } }
      }
      say c.parse: 'a';
      

      显示:

      outer my rule
      「a」
       bar => 「a」
      

      这篇关于Perl 6/Raku中捕获和不捕获正则表达式作用域的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆