Files
the-swift-programming-langu…/source/chapter3/02_Lexical_Structure.md

107 lines
8.3 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 语法结构
本页包含内容:
- 空白与注释(*Whitespace and Comments*
- 标识符(*Identifiers*
- 关键字(*Keywords*
- 字面值(*Literals*
- 操作符(*Operators*
Swift 的“语法结构(*lexical structure*)”描述了如何在该语言中用字符序列构建合法标记,组成该语言中最底层的代码快,并在之后的章节中用于描述语言的其他部分。
通常,标记在随后介绍的语法约束下,从 Swift 源文件的输入文本中提取可能的最长子串生成。这种方法称为“最长匹配项(*longest match*)”,或者“最大适合”(*maximal munch*)。
## 空白与注释
空白(*whitespace*)有两个用途:分隔源文件中的标记和区分操作符属于前缀还是后缀,(参见 [操作符](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-XID_871))在其他情况下则会被忽略。以下的字符会被当作空白:空格(*space*U+0020、换行符*line feed*U+000A、回车符*carriage return*U+000D、水平 tab*horizontal tab*U+0009、垂直 tab*vertical tab*U+000B、换页符*form feed*U+000C以及空*null*U+0000
注释(*comments*)被编译器当作空白处理。单行注释由 `//` 开始直到该行结束。多行注释由 `/*` 开始,以 `*/` 结束。可以嵌套注释,但注意注释标记必须匹配。
## 标识符
标识符(*identifiers*)可以由以下的字符开始:`A``Z` 的大写或者小写字母、下划线 `_`、基本多语言面(*Basic Multilingual Plane*)中的 Unicode 非组合字符以及基本多语言面以外的非专用区(*Private Use Area*)字符。首字符之后,标识符允许使用数字和 Unicode 字符组合。
使用保留字(*reserved word*)作为标识符,需要在其前后增加反引号 <code>\`</code>。例如,<code>class</code> 不是合法的标识符,但可以使用 <code>\`class\`</code>。反引号不属于标识符的一部分,<code>\`x\`</code>`x` 表示同一标识符。
闭包(*closure*)中如果没有明确指定参数名称,参数将被隐式命名为 <code>$0</code><code>$1</code><code>$2</code>... 这些命名在闭包作用域内是合法的标识符。
> 标识符语法
>
> *identifier* → [identifier-head­](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-head) [identifier-characters](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-characters)­ *opt*
>
> *identifier* → \`­ [identifier-head­](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-head) [identifier-characters­](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-characters) *opt­* \`­
>
> *identifier* → [implicit-parameter-name­](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/implicit-parameter-name)
>
> *identifier-list* → [identifier­](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier) | [identifier­](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier) , [­identifier-list](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-list)­
>
> *identifier-head* → A 到 Z 大写或小写字母
>
> *identifier-head* → U+00A8, U+00AA, U+00AD, U+00AF, U+00B2U+00B5, 或 U+00B7U+00BA
>
> *identifier-head* → U+00BCU+00BE, U+00C0U+00D6, U+00D8U+00F6, 或 U+00F8U+00FF
>
> *identifier-head* → U+0100U+02FF, U+0370U+167F, U+1681U+180D, 或 U+180FU+1DBF
>
> *identifier-head* → U+1E00U+1FFF
>
> *identifier-head* → U+200BU+200D, U+202AU+202E, U+203FU+2040, U+2054, 或 U+2060U+206F
>
> *identifier-head* → U+2070U+20CF, U+2100U+218F, U+2460U+24FF, 或 U+2776U+2793
>
> *identifier-head* → U+2C00U+2DFF 或 U+2E80U+2FFF
>
> *identifier-head* → U+3004U+3007, U+3021U+302F, U+3031U+303F, 或 U+3040U+D7FF
>
> *identifier-head* → U+F900U+FD3D, U+FD40U+FDCF, U+FDF0U+FE1F, 或 U+FE30U+FE44
>
> *identifier-head* → U+FE47U+FFFD
>
> *identifier-head* → U+10000U+1FFFD, U+20000U+2FFFD, U+30000U+3FFFD, 或 U+40000U+4FFFD
>
> *identifier-head* → U+50000U+5FFFD, U+60000U+6FFFD, U+70000U+7FFFD, 或 U+80000U+8FFFD
>
> *identifier-head* → U+90000U+9FFFD, U+A0000U+AFFFD, U+B0000U+BFFFD, 或 U+C0000U+CFFFD
>
> *identifier-head* → U+D0000U+DFFFD 或 U+E0000U+EFFFD
>
> *identifier-character* → 数字 0 到 9
>
> *identifier-character* → U+0300U+036F, U+1DC0U+1DFF, U+20D0U+20FF, or U+FE20U+FE2F
>
> *identifier-character* → [identifier-head­](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-head)
>
> *identifier-characters* → [identifier-character](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-character) [­identifier-characters­](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-characters) *opt­*
>
> *implicit-parameter-name* → **$­** [decimal-digits­](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digits)
## 关键字
被保留的关键字(*keywords*)不允许用作标识符,除非被反引号转义,参见 [标识符](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-XID_796)。
* **用作声明的关键字:**class、deinit、enum、extension、func、import、init、let、protocol、static、struct、subscript、typealias、var
* **用作语句的关键字:**break、case、continue、default、do、else、fallthrough、if、in、for、return、switch、where、while
* **用作表达和类型的关键字:**as、dynamicType、is、new、super、self、Self、Type、\_\_COLUMN\_\_、\_\_FILE\_\_、\_\_FUNCTION\_\_、\_\_LINE\_\_
* **特定上下文中被保留的关键字:**associativity、didSet、get、infix、inout、left、mutating、none、nonmutating、operator、override、postfix、precedence、prefix、right、set、unowned、unowned(safe)、unowned(unsafe)、weak、willSet这些关键字在特定上下文之外可以被用于标识符。
## 字面值
字面值表示整型、浮点型数字或文本类型的值,举例如下:
42 // 整数字面值
3.14159 // 浮点数字面值
"Hello, world!" // 文本字面值
> 字面值语法
>
> *literal* → [integer-literal­](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/integer-literal) | [floating-point-literal](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/floating-point-literal)­ | [string-literal­](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/string-literal)
### 整数字面值
### 浮点数字面值
### 文本字面值
## 操作符