翻译:StanZhai

校对:xielingwang

语法总结


本页包含内容:

语句

语句语法 statementexpression ; opt statementdeclaration ; opt statementloop-statement ; opt statementbranch-statement ; opt statementlabeled-statement statementcontrol-transfer-statement ; opt statementsstatement statements opt

循环语句语法 loop-statementfor-statement loop-statementfor-in-statement loop-statementwhile-statement loop-statementdo-while-statement

For 循环语法 for-statementfor for-init opt ; expression opt ; expression opt code-block for-statementfor ( for-init opt ; expression opt ; expression opt ) code-block for-initvariable-declaration | expression-list

For-In 循环语法 for-in-statementfor pattern in expression code-block

While 循环语法 while-statementwhile while-condition code-block while-conditionexpression | declaration

Do-While 循环语法 do-while-statementdo code-block while while-condition

分支语句语法 branch-statementif-statement branch-statementswitch-statement

If 语句语法 if-statementif if-condition code-block else-clause opt if-conditionexpression | declaration else-clauseelse code-block | else if-statement

Switch 语句语法 switch-statementswitch expression { switch-cases opt } switch-casesswitch-case switch-cases opt switch-casecase-label statements | default-label statements switch-casecase-label ; | default-label ; case-labelcase case-item-list : case-item-listpattern guard-clause opt | pattern guard-clause opt , case-item-list default-labeldefault : guard-clausewhere guard-expression guard-expressionexpression

带标签的语句语法 labeled-statementstatement-label loop-statement | statement-label switch-statement statement-labellabel-name : label-nameidentifier

控制传递语句语法 control-transfer-statementbreak-statement control-transfer-statementcontinue-statement control-transfer-statementfallthrough-statement control-transfer-statementreturn-statement

Break 语句语法 break-statementbreak label-name opt

Continue 语句语法 continue-statementcontinue label-name opt

Fallthrough 语句语法 fallthrough-statementfallthrough

Return 语句语法 return-statementreturn expression opt

泛型参数

泛型形参子句语法 generic-parameter-clause< generic-parameter-list requirement-clause opt > generic-parameter-listgeneric-parameter | generic-parameter , generic-parameter-list generic-parametertype-name generic-parametertype-name : type-identifier generic-parametertype-name : protocol-composition-type requirement-clausewhere requirement-list requirement-listrequirement | requirement , requirement-list requirementconformance-requirement | same-type-requirement conformance-requirementtype-identifier : type-identifier conformance-requirementtype-identifier : protocol-composition-type same-type-requirementtype-identifier == type-identifier

泛型实参子句语法 generic-argument-clause< generic-argument-list > generic-argument-listgeneric-argument | generic-argument , generic-argument-list generic-argumenttype

声明 (Declarations)

声明语法 declarationimport-declaration declarationconstant-declaration declarationvariable-declaration declarationtypealias-declaration declarationfunction-declaration declarationenum-declaration declarationstruct-declaration declarationclass-declaration declarationprotocol-declaration declarationinitializer-declaration declarationdeinitializer-declaration declarationextension-declaration declarationsubscript-declaration declarationoperator-declaration declarationsdeclaration declarations opt declaration-specifiersdeclaration-specifier declaration-specifiers opt declaration-specifierclass | mutating | nonmutating | override | static | unowned | unowned(safe) | unowned(unsafe) | weak

顶级声明语法 top-level-declarationstatements opt

代码块语法 code-block{ statements opt }

Import 声明语法 import-declarationattributes opt import import-kind opt import-path import-kindtypealias | struct | class | enum | protocol | var | func import-pathimport-path-identifier | import-path-identifier . import-path import-path-identifieridentifier | operator

常数声明语法 constant-declarationattributes opt declaration-specifiers opt let pattern-initializer-list pattern-initializer-listpattern-initializer | pattern-initializer , pattern-initializer-list pattern-initializerpattern initializer opt initializer= expression

