在 perl6 语法中放松空格的最佳方法是什么? [英] What's the best way to be lax on whitespace in a perl6 grammar?

查看:29
本文介绍了在 perl6 语法中放松空格的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个无论是否存在空格都宽松的语法......我想匹配:

I want to have a grammar that is lax in whether whitespace is present or not... I want to match:

this                '   <foo>    <bar>    <baz>    '
and also this       '<foo><bar><baz>'

这有效:

token TOP     { \s* <foo> \s* <bar> \s* <baz> \s* }

但是在阅读了关于 :sigspace、 <.ws>rule 的所有内容后,我可以想象有一种方法可以做到这一点没有重复的 *\s .(即.如何匹配 per6 中的十六进制数组语法)

But after reading all about :sigspace, <.ws> and rule I can imagine that there is a way to do this without the repeated *\s . (viz. How do I match a hex array in per6 grammar)

请有人告诉我在 perl6 语法中是否有更好的方法来做到这一点?

Please can someone tell me if there is nicer way to do this in a perl6 grammar?

注意.这不能通过简单地将 token 声明符更改为 rule 来解决 - 当我尝试这种方法时,我最终 匹配空格或不匹配空格(但是不是 both) 在解析字符串中.

NB. this is not solved by simply changing the token declarator to rule - when I try that approach I end up either matching space or no space (but not both) in the parse string.

推荐答案

也许你的问题是这三个规则gotchyas"之一:

Perhaps your problem is one these three rule "gotchyas":

  • 如果你想在规则的开始处匹配空白/标记边界,第一个原子之前,您必须显式提供它(通常使用显式<.ws>).

  • If you want white space / token boundary matching at the start of a rule, before the first atom, you must explicitly provide it (typically with an explicit <.ws>).

如果您想要在量化原子的每个匹配项之间匹配之间的空白/标记边界(例如<foo>*) 您必须在原子和量词之间包含空格(例如 *).

If you want white space / token boundary matching between each of the matches of a quantified atom (eg <foo>*) you must include space between the atom and the quantifier (eg <foo> *).

默认的 定义为 regex ws { <!ww>\s* }.如果您希望特定语法中的 rule 使用不同的模式,请在该语法中定义您自己的模式.(timotimo++)

The default <ws> is defined as regex ws { <!ww> \s* }. If you want rules in a particular grammar to use a different pattern, then define your own in that grammar. (timotimo++)

有关上述内容的进一步讨论,请参阅我对 如何匹配 per6 语法中的十六进制数组.

For further discussion of the above, see my updated answer to How do I match a hex array in per6 grammar.

以下四个正则表达式匹配您的两个示例字符串:

The following four regexes match both your sample strings:

my \test-strings := '   <foo>    <bar>    <baz>    ', '<foo><bar><baz>';

my \test-regexes := token { \s*   '<foo>' \s* '<bar>' \s* '<baz>' \s* },
                    rule  { \s*   '<foo>' \s* '<bar>' \s* '<baz>' \s* },
                    rule  { \s*   '<foo>'     '<bar>'     '<baz>'     },
                    rule  { <.ws> '<foo>'     '<bar>'     '<baz>'     }

say (test-strings X~~ test-regexes).all ~~ Match # True

这篇关于在 perl6 语法中放松空格的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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