Lexical Structure Flashcards
Understand the use of Swift keywords
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.