Summary of the Grammar
Read the whole formal grammar.
Lexical Structure
Grammar of whitespace:
whitespace → whitespace-item whitespace? \ whitespace-item → line-break \ whitespace-item → inline-space \ whitespace-item → comment \ whitespace-item → multiline-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-spaces → inline-space inline-spaces? \ inline-space → U+0009 or U+0020
comment → <code>//</code> comment-text line-break \ multiline-comment → *<code>/* multiline-comment-text <code>/**
comment-text → comment-text-item comment-text? \ comment-text-item → Any Unicode scalar value except U+000A or U+000D
multiline-comment-text → multiline-comment-text-item multiline-comment-text? \ multiline-comment-text-item → multiline-comment \ multiline-comment-text-item → comment-text-item \ multiline-comment-text-item → Any Unicode scalar value except *<code>/ or
/</code>*
Grammar of an identifier:
identifier → identifier-head identifier-characters? \ identifier → `<code> </code> `<code> identifier-head identifier-characters? </code><code> </code> `<code> \ identifier → implicit-parameter-name \ identifier → property-wrapper-projection \ identifier-list → identifier | identifier </code>,` identifier-list
identifier-head → Upper- or lowercase letter A through Z \ identifier-head → <code>_</code> \ 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-character → identifier-head \ identifier-characters → identifier-character identifier-characters?
implicit-parameter-name → <code>$</code> decimal-digits \ property-wrapper-projection → <code>$</code> identifier-characters
Grammar of a literal:
literal → numeric-literal | string-literal | regular-expression-literal | boolean-literal | nil-literal
numeric-literal → <code>-</code>? integer-literal | <code>-</code>? floating-point-literal \ boolean-literal → <code>true</code> | <code>false</code> \ nil-literal → <code>nil</code>
Grammar of an integer literal:
integer-literal → binary-literal \ integer-literal → octal-literal \ integer-literal → decimal-literal \ integer-literal → hexadecimal-literal
binary-literal → <code>0b</code> binary-digit binary-literal-characters? \ binary-digit → Digit 0 or 1 \ binary-literal-character → binary-digit | <code></code></strong> \ <em>binary-literal-characters</em> → <em>binary-literal-character</em> <em>binary-literal-characters</em>?_
octal-literal → <code>0o</code> octal-digit octal-literal-characters? \ octal-digit → Digit 0 through 7 \ octal-literal-character → octal-digit | <code></code></strong> \ <em>octal-literal-characters</em> → <em>octal-literal-character</em> <em>octal-literal-characters</em>?_
decimal-literal → decimal-digit decimal-literal-characters? \ decimal-digit → Digit 0 through 9 \ decimal-digits → decimal-digit decimal-digits? \ decimal-literal-character → decimal-digit | <code></code></strong> \ <em>decimal-literal-characters</em> → <em>decimal-literal-character</em> <em>decimal-literal-characters</em>?_
hexadecimal-literal → <code>0x</code> hexadecimal-digit hexadecimal-literal-characters? \ hexadecimal-digit → Digit 0 through 9, a through f, or A through F \ hexadecimal-literal-character → hexadecimal-digit | <code></code></strong> \ <em>hexadecimal-literal-characters</em> → <em>hexadecimal-literal-character</em> <em>hexadecimal-literal-characters</em>?_
Grammar of a floating-point literal:
floating-point-literal → decimal-literal decimal-fraction? decimal-exponent? \ floating-point-literal → hexadecimal-literal hexadecimal-fraction? hexadecimal-exponent
decimal-fraction → <code>.</code> decimal-literal \ decimal-exponent → floating-point-e sign? decimal-literal
hexadecimal-fraction → <code>.</code> hexadecimal-digit hexadecimal-literal-characters? \ hexadecimal-exponent → floating-point-p sign? decimal-literal
floating-point-e → <code>e</code> | <code>E</code> \ floating-point-p → <code>p</code> | <code>P</code> \ sign → <code>+</code> | <code>-</code>
Grammar of a string literal:
string-literal → static-string-literal | interpolated-string-literal
string-literal-opening-delimiter → extended-string-literal-delimiter? <code>"</code> \ string-literal-closing-delimiter → <code>"</code> extended-string-literal-delimiter?
static-string-literal → string-literal-opening-delimiter quoted-text? string-literal-closing-delimiter \ static-string-literal → multiline-string-literal-opening-delimiter multiline-quoted-text? multiline-string-literal-closing-delimiter
multiline-string-literal-opening-delimiter → extended-string-literal-delimiter? <code>"""</code> \ multiline-string-literal-closing-delimiter → <code>"""</code> extended-string-literal-delimiter? \ extended-string-literal-delimiter → <code>#</code> extended-string-literal-delimiter?
quoted-text → quoted-text-item quoted-text? \ quoted-text-item → escaped-character \ quoted-text-item → Any Unicode scalar value except <code>"</code>, ``, U+000A, or U+000D
multiline-quoted-text → multiline-quoted-text-item multiline-quoted-text? \ multiline-quoted-text-item → escaped-character \ multiline-quoted-text-item → Any Unicode scalar value except `` \ multiline-quoted-text-item → escaped-newline
interpolated-string-literal → string-literal-opening-delimiter interpolated-text? string-literal-closing-delimiter \ interpolated-string-literal → multiline-string-literal-opening-delimiter multiline-interpolated-text? multiline-string-literal-closing-delimiter
interpolated-text → interpolated-text-item interpolated-text? \ interpolated-text-item → <code>\(</code> expression <code>)</code> | quoted-text-item
multiline-interpolated-text → multiline-interpolated-text-item multiline-interpolated-text? \ multiline-interpolated-text-item → <code>\(</code> expression <code>)</code> | multiline-quoted-text-item
escape-sequence → <code>&grave; extended-string-literal-delimiter \ escaped-character → escape-sequence </code>0<code> | escape-sequence </code>` | escape-sequence <code>t</code> | escape-sequence <code>n</code> | escape-sequence <code>r</code> | escape-sequence <code>"</code> | escape-sequence <code>'</code> \ escaped-character → escape-sequence <code>u</code> <code>{</code> unicode-scalar-digits <code>}</code> \ unicode-scalar-digits → Between one and eight hexadecimal digits
escaped-newline → escape-sequence inline-spaces? line-break
Grammar of a regular expression literal:
regular-expression-literal → regular-expression-literal-opening-delimiter regular-expression regular-expression-literal-closing-delimiter \ regular-expression → Any regular expression
regular-expression-literal-opening-delimiter → extended-regular-expression-literal-delimiter? <code>/</code> \ regular-expression-literal-closing-delimiter → <code>/</code> extended-regular-expression-literal-delimiter?
extended-regular-expression-literal-delimiter → <code>#</code> extended-regular-expression-literal-delimiter?
Grammar of operators:
operator → operator-head operator-characters? \ operator → dot-operator-head dot-operator-characters
operator-head → <code>/</code> | <code>=</code> | <code>-</code> | <code>+</code> | <code>!</code> | *<code> |
%|<|>|&|||^|~|?* \ 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+3030operator-character → operator-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-characters → operator-character operator-characters?
dot-operator-head → <code>.</code> \ dot-operator-character → <code>.</code> | operator-character \ dot-operator-characters → dot-operator-character dot-operator-characters?
infix-operator → operator \ prefix-operator → operator \ postfix-operator → operator
Types
Grammar of a type:
type → function-type \ type → array-type \ type → dictionary-type \ type → type-identifier \ type → tuple-type \ type → optional-type \ type → implicitly-unwrapped-optional-type \ type → protocol-composition-type \ type → opaque-type \ type → metatype-type \ type → any-type \ type → self-type \ type → <code>(</code> type <code>)</code>
Grammar of a type annotation:
type-annotation → <code>:</code> attributes? <code>inout</code>? type
Grammar of a type identifier:
type-identifier → type-name generic-argument-clause? | type-name generic-argument-clause? <code>.</code> type-identifier \ type-name → identifier
Grammar of a tuple type:
tuple-type → <code>(</code> <code>)</code> | <code>(</code> tuple-type-element <code>,</code> tuple-type-element-list <code>)</code> \ tuple-type-element-list → tuple-type-element | tuple-type-element <code>,</code> tuple-type-element-list \ tuple-type-element → element-name type-annotation | type \ element-name → identifier
Grammar of a function type:
function-type → attributes? function-type-argument-clause <code>async</code>? throws-clause? <code>-></code> type
function-type-argument-clause → <code>(</code> <code>)</code> \ function-type-argument-clause → <code>(</code> function-type-argument-list <code>...</code>? <code>)</code>
function-type-argument-list → function-type-argument | function-type-argument <code>,</code> function-type-argument-list \ function-type-argument → attributes? <code>inout</code>? type | argument-label type-annotation \ argument-label → identifier
throws-clause → <code>throws</code> | <code>throws</code> <code>(</code> type <code>)</code>
Grammar of an array type:
array-type → <code>[</code> type <code>]</code>
Grammar of a dictionary type:
dictionary-type → <code>[</code> type <code>:</code> type <code>]</code>
Grammar of an optional type:
optional-type → type <code>?</code>
Grammar of an implicitly unwrapped optional type:
implicitly-unwrapped-optional-type → type <code>!</code>
Grammar of a protocol composition type:
protocol-composition-type → type-identifier <code>&</code> protocol-composition-continuation \ protocol-composition-continuation → type-identifier | protocol-composition-type
Grammar of an opaque type:
opaque-type → <code>some</code> type
Grammar of a boxed protocol type:
boxed-protocol-type → <code>any</code> type
Grammar of a metatype type:
metatype-type → type <code>.</code> <code>Type</code> | type <code>.</code> <code>Protocol</code>
Grammar of an Any type:
any-type → <code>Any</code>
Grammar of a Self type:
self-type → <code>Self</code>
Grammar of a type inheritance clause:
type-inheritance-clause → <code>:</code> type-inheritance-list \ type-inheritance-list → attributes? type-identifier | attributes? type-identifier <code>,</code> type-inheritance-list
Expressions
Grammar of an expression:
expression → try-operator? await-operator? prefix-expression infix-expressions?
Grammar of a prefix expression:
prefix-expression → prefix-operator? postfix-expression \ prefix-expression → in-out-expression
Grammar of an in-out expression:
in-out-expression → <code>&</code> primary-expression
Grammar of a try expression:
try-operator → <code>try</code> | <code>try</code> <code>?</code> | <code>try</code> <code>!</code>
Grammar of an await expression:
await-operator → <code>await</code>
Grammar of an infix expression:
infix-expression → infix-operator prefix-expression \ infix-expression → assignment-operator try-operator? await-operator? prefix-expression \ infix-expression → conditional-operator try-operator? await-operator? prefix-expression \ infix-expression → type-casting-operator \ infix-expressions → infix-expression infix-expressions?
Grammar of an assignment operator:
assignment-operator → <code>=</code>
Grammar of a conditional operator:
conditional-operator → <code>?</code> expression <code>:</code>
Grammar of a type-casting operator:
type-casting-operator → <code>is</code> type \ type-casting-operator → <code>as</code> type \ type-casting-operator → <code>as</code> <code>?</code> type \ type-casting-operator → <code>as</code> <code>!</code> type
Grammar of a primary expression:
primary-expression → identifier generic-argument-clause? \ primary-expression → literal-expression \ primary-expression → self-expression \ primary-expression → superclass-expression \ primary-expression → conditional-expression \ primary-expression → closure-expression \ primary-expression → parenthesized-expression \ primary-expression → tuple-expression \ primary-expression → implicit-member-expression \ primary-expression → wildcard-expression \ primary-expression → macro-expansion-expression \ primary-expression → key-path-expression \ primary-expression → selector-expression \ primary-expression → key-path-string-expression
Grammar of a literal expression:
literal-expression → literal \ literal-expression → array-literal | dictionary-literal | playground-literal
array-literal → <code>[</code> array-literal-items? <code>]</code> \ array-literal-items → array-literal-item <code>,</code>? | array-literal-item <code>,</code> array-literal-items \ array-literal-item → expression
dictionary-literal → <code>[</code> dictionary-literal-items <code>]</code> | <code>[</code> <code>:</code> <code>]</code> \ dictionary-literal-items → dictionary-literal-item <code>,</code>? | dictionary-literal-item <code>,</code> dictionary-literal-items \ dictionary-literal-item → expression <code>:</code> expression
playground-literal → <code>#colorLiteral</code> <code>(</code> <code>red</code> <code>:</code> expression <code>,</code> <code>green</code> <code>:</code> expression <code>,</code> <code>blue</code> <code>:</code> expression <code>,</code> <code>alpha</code> <code>:</code> expression <code>)</code> \ playground-literal → <code>#fileLiteral</code> <code>(</code> <code>resourceName</code> <code>:</code> expression <code>)</code> \ playground-literal → <code>#imageLiteral</code> <code>(</code> <code>resourceName</code> <code>:</code> expression <code>)</code>
Grammar of a self expression:
self-expression → <code>self</code> | self-method-expression | self-subscript-expression | self-initializer-expression
self-method-expression → <code>self</code> <code>.</code> identifier \ self-subscript-expression → <code>self</code> <code>[</code> function-call-argument-list <code>]</code> \ self-initializer-expression → <code>self</code> <code>.</code> <code>init</code>
Grammar of a superclass expression:
superclass-expression → superclass-method-expression | superclass-subscript-expression | superclass-initializer-expression
superclass-method-expression → <code>super</code> <code>.</code> identifier \ superclass-subscript-expression → <code>super</code> <code>[</code> function-call-argument-list <code>]</code> \ superclass-initializer-expression → <code>super</code> <code>.</code> <code>init</code>
Grammar of a conditional expression:
conditional-expression → if-expression | switch-expression
if-expression → <code>if</code> condition-list <code>{</code> statement <code>}</code> if-expression-tail \ if-expression-tail → <code>else</code> if-expression \ if-expression-tail → <code>else</code> <code>{</code> statement <code>}</code>
switch-expression → <code>switch</code> expression <code>{</code> switch-expression-cases <code>}</code> \ switch-expression-cases → switch-expression-case switch-expression-cases? \ switch-expression-case → case-label statement \ switch-expression-case → default-label statement
Grammar of a closure expression:
closure-expression → <code>{</code> attributes? closure-signature? statements? <code>}</code>
closure-signature → capture-list? closure-parameter-clause <code>async</code>? throws-clause? function-result? <code>in</code> \ closure-signature → capture-list <code>in</code>
closure-parameter-clause → <code>(</code> <code>)</code> | <code>(</code> closure-parameter-list <code>)</code> | identifier-list \ closure-parameter-list → closure-parameter | closure-parameter <code>,</code> closure-parameter-list \ closure-parameter → closure-parameter-name type-annotation? \ closure-parameter → closure-parameter-name type-annotation <code>...</code> \ closure-parameter-name → identifier
capture-list → <code>[</code> capture-list-items <code>]</code> \ capture-list-items → capture-list-item | capture-list-item <code>,</code> capture-list-items \ capture-list-item → capture-specifier? identifier \ capture-list-item → capture-specifier? identifier <code>=</code> expression \ capture-list-item → capture-specifier? self-expression \ capture-specifier → <code>weak</code> | <code>unowned</code> | <code>unowned(safe)</code> | <code>unowned(unsafe)</code>
Grammar of an implicit member expression:
implicit-member-expression → <code>.</code> identifier \ implicit-member-expression → <code>.</code> identifier <code>.</code> postfix-expression
Grammar of a parenthesized expression:
parenthesized-expression → <code>(</code> expression <code>)</code>
Grammar of a tuple expression:
tuple-expression → <code>(</code> <code>)</code> | <code>(</code> tuple-element <code>,</code> tuple-element-list <code>)</code> \ tuple-element-list → tuple-element | tuple-element <code>,</code> tuple-element-list \ tuple-element → expression | identifier <code>:</code> expression
Grammar of a wildcard expression:
wildcard-expression → <code>_</code>
Grammar of a macro-expansion expression:
macro-expansion-expression → <code>#</code> identifier generic-argument-clause? function-call-argument-clause? trailing-closures?
Grammar of a key-path expression:
key-path-expression → <code>&grave; type? </code>.<code> key-path-components \ key-path-components → key-path-component | key-path-component </code>.` key-path-components \ key-path-component → identifier key-path-postfixes? | key-path-postfixes
key-path-postfixes → key-path-postfix key-path-postfixes? \ key-path-postfix → <code>?</code> | <code>!</code> | <code>self</code> | <code>[</code> function-call-argument-list <code>]</code>
Grammar of a selector expression:
selector-expression → <code>#selector</code> <code>(</code> expression <code>)</code> \ selector-expression → <code>#selector</code> <code>(</code> <code>getter:</code> expression <code>)</code> \ selector-expression → <code>#selector</code> <code>(</code> <code>setter:</code> expression <code>)</code>
Grammar of a key-path string expression:
key-path-string-expression → <code>#keyPath</code> <code>(</code> expression <code>)</code>
Grammar of a postfix expression:
postfix-expression → primary-expression \ postfix-expression → postfix-expression postfix-operator \ postfix-expression → function-call-expression \ postfix-expression → initializer-expression \ postfix-expression → explicit-member-expression \ postfix-expression → postfix-self-expression \ postfix-expression → subscript-expression \ postfix-expression → forced-value-expression \ postfix-expression → optional-chaining-expression
Grammar of a function call expression:
function-call-expression → postfix-expression function-call-argument-clause \ function-call-expression → postfix-expression function-call-argument-clause? trailing-closures
function-call-argument-clause → <code>(</code> <code>)</code> | <code>(</code> function-call-argument-list <code>)</code> \ function-call-argument-list → function-call-argument | function-call-argument <code>,</code> function-call-argument-list \ function-call-argument → expression | identifier <code>:</code> expression \ function-call-argument → operator | identifier <code>:</code> operator
trailing-closures → closure-expression labeled-trailing-closures? \ labeled-trailing-closures → labeled-trailing-closure labeled-trailing-closures? \ labeled-trailing-closure → identifier <code>:</code> closure-expression
Grammar of an initializer expression:
initializer-expression → postfix-expression <code>.</code> <code>init</code> \ initializer-expression → postfix-expression <code>.</code> <code>init</code> <code>(</code> argument-names <code>)</code>
Grammar of an explicit member expression:
explicit-member-expression → postfix-expression <code>.</code> decimal-digits \ explicit-member-expression → postfix-expression <code>.</code> identifier generic-argument-clause? \ explicit-member-expression → postfix-expression <code>.</code> identifier <code>(</code> argument-names <code>)</code> \ explicit-member-expression → postfix-expression conditional-compilation-block
argument-names → argument-name argument-names? \ argument-name → identifier <code>:</code>
Grammar of a postfix self expression:
postfix-self-expression → postfix-expression <code>.</code> <code>self</code>
Grammar of a subscript expression:
subscript-expression → postfix-expression <code>[</code> function-call-argument-list <code>]</code>
Grammar of a forced-value expression:
forced-value-expression → postfix-expression <code>!</code>
Grammar of an optional-chaining expression:
optional-chaining-expression → postfix-expression <code>?</code>
Statements
Grammar of a statement:
statement → expression <code>;</code>? \ statement → declaration <code>;</code>? \ statement → loop-statement <code>;</code>? \ statement → branch-statement <code>;</code>? \ statement → labeled-statement <code>;</code>? \ statement → control-transfer-statement <code>;</code>? \ statement → defer-statement <code>;</code>? \ statement → do-statement <code>;</code>? \ statement → compiler-control-statement \ statements → statement statements?
Grammar of a loop statement:
loop-statement → for-in-statement \ loop-statement → while-statement \ loop-statement → repeat-while-statement
Grammar of a for-in statement:
for-in-statement → <code>for</code> <code>case</code>? pattern <code>in</code> expression where-clause? code-block
Grammar of a while statement:
while-statement → <code>while</code> condition-list code-block
condition-list → condition | condition <code>,</code> condition-list \ condition → expression | availability-condition | case-condition | optional-binding-condition
case-condition → <code>case</code> pattern initializer \ optional-binding-condition → <code>let</code> pattern initializer? | <code>var</code> pattern initializer?
Grammar of a repeat-while statement:
repeat-while-statement → <code>repeat</code> code-block <code>while</code> expression
Grammar of a branch statement:
branch-statement → if-statement \ branch-statement → guard-statement \ branch-statement → switch-statement
Grammar of an if statement:
if-statement → <code>if</code> condition-list code-block else-clause? \ else-clause → <code>else</code> code-block | <code>else</code> if-statement
Grammar of a guard statement:
guard-statement → <code>guard</code> condition-list <code>else</code> code-block
Grammar of a switch statement:
switch-statement → <code>switch</code> expression <code>{</code> switch-cases? <code>}</code> \ switch-cases → switch-case switch-cases? \ switch-case → case-label statements \ switch-case → default-label statements \ switch-case → conditional-switch-case
case-label → attributes? <code>case</code> case-item-list <code>:</code> \ case-item-list → pattern where-clause? | pattern where-clause? <code>,</code> case-item-list \ default-label → attributes? <code>default</code> <code>:</code>
where-clause → <code>where</code> where-expression \ where-expression → expression
conditional-switch-case → switch-if-directive-clause switch-elseif-directive-clauses? switch-else-directive-clause? endif-directive \ switch-if-directive-clause → if-directive compilation-condition switch-cases? \ switch-elseif-directive-clauses → elseif-directive-clause switch-elseif-directive-clauses? \ switch-elseif-directive-clause → elseif-directive compilation-condition switch-cases? \ switch-else-directive-clause → else-directive switch-cases?
Grammar of a labeled statement:
labeled-statement → statement-label loop-statement \ labeled-statement → statement-label if-statement \ labeled-statement → statement-label switch-statement \ labeled-statement → statement-label do-statement
statement-label → label-name <code>:</code> \ label-name → identifier
Grammar of a control transfer statement:
control-transfer-statement → break-statement \ control-transfer-statement → continue-statement \ control-transfer-statement → fallthrough-statement \ control-transfer-statement → return-statement \ control-transfer-statement → throw-statement
Grammar of a break statement:
break-statement → <code>break</code> label-name?
Grammar of a continue statement:
continue-statement → <code>continue</code> label-name?
Grammar of a fallthrough statement:
fallthrough-statement → <code>fallthrough</code>
Grammar of a return statement:
return-statement → <code>return</code> expression?
Grammar of a throw statement:
throw-statement → <code>throw</code> expression
Grammar of a defer statement:
defer-statement → <code>defer</code> code-block
Grammar of a do statement:
do-statement → <code>do</code> throws-clause? code-block catch-clauses? \ catch-clauses → catch-clause catch-clauses? \ catch-clause → <code>catch</code> catch-pattern-list? code-block \ catch-pattern-list → catch-pattern | catch-pattern <code>,</code> catch-pattern-list \ catch-pattern → pattern where-clause?
Grammar of a compiler control statement:
compiler-control-statement → conditional-compilation-block \ compiler-control-statement → line-control-statement \ compiler-control-statement → diagnostic-statement
Grammar of a conditional compilation block:
conditional-compilation-block → if-directive-clause elseif-directive-clauses? else-directive-clause? endif-directive
if-directive-clause → if-directive compilation-condition statements? \ elseif-directive-clauses → elseif-directive-clause elseif-directive-clauses? \ elseif-directive-clause → elseif-directive compilation-condition statements? \ else-directive-clause → else-directive statements? \ if-directive → <code>#if</code> \ elseif-directive → <code>#elseif</code> \ else-directive → <code>#else</code> \ endif-directive → <code>#endif</code>
compilation-condition → platform-condition \ compilation-condition → identifier \ compilation-condition → boolean-literal \ compilation-condition → <code>(</code> compilation-condition <code>)</code> \ compilation-condition → <code>!</code> compilation-condition \ compilation-condition → compilation-condition <code>&&</code> compilation-condition \ compilation-condition → compilation-condition <code>||</code> compilation-condition
platform-condition → <code>os</code> <code>(</code> operating-system <code>)</code> \ platform-condition → <code>arch</code> <code>(</code> architecture <code>)</code> \ platform-condition → <code>swift</code> <code>(</code> <code>>=</code> swift-version <code>)</code> | <code>swift</code> <code>(</code> <code><</code> swift-version <code>)</code> \ platform-condition → <code>compiler</code> <code>(</code> <code>>=</code> swift-version <code>)</code> | <code>compiler</code> <code>(</code> <code><</code> swift-version <code>)</code> \ platform-condition → <code>canImport</code> <code>(</code> import-path <code>)</code> \ platform-condition → <code>targetEnvironment</code> <code>(</code> environment <code>)</code>
operating-system → <code>macOS</code> | <code>iOS</code> | <code>watchOS</code> | <code>tvOS</code> | <code>visionOS</code> | <code>Linux</code> | <code>Windows</code> \ architecture → <code>i386</code> | <code>x8664</code></strong> | <strong><code>arm</code></strong> | <strong><code>arm64</code></strong> \ <em>swift-version</em> → <em>decimal-digits</em> <em>swift-version-continuation</em>? \ <em>swift-version-continuation</em> → <strong><code>.</code></strong> <em>decimal-digits</em> <em>swift-version-continuation</em>?_ \ environment → <code>simulator</code> | <code>macCatalyst</code>
Grammar of a line control statement:
line-control-statement → <code>#sourceLocation</code> <code>(</code> <code>file:</code> file-path <code>,</code> <code>line:</code> line-number <code>)</code> \ line-control-statement → <code>#sourceLocation</code> <code>(</code> <code>)</code> \ line-number → A decimal integer greater than zero \ file-path → static-string-literal
Grammar of an availability condition:
availability-condition → <code>#available</code> <code>(</code> availability-arguments <code>)</code> \ availability-condition → <code>#unavailable</code> <code>(</code> availability-arguments <code>)</code> \ availability-arguments → availability-argument | availability-argument <code>,</code> availability-arguments \ availability-argument → platform-name platform-version \ availability-argument → *<code>**
platform-name → <code>iOS</code> | <code>iOSApplicationExtension</code> \ platform-name → <code>macOS</code> | <code>macOSApplicationExtension</code> \ platform-name → <code>macCatalyst</code> | <code>macCatalystApplicationExtension</code> \ platform-name → <code>watchOS</code> | <code>watchOSApplicationExtension</code> \ platform-name → <code>tvOS</code> | <code>tvOSApplicationExtension</code> \ platform-name → <code>visionOS</code> | <code>visionOSApplicationExtension</code> \ platform-version → decimal-digits \ platform-version → decimal-digits <code>.</code> decimal-digits \ platform-version → decimal-digits <code>.</code> decimal-digits <code>.</code> decimal-digits
Declarations
Grammar of a declaration:
declaration → import-declaration \ declaration → constant-declaration \ declaration → variable-declaration \ declaration → typealias-declaration \ declaration → function-declaration \ declaration → enum-declaration \ declaration → struct-declaration \ declaration → class-declaration \ declaration → actor-declaration \ declaration → protocol-declaration \ declaration → initializer-declaration \ declaration → deinitializer-declaration \ declaration → extension-declaration \ declaration → subscript-declaration \ declaration → operator-declaration \ declaration → precedence-group-declaration
Grammar of a top-level declaration:
top-level-declaration → statements?
Grammar of a code block:
code-block → <code>{</code> statements? <code>}</code>
Grammar of an import declaration:
import-declaration → attributes? <code>import</code> import-kind? import-path
import-kind → <code>typealias</code> | <code>struct</code> | <code>class</code> | <code>enum</code> | <code>protocol</code> | <code>let</code> | <code>var</code> | <code>func</code> \ import-path → identifier | identifier <code>.</code> import-path
Grammar of a constant declaration:
constant-declaration → attributes? declaration-modifiers? <code>let</code> pattern-initializer-list
pattern-initializer-list → pattern-initializer | pattern-initializer <code>,</code> pattern-initializer-list \ pattern-initializer → pattern initializer? \ initializer → <code>=</code> expression
Grammar of a variable declaration:
variable-declaration → variable-declaration-head pattern-initializer-list \ variable-declaration → variable-declaration-head variable-name type-annotation code-block \ variable-declaration → variable-declaration-head variable-name type-annotation getter-setter-block \ variable-declaration → variable-declaration-head variable-name type-annotation getter-setter-keyword-block \ variable-declaration → variable-declaration-head variable-name initializer willSet-didSet-block \ variable-declaration → variable-declaration-head variable-name type-annotation initializer? willSet-didSet-block
variable-declaration-head → attributes? declaration-modifiers? <code>var</code> \ variable-name → identifier
getter-setter-block → code-block \ getter-setter-block → <code>{</code> getter-clause setter-clause? <code>}</code> \ getter-setter-block → <code>{</code> setter-clause getter-clause <code>}</code> \ getter-clause → attributes? mutation-modifier? <code>get</code> code-block \ setter-clause → attributes? mutation-modifier? <code>set</code> setter-name? code-block \ setter-name → <code>(</code> identifier <code>)</code>
getter-setter-keyword-block → <code>{</code> getter-keyword-clause setter-keyword-clause? <code>}</code> \ getter-setter-keyword-block → <code>{</code> setter-keyword-clause getter-keyword-clause <code>}</code> \ getter-keyword-clause → attributes? mutation-modifier? <code>get</code> \ setter-keyword-clause → attributes? mutation-modifier? <code>set</code>
willSet-didSet-block → <code>{</code> willSet-clause didSet-clause? <code>}</code> \ willSet-didSet-block → <code>{</code> didSet-clause willSet-clause? <code>}</code> \ willSet-clause → attributes? <code>willSet</code> setter-name? code-block \ didSet-clause → attributes? <code>didSet</code> setter-name? code-block
Grammar of a type alias declaration:
typealias-declaration → attributes? access-level-modifier? <code>typealias</code> typealias-name generic-parameter-clause? typealias-assignment \ typealias-name → identifier \ typealias-assignment → <code>=</code> type
Grammar of a function declaration:
function-declaration → function-head function-name generic-parameter-clause? function-signature generic-where-clause? function-body?
function-head → attributes? declaration-modifiers? <code>func</code> \ function-name → identifier | operator
function-signature → parameter-clause <code>async</code>? throws-clause? function-result? \ function-signature → parameter-clause <code>async</code>? <code>rethrows</code> function-result? \ function-result → <code>-></code> attributes? type \ function-body → code-block
parameter-clause → <code>(</code> <code>)</code> | <code>(</code> parameter-list <code>)</code> \ parameter-list → parameter | parameter <code>,</code> parameter-list \ parameter → external-parameter-name? local-parameter-name parameter-type-annotation default-argument-clause? \ parameter → external-parameter-name? local-parameter-name parameter-type-annotation \ parameter → external-parameter-name? local-parameter-name parameter-type-annotation <code>...</code>
external-parameter-name → identifier \ local-parameter-name → identifier \ parameter-type-annotation → <code>:</code> attributes? parameter-modifier? type \ parameter-modifier → <code>inout</code> | <code>borrowing</code> | <code>consuming</code> default-argument-clause → <code>=</code> expression
Grammar of an enumeration declaration:
enum-declaration → attributes? access-level-modifier? union-style-enum \ enum-declaration → attributes? access-level-modifier? raw-value-style-enum
union-style-enum → <code>indirect</code>? <code>enum</code> enum-name generic-parameter-clause? type-inheritance-clause? generic-where-clause? <code>{</code> union-style-enum-members? <code>}</code> \ union-style-enum-members → union-style-enum-member union-style-enum-members? \ union-style-enum-member → declaration | union-style-enum-case-clause | compiler-control-statement \ union-style-enum-case-clause → attributes? <code>indirect</code>? <code>case</code> union-style-enum-case-list \ union-style-enum-case-list → union-style-enum-case | union-style-enum-case <code>,</code> union-style-enum-case-list \ union-style-enum-case → enum-case-name tuple-type? \ enum-name → identifier \ enum-case-name → identifier
raw-value-style-enum → <code>enum</code> enum-name generic-parameter-clause? type-inheritance-clause generic-where-clause? <code>{</code> raw-value-style-enum-members <code>}</code> \ raw-value-style-enum-members → raw-value-style-enum-member raw-value-style-enum-members? \ raw-value-style-enum-member → declaration | raw-value-style-enum-case-clause | compiler-control-statement \ raw-value-style-enum-case-clause → attributes? <code>case</code> raw-value-style-enum-case-list \ raw-value-style-enum-case-list → raw-value-style-enum-case | raw-value-style-enum-case <code>,</code> raw-value-style-enum-case-list \ raw-value-style-enum-case → enum-case-name raw-value-assignment? \ raw-value-assignment → <code>=</code> raw-value-literal \ raw-value-literal → numeric-literal | static-string-literal | boolean-literal
Grammar of a structure declaration:
struct-declaration → attributes? access-level-modifier? <code>struct</code> struct-name generic-parameter-clause? type-inheritance-clause? generic-where-clause? struct-body \ struct-name → identifier \ struct-body → <code>{</code> struct-members? <code>}</code>
struct-members → struct-member struct-members? \ struct-member → declaration | compiler-control-statement
Grammar of a class declaration:
class-declaration → attributes? access-level-modifier? <code>final</code>? <code>class</code> class-name generic-parameter-clause? type-inheritance-clause? generic-where-clause? class-body \ class-declaration → attributes? <code>final</code> access-level-modifier? <code>class</code> class-name generic-parameter-clause? type-inheritance-clause? generic-where-clause? class-body \ class-name → identifier \ class-body → <code>{</code> class-members? <code>}</code>
class-members → class-member class-members? \ class-member → declaration | compiler-control-statement
Grammar of an actor declaration:
actor-declaration → attributes? access-level-modifier? <code>actor</code> actor-name generic-parameter-clause? type-inheritance-clause? generic-where-clause? actor-body \ actor-name → identifier \ actor-body → <code>{</code> actor-members? <code>}</code>
actor-members → actor-member actor-members? \ actor-member → declaration | compiler-control-statement
Grammar of a protocol declaration:
protocol-declaration → attributes? access-level-modifier? <code>protocol</code> protocol-name type-inheritance-clause? generic-where-clause? protocol-body \ protocol-name → identifier \ protocol-body → <code>{</code> protocol-members? <code>}</code>
protocol-members → protocol-member protocol-members? \ protocol-member → protocol-member-declaration | compiler-control-statement
protocol-member-declaration → protocol-property-declaration \ protocol-member-declaration → protocol-method-declaration \ protocol-member-declaration → protocol-initializer-declaration \ protocol-member-declaration → protocol-subscript-declaration \ protocol-member-declaration → protocol-associated-type-declaration \ protocol-member-declaration → typealias-declaration
Grammar of a protocol property declaration:
protocol-property-declaration → variable-declaration-head variable-name type-annotation getter-setter-keyword-block
Grammar of a protocol method declaration:
protocol-method-declaration → function-head function-name generic-parameter-clause? function-signature generic-where-clause?
Grammar of a protocol initializer declaration:
protocol-initializer-declaration → initializer-head generic-parameter-clause? parameter-clause throws-clause? generic-where-clause? \ protocol-initializer-declaration → initializer-head generic-parameter-clause? parameter-clause <code>rethrows</code> generic-where-clause?
Grammar of a protocol subscript declaration:
protocol-subscript-declaration → subscript-head subscript-result generic-where-clause? getter-setter-keyword-block
Grammar of a protocol associated type declaration:
protocol-associated-type-declaration → attributes? access-level-modifier? <code>associatedtype</code> typealias-name type-inheritance-clause? typealias-assignment? generic-where-clause?
Grammar of an initializer declaration:
initializer-declaration → initializer-head generic-parameter-clause? parameter-clause <code>async</code>? throws-clause? generic-where-clause? initializer-body \ initializer-declaration → initializer-head generic-parameter-clause? parameter-clause <code>async</code>? <code>rethrows</code> generic-where-clause? initializer-body \ initializer-head → attributes? declaration-modifiers? <code>init</code> \ initializer-head → attributes? declaration-modifiers? <code>init</code> <code>?</code> \ initializer-head → attributes? declaration-modifiers? <code>init</code> <code>!</code> \ initializer-body → code-block
Grammar of a deinitializer declaration:
deinitializer-declaration → attributes? <code>deinit</code> code-block
Grammar of an extension declaration:
extension-declaration → attributes? access-level-modifier? <code>extension</code> type-identifier type-inheritance-clause? generic-where-clause? extension-body \ extension-body → <code>{</code> extension-members? <code>}</code>
extension-members → extension-member extension-members? \ extension-member → declaration | compiler-control-statement
Grammar of a subscript declaration:
subscript-declaration → subscript-head subscript-result generic-where-clause? code-block \ subscript-declaration → subscript-head subscript-result generic-where-clause? getter-setter-block \ subscript-declaration → subscript-head subscript-result generic-where-clause? getter-setter-keyword-block \ subscript-head → attributes? declaration-modifiers? <code>subscript</code> generic-parameter-clause? parameter-clause \ subscript-result → <code>-></code> attributes? type
Grammar of a macro declaration:
macro-declaration → macro-head identifier generic-parameter-clause? macro-signature macro-definition? generic-where-clause \ macro-head → attributes? declaration-modifiers? <code>macro</code> \ macro-signature → parameter-clause macro-function-signature-result? \ macro-function-signature-result → <code>-></code> type \ macro-definition → <code>=</code> expression
Grammar of an operator declaration:
operator-declaration → prefix-operator-declaration | postfix-operator-declaration | infix-operator-declaration
prefix-operator-declaration → <code>prefix</code> <code>operator</code> operator \ postfix-operator-declaration → <code>postfix</code> <code>operator</code> operator \ infix-operator-declaration → <code>infix</code> <code>operator</code> operator infix-operator-group?
infix-operator-group → <code>:</code> precedence-group-name
Grammar of a precedence group declaration:
precedence-group-declaration → <code>precedencegroup</code> precedence-group-name <code>{</code> precedence-group-attributes? <code>}</code>
precedence-group-attributes → precedence-group-attribute precedence-group-attributes? \ precedence-group-attribute → precedence-group-relation \ precedence-group-attribute → precedence-group-assignment \ precedence-group-attribute → precedence-group-associativity
precedence-group-relation → <code>higherThan</code> <code>:</code> precedence-group-names \ precedence-group-relation → <code>lowerThan</code> <code>:</code> precedence-group-names
precedence-group-assignment → <code>assignment</code> <code>:</code> boolean-literal
precedence-group-associativity → <code>associativity</code> <code>:</code> <code>left</code> \ precedence-group-associativity → <code>associativity</code> <code>:</code> <code>right</code> \ precedence-group-associativity → <code>associativity</code> <code>:</code> <code>none</code>
precedence-group-names → precedence-group-name | precedence-group-name <code>,</code> precedence-group-names \ precedence-group-name → identifier
Grammar of a declaration modifier:
declaration-modifier → <code>class</code> | <code>convenience</code> | <code>dynamic</code> | <code>final</code> | <code>infix</code> | <code>lazy</code> | <code>optional</code> | <code>override</code> | <code>postfix</code> | <code>prefix</code> | <code>required</code> | <code>static</code> | <code>unowned</code> | <code>unowned</code> <code>(</code> <code>safe</code> <code>)</code> | <code>unowned</code> <code>(</code> <code>unsafe</code> <code>)</code> | <code>weak</code> \ declaration-modifier → access-level-modifier \ declaration-modifier → mutation-modifier \ declaration-modifier → actor-isolation-modifier \ declaration-modifiers → declaration-modifier declaration-modifiers?
access-level-modifier → <code>private</code> | <code>private</code> <code>(</code> <code>set</code> <code>)</code> \ access-level-modifier → <code>fileprivate</code> | <code>fileprivate</code> <code>(</code> <code>set</code> <code>)</code> \ access-level-modifier → <code>internal</code> | <code>internal</code> <code>(</code> <code>set</code> <code>)</code> \ access-level-modifier → <code>package</code> | <code>package</code> <code>(</code> <code>set</code> <code>)</code> \ access-level-modifier → <code>public</code> | <code>public</code> <code>(</code> <code>set</code> <code>)</code> \ access-level-modifier → <code>open</code> | <code>open</code> <code>(</code> <code>set</code> <code>)</code>
mutation-modifier → <code>mutating</code> | <code>nonmutating</code>
actor-isolation-modifier → <code>nonisolated</code>
Attributes
Grammar of an attribute:
attribute → <code>@</code> attribute-name attribute-argument-clause? \ attribute-name → identifier \ attribute-argument-clause → <code>(</code> balanced-tokens? <code>)</code> \ attributes → attribute attributes?
balanced-tokens → balanced-token balanced-tokens? \ balanced-token → <code>(</code> balanced-tokens? <code>)</code> \ balanced-token → <code>[</code> balanced-tokens? <code>]</code> \ balanced-token → <code>{</code> balanced-tokens? <code>}</code> \ balanced-token → Any identifier, keyword, literal, or operator \ balanced-token → Any punctuation except <code>(</code>, <code>)</code>, <code>[</code>, <code>]</code>, <code>{</code>, or <code>}</code>
Patterns
Grammar of a pattern:
pattern → wildcard-pattern type-annotation? \ pattern → identifier-pattern type-annotation? \ pattern → value-binding-pattern \ pattern → tuple-pattern type-annotation? \ pattern → enum-case-pattern \ pattern → optional-pattern \ pattern → type-casting-pattern \ pattern → expression-pattern
Grammar of a wildcard pattern:
wildcard-pattern → <code>_</code>
Grammar of an identifier pattern:
identifier-pattern → identifier
Grammar of a value-binding pattern:
value-binding-pattern → <code>var</code> pattern | <code>let</code> pattern
Grammar of a tuple pattern:
tuple-pattern → <code>(</code> tuple-pattern-element-list? <code>)</code> \ tuple-pattern-element-list → tuple-pattern-element | tuple-pattern-element <code>,</code> tuple-pattern-element-list \ tuple-pattern-element → pattern | identifier <code>:</code> pattern
Grammar of an enumeration case pattern:
enum-case-pattern → type-identifier? <code>.</code> enum-case-name tuple-pattern?
Grammar of an optional pattern:
optional-pattern → identifier-pattern <code>?</code>
Grammar of a type casting pattern:
type-casting-pattern → is-pattern | as-pattern \ is-pattern → <code>is</code> type \ as-pattern → pattern <code>as</code> type
Grammar of an expression pattern:
expression-pattern → expression
Generic Parameters and Arguments
Grammar of a generic parameter clause:
generic-parameter-clause → <code><</code> generic-parameter-list <code>></code> \ generic-parameter-list → generic-parameter | generic-parameter <code>,</code> generic-parameter-list \ generic-parameter → type-name \ generic-parameter → type-name <code>:</code> type-identifier \ generic-parameter → type-name <code>:</code> protocol-composition-type
generic-where-clause → <code>where</code> requirement-list \ requirement-list → requirement | requirement <code>,</code> requirement-list \ requirement → conformance-requirement | same-type-requirement
conformance-requirement → type-identifier <code>:</code> type-identifier \ conformance-requirement → type-identifier <code>:</code> protocol-composition-type \ same-type-requirement → type-identifier <code>==</code> type
Grammar of a generic argument clause:
generic-argument-clause → <code><</code> generic-argument-list <code>></code> \ generic-argument-list → generic-argument | generic-argument <code>,</code> generic-argument-list \ generic-argument → type