Skip to content

Latest commit

 

History

History
1107 lines (974 loc) · 51 KB

SummaryOfTheGrammar.md

File metadata and controls

1107 lines (974 loc) · 51 KB

Summary of the Grammar

Read the whole formal grammar.

Lexical Structure

Grammar of whitespace:

whitespacewhitespace-item whitespace?
whitespace-itemline-break
whitespace-iteminline-space
whitespace-itemcomment
whitespace-itemmultiline-comment
whitespace-item → U+0000, U+000B, or U+000C

line-break → U+000A
line-break → U+000D
line-break → U+000D followed by U+000A

inline-spacesinline-space inline-spaces?
inline-space → U+0009 or U+0020

comment// comment-text line-break
multiline-comment/* multiline-comment-text */

comment-textcomment-text-item comment-text?
comment-text-item → Any Unicode scalar value except U+000A or U+000D

multiline-comment-textmultiline-comment-text-item multiline-comment-text?
multiline-comment-text-itemmultiline-comment
multiline-comment-text-itemcomment-text-item
multiline-comment-text-item → Any Unicode scalar value except /* or */

Grammar of an identifier:

identifieridentifier-head identifier-characters?
identifier` identifier-head identifier-characters? `
identifierimplicit-parameter-name
identifierproperty-wrapper-projection
identifier-listidentifier | identifier , identifier-list

identifier-head → Upper- or lowercase letter A through Z
identifier-head_
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?

implicit-parameter-name$ decimal-digits
property-wrapper-projection$ identifier-characters

Grammar of a literal:

literalnumeric-literal | string-literal | regular-expression-literal | boolean-literal | nil-literal

numeric-literal-? integer-literal | -? floating-point-literal
boolean-literaltrue | false
nil-literalnil

Grammar of an integer literal:

integer-literalbinary-literal
integer-literaloctal-literal
integer-literaldecimal-literal
integer-literalhexadecimal-literal

binary-literal0b binary-digit binary-literal-characters?
binary-digit → Digit 0 or 1
binary-literal-characterbinary-digit | _
binary-literal-charactersbinary-literal-character binary-literal-characters?

octal-literal0o octal-digit octal-literal-characters?
octal-digit → Digit 0 through 7
octal-literal-characteroctal-digit | _
octal-literal-charactersoctal-literal-character octal-literal-characters?

decimal-literaldecimal-digit decimal-literal-characters?
decimal-digit → Digit 0 through 9
decimal-digitsdecimal-digit decimal-digits?
decimal-literal-characterdecimal-digit | _
decimal-literal-charactersdecimal-literal-character decimal-literal-characters?

hexadecimal-literal0x hexadecimal-digit hexadecimal-literal-characters?
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?

Grammar of a floating-point literal:

floating-point-literaldecimal-literal decimal-fraction? decimal-exponent?
floating-point-literalhexadecimal-literal hexadecimal-fraction? hexadecimal-exponent

decimal-fraction. decimal-literal
decimal-exponentfloating-point-e sign? decimal-literal

hexadecimal-fraction. hexadecimal-digit hexadecimal-literal-characters?
hexadecimal-exponentfloating-point-p sign? decimal-literal

floating-point-ee | E
floating-point-pp | P
sign+ | -

Grammar of a string literal:

string-literalstatic-string-literal | interpolated-string-literal

string-literal-opening-delimiterextended-string-literal-delimiter? "
string-literal-closing-delimiter" extended-string-literal-delimiter?

static-string-literalstring-literal-opening-delimiter quoted-text? string-literal-closing-delimiter
static-string-literalmultiline-string-literal-opening-delimiter multiline-quoted-text? multiline-string-literal-closing-delimiter

multiline-string-literal-opening-delimiterextended-string-literal-delimiter? """
multiline-string-literal-closing-delimiter""" extended-string-literal-delimiter?
extended-string-literal-delimiter# extended-string-literal-delimiter?

quoted-textquoted-text-item quoted-text?
quoted-text-itemescaped-character
quoted-text-item → Any Unicode scalar value except ", \, U+000A, or U+000D

multiline-quoted-textmultiline-quoted-text-item multiline-quoted-text?
multiline-quoted-text-itemescaped-character
multiline-quoted-text-item → Any Unicode scalar value except \
multiline-quoted-text-itemescaped-newline