变量声明语法 variable-declarationvariable-declaration-head pattern-initializer-list variable-declarationvariable-declaration-head variable-name type-annotation code-block variable-declarationvariable-declaration-head variable-name type-annotation getter-setter-block variable-declarationvariable-declaration-head variable-name type-annotation getter-setter-keyword-block variable-declarationvariable-declaration-head variable-name type-annotation initializer opt willSet-didSet-block variable-declaration-headattributes opt declaration-specifiers opt var variable-nameidentifier getter-setter-block{ getter-clause setter-clause opt } getter-setter-block{ setter-clause getter-clause } getter-clauseattributes opt get code-block setter-clauseattributes opt set setter-name opt code-block setter-name( identifier ) getter-setter-keyword-block{ getter-keyword-clause setter-keyword-clause opt } getter-setter-keyword-block{ setter-keyword-clause getter-keyword-clause } getter-keyword-clauseattributes opt get setter-keyword-clauseattributes opt set willSet-didSet-block{ willSet-clause didSet-clause opt } willSet-didSet-block{ didSet-clause willSet-clause } willSet-clauseattributes opt willSet setter-name opt code-block didSet-clauseattributes opt didSet setter-name opt code-block

类型别名声明语法 typealias-declarationtypealias-head typealias-assignment typealias-headtypealias typealias-name typealias-nameidentifier typealias-assignment= type

函数声明语法 function-declarationfunction-head function-name generic-parameter-clause opt function-signature function-body function-headattributes opt declaration-specifiers opt func function-nameidentifier | operator function-signatureparameter-clauses function-result opt function-result-> attributes opt type function-bodycode-block parameter-clausesparameter-clause parameter-clauses opt parameter-clause( ) | ( parameter-list opt ) parameter-listparameter | parameter , parameter-list parameterinout opt let opt # opt parameter-name local-parameter-name opt type-annotation default-argument-clause opt parameterinout opt var # opt parameter-name local-parameter-name opt type-annotation default-argument-clause opt parameterattributes opt type parameter-nameidentifier | _ local-parameter-nameidentifier | _ default-argument-clause= expression

枚举声明语法 enum-declarationattributes opt union-style-enum | attributes opt raw-value-style-enum union-style-enumenum-name generic-parameter-clause opt { union-style-enum-members opt } union-style-enum-membersunion-style-enum-member union-style-enum-members opt union-style-enum-memberdeclaration | union-style-enum-case-clause union-style-enum-case-clauseattributes opt case union-style-enum-case-list union-style-enum-case-listunion-style-enum-case | union-style-enum-case , union-style-enum-case-list union-style-enum-caseenum-case-name tuple-type opt enum-nameidentifier enum-case-nameidentifier raw-value-style-enumenum-name generic-parameter-clause opt : type-identifier { raw-value-style-enum-members opt } raw-value-style-enum-membersraw-value-style-enum-member raw-value-style-enum-members opt raw-value-style-enum-memberdeclaration | raw-value-style-enum-case-clause raw-value-style-enum-case-clauseattributes opt case raw-value-style-enum-case-list raw-value-style-enum-case-listraw-value-style-enum-case | raw-value-style-enum-case , raw-value-style-enum-case-list raw-value-style-enum-caseenum-case-name raw-value-assignment opt raw-value-assignment= literal

结构体声明语法 struct-declarationattributes opt struct struct-name generic-parameter-clause opt type-inheritance-clause opt struct-body struct-nameidentifier struct-body{ declarations opt }

类声明语法 class-declarationattributes opt class class-name generic-parameter-clause opt type-inheritance-clause opt class-body class-nameidentifier class-body{ declarations opt }

协议声明语法 protocol-declarationattributes opt protocol protocol-name type-inheritance-clause opt protocol-body protocol-nameidentifier protocol-body{ protocol-member-declarations opt } protocol-member-declarationprotocol-property-declaration protocol-member-declarationprotocol-method-declaration protocol-member-declarationprotocol-initializer-declaration protocol-member-declarationprotocol-subscript-declaration protocol-member-declarationprotocol-associated-type-declaration protocol-member-declarationsprotocol-member-declaration protocol-member-declarations opt

协议属性声明语法 protocol-property-declarationvariable-declaration-head variable-name type-annotation getter-setter-keyword-block

协议方法声明语法 protocol-method-declarationfunction-head function-name generic-parameter-clause opt function-signature

协议构造函数声明语法 protocol-initializer-declarationinitializer-head generic-parameter-clause opt parameter-clause

