Lexical Structure Flashcards
Understand the use of Swift keywords (38 cards)
What are the rules for the characters allowed in an identifier?
Identifiers begin with an upper case or lower case letter A through Z, an underscore (_), a noncombining alphanumeric Unicode character in the Basic Multilingual Plane, or a character outside the Basic Multilingual Plane that isn’t in a Private Use Area. After the first character, digits and combining Unicode characters are also allowed.
How can one use a reserved word as an identifier?
To use a reserved word as an identifier, put a backtick () before and after it. For example, class is not a valid identifier, but
class ` is valid. The backticks are not considered part of the identifier; ` x ` and x have the same meaning.
Describe the rule for implicit identifiers within closures.
Inside a closure with no explicit parameter names, the parameters are implicitly named $0, $1, $2, and so on. These names are valid identifiers within the scope of the closure.
List all the keywords that can appear in declarations.
class, deinit, enum, extension, func, import, init, let, protocol, static, struct, subscript, typealias, and var.
List all the keywords that can appear in statements.
break, case, continue, default, do, else, fallthrough, if, in, for, return, switch, where, and while.
List all the keywords that can appear in expressions and types.
as, dynamicType, is, new, super, self, Self, Type, __COLUMN__, __FILE__, __FUNCTION__, and __LINE__.
List all the keywords that are only reserved in particular contexts in the grammar.
associativity, didSet, get, infix, inout, left, mutating, none, nonmutating, operator, override, postfix, precedence, prefix, right, set, unowned, unowned(safe), unowned(unsafe), weak and willSet. Outside the context in which they appear in the grammar, these can be used as identifiers.
State the two uses of whitespace in Swift
Whitespace has two uses: to separate tokens in the source file and to help determine whether an operator is a prefix or postfix, but is otherwise ignored.
Which characters are considered whitespace?
The following characters are considered whitespace: space (U+0020), line feed (U+000A), carriage return (U+000D), horizontal tab (U+0009), vertical tab (U+000B), form feed (U+000C) and null (U+0000).
What rules govern comments?
Single line comments begin with // and continue until the end of the line. Multiline comments begin with /* and end with */. Nesting is allowed, but the comment markers must be balanced.
How can you use a Swift keyword as an identifier.
A keyword in Swift can be used as an identifier if it escaped with backticks. The backticks are not considered part of the identifier; x
and x have the same meaning.
What are the kinds of literals described as being part of Swift’s lexical structure?
literal → integer-literal | floating-point-literal | string-literal
What is an integer literal?
Integer literals represent integer values of unspecified precision. By default, integer literals are expressed in decimal. Decimal literals contain the digits 0 through 9. Negative integers literals are expressed by prepending a minus sign (-) to an integer literal, as in -42.
How do you specify an alternate base of an integer literal?
You can specify an alternate base using a prefix. Binary literals begin with 0b, octal literals begin with 0o, and hexadecimal literals begin with 0x.
What characters can appear in an integer literal?
Binary literals contain 0 and 1, octal literals contain 0 through 7, and hexadecimal literals contain 0 through 9 as well as A through F in upper- or lowercase. Underscores (_) are allowed between digits for readability, but are ignored and therefore don’t affect the value of the literal. Integer literals can begin with leading zeros (0), but are likewise ignored and don’t affect the base or value of the literal.
What is a floating point literal?
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).
How do you express a decimal floating point literal?
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 upper- or lowercase e prefix followed by sequence of decimal digits that indicates what power of 10 the value preceding the e is multiplied by.
How do you express a hexidecimal floating point literal?
Hexadecimal floating-point literals consist of a 0x prefix, followed by an optional hexadecimal fraction, followed by a hexadecimal exponent. The hexadecimal fraction consists of a decimal point followed by a sequence of hexadecimal digits. The exponent consists of an upper- or lowercase p prefix followed by sequence of decimal digits that indicates what power of 2 the value preceding the p is multiplied by.
What is the lexical rule for negative floating point literals?
Unlike with integer literals, negative floating-point numbers are expressed by applying the unary minus operator (-) to a floating-point literal, as in -42.0. The result is an expression, not a floating-point integer literal.
What is the type of an integer 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.
What is the type of a floating point literal?
Unless otherwise specified, the default type of a floating-point literal is the Swift standard library type Double, which represents a 64-bit floating-point number. The Swift standard library also defines a Float type, which represents a 32-bit floating-point number.
What is a string literal?
A string literal is a sequence of characters surrounded by double quotes, with the following form: “characters”
What unescaped characters cannot be contained in a string literal?
String literals cannot contain an unescaped double quote (“), an unescaped backslash (), a carriage return, or a line feed.
What special character escape sequences can appear in string literals?
Null Character (\0) Backslash (\) Horizontal Tab (\t) Line Feed (\n) Carriage Return (\r) Double Quote (") Single Quote (')