interpolated-string-literalstring-literal-opening-delimiter interpolated-text? string-literal-closing-delimiter
interpolated-string-literalmultiline-string-literal-opening-delimiter multiline-interpolated-text? multiline-string-literal-closing-delimiter

interpolated-textinterpolated-text-item interpolated-text?
interpolated-text-item\( expression ) | quoted-text-item

multiline-interpolated-textmultiline-interpolated-text-item multiline-interpolated-text?
multiline-interpolated-text-item\( expression ) | multiline-quoted-text-item

escape-sequence\ extended-string-literal-delimiter
escaped-characterescape-sequence 0 | escape-sequence \ | escape-sequence t | escape-sequence n | escape-sequence r | escape-sequence " | escape-sequence '
escaped-characterescape-sequence u { unicode-scalar-digits }
unicode-scalar-digits → Between one and eight hexadecimal digits

escaped-newlineescape-sequence inline-spaces? line-break

Grammar of a regular expression literal:

regular-expression-literalregular-expression-literal-opening-delimiter regular-expression regular-expression-literal-closing-delimiter
regular-expression → Any regular expression

regular-expression-literal-opening-delimiterextended-regular-expression-literal-delimiter? /
regular-expression-literal-closing-delimiter/ extended-regular-expression-literal-delimiter?

extended-regular-expression-literal-delimiter# extended-regular-expression-literal-delimiter?

Grammar of operators:

operatoroperator-head operator-characters?
operatordot-operator-head dot-operator-characters

operator-head/ | = | - | + | ! | * | % | < | > | & | | | ^ | ~ | ?
operator-head → U+00A1–U+00A7
operator-head → U+00A9 or U+00AB
operator-head → U+00AC or U+00AE
operator-head → U+00B0–U+00B1
operator-head → U+00B6, U+00BB, U+00BF, U+00D7, or U+00F7
operator-head → U+2016–U+2017
operator-head → U+2020–U+2027
operator-head → U+2030–U+203E
operator-head → U+2041–U+2053
operator-head → U+2055–U+205E
operator-head → U+2190–U+23FF
operator-head → U+2500–U+2775
operator-head → U+2794–U+2BFF
operator-head → U+2E00–U+2E7F
operator-head → U+3001–U+3003
operator-head → U+3008–U+3020
operator-head → U+3030

operator-characteroperator-head
operator-character → U+0300–U+036F
operator-character → U+1DC0–U+1DFF
operator-character → U+20D0–U+20FF
operator-character → U+FE00–U+FE0F
operator-character → U+FE20–U+FE2F
operator-character → U+E0100–U+E01EF
operator-charactersoperator-character operator-characters?

dot-operator-head.
dot-operator-character. | operator-character
dot-operator-charactersdot-operator-character dot-operator-characters?

infix-operatoroperator
prefix-operatoroperator
postfix-operatoroperator

Types

Grammar of a type:

typefunction-type
typearray-type
typedictionary-type
typetype-identifier
typetuple-type
typeoptional-type
typeimplicitly-unwrapped-optional-type
typeprotocol-composition-type
typeopaque-type
typemetatype-type
typeany-type
typeself-type
type( type )

Grammar of a type annotation:

type-annotation: attributes? inout? type

Grammar of a type identifier:

type-identifiertype-name generic-argument-clause? | type-name generic-argument-clause? . type-identifier
type-nameidentifier

Grammar of a tuple type:

tuple-type( ) | ( tuple-type-element , tuple-type-element-list )
tuple-type-element-listtuple-type-element | tuple-type-element , tuple-type-element-list
tuple-type-elementelement-name type-annotation | type
element-nameidentifier

Grammar of a function type:

function-typeattributes? function-type-argument-clause async? throws-clause? -> type

function-type-argument-clause( )
function-type-argument-clause( function-type-argument-list ...? )

function-type-argument-listfunction-type-argument | function-type-argument , function-type-argument-list
function-type-argumentattributes? inout? type | argument-label type-annotation
argument-labelidentifier

throws-clausethrows | throws ( type )

Grammar of an array type:

array-type[ type ]

Grammar of a dictionary type:

dictionary-type[ type : type ]

Grammar of an optional type:

optional-typetype ?

