-
Notifications
You must be signed in to change notification settings - Fork 157
/
LexicalStructure.md
1268 lines (1101 loc) · 42.7 KB
/
LexicalStructure.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Lexical Structure
Use the lowest-level components of the syntax.
The *lexical structure* of Swift describes what sequence of characters
form valid tokens of the language.
These valid tokens form the lowest-level building blocks of the language
and are used to describe the rest of the language in subsequent chapters.
A token consists of an identifier, keyword, punctuation, literal, or operator.
In most cases, tokens are generated from the characters of a Swift source file
by considering the longest possible substring from the input text,
within the constraints of the grammar that are specified below.
This behavior is referred to as *longest match*
or *maximal munch*.
## Whitespace and Comments
Whitespace has two uses: to separate tokens in the source file
and to distinguish between prefix, postfix, and infix operators
(see <doc:LexicalStructure#Operators>),
but is otherwise ignored.
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).
<!--
Whitespace characters are listed roughly from
most salient/common to least,
not in order of Unicode scalar value.
-->
Comments are treated as whitespace by the compiler.
Single line comments begin with `//`
and continue until a line feed (U+000A) or carriage return (U+000D).
Multiline comments begin with `/*` and end with `*/`.
Nesting multiline comments is allowed,
but the comment markers must be balanced.
Comments can contain additional formatting and markup,
as described in [Markup Formatting Reference](https://developer.apple.com/library/content/documentation/Xcode/Reference/xcode_markup_formatting_ref/index.html).
> Grammar of whitespace:
>
> *whitespace* → *whitespace-item* *whitespace*_?_ \
> *whitespace-item* → *line-break* \
> *whitespace-item* → *inline-space* \
> *whitespace-item* → *comment* \
> *whitespace-item* → *multiline-comment* \
> *whitespace-item* → U+0000, U+000B, or U+000C
>
> *line-break* → U+000A \
> *line-break* → U+000D \
> *line-break* → U+000D followed by U+000A
>
> *inline-spaces* → *inline-space* *inline-spaces*_?_ \
> *inline-space* → U+0009 or U+0020
>
> *comment* → **`//`** *comment-text* *line-break* \
> *multiline-comment* → **`/*`** *multiline-comment-text* **`*/`**
>
> *comment-text* → *comment-text-item* *comment-text*_?_ \
> *comment-text-item* → Any Unicode scalar value except U+000A or U+000D
>
> *multiline-comment-text* → *multiline-comment-text-item* *multiline-comment-text*_?_ \
> *multiline-comment-text-item* → *multiline-comment* \
> *multiline-comment-text-item* → *comment-text-item* \
> *multiline-comment-text-item* → Any Unicode scalar value except **`/*`** or **`*/`**
## Identifiers
*Identifiers* begin with
an uppercase or lowercase 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.
Treat identifiers that begin with an underscore,
subscripts whose first argument label begins with an underscore,
and initializers whose first argument label begins with an underscore,
as internal,
even if their declaration has the `public` access-level modifier.
This convention lets framework authors mark part of an API
that clients must not interact with or depend on,
even though some limitation requires the declaration to be public.
In addition,
identifiers that begin with two underscores
are reserved for the Swift compiler and standard library.
To use a reserved word as an identifier,
put a backtick (\`) before and after it.
For example, `class` isn't a valid identifier,
but `` `class` `` is valid.
The backticks aren't considered part of the identifier;
`` `x` `` and `x` have the same meaning.
<!--
The paragraph above produces a link-resolution warning
because of a known issue with ` in code voice.
https://github.com/apple/swift-book/issues/71
https://github.com/apple/swift-markdown/issues/93
-->
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.
The compiler synthesizes identifiers that begin with a dollar sign (`$`)
for properties that have a property wrapper projection.
Your code can interact with these identifiers,
but you can't declare identifiers with that prefix.
For more information, see the <doc:Attributes#propertyWrapper> section
of the <doc:Attributes> chapter.
<!--
The cross reference above includes both the section and chapter because,
even though "propertyWrapper" is the title of the section,
the section name isn't title case so it doesn't necessarily look like a title.
-->
<!--
The formal grammar below for 'identifier'
produces a link-resolution warning
because of a known issue with ` in code voice.
https://github.com/apple/swift-book/issues/71
https://github.com/apple/swift-markdown/issues/93
-->
> Grammar of an identifier:
>
> *identifier* → *identifier-head* *identifier-characters*_?_ \
> *identifier* → **`` ` ``** *identifier-head* *identifier-characters*_?_ **`` ` ``** \
> *identifier* → *implicit-parameter-name* \
> *identifier* → *property-wrapper-projection* \
> *identifier-list* → *identifier* | *identifier* **`,`** *identifier-list*
>
> *identifier-head* → Upper- or lowercase letter A through Z \
> *identifier-head* → **`_`** \
> *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* → Digit 0 through 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*_?_
>
> *implicit-parameter-name* → **`$`** *decimal-digits* \
> *property-wrapper-projection* → **`$`** *identifier-characters*
## Keywords and Punctuation
The following keywords are reserved and can't be used as identifiers,
unless they're escaped with backticks,
as described above in <doc:LexicalStructure#Identifiers>.
Keywords other than `inout`, `var`, and `let`
can be used as parameter names
in a function declaration or function call
without being escaped with backticks.
When a member has the same name as a keyword,
references to that member don't need to be escaped with backticks,
except when there's ambiguity between referring to the member
and using the keyword ---
for example, `self`, `Type`, and `Protocol`
have special meaning in an explicit member expression,
so they must be escaped with backticks in that context.
<!--
- test: `keywords-without-backticks`
```swifttest
-> func f(x: Int, in y: Int) {
print(x+y)
}
```
-->
<!--
- test: `var-requires-backticks`
```swifttest
-> func g(`var` x: Int) {}
-> func f(var x: Int) {}
!$ warning: 'var' in this position is interpreted as an argument label
!! func f(var x: Int) {}
!! ^~~
!! `var`
```
-->
<!--
- test: `let-requires-backticks`
```swifttest
-> func g(`let` x: Int) {}
-> func f(let x: Int) {}
!$ warning: 'let' in this position is interpreted as an argument label
!! func f(let x: Int) {}
!! ^~~
!! `let`
```
-->
<!--
- test: `inout-requires-backticks`
```swifttest
-> func g(`inout` x: Int) {}
-> func f(inout x: Int) {}
!$ error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
!! func f(inout x: Int) {}
!! ^~~~~
!! inout
```
-->
<!--
NOTE: This list of language keywords and punctuation
is derived from the file "swift/include/swift/Parse/Tokens.def"
and from "utils/gyb_syntax_support/Token.py",
which generates the TokenKinds.def file.
Last updated at Swift commit 2f1987567f5, for Swift 5.4.
-->
- Keywords used in declarations:
`associatedtype`,
`borrowing`,
`class`,
`consuming`,
`deinit`,
`enum`,
`extension`,
`fileprivate`,
`func`,
`import`,
`init`,
`inout`,
`internal`,
`let`,
`nonisolated`,
`open`,
`operator`,
`private`,
`precedencegroup`,
`protocol`,
`public`,
`rethrows`,
`static`,
`struct`,
`subscript`,
`typealias`,
and `var`.
<!--
Token.py doesn't include 'open' but DeclNodes.py does.
-->
- Keywords used in statements:
`break`,
`case`,
`catch`,
`continue`,
`default`,
`defer`,
`do`,
`else`,
`fallthrough`,
`for`,
`guard`,
`if`,
`in`,
`repeat`,
`return`,
`throw`,
`switch`,
`where`,
and `while`.
- Keywords used in expressions and types:
`Any`,
`as`,
`await`,
`catch`,
`false`,
`is`,
`nil`,
`rethrows`,
`self`,
`Self`,
`super`,
`throw`,
`throws`,
`true`,
and `try`.
- Keywords used in patterns:
`_`.
- Keywords that begin with a number sign (`#`):
`#available`,
`#colorLiteral`,
`#else`,
`#elseif`,
`#endif`,
`#fileLiteral`,
`#if`,
`#imageLiteral`,
`#keyPath`,
`#selector`,
`#sourceLocation`,
`#unavailable`.
> Note:
> Prior to Swift 5.9,
> the following keywords were reserved:
> `#column`,
> `#dsohandle`,
> `#error`,
> `#fileID`,
> `#filePath`,
> `#file`,
> `#function`,
> `#line`,
> and `#warning`.
> These are now implemented as macros in the Swift standard library:
> [`column`](https://developer.apple.com/documentation/swift/column()),
> [`dsohandle`](https://developer.apple.com/documentation/swift/dsohandle()),
> [`error(_:)`](https://developer.apple.com/documentation/swift/error(_:)),
> [`fileID`](https://developer.apple.com/documentation/swift/fileID()),
> [`filePath`](https://developer.apple.com/documentation/swift/filePath()),
> [`file`](https://developer.apple.com/documentation/swift/file()),
> [`function`](https://developer.apple.com/documentation/swift/function()),
> [`line`](https://developer.apple.com/documentation/swift/line()),
> and [`warning(_:)`](https://developer.apple.com/documentation/swift/warning(_:)).
<!--
Token.py includes #assert,
which looks like it's part of an experimental feature
based on the pound_assert_disabled diagnostic's error message:
#assert is an experimental feature that is currently disabled
-->
<!--
Token.py includes #fileID,
which looks like it's part of a future feature related to
-enable-experimental-concise-pound-file (see also Swift commit 0e569f5d9e66)
-->
<!--
Token.py includes 'yield' as a keyword,
which looks like it's related to a future feature around memory ownership.
-->
- Keywords reserved in particular contexts:
`associativity`,
`convenience`,
`didSet`,
`dynamic`,
`final`,
`get`,
`indirect`,
`infix`,
`lazy`,
`left`,
`mutating`,
`none`,
`nonmutating`,
`optional`,
`override`,
`package`,
`postfix`,
`precedence`,
`prefix`,
`Protocol`,
`required`,
`right`,
`set`,
`some`,
`Type`,
`unowned`,
`weak`,
and `willSet`.
Outside the context in which they appear in the grammar,
they can be used as identifiers.
<!--
NOTE: The list of context-sensitive keywords above
is derived from the file "swift/include/swift/AST/Attr.def"
where they're marked CONTEXTUAL_SIMPLE_DECL_ATTR.
However, not all context-sensitive keywords appear there;
-->
The following tokens are reserved as punctuation
and can't be used as custom operators:
`(`, `)`, `{`, `}`, `[`, `]`,
`.`, `,`, `:`, `;`, `=`, `@`, `#`,
`&` (as a prefix operator), `->`, `` ` ``,
`?`, and `!` (as a postfix operator).
## Literals
A *literal* is the source code representation of a value of a type,
such as a number or string.
The following are examples of literals:
```swift
42 // Integer literal
3.14159 // Floating-point literal
"Hello, world!" // String literal
/Hello, .*/ // Regular expression literal
true // Boolean literal
```
<!--
- test: `basic-literals`
```swifttest
>> let r0 =
-> 42 // Integer literal
>> let r1 =
-> 3.14159 // Floating-point literal
>> let r2 =
-> "Hello, world!" // String literal
>> let r4 =
-> /Hello, .*/ // Regular expression literal
>> let r3 =
-> true // Boolean literal
>> for x in [r0, r1, r2, r3] as [Any] { print(type(of: x)) }
<< Int
<< Double
<< String
<< Bool
```
-->
<!--
Refactor the above if possible to avoid using bare expressions.
Tracking bug is <rdar://problem/35301593>
-->
A literal doesn't have a type on its own.
Instead, a literal is parsed as having infinite precision and Swift's type inference
attempts to infer a type for the literal. For example,
in the declaration `let x: Int8 = 42`,
Swift uses the explicit type annotation (`: Int8`) to infer
that the type of the integer literal `42` is `Int8`.
If there isn't suitable type information available,
Swift infers that the literal's type is one of the default literal types
defined in the Swift standard library
and listed in the table below.
When specifying the type annotation for a literal value,
the annotation's type must be a type that can be instantiated from that literal value.
That is, the type must conform to the Swift standard library protocols
listed in the table below.
| Literal | Default type | Protocol |
| ------- | ------------ | -------- |
| Integer | `Int` | `ExpressibleByIntegerLiteral` |
| Floating-point | `Double` | `ExpressibleByFloatLiteral` |
| String | `String` | `ExpressibleByStringLiteral`, `ExpressibleByUnicodeScalarLiteral` for string literals that contain only a single Unicode scalar, `ExpressibleByExtendedGraphemeClusterLiteral` for string literals that contain only a single extended grapheme cluster |
| Regular expression | `Regex` | None |
| Boolean | `Bool` | `ExpressibleByBooleanLiteral` |
For example, in the declaration `let str = "Hello, world"`,
the default inferred type of the string
literal `"Hello, world"` is `String`.
Also, `Int8` conforms to the `ExpressibleByIntegerLiteral` protocol,
and therefore it can be used in the type annotation for the integer literal `42`
in the declaration `let x: Int8 = 42`.
<!--
The list of ExpressibleBy... protocols above also appears in Declarations_EnumerationsWithRawCaseValues.
ExpressibleByNilLiteral is left out of the list because conformance to it isn't recommended.
There is no protocol for regex literal in the list because the stdlib intentionally omits that.
-->
> Grammar of a literal:
>
> *literal* → *numeric-literal* | *string-literal* | *regular-expression-literal* | *boolean-literal* | *nil-literal*
>
> *numeric-literal* → **`-`**_?_ *integer-literal* | **`-`**_?_ *floating-point-literal* \
> *boolean-literal* → **`true`** | **`false`** \
> *nil-literal* → **`nil`**
### Integer Literals
*Integer literals* represent integer values of unspecified precision.
By default, integer literals are expressed in decimal;
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`.
Decimal literals contain the digits `0` through `9`.
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.
Negative integers literals are expressed by prepending a minus sign (`-`)
to an integer literal, as in `-42`.
Underscores (`_`) are allowed between digits for readability,
but they're ignored and therefore don't affect the value of the literal.
Integer literals can begin with leading zeros (`0`),
but they're likewise ignored and don't affect the base or value of the literal.
Unless otherwise specified,
the default inferred 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 <doc:TheBasics#Integers>.
<!--
TR: The prose assumes underscores only belong between digits.
Is there a reason to allow them at the end of a literal?
Java and Ruby both require underscores to be between digits.
Also, are adjacent underscores meant to be allowed, like 5__000?
(REPL supports them as of swift-1.21 but it seems odd.)
-->
<!--
NOTE: Updated the syntax-grammar to reflect [Contributor 7746]'s comment in
<rdar://problem/15181997> Teach the compiler about a concept of negative integer literals.
This feels very strange from a grammatical point of view.
Update: This is a parser hack, not a lexer hack. Therefore,
it's not part of the grammar for integer literal, contrary to [Contributor 2562]'s claim.
(Doug confirmed this, 4/2/2014.)
-->
> Grammar of an integer literal:
>
> *integer-literal* → *binary-literal* \
> *integer-literal* → *octal-literal* \
> *integer-literal* → *decimal-literal* \
> *integer-literal* → *hexadecimal-literal*
>
> *binary-literal* → **`0b`** *binary-digit* *binary-literal-characters*_?_ \
> *binary-digit* → Digit 0 or 1 \
> *binary-literal-character* → *binary-digit* | **`_`** \
> *binary-literal-characters* → *binary-literal-character* *binary-literal-characters*_?_
>
> *octal-literal* → **`0o`** *octal-digit* *octal-literal-characters*_?_ \
> *octal-digit* → Digit 0 through 7 \
> *octal-literal-character* → *octal-digit* | **`_`** \
> *octal-literal-characters* → *octal-literal-character* *octal-literal-characters*_?_
>
> *decimal-literal* → *decimal-digit* *decimal-literal-characters*_?_ \
> *decimal-digit* → Digit 0 through 9 \
> *decimal-digits* → *decimal-digit* *decimal-digits*_?_ \
> *decimal-literal-character* → *decimal-digit* | **`_`** \
> *decimal-literal-characters* → *decimal-literal-character* *decimal-literal-characters*_?_
>
> *hexadecimal-literal* → **`0x`** *hexadecimal-digit* *hexadecimal-literal-characters*_?_ \
> *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*_?_
### 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 upper- or lowercase `e` prefix
followed by a sequence of decimal digits that indicates
what power of 10 the value preceding the `e` is multiplied by.
For example, `1.25e2` represents 1.25 x 10²,
which evaluates to `125.0`.
Similarly, `1.25e-2` represents 1.25 x 10⁻²,
which evaluates to `0.0125`.
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 a sequence of decimal digits that indicates
what power of 2 the value preceding the `p` is multiplied by.
For example, `0xFp2` represents 15 x 2²,
which evaluates to `60`.
Similarly, `0xFp-2` represents 15 x 2⁻²,
which evaluates to `3.75`.
Negative floating-point literals are expressed by prepending a minus sign (`-`)
to a floating-point literal, as in `-42.5`.
Underscores (`_`) are allowed between digits for readability,
but they're ignored and therefore don't affect the value of the literal.
Floating-point literals can begin with leading zeros (`0`),
but they're likewise ignored and don't affect the base or value of the literal.
Unless otherwise specified,
the default inferred 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.
> Grammar of a floating-point literal:
>
> *floating-point-literal* → *decimal-literal* *decimal-fraction*_?_ *decimal-exponent*_?_ \
> *floating-point-literal* → *hexadecimal-literal* *hexadecimal-fraction*_?_ *hexadecimal-exponent*
>
> *decimal-fraction* → **`.`** *decimal-literal* \
> *decimal-exponent* → *floating-point-e* *sign*_?_ *decimal-literal*
>
> *hexadecimal-fraction* → **`.`** *hexadecimal-digit* *hexadecimal-literal-characters*_?_ \
> *hexadecimal-exponent* → *floating-point-p* *sign*_?_ *decimal-literal*
>
> *floating-point-e* → **`e`** | **`E`** \
> *floating-point-p* → **`p`** | **`P`** \
> *sign* → **`+`** | **`-`**
### String Literals
A string literal is a sequence of characters surrounded by quotation marks.
A single-line string literal is surrounded by double quotation marks
and has the following form:
```swift
"<#characters#>"
```
String literals can't contain
an unescaped double quotation mark (`"`),
an unescaped backslash (`\`),
a carriage return, or a line feed.
A multiline string literal is surrounded by three double quotation marks
and has the following form:
```swift
"""
<#characters#>
"""
```
Unlike a single-line string literal,
a multiline string literal can contain
unescaped double quotation marks (`"`), carriage returns, and line feeds.
It can't contain three unescaped double quotation marks next to each other.
The line break after the `"""`
that begins the multiline string literal
isn't part of the string.
The line break before the `"""`
that ends the literal is also not part of the string.
To make a multiline string literal
that begins or ends with a line feed,
write a blank line as its first or last line.
A multiline string literal can be indented
using any combination of spaces and tabs;
this indentation isn't included in the string.
The `"""` that ends the literal
determines the indentation:
Every nonblank line in the literal must begin
with exactly the same indentation
that appears before the closing `"""`;
there's no conversion between tabs and spaces.
You can include additional spaces and tabs after that indentation;
those spaces and tabs appear in the string.
Line breaks in a multiline string literal are
normalized to use the line feed character.
Even if your source file has a mix of carriage returns and line feeds,
all of the line breaks in the string will be the same.
In a multiline string literal,
writing a backslash (`\`) at the end of a line
omits that line break from the string.
Any whitespace between the backslash and the line break
is also omitted.
You can use this syntax
to hard wrap a multiline string literal in your source code,
without changing the value of the resulting string.
Special characters
can be included in string literals
of both the single-line and multiline forms
using the following escape sequences:
- Null character (`\0`)
- Backslash (`\\`)
- Horizontal tab (`\t`)
- Line feed (`\n`)
- Carriage return (`\r`)
- Double quotation mark (`\"`)
- Single quotation mark (`\'`)
- Unicode scalar (`\u{`*n*`}`),
where *n* is a hexadecimal number
that has one to eight digits
<!--
The behavior of \n and \r isn't the same as C.
We specify exactly what those escapes mean.
The behavior on C is platform dependent --
in text mode, \n maps to the platform's line separator
which could be CR or LF or CRLF.
-->
The value of an expression can be inserted into a string literal
by placing the expression in parentheses after a backslash (`\`).
The interpolated expression can contain a string literal,
but can't contain an unescaped backslash,
a carriage return, or a line feed.
For example, all of the following string literals have the same value:
```swift
"1 2 3"
"1 2 \("3")"
"1 2 \(3)"
"1 2 \(1 + 2)"
let x = 3; "1 2 \(x)"
```
<!--
- test: `string-literals`
```swifttest
>> let r0 =
-> "1 2 3"
>> let r1 =
-> "1 2 \("3")"
>> assert(r0 == r1)
>> let r2 =
-> "1 2 \(3)"
>> assert(r0 == r2)
>> let r3 =
-> "1 2 \(1 + 2)"
>> assert(r0 == r3)
-> let x = 3; "1 2 \(x)"
>> assert(r0 == "1 2 \(x)")
!$ warning: string literal is unused
!! let x = 3; "1 2 \(x)"
!! ^~~~~~~~~~
```
-->
<!--
Refactor the above if possible to avoid using bare expressions.
Tracking bug is <rdar://problem/35301593>
-->
A string delimited by extended delimiters is a sequence of characters
surrounded by quotation marks and a balanced set of one or more number signs (`#`).
A string delimited by extended delimiters has the following forms:
```swift
#"<#characters#>"#
#"""
<#characters#>
"""#
```
Special characters in a string delimited by extended delimiters
appear in the resulting string as normal characters
rather than as special characters.
You can use extended delimiters to create strings with characters
that would ordinarily have a special effect
such as generating a string interpolation,
starting an escape sequence,
or terminating the string.
The following example shows a string literal
and a string delimited by extended delimiters
that create equivalent string values:
```swift
let string = #"\(x) \ " \u{2603}"#
let escaped = "\\(x) \\ \" \\u{2603}"
print(string)
// Prints "\(x) \ " \u{2603}"
print(string == escaped)
// Prints "true"
```
<!--
- test: `extended-string-delimiters`
```swifttest
-> let string = #"\(x) \ " \u{2603}"#
-> let escaped = "\\(x) \\ \" \\u{2603}"
-> print(string)
<- \(x) \ " \u{2603}
-> print(string == escaped)
<- true
```
-->
If you use more than one number sign to form
a string delimited by extended delimiters,
don't place whitespace in between the number signs:
<!--
- test: `extended-string-delimiters`
```swifttest
-> print(###"Line 1\###nLine 2"###) // OK
<< Line 1
<< Line 2
```
-->
```swift
print(###"Line 1\###nLine 2"###) // OK
print(# # #"Line 1\# # #nLine 2"# # #) // Error
```
<!--
- test: `extended-string-delimiters-err`
```swifttest
-> print(###"Line 1\###nLine 2"###) // OK
-> print(# # #"Line 1\# # #nLine 2"# # #) // Error
!$ error: expected expression in list of expressions
!! print(# # #"Line 1\# # #nLine 2"# # #) // Error
!! ^
!$ error: invalid escape sequence in literal
!! print(# # #"Line 1\# # #nLine 2"# # #) // Error
!! ^
```
-->
Multiline string literals that you create using extended delimiters
have the same indentation requirements as regular multiline string literals.
The default inferred type of a string literal is `String`.
For more information about the `String` type,
see <doc:StringsAndCharacters>
and [`String`](https://developer.apple.com/documentation/swift/string).
String literals that are concatenated by the `+` operator
are concatenated at compile time.
For example, the values of `textA` and `textB`
in the example below are identical ---
no runtime concatenation is performed.
```swift
let textA = "Hello " + "world"
let textB = "Hello world"
```
<!--
- test: `concatenated-strings`
```swifttest
-> let textA = "Hello " + "world"
-> let textB = "Hello world"
```
-->
> Grammar of a string literal:
>
> *string-literal* → *static-string-literal* | *interpolated-string-literal*
>
> *string-literal-opening-delimiter* → *extended-string-literal-delimiter*_?_ **`"`** \
> *string-literal-closing-delimiter* → **`"`** *extended-string-literal-delimiter*_?_
>
> *static-string-literal* → *string-literal-opening-delimiter* *quoted-text*_?_ *string-literal-closing-delimiter* \
> *static-string-literal* → *multiline-string-literal-opening-delimiter* *multiline-quoted-text*_?_ *multiline-string-literal-closing-delimiter*
>
> *multiline-string-literal-opening-delimiter* → *extended-string-literal-delimiter*_?_ **`"""`** \
> *multiline-string-literal-closing-delimiter* → **`"""`** *extended-string-literal-delimiter*_?_ \
> *extended-string-literal-delimiter* → **`#`** *extended-string-literal-delimiter*_?_
>
> *quoted-text* → *quoted-text-item* *quoted-text*_?_ \
> *quoted-text-item* → *escaped-character* \
> *quoted-text-item* → Any Unicode scalar value except **`"`**, **`\`**, U+000A, or U+000D
>
> *multiline-quoted-text* → *multiline-quoted-text-item* *multiline-quoted-text*_?_ \
> *multiline-quoted-text-item* → *escaped-character* \
> *multiline-quoted-text-item* → Any Unicode scalar value except **`\`** \
> *multiline-quoted-text-item* → *escaped-newline*
>
> *interpolated-string-literal* → *string-literal-opening-delimiter* *interpolated-text*_?_ *string-literal-closing-delimiter* \
> *interpolated-string-literal* → *multiline-string-literal-opening-delimiter* *multiline-interpolated-text*_?_ *multiline-string-literal-closing-delimiter*
>
> *interpolated-text* → *interpolated-text-item* *interpolated-text*_?_ \
> *interpolated-text-item* → **`\(`** *expression* **`)`** | *quoted-text-item*
>
> *multiline-interpolated-text* → *multiline-interpolated-text-item* *multiline-interpolated-text*_?_ \
> *multiline-interpolated-text-item* → **`\(`** *expression* **`)`** | *multiline-quoted-text-item*
>
> *escape-sequence* → **`\`** *extended-string-literal-delimiter* \
> *escaped-character* → *escape-sequence* **`0`** | *escape-sequence* **`\`** | *escape-sequence* **`t`** | *escape-sequence* **`n`** | *escape-sequence* **`r`** | *escape-sequence* **`"`** | *escape-sequence* **`'`** \
> *escaped-character* → *escape-sequence* **`u`** **`{`** *unicode-scalar-digits* **`}`** \
> *unicode-scalar-digits* → Between one and eight hexadecimal digits
>
> *escaped-newline* → *escape-sequence* *inline-spaces*_?_ *line-break*
<!--
Quoted text resolves to a sequence of escaped characters by way of
the quoted-text rule which allows repetition; no need to allow
repetition in the quoted-text/escaped-character rule too.
-->
<!--
Now that single quotes are gone, we don't have a character literal.
Because we may one bring them back, here's the old grammar for them:
textual-literal -> character-literal | string-literal
character-literal -> ``'`` quoted-character ``'``
quoted-character -> escaped-character
quoted-character -> Any Unicode scalar value except ``'``, ``\``, U+000A, or U+000D
-->
### Regular Expression Literals
A regular expression literal is a sequence of characters
surrounded by slashes (`/`) with the following form:
```swift
/<#regular expression#>/
```
Regular expression literals
must not begin with an unescaped tab or space,
and they can't contain
an unescaped slash (`/`),
a carriage return, or a line feed.
Within a regular expression literal,
a backslash is understood as a part of that regular expression,
not just as an escape character like in string literals.
It indicates that the following special character
should be interpreted literally,
or that the following nonspecial character
should be interpreted in a special way.
For example,
`/\(/` matches a single left parenthesis
and `/\d/` matches a single digit.
<!--
OUTLINE
Doc comments on Regex struct don't have more syntax details,
or a cross reference to where you can learn more.
We probably need at least some baseline coverage
of the supported syntax here.
(Unified dialect/superset of POSIX + PCRE 2 + Oniguruma + .NET)
https://github.com/apple/swift-experimental-string-processing/blob/main/Sources/_StringProcessing/Regex/Core.swift
Regex literals and the DSL take different approaches to captures.
The literals give you more type safety.
The DSL lets you access stuff by name.
From SE-0354:
A regex literal may be used with a prefix operator,
e.g `let r = ^^/x/` is parsed as `let r = ^^(/x/)`.
In this case,
when encountering operator characters containing `/` in an expression position,
the characters up to the first `/` are split into a prefix operator,
and regex literal parsing continues as normal.
-->
A regular expression literal delimited by extended delimiters
is a sequence of characters surrounded by slashes (`/`)
and a balanced set of one or more number signs (`#`).
A regular expression literal
delimited by extended delimiters has the following forms:
```swift
#/<#regular expression#>/#
#/
<#regular expression#>
/#
```