协议附属脚本声明语法 protocol-subscript-declarationsubscript-head subscript-result getter-setter-keyword-block

协议关联类型声明语法 protocol-associated-type-declarationtypealias-head type-inheritance-clause opt typealias-assignment opt

构造函数声明语法 initializer-declarationinitializer-head generic-parameter-clause opt parameter-clause initializer-body initializer-headattributes opt convenience opt init initializer-bodycode-block

析构函数声明语法 deinitializer-declarationattributes opt deinit code-block

扩展声明语法 extension-declarationextension type-identifier type-inheritance-clause opt extension-body extension-body{ declarations opt }

附属脚本声明语法 subscript-declarationsubscript-head subscript-result code-block subscript-declarationsubscript-head subscript-result getter-setter-block subscript-declarationsubscript-head subscript-result getter-setter-keyword-block subscript-headattributes opt subscript parameter-clause subscript-result-> attributes opt type

运算符声明语法 operator-declarationprefix-operator-declaration | postfix-operator-declaration | infix-operator-declaration prefix-operator-declarationoperator prefix operator { } postfix-operator-declarationoperator postfix operator { } infix-operator-declarationoperator infix operator { infix-operator-attributes opt } infix-operator-attributesprecedence-clause opt associativity-clause opt precedence-clauseprecedence precedence-level precedence-level → Digit 0 through 255 associativity-clauseassociativity associativity associativityleft | right | none

模式

模式语法 patternwildcard-pattern type-annotation opt patternidentifier-pattern type-annotation opt patternvalue-binding-pattern patterntuple-pattern type-annotation opt patternenum-case-pattern patterntype-casting-pattern patternexpression-pattern

通配符模式语法 wildcard-pattern_

标识符模式语法 identifier-patternidentifier

值绑定模式语法 value-binding-patternvar pattern | let pattern

元组模式语法 tuple-pattern( tuple-pattern-element-list opt ) tuple-pattern-element-listtuple-pattern-element | tuple-pattern-element , tuple-pattern-element-list tuple-pattern-elementpattern

枚举用例模式语法 enum-case-patterntype-identifier opt . enum-case-name tuple-pattern opt

类型转换模式语法 type-casting-patternis-pattern | as-pattern is-patternis type as-patternpattern as type

表达式模式语法 expression-patternexpression

特性

特性语法 attribute@ attribute-name attribute-argument-clause opt attribute-nameidentifier attribute-argument-clause( balanced-tokens opt ) attributesattribute attributes opt balanced-tokensbalanced-token balanced-tokens opt balanced-token( balanced-tokens opt ) balanced-token[ balanced-tokens opt ] balanced-token{ balanced-tokens opt } balanced-token → Any identifier, keyword, literal, or operator balanced-token → Any punctuation except (­, )­, [­, ]­, {­, or }­

表达式

表达式语法 expressionprefix-expression binary-expressions opt expression-listexpression | expression , expression-list

前缀表达式语法 prefix-expressionprefix-operator opt postfix-expression prefix-expressionin-out-expression in-out-expression& identifier

二进制表达式语法 binary-expressionbinary-operator prefix-expression binary-expressionassignment-operator prefix-expression binary-expressionconditional-operator prefix-expression binary-expressiontype-casting-operator binary-expressionsbinary-expression binary-expressions opt

赋值运算符语法 assignment-operator=

条件运算符语法 conditional-operator? expression :

类型转换运算符语法 type-casting-operatoris type | as ? opt type

主表达式语法 primary-expressionidentifier generic-argument-clause opt primary-expressionliteral-expression primary-expressionself-expression primary-expressionsuperclass-expression primary-expressionclosure-expression primary-expressionparenthesized-expression primary-expressionimplicit-member-expression primary-expressionwildcard-expression

字面量表达式语法 literal-expressionliteral literal-expressionarray-literal | dictionary-literal literal-expression__FILE__ | __LINE__ | __COLUMN__ | __FUNCTION__ array-literal[ array-literal-items opt ] array-literal-itemsarray-literal-item , opt | array-literal-item , array-literal-items array-literal-itemexpression dictionary-literal[ dictionary-literal-items ] | [ : ] dictionary-literal-itemsdictionary-literal-item , opt | dictionary-literal-item , dictionary-literal-items dictionary-literal-itemexpression : expression

