1
- <!--
2
- 要翻译的文件:https://github.com/SwiftGGTeam/the-swift-programming-language-in-chinese/blob/swift-6-beta-translation/swift-6-beta.docc/ReferenceManual/LexicalStructure.md
3
- Swift 文档源文件地址:https://docs.swift.org/swift-book/documentation/the-swift-programming-language/lexicalstructure
4
- 翻译估计用时:⭐️⭐️⭐️⭐️⭐️
5
- -->
1
+ # 词法结构
2
+
3
+ 使用语法的最低层级组件。
4
+
5
+ Swift 的 * 词法结构* 描述了哪些字符序列构成了语言中的合法标记(tokens)。这些合法标记构成了语言的最低层级构建块,并在后续章节中用于描述语言的其他部分。一个标记可以由标识符、关键字、标点符号、字面量或运算符组成。
6
+
7
+ 在大多数情况下,这些标记是从 Swift 源文件的字符中生成的,生成过程考虑了输入文本中最长的可能子字符串,并受以下语法规则的约束。这种行为策略被称为 * 最长匹配策略(longest match)* 或 * 最大吞噬策略(maximal munch)* 。
6
8
7
- # Lexical Structure
8
-
9
- Use the lowest-level components of the syntax.
10
-
11
- The * lexical structure* of Swift describes what sequence of characters
12
- form valid tokens of the language.
13
- These valid tokens form the lowest-level building blocks of the language
14
- and are used to describe the rest of the language in subsequent chapters.
15
- A token consists of an identifier, keyword, punctuation, literal, or operator.
16
-
17
- In most cases, tokens are generated from the characters of a Swift source file
18
- by considering the longest possible substring from the input text,
19
- within the constraints of the grammar that are specified below.
20
- This behavior is referred to as * longest match*
21
- or * maximal munch* .
22
-
23
- ## Whitespace and Comments
24
-
25
- Whitespace has two uses: to separate tokens in the source file
26
- and to distinguish between prefix, postfix, and infix operators
27
- (see < doc:LexicalStructure#Operators > ),
28
- but is otherwise ignored.
29
- The following characters are considered whitespace:
30
- space (U+0020),
31
- line feed (U+000A),
32
- carriage return (U+000D),
33
- horizontal tab (U+0009),
34
- vertical tab (U+000B),
35
- form feed (U+000C)
36
- and null (U+0000).
9
+ ## 空白和注释
10
+
11
+ 空白字符有两个用途:在源文件中分隔标记,并区分前缀、后缀和中缀运算符(参见 < doc:LexicalStructure#Operators > ),除此之外,空白字符会被忽略。以下字符被视为空白字符:空格 (U+0020)、换行符 (U+000A)、回车符 (U+000D)、水平制表符 (U+0009)、垂直制表符 (U+000B)、换页符 (U+000C) 和空字符 (U+0000)。
37
12
38
13
<!--
39
14
Whitespace characters are listed roughly from
40
15
most salient/common to least,
41
16
not in order of Unicode scalar value.
42
17
-->
43
18
44
- Comments are treated as whitespace by the compiler.
45
- Single line comments begin with ` // `
46
- and continue until a line feed (U+000A) or carriage return (U+000D).
47
- Multiline comments begin with ` /* ` and end with ` */ ` .
48
- Nesting multiline comments is allowed,
49
- but the comment markers must be balanced.
19
+ 编译器将注释视为空白字符。单行注释以 ` // ` 开头,并持续到换行符 (U+000A) 或回车符 (U+000D)。多行注释以 ` /* ` 开头,以 ` */ ` 结束。多行注释可以嵌套,但注释符号必须头尾匹配。
50
20
51
- Comments can contain additional formatting and markup,
52
- as described in [ Markup Formatting Reference] ( https://developer.apple.com/library/content/documentation/Xcode/Reference/xcode_markup_formatting_ref/index.html ) .
21
+ 注释中可以包含额外的格式和标记,如 [ 标记格式参考] ( https://developer.apple.com/library/content/documentation/Xcode/Reference/xcode_markup_formatting_ref/index.html ) 中所述。
53
22
54
- > Grammar of whitespace :
23
+ > 空白字符的语法 :
55
24
>
56
25
> * whitespace* → * whitespace-item* * whitespace* _ ?_ \
57
26
> * whitespace-item* → * line-break* \
58
27
> * whitespace-item* → * inline-space* \
59
28
> * whitespace-item* → * comment* \
60
29
> * whitespace-item* → * multiline-comment* \
61
- > * whitespace-item* → U+0000, U+000B, or U+000C
30
+ > * whitespace-item* → U+0000、 U+000B 或 U+000C
62
31
>
63
32
> * line-break* → U+000A \
64
33
> * line-break* → U+000D \
65
- > * line-break* → U+000D followed by U+000A
34
+ > * line-break* → U+000D 后跟 U+000A
66
35
>
67
36
> * inline-spaces* → * inline-space* * inline-spaces* _ ?_ \
68
- > * inline-space* → U+0009 or U+0020
37
+ > * inline-space* → U+0009 或 U+0020
69
38
>
70
39
> * comment* → ** ` // ` ** * comment-text* * line-break* \
71
40
> * multiline-comment* → ** ` /* ` ** * multiline-comment-text* ** ` */ ` **
72
41
>
73
42
> * comment-text* → * comment-text-item* * comment-text* _ ?_ \
74
- > * comment-text-item* → Any Unicode scalar value except U+000A or U+000D
43
+ > * comment-text-item* → 除 U+000A 或 U+000D 之外的任意 Unicode 标量值
75
44
>
76
45
> * multiline-comment-text* → * multiline-comment-text-item* * multiline-comment-text* _ ?_ \
77
46
> * multiline-comment-text-item* → * multiline-comment* \
78
47
> * multiline-comment-text-item* → * comment-text-item* \
79
- > * multiline-comment-text-item* → Any Unicode scalar value except ** ` /* ` ** or ** ` */ ` **
48
+ > * multiline-comment-text-item* → 除 ** ` /* ` ** 或 ** ` */ ` ** 之外的任意 Unicode 标量值
80
49
81
- ## Identifiers
50
+ ## 标识符
82
51
83
- * Identifiers* begin with
84
- an uppercase or lowercase letter A through Z,
85
- an underscore (` _ ` ),
86
- a noncombining alphanumeric Unicode character
87
- in the Basic Multilingual Plane,
88
- or a character outside the Basic Multilingual Plane
89
- that isn't in a Private Use Area.
90
- After the first character,
91
- digits and combining Unicode characters are also allowed.
92
-
93
- Treat identifiers that begin with an underscore,
94
- subscripts whose first argument label begins with an underscore,
95
- and initializers whose first argument label begins with an underscore,
96
- as internal,
97
- even if their declaration has the ` public ` access-level modifier.
98
- This convention lets framework authors mark part of an API
99
- that clients must not interact with or depend on,
100
- even though some limitation requires the declaration to be public.
101
- In addition,
102
- identifiers that begin with two underscores
103
- are reserved for the Swift compiler and standard library.
104
-
105
- To use a reserved word as an identifier,
106
- put a backtick (\` ) before and after it.
107
- For example, ` class ` isn't a valid identifier,
108
- but `` `class` `` is valid.
109
- The backticks aren't considered part of the identifier;
110
- `` `x` `` and ` x ` have the same meaning.
52
+ * 标识符* 以大写或小写字母 A 到 Z、下划线 (` _ ` )、基本多语言平面(Basic Multilingual Plane)的非组合字母数字 Unicode 字符(Noncombining Alphanumeric Unicode Character),或基本多语言平面之外但不在私用区(Private Use Area)的字符开头。在第一个字符之后,还允许使用数字和组合 Unicode 字符(Combining Unicode Character)。
53
+
54
+ 即使声明具有 ` public ` 访问级别修饰符,也应将以下内容视为仅内部使用:以下划线开头的标识符、第一个参数标签以下划线开头的下标操作,以及第一个参数标签以下划线开头的构造函数。这个约定允许框架作者以此方式标记某个 API 的一部分内容,以防止客户端与之交互或依赖,尽管某些限制要求这些声明是公开可访问的。此外,以两个下划线开头的标识符需保留给 Swift 编译器和标准库使用。
55
+
56
+ 要将保留字用作标识符,可以在其前后加上反引号(\` )。例如,` class ` 不是一个合法的标识符,但 `` `class` `` 是合法的。反引号不被视为标识符的一部分;`` `x` `` 和 ` x ` 具有相同的指代含义。
111
57
112
58
<!--
113
59
The paragraph above produces a link-resolution warning
@@ -117,16 +63,9 @@ https://github.com/apple/swift-book/issues/71
117
63
https://github.com/apple/swift-markdown/issues/93
118
64
-->
119
65
120
- Inside a closure with no explicit parameter names,
121
- the parameters are implicitly named ` $0 ` , ` $1 ` , ` $2 ` , and so on.
122
- These names are valid identifiers within the scope of the closure.
66
+ 在没有显式参数名称的闭包中,参数会被隐式命名为 ` $0 ` 、` $1 ` 、` $2 ` 等。这些名称在闭包的范围内是合法的标识符。
123
67
124
- The compiler synthesizes identifiers that begin with a dollar sign (` $ ` )
125
- for properties that have a property wrapper projection.
126
- Your code can interact with these identifiers,
127
- but you can't declare identifiers with that prefix.
128
- For more information, see the < doc:Attributes#propertyWrapper > section
129
- of the < doc:Attributes > chapter.
68
+ 编译器会为具有属性包装器投射(Property Wrapper Projection)的属性合成以美元符号 (` $ ` ) 开头的标识符。你的代码可以与这些标识符交互,但你不能声明带有该前缀的标识符。有关更多信息,请参阅 < doc:Attributes > 章节的 < doc:Attributes#propertyWrapper > 部分。
130
69
131
70
<!--
132
71
The cross reference above includes both the section and chapter because,
@@ -143,33 +82,33 @@ https://github.com/apple/swift-book/issues/71
143
82
https://github.com/apple/swift-markdown/issues/93
144
83
-->
145
84
146
- > Grammar of an identifier :
85
+ > 标识符的语法 :
147
86
>
148
87
> * identifier* → * identifier-head* * identifier-characters* _ ?_ \
149
88
> * identifier* → ** `` ` `` ** * identifier-head* * identifier-characters* _ ?_ ** `` ` `` ** \
150
89
> * identifier* → * implicit-parameter-name* \
151
90
> * identifier* → * property-wrapper-projection* \
152
91
> * identifier-list* → * identifier* | * identifier* ** ` , ` ** * identifier-list*
153
92
>
154
- > * identifier-head* → Upper- or lowercase letter A through Z \
93
+ > * identifier-head* → 大写或小写字母 A 到 Z \
155
94
> * identifier-head* → ** ` _ ` ** \
156
- > * identifier-head* → U+00A8, U+00AA, U+00AD, U+00AF, U+00B2–U+00B5, or U+00B7–U+00BA \
157
- > * identifier-head* → U+00BC–U+00BE, U+00C0–U+00D6, U+00D8–U+00F6, or U+00F8–U+00FF \
158
- > * identifier-head* → U+0100–U+02FF, U+0370–U+167F, U+1681–U+180D, or U+180F–U+1DBF \
95
+ > * identifier-head* → U+00A8、 U+00AA、 U+00AD、 U+00AF、 U+00B2–U+00B5 或 U+00B7–U+00BA \
96
+ > * identifier-head* → U+00BC–U+00BE、 U+00C0–U+00D6、 U+00D8–U+00F6 或 U+00F8–U+00FF \
97
+ > * identifier-head* → U+0100–U+02FF、 U+0370–U+167F、 U+1681–U+180D 或 U+180F–U+1DBF \
159
98
> * identifier-head* → U+1E00–U+1FFF \
160
- > * identifier-head* → U+200B–U+200D, U+202A–U+202E, U+203F–U+2040, U+2054, or U+2060–U+206F \
161
- > * identifier-head* → U+2070–U+20CF, U+2100–U+218F, U+2460–U+24FF, or U+2776–U+2793 \
162
- > * identifier-head* → U+2C00–U+2DFF or U+2E80–U+2FFF \
163
- > * identifier-head* → U+3004–U+3007, U+3021–U+302F, U+3031–U+303F, or U+3040–U+D7FF \
164
- > * identifier-head* → U+F900–U+FD3D, U+FD40–U+FDCF, U+FDF0–U+FE1F, or U+FE30–U+FE44 \
99
+ > * identifier-head* → U+200B–U+200D、 U+202A–U+202E、 U+203F–U+2040、 U+2054 或 U+2060–U+206F \
100
+ > * identifier-head* → U+2070–U+20CF、 U+2100–U+218F、 U+2460–U+24FF 或 U+2776–U+2793 \
101
+ > * identifier-head* → U+2C00–U+2DFF 或 U+2E80–U+2FFF \
102
+ > * identifier-head* → U+3004–U+3007、 U+3021–U+302F、 U+3031–U+303F 或 U+3040–U+D7FF \
103
+ > * identifier-head* → U+F900–U+FD3D、 U+FD40–U+FDCF、 U+FDF0–U+FE1F 或 U+FE30–U+FE44 \
165
104
> * identifier-head* → U+FE47–U+FFFD \
166
- > * identifier-head* → U+10000–U+1FFFD, U+20000–U+2FFFD, U+30000–U+3FFFD, or U+40000–U+4FFFD \
167
- > * identifier-head* → U+50000–U+5FFFD, U+60000–U+6FFFD, U+70000–U+7FFFD, or U+80000–U+8FFFD \
168
- > * identifier-head* → U+90000–U+9FFFD, U+A0000–U+AFFFD, U+B0000–U+BFFFD, or U+C0000–U+CFFFD \
169
- > * identifier-head* → U+D0000–U+DFFFD or U+E0000–U+EFFFD
105
+ > * identifier-head* → U+10000–U+1FFFD、 U+20000–U+2FFFD、 U+30000–U+3FFFD 或 U+40000–U+4FFFD \
106
+ > * identifier-head* → U+50000–U+5FFFD、 U+60000–U+6FFFD、 U+70000–U+7FFFD 或 U+80000–U+8FFFD \
107
+ > * identifier-head* → U+90000–U+9FFFD、 U+A0000–U+AFFFD、 U+B0000–U+BFFFD 或 U+C0000–U+CFFFD \
108
+ > * identifier-head* → U+D0000–U+DFFFD 或 U+E0000–U+EFFFD
170
109
>
171
- > * identifier-character* → Digit 0 through 9 \
172
- > * identifier-character* → U+0300–U+036F, U+1DC0–U+1DFF, U+20D0–U+20FF, or U+FE20–U+FE2F \
110
+ > * identifier-character* → 数字 0 到 9 \
111
+ > * identifier-character* → U+0300–U+036F、 U+1DC0–U+1DFF、 U+20D0–U+20FF 或 U+FE20–U+FE2F \
173
112
> * identifier-character* → * identifier-head* \
174
113
> * identifier-characters* → * identifier-character* * identifier-characters* _ ?_
175
114
>
0 commit comments