The_Swift_Programming_Language
.pdfLanguage Reference
About the Language Reference
This part of the book describes the formal grammar of the Swift programming language. The grammar described here is intended to help you understand the language in more detail, rather than to allow you to directly implement a parser or compiler.
The Swift language is relatively small, because many common types, functions, and operators that appear virtually everywhere in Swift code are actually defined in the Swift standard library. Although these types, functions, and operators are not part of the Swift language itself, they are used extensively in the discussions and code examples in this part of the book.
How to Read the Grammar
The notation used to describe the formal grammar of the Swift programming language follows a few conventions:
An arrow (→) is used to mark grammar productions and can be read as “can consist of.“
Syntactic categories are indicated by italic text and appear on both sides of a grammar production rule.
Literal words and punctuation are indicated by boldface constant width text and appear only on the right-hand side of a grammar production rule.
Alternative grammar productions are separated by vertical bars (|). When alternative productions are too long to read easily, they are broken into multiple grammar production rules on new lines.
In a few cases, regular font text is used to describe the right-hand side of a grammar production rule.
Optional syntactic categories and literals are marked by a trailing subscript, opt.
As an example, the grammar of a getter-setter block is defined as follows:
G R A M M A R O F A G E T T E R- S E T T E R B L O C K
getter-setter-block → { getter-clause setter-clause opt } { setter-clause getterclause }
This definition indicates that a getter-setter block can consist of a getter clause followed by an optional setter clause, enclosed in braces, or a setter clause followed by
a getter clause, enclosed in braces. The grammar production above is equivalent to the following two productions, where the alternatives are spelled out explicitly:
G R A M M A R O F A G E T T E R S E T T E R B L O C K
getter-setter-block → { getter-clause setter-clause opt } getter-setter-block → { setter-clause getter-clause }
zeros (0), but are likewise ignored and don’t affect the base or value of the literal.
Unless otherwise specified, the default type of an integer literal is the Swift standard library type Int. The Swift standard library also defines types for various sizes of signed and unsigned integers, as described in Integers.
G R A M M A R O F A N I N T E G E R L I T E R A L
integer-literal → binary-literal integer-literal → octal-literal integer-literal → decimal-literal integer-literal → hexadecimal-literal
binary-literal → 0b binary-digit binary-literal-characters opt binary-digit → Digit 0 or 1
binary-literal-character → binary-digit _
binary-literal-characters → binary-literal-character binary-literal-characters opt
octal-literal → 0o octal-digit octal-literal-characters opt octal-digit → Digit 0 through 7
octal-literal-character → octal-digit _
octal-literal-characters → octal-literal-character octal-literal-characters opt
decimal-literal → decimal-digit decimal-literal-characters opt decimal-digit → Digit 0 through 9
decimal-digits → decimal-digit decimal-digits opt decimal-literal-character → decimal-digit _
decimal-literal-characters → decimal-literal-character decimal-literal-characters opt
hexadecimal-literal → 0x hexadecimal-digit hexadecimal-literal-characters opt hexadecimal-digit → Digit 0 through 9, a through f, or A through F hexadecimal-literal-character → hexadecimal-digit _ hexadecimal-literal-characters → hexadecimal-literal-character hexadecimal-literal-
characters opt
Floating-Point Literals
Floating-point literals represent floating-point values of unspecified precision.
By default, floating-point literals are expressed in decimal (with no prefix), but they can also be expressed in hexadecimal (with a 0x prefix).
Decimal floating-point literals consist of a sequence of decimal digits followed by either a decimal fraction, a decimal exponent, or both. The decimal fraction consists of a decimal point (.) followed by a sequence of decimal digits. The exponent consists of an
