我可以在方法中更改 Perl 6 俚语吗?

编程入门 行业动态 更新时间:2024-10-28 21:20:56
本文介绍了我可以在方法中更改 Perl 6 俚语吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Perl 6 Regex 是一种更具体的 方法,所以我有一个想法,也许我可以在产生同样事情的常规方法中做一些黑魔法的事情.我特别好奇在不改变任何语法的情况下这样做.

A Perl 6 Regex is a more specific type of Method, so I had the idea that maybe I could do something black-magicky in a regular method that produces the same thing. I particularly am curious about doing this without changing any grammars.

但是,查看 Perl6/Grammar.nqp(我几乎不明白),这真的不是继承的东西.我认为,根据我的阅读,Perl 6 语法在看到正则表达式声明符之一时会切换俚语(子语言).也就是说,不同的语法解析了 regex { ... } 和 method {...} 的内容.

However, looking at Perl6/Grammar.nqp (which I barely understand), that this is really not an inheritance thing. I think, based on my reading, that the Perl 6 grammar switches slangs (sub languages) when it sees one of the regex declarators. That is, a different grammar parses the guts of regex { ... } and method {...}.

那么,首先,是吗?

然后,只是为了傻笑,我想也许我可以在一个方法块中,但告诉它使用不同的俚语(例如,参见 "Slangs" 来自 2013 Perl 6 Advent Calendar 或 今日俚语").

Then, just for giggles, I thought that maybe I could be inside a method block but tell it to use a different slang (see for instance, "Slangs" from the 2013 Perl 6 Advent Calendar or "Slangs Today").

但是,我发现的所有内容看起来都想改变语法.有没有办法做到这一点,并返回一个字符串,该字符串被视为来自 regex { ... } ?

However, everything I've found looks like it wants to change the grammar. Is there a way to do it without that and return a string that is treated as if it had come out of regex { ... }?

method actually-returns-a-regex { ... }

我对此没有任何实际用途.我只是一直在想.

I don't have any practical use for this. I just keep wondering about it.

推荐答案

首先,Perl 6 设计文档要求使用 API,其中正则表达式返回可能匹配的惰性列表.如果 Rakudo 遵循该 API,您可以轻松编写一个充当正则表达式的方法,但解析将非常缓慢(因为惰性列表的性能往往比充当回溯堆栈的字符串位置(整数)的紧凑列表差得多).

First of all, the Perl 6 design documents mandate an API where regexes return a lazy list of possible matches. If Rakudo adhered to that API, you could easily write a method that acted as a regex, but parsing would be very slow (because lazy lists tend to perform much worse than a compact list of string positions (integers) that act as a backtracking stack).

相反,Perl 6 正则表达式返回匹配项.你也可以这样做.这是在语法中像正则表达式一样调用的方法示例:

Instead, Perl 6 regexes return matches. And you can do the same. Here is an example of a method that is called like a regex inside of a grammar:

grammar Foo { token TOP { a <rest> } method rest() { if self.target.substr(self.pos, 1) eq 'b' { return Match.new( orig => self.orig, target => self.target, from => self.pos, to => self.target.chars, ); } else { return Match.new(); } } } say Foo.parse('abc'); say Foo.parse('axc');

方法 rest 实现了与正则表达式 b.* 等效的方法.我希望这回答了你的问题.

Method rest implements the equivalent of the regex b.*. I hope this answers your question.

更新:我可能误解了这个问题.如果问题是我如何创建正则表达式对象"(而不是我如何编写像正则表达式一样的代码",正如我所理解的那样),答案是您必须通过 rx// 引用结构:

Update: I might have misunderstood the question. If the question is "How can I create a regex object" (and not "how can I write code that acts like a regex", as I understood it), the answer is that you have to go through the rx// quoting construct:

my $str = 'ab.*'; my $re = rx/ <$str> /; say 'fooabc' ~~ $re; # Output: 「abc」

更多推荐

我可以在方法中更改 Perl 6 俚语吗?

本文发布于:2023-10-22 20:00:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518559.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:俚语   方法   Perl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!