Grammar of an implicitly unwrapped optional type:

implicitly-unwrapped-optional-typetype !

Grammar of a protocol composition type:

protocol-composition-typetype-identifier & protocol-composition-continuation
protocol-composition-continuationtype-identifier | protocol-composition-type

Grammar of an opaque type:

opaque-typesome type

Grammar of a boxed protocol type:

boxed-protocol-typeany type

Grammar of a metatype type:

metatype-typetype . Type | type . Protocol

Grammar of an Any type:

any-typeAny

Grammar of a Self type:

self-typeSelf

Grammar of a type inheritance clause:

type-inheritance-clause: type-inheritance-list
type-inheritance-listattributes? type-identifier | attributes? type-identifier , type-inheritance-list

Expressions

Grammar of an expression:

expressiontry-operator? await-operator? prefix-expression infix-expressions?

Grammar of a prefix expression:

prefix-expressionprefix-operator? postfix-expression
prefix-expressionin-out-expression

Grammar of an in-out expression:

in-out-expression& primary-expression

Grammar of a try expression:

try-operatortry | try ? | try !

Grammar of an await expression:

await-operatorawait

Grammar of an infix expression:

infix-expressioninfix-operator prefix-expression
infix-expressionassignment-operator try-operator? await-operator? prefix-expression
infix-expressionconditional-operator try-operator? await-operator? prefix-expression
infix-expressiontype-casting-operator
infix-expressionsinfix-expression infix-expressions?

Grammar of an assignment operator:

assignment-operator=

Grammar of a conditional operator:

conditional-operator? expression :

Grammar of a type-casting operator:

type-casting-operatoris type
type-casting-operatoras type
type-casting-operatoras ? type
type-casting-operatoras ! type

Grammar of a primary expression:

primary-expressionidentifier generic-argument-clause?
primary-expressionliteral-expression
primary-expressionself-expression
primary-expressionsuperclass-expression
primary-expressionconditional-expression
primary-expressionclosure-expression
primary-expressionparenthesized-expression
primary-expressiontuple-expression
primary-expressionimplicit-member-expression
primary-expressionwildcard-expression
primary-expressionmacro-expansion-expression
primary-expressionkey-path-expression
primary-expressionselector-expression
primary-expressionkey-path-string-expression

Grammar of a literal expression:

literal-expressionliteral
literal-expressionarray-literal | dictionary-literal | playground-literal

array-literal[ array-literal-items? ]
array-literal-itemsarray-literal-item ,? | array-literal-item , array-literal-items
array-literal-itemexpression

dictionary-literal[ dictionary-literal-items ] | [ : ]
dictionary-literal-itemsdictionary-literal-item ,? | dictionary-literal-item , dictionary-literal-items
dictionary-literal-itemexpression : expression

playground-literal#colorLiteral ( red : expression , green : expression , blue : expression , alpha : expression )
playground-literal#fileLiteral ( resourceName : expression )
playground-literal#imageLiteral ( resourceName : expression )

Grammar of a self expression:

self-expressionself | self-method-expression | self-subscript-expression | self-initializer-expression

self-method-expressionself . identifier
self-subscript-expressionself [ function-call-argument-list ]
self-initializer-expressionself . init

Grammar of a superclass expression:

superclass-expressionsuperclass-method-expression | superclass-subscript-expression | superclass-initializer-expression

superclass-method-expressionsuper . identifier
superclass-subscript-expressionsuper [ function-call-argument-list ]
superclass-initializer-expressionsuper . init

Grammar of a conditional expression:

conditional-expressionif-expression | switch-expression

if-expressionif condition-list { statement } if-expression-tail
if-expression-tailelse if-expression
if-expression-tailelse { statement }

switch-expressionswitch expression { switch-expression-cases }
switch-expression-casesswitch-expression-case switch-expression-cases?
switch-expression-casecase-label statement
switch-expression-casedefault-label statement

Grammar of a closure expression:

closure-expression{ attributes? closure-signature? statements? }

closure-signaturecapture-list? closure-parameter-clause async? throws-clause? function-result? in
closure-signaturecapture-list in

closure-parameter-clause( ) | ( closure-parameter-list ) | identifier-list
closure-parameter-listclosure-parameter | closure-parameter , closure-parameter-list
closure-parameterclosure-parameter-name type-annotation?
closure-parameterclosure-parameter-name type-annotation ...
closure-parameter-nameidentifier