Self 表达式语法 self-expressionself self-expressionself . identifier self-expressionself [ expression ] self-expressionself . init

超类表达式语法 superclass-expressionsuperclass-method-expression | superclass-subscript-expression | superclass-initializer-expression superclass-method-expressionsuper . identifier superclass-subscript-expressionsuper [ expression ] superclass-initializer-expressionsuper . init

闭包表达式语法 closure-expression{ closure-signature opt statements } closure-signatureparameter-clause function-result opt in closure-signatureidentifier-list function-result opt in closure-signaturecapture-list parameter-clause function-result opt in closure-signaturecapture-list identifier-list function-result opt in closure-signaturecapture-list in capture-list[ capture-specifier expression ] capture-specifierweak | unowned | unowned(safe) | unowned(unsafe)

隐式成员表达式语法 implicit-member-expression. identifier

带圆括号的表达式语法 parenthesized-expression( expression-element-list opt ) expression-element-listexpression-element | expression-element , expression-element-list expression-elementexpression | identifier : expression

通配符表达式语法 wildcard-expression_

后缀表达式语法 postfix-expressionprimary-expression postfix-expressionpostfix-expression postfix-operator postfix-expressionfunction-call-expression postfix-expressioninitializer-expression postfix-expressionexplicit-member-expression postfix-expressionpostfix-self-expression postfix-expressiondynamic-type-expression postfix-expressionsubscript-expression postfix-expressionforced-value-expression postfix-expressionoptional-chaining-expression

函数调用表达式语法 function-call-expressionpostfix-expression parenthesized-expression function-call-expressionpostfix-expression parenthesized-expression opt trailing-closure trailing-closureclosure-expression

初始化表达式语法 initializer-expressionpostfix-expression . init

显式成员表达式语法 explicit-member-expressionpostfix-expression . decimal-digit explicit-member-expressionpostfix-expression . identifier generic-argument-clause opt

Self 表达式语法 postfix-self-expressionpostfix-expression . self

动态类型表达式语法 dynamic-type-expressionpostfix-expression . dynamicType

附属脚本表达式语法 subscript-expressionpostfix-expression [ expression-list ]

Forced-value 表达式语法 forced-value-expressionpostfix-expression !

可选链表达式语法 optional-chaining-expressionpostfix-expression ?

词法结构

标识符语法 identifieridentifier-head identifier-characters opt identifier` identifier-head identifier-characters opt ` identifierimplicit-parameter-name identifier-listidentifier | identifier , identifier-list identifier-head → Upper- or lowercase letter A through Z identifier-head → U+00A8, U+00AA, U+00AD, U+00AF, U+00B2–U+00B5, or U+00B7–U+00BA identifier-head → U+00BC–U+00BE, U+00C0–U+00D6, U+00D8–U+00F6, or U+00F8–U+00FF identifier-head → U+0100–U+02FF, U+0370–U+167F, U+1681–U+180D, or U+180F–U+1DBF identifier-head → U+1E00–U+1FFF identifier-head → U+200B–U+200D, U+202A–U+202E, U+203F–U+2040, U+2054, or U+2060–U+206F identifier-head → U+2070–U+20CF, U+2100–U+218F, U+2460–U+24FF, or U+2776–U+2793 identifier-head → U+2C00–U+2DFF or U+2E80–U+2FFF identifier-head → U+3004–U+3007, U+3021–U+302F, U+3031–U+303F, or U+3040–U+D7FF identifier-head → U+F900–U+FD3D, U+FD40–U+FDCF, U+FDF0–U+FE1F, or U+FE30–U+FE44 identifier-head → U+FE47–U+FFFD identifier-head → U+10000–U+1FFFD, U+20000–U+2FFFD, U+30000–U+3FFFD, or U+40000–U+4FFFD identifier-head → U+50000–U+5FFFD, U+60000–U+6FFFD, U+70000–U+7FFFD, or U+80000–U+8FFFD identifier-head → U+90000–U+9FFFD, U+A0000–U+AFFFD, U+B0000–U+BFFFD, or U+C0000–U+CFFFD identifier-head → U+D0000–U+DFFFD or U+E0000–U+EFFFD identifier-character → Digit 0 through 9 identifier-character → U+0300–U+036F, U+1DC0–U+1DFF, U+20D0–U+20FF, or U+FE20–U+FE2F identifier-characteridentifier-head identifier-charactersidentifier-character identifier-characters opt implicit-parameter-name$ decimal-digits

