Perl 6 标识符中允许什么? [英] What's allowed in a Perl 6 identifier?

查看:51
本文介绍了Perl 6 标识符中允许什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概要 2 说:

标识符由一个字母字符后跟任意序列的字母数字字符组成.字母和数字的定义包括适当的 Unicode 字符.下划线始终被视为字母.标识符还可以包含独立的撇号或连字符,前提是下一个字符是字母.

An identifier is composed of an alphabetic character followed by any sequence of alphanumeric characters. The definitions of alphabetic and numeric include appropriate Unicode characters. Underscore is always considered alphabetic. An identifier may also contain isolated apostrophes or hyphens provided the next character is alphabetic.

Perl 6 文档中的

语法 说:

Syntax in the Perl 6 docs says:

标识符是出现在多个地方的语法构建块.标识符是原始名称,必须以字母字符(或下划线)开头,后跟零个或多个单词字符(字母、下划线或数字).您还可以在中间嵌入破折号 - 或单引号 ',但不能连续两个.

Identifiers are a grammatical building block that occur in several places. An identifier is a primitive name, and must start with an alphabetic character (or an underscore), followed by zero or more word characters (alphabetic, underscore or number). You can also embed dashes - or single quotes ' in the middle, but not two in a row.

术语适当的 Unicode 字符"回避了我们知道什么是适当的问题.

The term "appropriate Unicode character" begs the question that we know what appropriate is.

如果我要选择 ASCII 字符以外的字符,我觉得这太含糊了.我在 Perl6::Grammar 中找到了这个产生式,但没有找到 <.ident>:

I find that to be too vague if I'm going to choose beyond ASCII characters. I find in Perl6::Grammar this production, but not the definition for <.ident>:

token identifier {
    <.ident> [ <.apostrophe> <.ident> ]*
}

但这也引出了一个问题,即您必须知道标识符是什么才能定义标识符.那么,<.ident>在哪里?

But this also begs the question that you have to know what an identifier is to define an identifier. So, where is <.ident>?

raiph 指出 <.ident>QRegex::Cursor 中的 ident 方法,但它根据nqp::const::CCLASS_WORD.现在我必须追踪那个.

raiph points out that <.ident> is the ident method in QRegex::Cursor, but that defines it in terms of nqp::const::CCLASS_WORD. Now I have to track down that.

我尝试使用 U+00B2 (SUPERSCRIPT TWO)(一般类别 No,Other_Number),因为我想传递昂贵的平方运算的结果,嘿,Perl 6 应该允许这样做:

I tried to use U+00B2 (SUPERSCRIPT TWO) (General categories No, Other_Number) because I wanted to pass around the result of an expensive square operation, and hey, Perl 6 is supposed to allow this:

my $a² = $a**2;

但是,事实证明 ² 和其他上标都是运算符.没关系,但是 ² 之类的东西没有被列为操作员IntInt 继承的行为:

But, it turns out that ², along with the other superscripts, are operators. That's fine, but ² and the like aren't listed as an operator or in Int or the behavior Int inherits:

$ perl6 -e 'my $Δ² = 6; say $*PERL; say $Δ²'
Use of uninitialized value of type Any in numeric context  in block <unit> at -e line 1
Cannot modify an immutable Int
  in block <unit> at -e line 1

$ perl6 -e 'my $Δ = 6; say $*PERL; say $Δ²'
Perl 6 (6.c)
36

$ perl6 -e 'my $Δ = 6; say $*PERL; say $Δ³'
Perl 6 (6.c)
216

$ perl6 -e 'my $Δ = 6; say $*PERL; say $Δ⁹'
Perl 6 (6.c)
10077696

但我不能使用 ½ U+00BD(庸俗分数一半)(No 和 Other_Number 的一般类别):

But I can't use ½ U+00BD (VULGAR FRACTION ONE HALF) (General categories of No and Other_Number):

$ perl6 -e 'my $Δ½ = 6; say $*PERL; say $Δ½'
===SORRY!=== Error while compiling -e
Bogus postfix
at -e:1
------> my $Δ⏏½ = 6; say $*PERL; say $Δ½
    expecting any of:
        constraint
        infix
        infix stopper
        postfix
        statement end
        statement modifier
        statement modifier loop

但是,如果我不在 中输入数字怎么办?

But, what if I don't put a number in ?

$ perl6 -e 'my $Δ = "foo"; say $*PERL; say $Δ²'
Cannot convert string to number: base-10 number must begin with valid digits or '.' in '⏏foo' (indicated by ⏏)

在 -e 第 1 行的块

in block at -e line 1

Actually thrown at:

在 -e 第 1 行的块

in block at -e line 1

我担心定义后缀运算符的人会破坏语言,但这似乎有效:

I was worried that someone defining a postfix operator could break the language, but this seems to work:

$ perl6 -e 'multi sub postfix:<Δ>(Int $n) { 137 }; say  6Δ;'
137

$ perl6 -e 'multi sub postfix:<Δ>(Int $n) { 137 }; my $ΔΔ = 6; say $ΔΔ;'
6

$ perl6 -e 'multi sub postfix:<Δ>(Int $n) { 137 }; my $Δ = 6; say $ΔΔ;'===SORRY!=== Error while compiling -e
Variable '$ΔΔ' is not declared
at -e:1
------> fix:<Δ>(Int $n) { 137 }; my $Δ = 6; say ⏏$ΔΔ;

那么,那里发生了什么?

So, what's going on there?

推荐答案

语法有标识符 定义为

token apostrophe {
    <[ ' \- ]>
}

token identifier {
    <.ident> [ <.apostrophe> <.ident> ]*
}

使用 ident 一个 游标方法 接受以 CCLASS_ALPHABETIC 字符或下划线 _ 开头并以零个或多个 CCLASS_WORD 字符开头的输入.

with ident a method on cursors which accepts input that starts with a CCLASS_ALPHABETIC character or an underscore _ and continues with zero or more CCLASS_WORD characters.

这些类在 MoarVM 中实现并映射到各种 Unicode类别.

These classes are implemented in MoarVM and map to various Unicode categories.

具体来说,CCLASS_ALPHABETIC 检查字母,小写字母,大写信,标题字母、修饰符字母、其他.

Specifically, CCLASS_ALPHABETIC checks for Letter, Lowercase; Letter, Uppercase; Letter, Titlecase; Letter, Modifier and Letter, Other.

CCLASS_WORD 还接受类别数字、十进制数字以及下划线的字符.

CCLASS_WORD additionally accepts characters of category Number, Decimal Digit as well as undercores.

至于为什么后缀运算符不破坏标识符,这是由于最长的令牌匹配.

As to why postfix operators do not break identifiers, that's due to longest token matching.

如果你想在变量上调用后缀运算符Δ,你必须添加一个反斜杠,即

If you want to call a postfix operator Δ on a variable , you have to add a backslash, ie

multi sub postfix:<Δ>(Int $n) { 137 };
my $Δ = 6;
say $Δ\Δ;

或'unspace'

say $Δ\   Δ;

这篇关于Perl 6 标识符中允许什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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