capture-list[ capture-list-items ]
capture-list-itemscapture-list-item | capture-list-item , capture-list-items
capture-list-itemcapture-specifier? identifier
capture-list-itemcapture-specifier? identifier = expression
capture-list-itemcapture-specifier? self-expression
capture-specifierweak | unowned | unowned(safe) | unowned(unsafe)

Grammar of an implicit member expression:

implicit-member-expression. identifier
implicit-member-expression. identifier . postfix-expression

Grammar of a parenthesized expression:

parenthesized-expression( expression )

Grammar of a tuple expression:

tuple-expression( ) | ( tuple-element , tuple-element-list )
tuple-element-listtuple-element | tuple-element , tuple-element-list
tuple-elementexpression | identifier : expression

Grammar of a wildcard expression:

wildcard-expression_

Grammar of a macro-expansion expression:

macro-expansion-expression# identifier generic-argument-clause? function-call-argument-clause? trailing-closures?

Grammar of a key-path expression:

key-path-expression\ type? . key-path-components
key-path-componentskey-path-component | key-path-component . key-path-components
key-path-componentidentifier key-path-postfixes? | key-path-postfixes

key-path-postfixeskey-path-postfix key-path-postfixes?
key-path-postfix? | ! | self | [ function-call-argument-list ]

Grammar of a selector expression:

selector-expression#selector ( expression )
selector-expression#selector ( getter: expression )
selector-expression#selector ( setter: expression )

Grammar of a key-path string expression:

key-path-string-expression#keyPath ( expression )

Grammar of a postfix 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-expressionsubscript-expression
postfix-expressionforced-value-expression
postfix-expressionoptional-chaining-expression

Grammar of a function call expression:

function-call-expressionpostfix-expression function-call-argument-clause
function-call-expressionpostfix-expression function-call-argument-clause? trailing-closures

function-call-argument-clause( ) | ( function-call-argument-list )
function-call-argument-listfunction-call-argument | function-call-argument , function-call-argument-list
function-call-argumentexpression | identifier : expression
function-call-argumentoperator | identifier : operator

trailing-closuresclosure-expression labeled-trailing-closures?
labeled-trailing-closureslabeled-trailing-closure labeled-trailing-closures?
labeled-trailing-closureidentifier : closure-expression

Grammar of an initializer expression:

initializer-expressionpostfix-expression . init
initializer-expressionpostfix-expression . init ( argument-names )

Grammar of an explicit member expression:

explicit-member-expressionpostfix-expression . decimal-digits
explicit-member-expressionpostfix-expression . identifier generic-argument-clause?
explicit-member-expressionpostfix-expression . identifier ( argument-names )
explicit-member-expressionpostfix-expression conditional-compilation-block

argument-namesargument-name argument-names?
argument-nameidentifier :

Grammar of a postfix self expression:

postfix-self-expressionpostfix-expression . self

Grammar of a subscript expression:

subscript-expressionpostfix-expression [ function-call-argument-list ]

Grammar of a forced-value expression:

forced-value-expressionpostfix-expression !

Grammar of an optional-chaining expression:

optional-chaining-expressionpostfix-expression ?

Statements

Grammar of a statement:

statementexpression ;?
statementdeclaration ;?
statementloop-statement ;?
statementbranch-statement ;?
statementlabeled-statement ;?
statementcontrol-transfer-statement ;?
statementdefer-statement ;?
statementdo-statement ;?
statementcompiler-control-statement
statementsstatement statements?

Grammar of a loop statement:

loop-statementfor-in-statement
loop-statementwhile-statement
loop-statementrepeat-while-statement

Grammar of a for-in statement:

for-in-statementfor case? pattern in expression where-clause? code-block

Grammar of a while statement:

while-statementwhile condition-list code-block

condition-listcondition | condition , condition-list
conditionexpression | availability-condition | case-condition | optional-binding-condition

case-conditioncase pattern initializer
optional-binding-conditionlet pattern initializer? | var pattern initializer?

Grammar of a repeat-while statement:

repeat-while-statementrepeat code-block while expression

Grammar of a branch statement:

branch-statementif-statement
branch-statementguard-statement
branch-statementswitch-statement

Grammar of an if statement:

if-statementif condition-list code-block else-clause?
else-clauseelse code-block | else if-statement

Grammar of a guard statement:

guard-statementguard condition-list else code-block

Grammar of a switch statement:

switch-statementswitch expression { switch-cases? }
switch-casesswitch-case switch-cases?
switch-casecase-label statements
switch-casedefault-label statements
switch-caseconditional-switch-case

case-labelattributes? case case-item-list :
case-item-listpattern where-clause? | pattern where-clause? , case-item-list
default-labelattributes? default :

where-clausewhere where-expression
where-expressionexpression

conditional-switch-caseswitch-if-directive-clause switch-elseif-directive-clauses? switch-else-directive-clause? endif-directive
switch-if-directive-clauseif-directive compilation-condition switch-cases?
switch-elseif-directive-clauseselseif-directive-clause switch-elseif-directive-clauses?
switch-elseif-directive-clauseelseif-directive compilation-condition switch-cases?
switch-else-directive-clauseelse-directive switch-cases?

Grammar of a labeled statement:

labeled-statementstatement-label loop-statement
labeled-statementstatement-label if-statement
labeled-statementstatement-label switch-statement
labeled-statementstatement-label do-statement

statement-labellabel-name :
label-nameidentifier

Grammar of a control transfer statement:

control-transfer-statementbreak-statement
control-transfer-statementcontinue-statement
control-transfer-statementfallthrough-statement
control-transfer-statementreturn-statement
control-transfer-statementthrow-statement

Grammar of a break statement:

break-statementbreak label-name?

Grammar of a continue statement:

continue-statementcontinue label-name?

Grammar of a fallthrough statement:

fallthrough-statementfallthrough

Grammar of a return statement:

return-statementreturn expression?

Grammar of a throw statement:

throw-statementthrow expression

Grammar of a defer statement:

defer-statementdefer code-block

Grammar of a do statement:

do-statementdo throws-clause? code-block catch-clauses?
catch-clausescatch-clause catch-clauses?
catch-clausecatch catch-pattern-list? code-block
catch-pattern-listcatch-pattern | catch-pattern , catch-pattern-list
catch-patternpattern where-clause?

Grammar of a compiler control statement:

compiler-control-statementconditional-compilation-block
compiler-control-statementline-control-statement
compiler-control-statementdiagnostic-statement

Grammar of a conditional compilation block:

conditional-compilation-blockif-directive-clause elseif-directive-clauses? else-directive-clause? endif-directive

if-directive-clauseif-directive compilation-condition statements?
elseif-directive-clauseselseif-directive-clause elseif-directive-clauses?
elseif-directive-clauseelseif-directive compilation-condition statements?
else-directive-clauseelse-directive statements?
if-directive#if
elseif-directive#elseif
else-directive#else
endif-directive#endif

compilation-conditionplatform-condition
compilation-conditionidentifier
compilation-conditionboolean-literal
compilation-condition( compilation-condition )
compilation-condition! compilation-condition
compilation-conditioncompilation-condition && compilation-condition
compilation-conditioncompilation-condition || compilation-condition

platform-conditionos ( operating-system )
platform-conditionarch ( architecture )
platform-conditionswift ( >= swift-version ) | swift ( < swift-version )
platform-conditioncompiler ( >= swift-version ) | compiler ( < swift-version )
platform-conditioncanImport ( import-path )
platform-conditiontargetEnvironment ( environment )

operating-systemmacOS | iOS | watchOS | tvOS | visionOS | Linux | Windows
architecturei386 | x86_64 | arm | arm64
swift-versiondecimal-digits swift-version-continuation?
swift-version-continuation. decimal-digits swift-version-continuation?
environmentsimulator | macCatalyst

Grammar of a line control statement:

line-control-statement#sourceLocation ( file: file-path , line: line-number )
line-control-statement#sourceLocation ( )
line-number → A decimal integer greater than zero
file-pathstatic-string-literal

Grammar of an availability condition:

availability-condition#available ( availability-arguments )
availability-condition#unavailable ( availability-arguments )
availability-argumentsavailability-argument | availability-argument , availability-arguments
availability-argumentplatform-name platform-version
availability-argument*

