Perl 6 标识符允许什么?
What's allowed in a Perl 6 identifier?
Synopsis 2 说:
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.
Syntax 在 Perl 6 文档中说:
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.
"appropriate Unicode character" 这个词回避了我们知道什么是合适的问题。
如果我要选择 ASCII 字符以外的字符,我发现这太模糊了。我在 Perl6::Grammar 中找到了这个作品,但没有找到 <.ident>
:
的定义
token identifier {
<.ident> [ <.apostrophe> <.ident> ]*
}
但这也引出了一个问题,您必须知道标识符是什么才能定义标识符。那么,<.ident>
在哪里?
raiph 指出 <.ident>
是 QRegex::Cursor
中的 ident
方法,但它是根据 nqp::const::CCLASS_WORD
定义的。现在我必须找到它。
我尝试使用 U+00B2 (SUPERSCRIPT TWO)(一般类别否,Other_Number)因为我想传递一个昂贵的平方运算的结果,嘿,Perl 6 应该允许这个:
my $a² = $a**2;
但是,事实证明 ² 和其他上标都是运算符。没关系,但是 ² 等不是 listed as an operator 或 Int
或 Int
继承的行为:
$ 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 的一般类别):
$ 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
但是,如果我不在 $Δ
中输入数字呢?
$ 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 行的块中
Actually thrown at:
在 -e 第 1 行的块中
我担心定义后缀运算符的人会破坏语言,但这似乎有效:
$ 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 ⏏$ΔΔ;
那么,那里发生了什么?
语法有标识符defined as
token apostrophe {
<[ ' \- ]>
}
token identifier {
<.ident> [ <.apostrophe> <.ident> ]*
}
with ident
a method on cursors 接受以 CCLASS_ALPHABETIC
字符或下划线 _
开头并以零个或多个 CCLASS_WORD
字符继续的输入.
这些 类 是 implemented in MoarVM 并映射到各种 Unicode 类别。
具体来说,CCLASS_ALPHABETIC
检查 小写字母 ; 字母,大写; Letter, Titlecase; 字母、修饰符和字母、其他。
CCLASS_WORD
还接受类别 数字、十进制数字 以及下划线的字符。
至于为什么后缀运算符不破坏标识符,那是因为最长的标记匹配。
如果要在变量$Δ
上调用后缀运算符Δ
,则必须添加反斜杠,即
multi sub postfix:<Δ>(Int $n) { 137 };
my $Δ = 6;
say $Δ\Δ;
或 'unspace'
say $Δ\ Δ;
Synopsis 2 说:
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.
Syntax 在 Perl 6 文档中说:
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.
"appropriate Unicode character" 这个词回避了我们知道什么是合适的问题。
如果我要选择 ASCII 字符以外的字符,我发现这太模糊了。我在 Perl6::Grammar 中找到了这个作品,但没有找到 <.ident>
:
token identifier {
<.ident> [ <.apostrophe> <.ident> ]*
}
但这也引出了一个问题,您必须知道标识符是什么才能定义标识符。那么,<.ident>
在哪里?
raiph 指出 <.ident>
是 QRegex::Cursor
中的 ident
方法,但它是根据 nqp::const::CCLASS_WORD
定义的。现在我必须找到它。
我尝试使用 U+00B2 (SUPERSCRIPT TWO)(一般类别否,Other_Number)因为我想传递一个昂贵的平方运算的结果,嘿,Perl 6 应该允许这个:
my $a² = $a**2;
但是,事实证明 ² 和其他上标都是运算符。没关系,但是 ² 等不是 listed as an operator 或 Int
或 Int
继承的行为:
$ 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 的一般类别):
$ 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
但是,如果我不在 $Δ
中输入数字呢?
$ 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 行的块中
Actually thrown at:
在 -e 第 1 行的块中
我担心定义后缀运算符的人会破坏语言,但这似乎有效:
$ 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 ⏏$ΔΔ;
那么,那里发生了什么?
语法有标识符defined as
token apostrophe {
<[ ' \- ]>
}
token identifier {
<.ident> [ <.apostrophe> <.ident> ]*
}
with ident
a method on cursors 接受以 CCLASS_ALPHABETIC
字符或下划线 _
开头并以零个或多个 CCLASS_WORD
字符继续的输入.
这些 类 是 implemented in MoarVM 并映射到各种 Unicode 类别。
具体来说,CCLASS_ALPHABETIC
检查 小写字母 ; 字母,大写; Letter, Titlecase; 字母、修饰符和字母、其他。
CCLASS_WORD
还接受类别 数字、十进制数字 以及下划线的字符。
至于为什么后缀运算符不破坏标识符,那是因为最长的标记匹配。
如果要在变量$Δ
上调用后缀运算符Δ
,则必须添加反斜杠,即
multi sub postfix:<Δ>(Int $n) { 137 };
my $Δ = 6;
say $Δ\Δ;
或 'unspace'
say $Δ\ Δ;