Skip to content

Commit 70bb588

Browse files
committed
Various updates thanks to comments from DaveZ, add a right hand sidebar with commentary.
Move the .js file internally so we're not dependent on the go server. Swift SVN r213
1 parent 906ba90 commit 70bb588

File tree

2 files changed

+253
-21
lines changed

2 files changed

+253
-21
lines changed

Diff for: docs/LangRef.html

+100-21
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,13 @@
99
content="Swift Language Reference Manual.">
1010
<link rel="stylesheet" href="swift.css" type="text/css">
1111

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>
1413

1514
</head>
1615

1716
<body>
1817

1918
<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"/>
2419

2520
<p>
2621
<!-- The Table of Contents is automatically inserted in this <div>.
@@ -33,6 +28,14 @@ <h1>Swift Language Reference</h1>
3328
<!-- ********************************************************************* -->
3429
<h2>Introduction</h2>
3530
<!-- ********************************************************************* -->
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+
3639

3740
<p>This is the language reference manual for the Swift language, which is
3841
highly volatile and constantly under development. It is my (Chris') intention
@@ -41,11 +44,17 @@ <h2>Introduction</h2>
4144
<p>The grammar and structure of the language is defined in BNF form in yellow
4245
boxes. Examples are shown in gray boxes, and assume that the standard library
4346
is in use (unless otherwise specified).</p>
44-
47+
4548
<!-- ===================================================================== -->
4649
<h3>Basic Goals</h3>
4750
<!-- ===================================================================== -->
4851

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+
4958
<ol>
5059
<li>Support building great frameworks and applications, making it easier to
5160
do the right thing by default and reducing the barrier of entry to our
@@ -87,13 +96,26 @@ <h3>Basic Approach</h3>
8796
<h2>Lexical Structure</h2>
8897
<!-- ********************************************************************* -->
8998

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+
90105
<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>
92110

93111
<!-- ===================================================================== -->
94112
<h3>Whitespace and Comments</h3>
95113
<!-- ===================================================================== -->
96114

115+
<div class="commentary">
116+
TODO: /**/ comments will be supported when I get around to it.
117+
</div>
118+
97119
<pre class="grammar">
98120
whitespace ::= ' '
99121
whitespace ::= '\n'
@@ -109,12 +131,17 @@ <h3>Whitespace and Comments</h3>
109131
<p>Comments follow the BCPL style, starting with a "//" and running to the end
110132
of the file. Comments are ignored as whitespace.</p>
111133

112-
<p>TODO: /**/ comments will be supported someday.</p>
113-
114134
<!-- ===================================================================== -->
115135
<h3>Reserved Punctuation Tokens</h3>
116136
<!-- ===================================================================== -->
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 -&gt; is used for function types "() -&gt; int", not pointer
142+
dereferencing.
143+
</div>
144+
118145
<pre class="grammar">
119146
punctuation ::= '('
120147
punctuation ::= ')'
@@ -132,13 +159,24 @@ <h3>Reserved Punctuation Tokens</h3>
132159
</pre>
133160

134161
<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>.
136163
</p>
137164

138165
<!-- ===================================================================== -->
139166
<h3>Reserved Keywords</h3>
140167
<!-- ===================================================================== -->
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+
142180
<pre class="grammar">
143181
keyword ::= '__builtin_int32_type'
144182
keyword ::= 'oneof'
@@ -151,12 +189,14 @@ <h3>Reserved Keywords</h3>
151189

152190
<p>These are the builtin keywords. Swift intentionally tries to reduce the
153191
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>
157192

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>
158195
<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>
160200

161201

162202
<!-- ===================================================================== -->
@@ -183,7 +223,9 @@ <h3 id="identifier">Identifier Tokens</h3>
183223

184224
<p>There are two different regular expressions for identifiers, one for normal
185225
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.
187229
</p>
188230

189231

@@ -206,7 +248,12 @@ <h2 id="decl">Declarations</h2>
206248
Extraneous semi colons are allowed at top level, as are a number of other
207249
declarations.</p>
208250

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>
210257

211258
<!-- ===================================================================== -->
212259
<h3 id="decl-var">var</h3>
@@ -336,11 +383,21 @@ <h3 id="decl-typealias">typealias</h3>
336383
typealias pair_fn : int -> int -> (first : int, second : int)
337384
</pre>
338385

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>
339389

340390
<!-- ===================================================================== -->
341391
<h3 id="decl-oneof">oneof</h3>
342392
<!-- ===================================================================== -->
343393

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+
344401
<pre class="grammar">
345402
decl-oneof ::= 'oneof' <a href="#attribute-list">attribute-list</a>? <a href="#identifier">identifier</a> '{' oneof-element-list '}'
346403

@@ -709,6 +766,13 @@ <h3 id="type-array">Array Types</h3>
709766
<h2 id="expr">Expressions</h2>
710767
<!-- ********************************************************************* -->
711768

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+
712776
<pre class="grammar">
713777
expr ::= <a href="#expr-single">expr-single</a>+
714778
</pre>
@@ -722,8 +786,11 @@ <h2 id="expr">Expressions</h2>
722786
<i>// A silly, but valid, example:</i>
723787
4 4 (4+5) 4 4
724788

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()
727794
</i>
728795
</pre>
729796

@@ -780,6 +847,16 @@ <h3 id="expr-primary">Primary Expressions</h3>
780847
<h3 id="expr-literal">Simple Literals</h3>
781848
<!-- ===================================================================== -->
782849

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+
783860
<pre class="grammar">
784861
expr-literal ::= <a href="#numeric_constant">numeric_constant</a>
785862
</pre>
@@ -841,6 +918,8 @@ <h3 id="expr-field">Field Expressions</h3>
841918

842919
<p>No other field accesses are currently allowed.</p>
843920

921+
<p>FIXME: Change field0 to $0</p>
922+
844923
<!-- ===================================================================== -->
845924
<h3 id="expr-subscript">Subscript Expressions</h3>
846925
<!-- ===================================================================== -->

0 commit comments

Comments
 (0)