字面量语法 literalinteger-literal | floating-point-literal | string-literal

整形字面量语法 integer-literalbinary-literal integer-literaloctal-literal integer-literaldecimal-literal integer-literalhexadecimal-literal binary-literal0b binary-digit binary-literal-characters opt binary-digit → Digit 0 or 1 binary-literal-characterbinary-digit | _ binary-literal-charactersbinary-literal-character binary-literal-characters opt octal-literal0o octal-digit octal-literal-characters opt octal-digit → Digit 0 through 7 octal-literal-characteroctal-digit | _ octal-literal-charactersoctal-literal-character octal-literal-characters opt decimal-literaldecimal-digit decimal-literal-characters opt decimal-digit → Digit 0 through 9 decimal-digitsdecimal-digit decimal-digits opt decimal-literal-characterdecimal-digit | _ decimal-literal-charactersdecimal-literal-character decimal-literal-characters opt hexadecimal-literal0x hexadecimal-digit hexadecimal-literal-characters opt hexadecimal-digit → Digit 0 through 9, a through f, or A through F hexadecimal-literal-characterhexadecimal-digit | _ hexadecimal-literal-charactershexadecimal-literal-character hexadecimal-literal-characters opt

浮点型字面量语法 floating-point-literaldecimal-literal decimal-fraction opt decimal-exponent opt floating-point-literalhexadecimal-literal hexadecimal-fraction opt hexadecimal-exponent decimal-fraction. decimal-literal decimal-exponentfloating-point-e sign opt decimal-literal hexadecimal-fraction. hexadecimal-literal opt hexadecimal-exponentfloating-point-p sign opt hexadecimal-literal floating-point-ee | E floating-point-pp | P sign+ | -

字符型字面量语法 string-literal quoted-text quoted-textquoted-text-item quoted-text opt quoted-text-itemescaped-character quoted-text-item( expression ) quoted-text-item → Any Unicode extended grapheme cluster except “­, \­, U+000A, or U+000D escaped-character\0 | \ | \t | \n | \r | \” | \’ escaped-character\x hexadecimal-digit hexadecimal-digit escaped-character\u hexadecimal-digit hexadecimal-digit hexadecimal-digit hexadecimal-digit escaped-character\U hexadecimal-digit hexadecimal-digit hexadecimal-digit hexadecimal-digit hexadecimal-digit hexadecimal-digit hexadecimal-digit hexadecimal-digit

运算符语法语法 operatoroperator-character operator opt operator-character/ | = | - | + | ! | * | % | < | > | & | | | ^ | ~ | . binary-operatoroperator prefix-operatoroperator postfix-operatoroperator

类型

类型语法 typearray-type | function-type | type-identifier | tuple-type | optional-type | implicitly-unwrapped-optional-type | protocol-composition-type | metatype-type

类型标注语法 type-annotation: attributes opt type

类型标识语法 type-identifiertype-name generic-argument-clause opt | type-name generic-argument-clause opt . type-identifier type-nameidentifier

元组类型语法 tuple-type( tuple-type-body opt ) tuple-type-bodytuple-type-element-list opt tuple-type-element-listtuple-type-element | tuple-type-element , tuple-type-element-list tuple-type-elementattributes opt inout opt type | inout opt element-name type-annotation element-nameidentifier

函数类型语法 function-typetype -> type

数组类型语法 array-typetype [ ] | array-type [ ]

可选类型语法 optional-typetype ?

隐式解析可选类型语法 implicitly-unwrapped-optional-typetype !

协议合成类型语法 protocol-composition-typeprotocol < protocol-identifier-list opt > protocol-identifier-listprotocol-identifier | protocol-identifier , protocol-identifier-list protocol-identifiertype-identifier

元类型语法 metatype-typetype . Type | type . Protocol

类型继承子句语法 type-inheritance-clause: type-inheritance-list type-inheritance-listtype-identifier | type-identifier , type-inheritance-list