Swift Lexical Structure
5 (2)

Last Updated on November 14, 2021 by deya

Swift lexical structure, consists of valid tokens (lowest-level building blocks) that form the structure of any swift program, these tokens describe the rest of whole swift language…

A token consists of an identifier, keyword, punctuation, literal, or operator.

1) Identifiers:
An example of an identifier is a variable name, for example here “pet” is an identifier.

let pet = "Happy Dinosaur 🦖";

Identifiers support unicode characters, you can name you variable in you native language, and as in other programming languages, you cannot use keywords as identifiers, this is still possible if you surrounding a keyword with back-ticks,

var `var` = "var"

examples of unicode identifiers are

var _latitude = 32.0;
var アップル = "apple"
;

2) Keywords:
The list of basic keywords in swift are listed below, see (Swift Reserved Keywords) for comprehensive list and details.

classdeinitenum
extensionfuncimport
initletprotocol
staticstructsubscript
typealiasvarbreak
continuedefaultdo
elsefallthroughif
inforreturn
switchwherewhile
asdynamicTypeis
newsuperself
SelfType__COLUMN__
__FILE____FUNCTION____LINE__
associativitydidSetget
infixinoutleft
mutatingnonenonmutating
overrideprecedenceprefix
rightsetunowned
unowned(safe)unowned(unsafe)weak
willSet

3) Literals:
literals fall into 3 categories, integer, floating point, and string literals

Integer Literals
var a = 10

//Binary
var b = 00010100b


//Hexadecimal
var c = 14x


//Octal
var d = 24o


leading zeros will be ignored by the compiler, and the use of underscores is possible to increase readability.

var a = 100_000_000

Floating Point Literals
//Simple floating point number
var a = 10.7


//Exponent floating point number
var b = 10.6e2

var c = 10.1e-2
//Exponent floating point number

//Hexa decimal exponent
var d = 0xAp2


//Hexa decimal exponent
var d = 0xAp-2


String Literals

String literals are characters are enclosed within double quotes. Strings can contain escape sequences to represent characters like qoutes. Example for string literal is shown below.

var a = “test”
var a = “Hello\nWorld”

\0 Null Character
\ Backslash
\t Horizontal Tab
\n New line
\r Carriage Return
\” Double Quote
\’ Single Quote


4) Operators:
There are different operators supported in swift which includes
+ : Addition
– : Subtraction
* : Multiplication
/ : Division
% : Remainder
^ : Exponent
& : Bitwise And
&& : Logical And
| : Bitwise Or
|| : Logical Or
++ : Increment Operator
– : Minus
~ : Bitwise Not
< : Less Than
> : Greater Than
… etc.

Keep in mind, as in Swift’s official documentation, this is a list of reserved punctuation and can’t be used as custom operators:
(){}[].,:;=@#& (as a prefix operator), ->`?, and ! (as a postfix operator)”

Swift Whitespace:
White spaces are used to separate tokens and to distinguish prefixes, otherwise it’s normally omitted by the compiler.

Swift Comments:
these are statements that are ignored by the compiler, and meant for documentation purposes of our code, they could be either one-line or multi-line.

// This is a single line comment

/* Multi line (block) comment - can have
more than one line! */



Click to rate this post!
[Total: 2 Average: 5]

Leave a Reply

Your email address will not be published. Required fields are marked *