Skip to content

Commit 8cd1f6b

Browse files
committed
wip: Precedence Group Declaration
1 parent 23a6b80 commit 8cd1f6b

File tree

1 file changed

+27
-76
lines changed

1 file changed

+27
-76
lines changed

swift-6-beta.docc/ReferenceManual/Declarations.md

Lines changed: 27 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,14 +2709,11 @@ postfix operator <#operator name#>
27092709
>
27102710
> *中缀运算符组***`:`** *优先级组名称*
27112711
2712-
## Precedence Group Declaration
2712+
## 优先级组声明
27132713

2714-
A *precedence group declaration* introduces
2715-
a new grouping for infix operator precedence into your program.
2716-
The precedence of an operator specifies how tightly the operator
2717-
binds to its operands, in the absence of grouping parentheses.
2714+
*优先级组声明*在程序中引入了一个新的中缀运算符优先级分组。运算符的优先级指定了在没有分组括号的情况下,运算符与其操作数的绑定紧密程度。
27182715

2719-
A precedence group declaration has the following form:
2716+
优先级组声明具有以下形式:
27202717

27212718
```swift
27222719
precedencegroup <#precedence group name#> {
@@ -2727,82 +2724,36 @@ precedencegroup <#precedence group name#> {
27272724
}
27282725
```
27292726

