在Perl 6规则中,.parse锚点或:sigspace是否首先存在? [英] Does .parse anchor or :sigspace first in a Perl 6 rule?

查看:74
本文介绍了在Perl 6规则中,.parse锚点或:sigspace是否首先存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题.我显示的行为是否正确?如果正确,是否记录在某处?

I have two questions. Is the behavior I show correct, and if so, is it documented somewhere?

我正在使用语法TOP方法.声明为rule,它表示字符串的开始和结束锚以及:sigspace:

I was playing with the grammar TOP method. Declared as a rule, it implies beginning- and end-of-string anchors along with :sigspace:

grammar Number {
    rule TOP { \d+ }
    }

my @strings = '137', '137 ', ' 137 ';

for @strings -> $string {
    my $result = Number.parse( $string );
    given $result {
        when Match { put "<$string> worked!" }
        when Any   { put "<$string> failed!" }
        }
    }

在没有空格或仅尾部空格的情况下,字符串将进行解析.对于领先的空格,它会失败:

With no whitespace or trailing whitespace only, the string parses. With leading whitespace, it fails:

<137> worked!
<137 > worked!
< 137 > failed!

我认为这意味着rule首先应用:sigspace,然后再应用锚点:

I figure this means that rule is applying :sigspace first and the anchors afterward:

grammar Foo {
    regex TOP { ^ :sigspace \d+ $ }
    }

我希望rule允许前导空格,如果您切换顺序会发生这种情况:

I expected a rule to allow leading whitespace, which would happen if you switched the order:

grammar Foo {
    regex TOP { :sigspace ^  \d+ $ }
    }

我可以在rule中为字符串的开头添加一个显式标记:

I could add an explicit token in rule for the beginning of the string:

grammar Number {
    rule TOP { ^ \d+ }
    }

现在一切正常:

<137> worked!
<137 > worked!
< 137 > worked!

我没有任何理由认为这应该是一种方法. 语法文档表示发生了两件事,但是文档未说明这些效果的适用顺序:

I don't have any reason to think it should be one way or the other. The Grammars docs say two things happen, but the docs do not say which order these effects apply:

请注意,如果您使用.parse方法进行解析,则令牌TOP会自动定位

Note that if you're parsing with .parse method, token TOP is automatically anchored

使用规则而不是令牌时,原子后的任何空格都会变成对ws的非捕获调用.

When rule instead of token is used, any whitespace after an atom is turned into a non-capturing call to ws.


我认为答案是该规则实际上并没有固定在模式意义上.这就是.parse的工作方式.光标必须从位置0开始,到字符串的最后一个位置结束.那是模式之外的东西.


I think the answer is that the rule isn't actually anchored in the pattern sense. It's the way .parse works. The cursor has to start at position 0 and end at the last position in the string. That's something outside of the pattern.

推荐答案

没有两个正则表达式发生作用. rule适用:sigspace.之后,定义语法.调用.parse时,它从字符串的开头开始,然后到结尾(或失败).锚定不是语法的一部分.这是.parse应用语法的一部分.

There aren't two regex effects going on. The rule applies :sigspace. After that, the grammar is defined. When you call .parse, it starts at the beginning of the string and goes to the end (or fails). That anchoring isn't part of the grammar. It's part of how .parse applies the grammar.

我的主要问题是文档中某些词的奇怪用法.从技术上讲,它们不是错误的,但是它们也倾向于假定读者对某些知识可能不了解.在这种情况下,关于锚定TOP的随意评论并不像看起来那样特别.传递给.parse的任何规则都以相同的方式锚定.除了调用.parse:rule的默认值,该规则名称没有其他特殊行为.

My main issue was the odd way some of the things are worded in the docs. They aren't technically wrong, but they also tend to assume knowledge about things the reader might not know. In this case, the casual comment about anchoring TOP isn't as special as it seems. Any rule passed to .parse is anchored in the same way. There's no special behavior for that rule name other than it's the default value for :rule in a call to .parse.

这篇关于在Perl 6规则中,.parse锚点或:sigspace是否首先存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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