Swift’s “lexical structure” describes how sequences of characters are used to construct legal tokens in the language, which form the lowest code blocks in the language and are used to describe other parts of the language in later chapters.
Typically, tags are generated by extracting the largest possible string from the input text of the Swift source file, subject to the syntax constraints described later. This method is called “longest match” or “maximal munch”.
White space and comments
Whitespace serves two purposes: separating tags in source files and distinguishing whether operators are prefixes or suffixes (see operators), which are otherwise ignored. The following characters are treated as blank: Space (U+0020), Line feed (U+000A), Carriage Return (U+000D), horizontal TAB (U+0009), vertical TAB (vertical) TAB (U+000B), form feed (U+000C), and null (U+0000).
Comments are treated as blank by the compiler. Single-line comments start with // and end with the line. Multi-line comments start with /* and end with */. You can nest comments, but note that the comment tags must match.
As a developer, it is particularly important to have a learning atmosphere and a communication circle. This is my iOS development public number: Programming Daxin, whether you are small white or big ox are welcome to enter. Let us progress together and develop together! (The group will provide some free study books collected by the group owner and hundreds of interview questions and answers documents for free!)
identifier
Identifiers can start with the following characters: Uppercase and lowercase letters A to Z, underscores _, Unicode uncombined characters in Basic Multilingual Plane and Private Use Area characters outside Basic Multilingual Plane. After the first character, identifiers allow numeric and Unicode character combinations.
If reserved words are used as identifiers, add back quotes around them. For example, class is not a legal identifier, but class can be used. The backquotes are not part of the identifier; x ‘and x represent the same identifier.
If the argument name is not specified in the closure, the argument is implicitly named 0, 0, 0, 1, $2… These names are valid identifiers within the closure scope.
Identifier syntax identifier → identifier-head identifier-characters opt identifier → [identifier-head](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_La nguage/LexicalStructure.html#//apple_ref/swift/grammar/identifier-head) [identifier-characters](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programm Ing_Language/LexicalStructure html# / / apple_ref/swift/grammar/identifier – characters) opt and identifier Implicit – parameter – the name identifier – the list – identifier | identifier, Identifier -list identifier-head → A to Z Upper or lower case 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 → numbers 0 to 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 opt implicit-parameter-name → $decimal-digits
The keyword
Reserved keywords (keywords) are not allowed to be used as identifiers unless they are escaped by backquotes, see Identifiers.
- Keywords used for declarations: class, deinit, enum, extension, func, import, init, let, protocol, static, struct, subscript, typeAlias, var
- Keywords used for statements: break, case, continue, default, do, else, fallthrough, if, in, for, return, switch, WHERE, while
- Keywords used for expression and Type: as, dynamicType, is, new, super, self, self, Type, COLUMN, FILE, FUNCTION, LINE
- Reserved keywords in a particular context: Associativity, didSet, GET, Infix, Inout, left, mutating, None, nonmutating, Operator, Override, Postfix, precedence, prefix, right, se T, unowned, unowned(safe), unowned(unsafe), weak, and willSet. These keywords can be used for identifiers outside of a specific context.
literal
A literal represents a value of an integer, floating-point number, or text type, as shown in the following example:
1. 42 // integer literals 2. 3.14159 // floating point literals 3. "Hello, world!" // Text literalsCopy the code
Literal syntax and literal integer – literal | floating – point – the literal | string literal
Integer literals
Integer literals represent values for integers of unspecified precision. Integer literals are expressed in decimal by default, and can be prefixed to specify other bases: binary literals add 0b, octal literals add 0O, and hexadecimal literals add 0x.
Decimal literals contain the numbers 0 through 9. Binary literals contain only 0 or 1, octal literals contain the numbers 0 through 7, and hexadecimal literals contain the numbers 0 through 9 and the letters A through F (case and case).
Negative integer literals are preceded by a sign -, such as -42.
Underscores are allowed to increase the readability of numbers. Underscores do not affect literal values. Integer literals can also be preceded by 0, again without affecting the literal value.
1. 1000_000 // equals 1000000. 2. 005 // Equals 5Copy the code
Unless specified otherwise, the default type for integer literals is Int in the Swift library type. The Swift library also defines other integer types with different lengths and with or without signs, see Integer Types.
Integer literals 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 binary 1 – the 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 → digits 0 to 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 → 0 to 9 decimal-digits → Decimal – digit decimal – who opt decimal — literal – character – a decimal digit | _ a decimal — literal characters – > Decimal-literal-character decimal-literal-characters opt hexadecimal al-literal → 0x hexadecimal al-digit Hexadecimal -literal-characters opt hexadecimal al-digit → digits 0 to 9, a to F, Or A to F hexadecimal — literal – character – hexadecimal digit | _ hexadecimal — literal characters – > hexadecimal-literal-character hexadecimal-literal-characters opt
Floating point literals
Floating-point literals represent values for floating point numbers with unspecified precision.
Floating-point literals are expressed in decimal by default (without a prefix) or hexadecimal (prefixed with 0x).
Decimal floating-point literals consist of a decimal number string followed by a decimal or exponential part (or both). Decimal The decimal part consists of a decimal point. Followed by a decimal string of digits. The exponent part consists of the uppercase or lowercase letter E followed by a decimal string of digits that represents the number before e multiplied by 10 to the power of 10. For example, 1.25e2 means 1.25e ^2, or 125.0; Similarly, 1.25E-2 means 1.25e 10^-2, or 0.0125.
Hexadecimal floating-point literals consist of the prefix 0x followed by an optional hexadecimal decimal part and a hexadecimal exponential part. The hexadecimal decimal part consists of a decimal point followed by a string of hexadecimal digits. The exponent part consists of an uppercase or lowercase letter P followed by a decimal string of digits that represents the number before P multiplied by two to the power of two. For example, 0xFp2 means 15 e ^2, which is 60; Again, 0xfP-2 means 15 e ^ 2, or 3.75.
Unlike integer literals, negative floating-point literals consist of the unary operator minus – and floating-point literals, such as -42.0. This represents an expression, not a floating-point integer literal.
Underline _ is allowed to enhance readability and does not affect literal values. Floating-point literals can also be preceded by 0, again without affecting the literal value.
1. 10_000.56 // Equals 10000.56 2. 005000.76 // Equals 5000.76Copy the code
Unless specified otherwise, the default type for floating-point literals is Double in the Swift library type, which represents a 64-bit floating-point number. The Swift standard library also defines the Float type, which represents a 32-bit floating point number.
Floating-point-literal → decimal-literal decimal-fraction opt decimal-exponent opt floating-point-literal → decimal-literal Hexadecimal -literal hexadecimal-fraction opt hexadecimal-exponent decimal-fraction →. Decimal -literal decimal-exponent Floating-point-e sign opt decimal-literal hexadecimal-fraction →. Hexadecimal -literal opt hexadecimal-exponent → Floating – point – p sign opt hexadecimal – literal floating – point – e – e | e floating – point – p – p + | | p sign – –
Text literals
String literals consist of strings in double quotes and have the following form:
1. "characters"
Copy the code
Text literals cannot contain unescaped double quotes “, unescaped backslashes \, carriage return, or line feeds.
Escape special symbols that can be used in literal literals are as follows:
- Null Character \0
- Backslash \
- Horizontal Tab \t
- Line Feed \n
- Carriage Return \r
- Double Quote”
- Single Quote ‘
Characters can also be represented as follows:
- \x followed by two hexadecimal digits
- \u followed by four hexadecimal digits
- \U followed by eight hexadecimal digits
The number followed represents a Unicode code point.
Text literals allow the value of an expression to be inserted in backslash parentheses (). Interpolated expression cannot contain unescaped double quotes, backslashes, carriage returns, or newlines. The type of an expression value must have a corresponding initialization method in the String class.
For example, all of the following literal literals have the same value:
1. "1 2 3"
2. "1 2 \(3)"
3. "1 2 \(1 + 2)"
4. var x = 3; "1 2 \(x)"
Copy the code
The default type of text literals is String. The Character type that makes up the string is Character. For more information about strings and Character, see Strings and Characters.
Quoted-text → “quoted-text” quoted-text → quoted-text opt quoted-text → “quoted-text” → “quoted-text” Draft-text → (expression) draft-text → any Unicode extended character set except “, \, U+000A or U+000D He escaped – character – \ | 0 \ | t | | \ n \ r \ | “| ‘escaped hexadecimal character – > \ x – 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-digithexadecimal-digit hexadecimal-digit hexadecimal-digit
The operator
The Swift standard library defines a number of operators that can be used, most of which are described in basic and advanced operators. Here we describe which characters can be used as operators.
The operator consists of one or more of the following characters: /, =, -, +,! ,, %, <, >, &, |, ^ ~,.. That is, the tokens =, ->, //, /, */, and the unary prefix operators & are reserved words and cannot be overridden or used for custom operators.
The whitespace around the operator is used to distinguish whether the operator is a prefix operator, a postfix operator, or a binary operator. The rules are summarized as follows:
- An operator is considered binary if it has whitespace on either side or no whitespace on either side. For example, the operator + in a+b and a+b is treated as a binary operator.
- If the operator has only left whitespace, it is treated as a prefixed unary operator. For example, the ++ in a ++b is treated as the prefixed unary operator.
- If the operator has only right-side whitespace, it is treated as a postfix unary operator. For example, the ++ in a++ b is treated as the postfix unary operator.
- An operator is treated as a postfix unary operator if there is no whitespace to the left followed by. For example, the ++ in a++. B is treated as a postfix unary operator (similarly, the ++ in a++. B is a postfix unary operator and the ++ in a++. B is not).
Given these rules, the characters before the operators (, [and {;),] and} and the characters,,,; Both and: will be used for blank detection.
The above rules should be noted if the operator! Or? If there is no whitespace on the left, it is treated as a postfix operator whether or not there is whitespace on the right. If you have? As an optional type modifier, there must be no white space on the left. If used for conditional operators? There must be blank space on both sides.
Operators beginning with < or > are split into two or more tokens in a given composition, and the rest is split again in the same way. Therefore, there is no need to add whitespace to disambiguate the closed character > in Dictionary<String, Array>. In this example, the closing character > is treated as a single-character token and is not misunderstood as the shift operator >>.
To learn how to customize new operators, see Custom Operators and operator declarations. To learn how to override an existing operator, see operator methods.
Operator syntax operator – > operator – character operator opt operator – character – / + | | = | – |! < | | | | * % > | and | | | ^ | | ~. The binary – operator – > operator prefix – operator – > operator postfix operator – > operator