platform-nameiOS | iOSApplicationExtension
platform-namemacOS | macOSApplicationExtension
platform-namemacCatalyst | macCatalystApplicationExtension
platform-namewatchOS | watchOSApplicationExtension
platform-nametvOS | tvOSApplicationExtension
platform-namevisionOS | visionOSApplicationExtension
platform-versiondecimal-digits
platform-versiondecimal-digits . decimal-digits
platform-versiondecimal-digits . decimal-digits . decimal-digits

Declarations

Grammar of a declaration:

declarationimport-declaration
declarationconstant-declaration
declarationvariable-declaration
declarationtypealias-declaration
declarationfunction-declaration
declarationenum-declaration
declarationstruct-declaration
declarationclass-declaration
declarationactor-declaration
declarationprotocol-declaration
declarationinitializer-declaration
declarationdeinitializer-declaration
declarationextension-declaration
declarationsubscript-declaration
declarationoperator-declaration
declarationprecedence-group-declaration

Grammar of a top-level declaration:

top-level-declarationstatements?

Grammar of a code block:

code-block{ statements? }

Grammar of an import declaration:

import-declarationattributes? import import-kind? import-path

import-kindtypealias | struct | class | enum | protocol | let | var | func
import-pathidentifier | identifier . import-path

Grammar of a constant declaration:

constant-declarationattributes? declaration-modifiers? let pattern-initializer-list

pattern-initializer-listpattern-initializer | pattern-initializer , pattern-initializer-list
pattern-initializerpattern initializer?
initializer= expression

Grammar of a variable declaration:

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 initializer willSet-didSet-block
variable-declarationvariable-declaration-head variable-name type-annotation initializer? willSet-didSet-block

variable-declaration-headattributes? declaration-modifiers? var
variable-nameidentifier

getter-setter-blockcode-block
getter-setter-block{ getter-clause setter-clause? }
getter-setter-block{ setter-clause getter-clause }
getter-clauseattributes? mutation-modifier? get code-block
setter-clauseattributes? mutation-modifier? set setter-name? code-block
setter-name( identifier )

getter-setter-keyword-block{ getter-keyword-clause setter-keyword-clause? }
getter-setter-keyword-block{ setter-keyword-clause getter-keyword-clause }
getter-keyword-clauseattributes? mutation-modifier? get
setter-keyword-clauseattributes? mutation-modifier? set

willSet-didSet-block{ willSet-clause didSet-clause? }
willSet-didSet-block{ didSet-clause willSet-clause? }
willSet-clauseattributes? willSet setter-name? code-block
didSet-clauseattributes? didSet setter-name? code-block

Grammar of a type alias declaration:

typealias-declarationattributes? access-level-modifier? typealias typealias-name generic-parameter-clause? typealias-assignment
typealias-nameidentifier
typealias-assignment= type

Grammar of a function declaration:

function-declarationfunction-head function-name generic-parameter-clause? function-signature generic-where-clause? function-body?

function-headattributes? declaration-modifiers? func
function-nameidentifier | operator

function-signatureparameter-clause async? throws-clause? function-result?
function-signatureparameter-clause async? rethrows function-result?
function-result-> attributes? type
function-bodycode-block

parameter-clause( ) | ( parameter-list )
parameter-listparameter | parameter , parameter-list
parameterexternal-parameter-name? local-parameter-name parameter-type-annotation default-argument-clause?
parameterexternal-parameter-name? local-parameter-name parameter-type-annotation
parameterexternal-parameter-name? local-parameter-name parameter-type-annotation ...

external-parameter-nameidentifier
local-parameter-nameidentifier
parameter-type-annotation: attributes? parameter-modifier? type
parameter-modifierinout | borrowing | consuming default-argument-clause= expression

Grammar of an enumeration declaration:

enum-declarationattributes? access-level-modifier? union-style-enum
enum-declarationattributes? access-level-modifier? raw-value-style-enum

union-style-enumindirect? enum enum-name generic-parameter-clause? type-inheritance-clause? generic-where-clause? { union-style-enum-members? }
union-style-enum-membersunion-style-enum-member union-style-enum-members?
union-style-enum-memberdeclaration | union-style-enum-case-clause | compiler-control-statement
union-style-enum-case-clauseattributes? indirect? 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?
enum-nameidentifier
enum-case-nameidentifier

