9
9
content ="Swift Language Reference Manual. ">
10
10
< link rel ="stylesheet " href ="swift.css " type ="text/css ">
11
11
12
- <!-- FIXME: Self contained! -->
13
- < script type ="text/javascript " src ="http://golang.org/doc/godocs.js "> </ script >
12
+ < script type ="text/javascript " src ="toc.js "> </ script >
14
13
15
14
</ head >
16
15
17
16
< body >
18
17
19
18
< h1 > Swift Language Reference</ h1 >
20
-
21
- <!-- Hack to be compatible with the go stuff -->
22
- < input id ="search " type ="text " name ="q " value ="code search " class ="inactive "
23
- style ="display: none "/>
24
19
25
20
< p >
26
21
<!-- The Table of Contents is automatically inserted in this <div>.
@@ -33,6 +28,14 @@ <h1>Swift Language Reference</h1>
33
28
<!-- ********************************************************************* -->
34
29
< h2 > Introduction</ h2 >
35
30
<!-- ********************************************************************* -->
31
+
32
+ < div class ="commentary ">
33
+ In addition to the main spec, there are lots of open ended questions,
34
+ justification, and ideas of what best practices should be. That random
35
+ discussion is placed in boxes to the right side of the main text (like this
36
+ one) to make clarify what is normative and what is discussion.
37
+ </ div >
38
+
36
39
37
40
< p > This is the language reference manual for the Swift language, which is
38
41
highly volatile and constantly under development. It is my (Chris') intention
@@ -41,11 +44,17 @@ <h2>Introduction</h2>
41
44
< p > The grammar and structure of the language is defined in BNF form in yellow
42
45
boxes. Examples are shown in gray boxes, and assume that the standard library
43
46
is in use (unless otherwise specified).</ p >
44
-
47
+
45
48
<!-- ===================================================================== -->
46
49
< h3 > Basic Goals</ h3 >
47
50
<!-- ===================================================================== -->
48
51
52
+ < div class ="commentary ">
53
+ A non-goal of the Swift project in general is to become some amazing
54
+ research project. We really want to focus on delivering a real product,
55
+ and having the design and spec co-evolve.
56
+ </ div >
57
+
49
58
< ol >
50
59
< li > Support building great frameworks and applications, making it easier to
51
60
do the right thing by default and reducing the barrier of entry to our
@@ -87,13 +96,26 @@ <h3>Basic Approach</h3>
87
96
< h2 > Lexical Structure</ h2 >
88
97
<!-- ********************************************************************* -->
89
98
99
+ < div class ="commentary ">
100
+ Not all characters are "taken" in the language, this is because it is still
101
+ growing. As there becomes a reason to assign things into the identifier or
102
+ punctuation bucket, we will do so as swift evolves.
103
+ </ div >
104
+
90
105
< p > The lexical structure of a Swift file is very simple: the files are
91
- tokenized according to the following productions and categories.</ p >
106
+ tokenized according to the following productions and categories. As is
107
+ usual with most languages, tokenization uses the maximal munch rule and
108
+ whitespace separates tokens. This means that "a b" and "ab" lex into
109
+ different token streams and are therefore different in the grammar.</ p >
92
110
93
111
<!-- ===================================================================== -->
94
112
< h3 > Whitespace and Comments</ h3 >
95
113
<!-- ===================================================================== -->
96
114
115
+ < div class ="commentary ">
116
+ TODO: /**/ comments will be supported when I get around to it.
117
+ </ div >
118
+
97
119
< pre class ="grammar ">
98
120
whitespace ::= ' '
99
121
whitespace ::= '\n'
@@ -109,12 +131,17 @@ <h3>Whitespace and Comments</h3>
109
131
< p > Comments follow the BCPL style, starting with a "//" and running to the end
110
132
of the file. Comments are ignored as whitespace.</ p >
111
133
112
- < p > TODO: /**/ comments will be supported someday.</ p >
113
-
114
134
<!-- ===================================================================== -->
115
135
< h3 > Reserved Punctuation Tokens</ h3 >
116
136
<!-- ===================================================================== -->
117
-
137
+
138
+ < div class ="commentary ">
139
+ The difference between reserved punctuation and identifiers is that you
140
+ can't "overload an operator" with one of these names.< br > < br >
141
+ Note that -> is used for function types "() -> int", not pointer
142
+ dereferencing.
143
+ </ div >
144
+
118
145
< pre class ="grammar ">
119
146
punctuation ::= '('
120
147
punctuation ::= ')'
@@ -132,13 +159,24 @@ <h3>Reserved Punctuation Tokens</h3>
132
159
</ pre >
133
160
134
161
< p > These are all reserved punctuation that are lexed into tokens. Most other
135
- punctionation is matched as < a href ="identifier "> identifiers</ a > .
162
+ punctuation is matched as < a href ="identifier "> identifiers</ a > .
136
163
</ p >
137
164
138
165
<!-- ===================================================================== -->
139
166
< h3 > Reserved Keywords</ h3 >
140
167
<!-- ===================================================================== -->
141
-
168
+
169
+ < div class ="commentary ">
170
+ The number of keywords is reduced by pushing most control flow and other
171
+ stuff into the library. This allows us to add new stuff to the library in
172
+ the future without worrying about conflicting with the user's namespace.
173
+ Because 'if' is just a library function, you can shadow "if" with your own
174
+ variable (though this is an example of a silly thing that is not a good
175
+ practice of course).< br > < br >
176
+
177
+ Idea: Should _foo be "private" and "foo" be public?
178
+ </ div >
179
+
142
180
< pre class ="grammar ">
143
181
keyword ::= '__builtin_int32_type'
144
182
keyword ::= 'oneof'
@@ -151,12 +189,14 @@ <h3>Reserved Keywords</h3>
151
189
152
190
< p > These are the builtin keywords. Swift intentionally tries to reduce the
153
191
number of keywords where possible.</ p >
154
-
155
- < p > FIXME: $0 and friends are modeled as an identifier not a keyword, the
156
- proto needs to be fixed so that you aren't allowed to define $0.</ p >
157
192
193
+ < p > FIXME: $0 and friends are modeled as an identifier not a keyword, the
194
+ proto needs to be fixed so that you aren't allowed to define $0.</ p >
158
195
< p > FIXME: Change "foo.field0" to "foo.$0" for accessing an anonymous tuple
159
- element.</ p >
196
+ element.</ p >
197
+ < p > FIXME: Allow $1234</ p >
198
+ < p > FIXME: __ should be considered the compiler/languages reserved namespace,
199
+ any use of an unknown identifier here should be an error.</ p >
160
200
161
201
162
202
<!-- ===================================================================== -->
@@ -183,7 +223,9 @@ <h3 id="identifier">Identifier Tokens</h3>
183
223
184
224
< p > There are two different regular expressions for identifiers, one for normal
185
225
identifiers and one for "punctuation identifiers". This ensures that
186
- something like "foo+bar" gets lexed into three identifiers, not one.
226
+ something like "foo+bar" gets lexed into three identifiers, not one. Aside
227
+ from the regex that controls lexing behavior, there is no other difference
228
+ between these two forms of identifiers.
187
229
</ p >
188
230
189
231
@@ -206,7 +248,12 @@ <h2 id="decl">Declarations</h2>
206
248
Extraneous semi colons are allowed at top level, as are a number of other
207
249
declarations.</ p >
208
250
209
- < p > TODO: Need to define the module system.</ p >
251
+ < p > TODO: Need to define the module system, which will give us 'import' and
252
+ 'module'.</ p >
253
+
254
+ < p > FIXME: Code should be definable at the top level of a file as well, this
255
+ becomes a static constructor for a library and is the "main" for an
256
+ executable.</ p >
210
257
211
258
<!-- ===================================================================== -->
212
259
< h3 id ="decl-var "> var</ h3 >
@@ -336,11 +383,21 @@ <h3 id="decl-typealias">typealias</h3>
336
383
typealias pair_fn : int -> int -> (first : int, second : int)
337
384
</ pre >
338
385
386
+ < p > FIXME: It's not "from that point on", you should be able to declare a type
387
+ alias after using it. Need to separate name binding from parsing logic.
388
+ Without this, mutually dependent component members can't exist.</ p >
339
389
340
390
<!-- ===================================================================== -->
341
391
< h3 id ="decl-oneof "> oneof</ h3 >
342
392
<!-- ===================================================================== -->
343
393
394
+ < div class ="commentary ">
395
+ In actual practice, we expect oneof to be commonly used for "enums" and
396
+ "struct" below to be used for data declarations. The use of "oneof" for
397
+ discriminated unions will be much less common (but is still very important)
398
+ than its use for "enums".
399
+ </ div >
400
+
344
401
< pre class ="grammar ">
345
402
decl-oneof ::= 'oneof' < a href ="#attribute-list "> attribute-list</ a > ? < a href ="#identifier "> identifier</ a > '{' oneof-element-list '}'
346
403
@@ -709,6 +766,13 @@ <h3 id="type-array">Array Types</h3>
709
766
< h2 id ="expr "> Expressions</ h2 >
710
767
<!-- ********************************************************************* -->
711
768
769
+ < div class ="commentary ">
770
+ The goal of semicolon elision is to clean up the common case where ;'s are
771
+ just clutter. While they allow people to write confusing and obfuscated
772
+ code, any language feature can be abused. We'll encourage good practices in
773
+ the documentation and frameworks.
774
+ </ div >
775
+
712
776
< pre class ="grammar ">
713
777
expr ::= < a href ="#expr-single "> expr-single</ a > +
714
778
</ pre >
@@ -722,8 +786,11 @@ <h2 id="expr">Expressions</h2>
722
786
< i > // A silly, but valid, example:</ i >
723
787
4 4 (4+5) 4 4
724
788
725
- < i > // A more reasonable example (usually written on multiple lines.</ i >
726
- foo() x = 12 bar(49+1) baz()
789
+ < i > // A more reasonable example.</ i >
790
+ foo()
791
+ x = 12
792
+ bar(49+1)
793
+ baz()
727
794
</ i >
728
795
</ pre >
729
796
@@ -780,6 +847,16 @@ <h3 id="expr-primary">Primary Expressions</h3>
780
847
< h3 id ="expr-literal "> Simple Literals</ h3 >
781
848
<!-- ===================================================================== -->
782
849
850
+ < div class ="commentary ">
851
+ In the short-term, this works, but this isn't satisfactory: The whole idea
852
+ of having 'int' be defined in the library is that we want the runtime to
853
+ define it. Having literals fixed to a specific type defeats this. This
854
+ also breaks things like 64-bit integers etc. There are a couple of ways to
855
+ handle this, such as C++'0x user defined literal suffixes, go's approach of
856
+ treating them as arbitrary precision integers that are resolved to a type
857
+ later, etc. We must return to this at some point.
858
+ </ div >
859
+
783
860
< pre class ="grammar ">
784
861
expr-literal ::= < a href ="#numeric_constant "> numeric_constant</ a >
785
862
</ pre >
@@ -841,6 +918,8 @@ <h3 id="expr-field">Field Expressions</h3>
841
918
842
919
< p > No other field accesses are currently allowed.</ p >
843
920
921
+ < p > FIXME: Change field0 to $0</ p >
922
+
844
923
<!-- ===================================================================== -->
845
924
< h3 id ="expr-subscript "> Subscript Expressions</ h3 >
846
925
<!-- ===================================================================== -->
0 commit comments