2730-
The *lower group names* and *higher group names* lists specify
2731-
the new precedence group's relation to existing precedence groups.
2732-
The `lowerThan` precedence group attribute may only be used
2733-
to refer to precedence groups declared outside of the current module.
2734-
When two operators compete with each other for their operands,
2735-
such as in the expression `2 + 3 * 5`,
2736-
the operator with the higher relative precedence
2737-
binds more tightly to its operands.
2738-
2739-
> Note: Precedence groups related to each other
2740-
> using *lower group names* and *higher group names*
2741-
> must fit into a single relational hierarchy,
2742-
> but they *don't* have to form a linear hierarchy.
2743-
> This means it's possible to have precedence groups
2744-
> with undefined relative precedence.
2745-
> Operators from those precedence groups
2746-
> can't be used next to each other without grouping parentheses.
2747-
2748-
Swift defines numerous precedence groups to go along
2749-
with the operators provided by the Swift standard library.
2750-
For example, the addition (`+`) and subtraction (`-`) operators
2751-
belong to the `AdditionPrecedence` group,
2752-
and the multiplication (`*`) and division (`/`) operators
2753-
belong to the `MultiplicationPrecedence` group.
2754-
For a complete list of precedence groups
2755-
provided by the Swift standard library,
2756-
see [Operator Declarations](https://developer.apple.com/documentation/swift/operator_declarations).
2757-
2758-
The *associativity* of an operator specifies how a sequence of operators
2759-
with the same precedence level are grouped together in the absence of grouping parentheses.
2760-
You specify the associativity of an operator by writing
2761-
one of the context-sensitive keywords `left`, `right`, or `none` ---
2762-
if your omit the associativity, the default is `none`.
2763-
Operators that are left-associative group left-to-right.
2764-
For example,
2765-
the subtraction operator (`-`) is left-associative,
2766-
so the expression `4 - 5 - 6` is grouped as `(4 - 5) - 6`
2767-
and evaluates to `-7`.
2768-
Operators that are right-associative group right-to-left,
2769-
and operators that are specified with an associativity of `none`
2770-
don't associate at all.
2771-
Nonassociative operators of the same precedence level
2772-
can't appear adjacent to each to other.
2773-
For example,
2774-
the `<` operator has an associativity of `none`,
2775-
which means `1 < 2 < 3` isn't a valid expression.
2776-
2777-
The *assignment* of a precedence group specifies the precedence of an operator
2778-
when used in an operation that includes optional chaining.
2779-
When set to `true`, an operator in the corresponding precedence group
2780-
uses the same grouping rules during optional chaining
2781-
as the assignment operators from the Swift standard library.
2782-
Otherwise, when set to `false` or omitted,
2783-
operators in the precedence group follows the same optional chaining rules
2784-
as operators that don't perform assignment.
2785-
2786-
> Grammar of a precedence group declaration:
2727+
*低级组名称**高级组名称*列表指定了新优先级组与现有优先级组的关系。`lowerThan` 优先级组属性只能用于引用当前模块外声明的优先级组。当两个运算符争夺其操作数时,如在表达式 `2 + 3 * 5` 中,具有较高相对优先级的运算符会更紧密地绑定到其操作数上。
2728+
2729+
> 注意:使用*低级组名称**高级组名称*相关联的优先级组必须适合于单一的关系层次结构,但它们**必形成线性层次结构。这意味着可以有相对优先级未定义的优先级组。来自这些优先级组的运算符不能在没有分组括号的情况下相互使用。
2730+
2731+
Swift 定义了许多优先级组,以配合 Swift 标准库提供的运算符。例如,加法 (`+`) 和减法 (`-`) 运算符属于 `AdditionPrecedence` 组,而乘法 (`*`) 和除法 (`/`) 运算符属于 `MultiplicationPrecedence` 组。有关 Swift 标准库提供的优先级组的完整列表,请参见 [运算符声明](https://developer.apple.com/documentation/swift/operator*declarations)
2732+
2733+
运算符的*结合性*指定了在没有分组括号的情况下,具有相同优先级的运算符序列是如何分组的。通过写入上下文敏感的关键字之一来指定运算符的结合性:`left``right``none` ——如果您省略结合性,默认值为 `none`。左结合的运算符从左到右分组。例如,减法运算符(`-`)是左结合的,因此表达式`4 - 5 - 6` 被分组为 `(4 - 5) - 6`,并计算为 `-7`。右结合的运算符从右到左分组,而指定为 `none` 的运算符则完全不结合。相同优先级的非结合运算符不能相邻出现。例如,`<` 运算符的结合性为 `none`,这意味着 `1 < 2 < 3` 不是一个有效的表达式。
2734+
2735+
*赋值*优先级组的设置指定了运算符在包含可选链操作中的优先级。当设置为 `true` 时,对应优先级组中的运算符在可选链操作期间使用与 Swift 标准库中的赋值运算符相同的分组规则。否则,当设置为 `false` 或省略时,该优先级组中的运算符将遵循与不执行赋值的运算符相同的可选链规则。
2736+
2737+
> 优先级组声明的语法:
27872738
>
2788-
> *precedence-group-declaration***`precedencegroup`** *precedence-group-name* **`{`** *precedence-group-attributes*_?_ **`}`**
2739+
> *优先级组声明***`precedencegroup`** *优先级组名称* **`{`** *优先级组属性*_?_ **`}`**
27892740
>
2790-
> *precedence-group-attributes**precedence-group-attribute* *precedence-group-attributes*_?_ \
2791-
> *precedence-group-attribute**precedence-group-relation* \
2792-
> *precedence-group-attribute**precedence-group-assignment* \
2793-
> *precedence-group-attribute**precedence-group-associativity*
2741+
> *优先级组属性**优先级组属性* *优先级组属性*_?_ \
2742+
> *优先级组属性**优先级组关系* \
2743+
> *优先级组属性**优先级组分配* \
2744+
> *优先级组属性**优先级组结合性*
27942745
>
2795-
> *precedence-group-relation***`higherThan`** **`:`** *precedence-group-names* \
2796-
> *precedence-group-relation***`lowerThan`** **`:`** *precedence-group-names*
2746+
> *优先级组关系***`高于`** **`:`** *优先级组名称* \
2747+
> *优先级组关系***`低于`** **`:`** *优先级组名称*
27972748
>
2798-
> *precedence-group-assignment***`assignment`** **`:`** *boolean-literal*
2749+
> *优先级组分配***`分配`** **`:`** *布尔字面量*
27992750
>
2800-
> *precedence-group-associativity***`associativity`** **`:`** **`left`** \
2801-
> *precedence-group-associativity***`associativity`** **`:`** **`right`** \
2802-
> *precedence-group-associativity***`associativity`** **`:`** **`none`**
2751+
> *优先级组结合性***`结合性`** **`:`** **``** \
2752+
> *优先级组结合性***`结合性`** **`:`** **``** \
2753+
> *优先级组关联性***`关联性`** **`:`** **``**
28032754
>
2804-
> *precedence-group-names**precedence-group-name* | *precedence-group-name* **`,`** *precedence-group-names* \
2805-
> *precedence-group-name**identifier*
2755+
> *优先级组名称**优先级组名称* | *优先级组名称* **`,`** *优先级组名称*
2756+
> *优先级组名称**标识符*
28062757
28072758
## Declaration Modifiers
28082759

0 commit comments

Comments
 (0)