raw-value-style-enumenum enum-name generic-parameter-clause? type-inheritance-clause generic-where-clause? { raw-value-style-enum-members }
raw-value-style-enum-membersraw-value-style-enum-member raw-value-style-enum-members?
raw-value-style-enum-memberdeclaration | raw-value-style-enum-case-clause | compiler-control-statement
raw-value-style-enum-case-clauseattributes? 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?
raw-value-assignment= raw-value-literal
raw-value-literalnumeric-literal | static-string-literal | boolean-literal

Grammar of a structure declaration:

struct-declarationattributes? access-level-modifier? struct struct-name generic-parameter-clause? type-inheritance-clause? generic-where-clause? struct-body
struct-nameidentifier
struct-body{ struct-members? }

struct-membersstruct-member struct-members?
struct-memberdeclaration | compiler-control-statement

Grammar of a class declaration:

class-declarationattributes? access-level-modifier? final? class class-name generic-parameter-clause? type-inheritance-clause? generic-where-clause? class-body
class-declarationattributes? final access-level-modifier? class class-name generic-parameter-clause? type-inheritance-clause? generic-where-clause? class-body
class-nameidentifier
class-body{ class-members? }

class-membersclass-member class-members?
class-memberdeclaration | compiler-control-statement

Grammar of an actor declaration:

actor-declarationattributes? access-level-modifier? actor actor-name generic-parameter-clause? type-inheritance-clause? generic-where-clause? actor-body
actor-nameidentifier
actor-body{ actor-members? }

actor-membersactor-member actor-members?
actor-memberdeclaration | compiler-control-statement

Grammar of a protocol declaration:

protocol-declarationattributes? access-level-modifier? protocol protocol-name type-inheritance-clause? generic-where-clause? protocol-body
protocol-nameidentifier
protocol-body{ protocol-members? }

protocol-membersprotocol-member protocol-members?
protocol-memberprotocol-member-declaration | compiler-control-statement

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-declarationtypealias-declaration

Grammar of a protocol property declaration:

protocol-property-declarationvariable-declaration-head variable-name type-annotation getter-setter-keyword-block

Grammar of a protocol method declaration:

protocol-method-declarationfunction-head function-name generic-parameter-clause? function-signature generic-where-clause?

Grammar of a protocol initializer declaration:

protocol-initializer-declarationinitializer-head generic-parameter-clause? parameter-clause throws-clause? generic-where-clause?
protocol-initializer-declarationinitializer-head generic-parameter-clause? parameter-clause rethrows generic-where-clause?

Grammar of a protocol subscript declaration:

protocol-subscript-declarationsubscript-head subscript-result generic-where-clause? getter-setter-keyword-block

Grammar of a protocol associated type declaration:

protocol-associated-type-declarationattributes? access-level-modifier? associatedtype typealias-name type-inheritance-clause? typealias-assignment? generic-where-clause?

Grammar of an initializer declaration:

initializer-declarationinitializer-head generic-parameter-clause? parameter-clause async? throws-clause? generic-where-clause? initializer-body
initializer-declarationinitializer-head generic-parameter-clause? parameter-clause async? rethrows generic-where-clause? initializer-body
initializer-headattributes? declaration-modifiers? init
initializer-headattributes? declaration-modifiers? init ?
initializer-headattributes? declaration-modifiers? init !
initializer-bodycode-block

Grammar of a deinitializer declaration:

deinitializer-declarationattributes? deinit code-block

Grammar of an extension declaration:

extension-declarationattributes? access-level-modifier? extension type-identifier type-inheritance-clause? generic-where-clause? extension-body
extension-body{ extension-members? }

extension-membersextension-member extension-members?
extension-memberdeclaration | compiler-control-statement

Grammar of a subscript declaration:

subscript-declarationsubscript-head subscript-result generic-where-clause? code-block
subscript-declarationsubscript-head subscript-result generic-where-clause? getter-setter-block
subscript-declarationsubscript-head subscript-result generic-where-clause? getter-setter-keyword-block
subscript-headattributes? declaration-modifiers? subscript generic-parameter-clause? parameter-clause
subscript-result-> attributes? type

Grammar of a macro declaration:

macro-declarationmacro-head identifier generic-parameter-clause? macro-signature macro-definition? generic-where-clause
macro-headattributes? declaration-modifiers? macro
macro-signatureparameter-clause macro-function-signature-result?
macro-function-signature-result-> type
macro-definition= expression

Grammar of an operator declaration:

operator-declarationprefix-operator-declaration | postfix-operator-declaration | infix-operator-declaration

prefix-operator-declarationprefix operator operator
postfix-operator-declarationpostfix operator operator
infix-operator-declarationinfix operator operator infix-operator-group?

infix-operator-group: precedence-group-name

Grammar of a precedence group declaration:

precedence-group-declarationprecedencegroup precedence-group-name { precedence-group-attributes? }

precedence-group-attributesprecedence-group-attribute precedence-group-attributes?
precedence-group-attributeprecedence-group-relation
precedence-group-attributeprecedence-group-assignment
precedence-group-attributeprecedence-group-associativity

precedence-group-relationhigherThan : precedence-group-names
precedence-group-relationlowerThan : precedence-group-names

precedence-group-assignmentassignment : boolean-literal

precedence-group-associativityassociativity : left
precedence-group-associativityassociativity : right
precedence-group-associativityassociativity : none

precedence-group-namesprecedence-group-name | precedence-group-name , precedence-group-names
precedence-group-nameidentifier

Grammar of a declaration modifier:

declaration-modifierclass | convenience | dynamic | final | infix | lazy | optional | override | postfix | prefix | required | static | unowned | unowned ( safe ) | unowned ( unsafe ) | weak
declaration-modifieraccess-level-modifier
declaration-modifiermutation-modifier
declaration-modifieractor-isolation-modifier
declaration-modifiersdeclaration-modifier declaration-modifiers?

access-level-modifierprivate | private ( set )
access-level-modifierfileprivate | fileprivate ( set )
access-level-modifierinternal | internal ( set )
access-level-modifierpackage | package ( set )
access-level-modifierpublic | public ( set )
access-level-modifieropen | open ( set )

mutation-modifiermutating | nonmutating

actor-isolation-modifiernonisolated

Attributes

Grammar of an attribute:

attribute@ attribute-name attribute-argument-clause?
attribute-nameidentifier
attribute-argument-clause( balanced-tokens? )
attributesattribute attributes?

balanced-tokensbalanced-token balanced-tokens?
balanced-token( balanced-tokens? )
balanced-token[ balanced-tokens? ]
balanced-token{ balanced-tokens? }
balanced-token → Any identifier, keyword, literal, or operator
balanced-token → Any punctuation except (, ), [, ], {, or }

Patterns

Grammar of a pattern:

patternwildcard-pattern type-annotation?
patternidentifier-pattern type-annotation?
patternvalue-binding-pattern
patterntuple-pattern type-annotation?
patternenum-case-pattern
patternoptional-pattern
patterntype-casting-pattern
patternexpression-pattern

Grammar of a wildcard pattern:

wildcard-pattern_

Grammar of an identifier pattern:

identifier-patternidentifier

Grammar of a value-binding pattern:

value-binding-patternvar pattern | let pattern

Grammar of a tuple pattern:

tuple-pattern( tuple-pattern-element-list? )
tuple-pattern-element-listtuple-pattern-element | tuple-pattern-element , tuple-pattern-element-list
tuple-pattern-elementpattern | identifier : pattern

Grammar of an enumeration case pattern:

enum-case-patterntype-identifier? . enum-case-name tuple-pattern?

Grammar of an optional pattern:

optional-patternidentifier-pattern ?

Grammar of a type casting pattern:

type-casting-patternis-pattern | as-pattern
is-patternis type
as-patternpattern as type

Grammar of an expression pattern:

expression-patternexpression

Generic Parameters and Arguments

Grammar of a generic parameter clause:

generic-parameter-clause< generic-parameter-list >
generic-parameter-listgeneric-parameter | generic-parameter , generic-parameter-list
generic-parametertype-name
generic-parametertype-name : type-identifier
generic-parametertype-name : protocol-composition-type

generic-where-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

Grammar of a generic argument clause:

generic-argument-clause< generic-argument-list >
generic-argument-listgeneric-argument | generic-argument , generic-argument-list
generic-argumenttype