-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautomatic-semicolon-insertion.html
More file actions
390 lines (349 loc) · 10.8 KB
/
Copy pathautomatic-semicolon-insertion.html
File metadata and controls
390 lines (349 loc) · 10.8 KB
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Automatic Semicolon Insertion for Silicon</title>
<style>
:root {
color-scheme: light dark;
--bg: #fbfaf7;
--panel: #ffffff;
--text: #202124;
--muted: #5c6470;
--line: #d9dee7;
--accent: #245c8f;
--accent-soft: #e8f2fb;
--code-bg: #111827;
--code-text: #f7fafc;
--warn-bg: #fff5db;
--warn-line: #e5bb55;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #121417;
--panel: #1b1f24;
--text: #eceff3;
--muted: #b1bac4;
--line: #343b45;
--accent: #8ab7e6;
--accent-soft: #172433;
--code-bg: #080b10;
--code-text: #f7fafc;
--warn-bg: #2b2414;
--warn-line: #8f6b1f;
}
}
* { box-sizing: border-box; }
body {
margin: 0;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
line-height: 1.55;
}
main {
width: min(980px, calc(100% - 32px));
margin: 0 auto;
padding: 48px 0 72px;
}
header {
padding-bottom: 28px;
border-bottom: 1px solid var(--line);
}
.eyebrow {
margin: 0 0 10px;
color: var(--accent);
font-size: 0.82rem;
font-weight: 700;
letter-spacing: 0;
text-transform: uppercase;
}
h1, h2, h3 {
margin: 0;
line-height: 1.15;
letter-spacing: 0;
}
h1 {
max-width: 760px;
font-size: clamp(2rem, 5vw, 4rem);
}
h2 {
margin-top: 42px;
padding-top: 18px;
border-top: 1px solid var(--line);
font-size: 1.55rem;
}
h3 {
margin-top: 26px;
font-size: 1.05rem;
}
p {
max-width: 760px;
margin: 14px 0 0;
}
a { color: var(--accent); }
.lede {
max-width: 760px;
margin-top: 18px;
color: var(--muted);
font-size: 1.08rem;
}
.notice {
margin-top: 22px;
max-width: 760px;
border-left: 4px solid var(--warn-line);
background: var(--warn-bg);
padding: 14px 16px;
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 16px;
margin-top: 18px;
}
.card {
border: 1px solid var(--line);
border-radius: 8px;
background: var(--panel);
padding: 16px;
}
ul, ol {
max-width: 780px;
margin: 14px 0 0;
padding-left: 1.4rem;
}
li + li { margin-top: 8px; }
code {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
font-size: 0.92em;
}
:not(pre) > code {
padding: 0.1em 0.28em;
border-radius: 4px;
background: var(--accent-soft);
}
pre {
overflow-x: auto;
margin: 14px 0 0;
border-radius: 8px;
background: var(--code-bg);
color: var(--code-text);
padding: 16px;
}
pre code {
display: block;
min-width: max-content;
font-size: 0.9rem;
line-height: 1.55;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 16px;
background: var(--panel);
border: 1px solid var(--line);
border-radius: 8px;
overflow: hidden;
}
th, td {
padding: 12px 14px;
border-bottom: 1px solid var(--line);
text-align: left;
vertical-align: top;
}
tr:last-child td { border-bottom: 0; }
th { color: var(--muted); font-size: 0.9rem; }
</style>
</head>
<body>
<main>
<header>
<p class="eyebrow">Accepted · ADR 0026</p>
<h1>Automatic Semicolon Insertion for Silicon</h1>
<p class="lede">
Silicon should let a newline terminate a complete statement without
turning newline into expression syntax. ASI is accepted only where the
parser already expects a statement terminator.
</p>
<div class="notice">
This page documents the accepted ADR 0026 behavior implemented by the
handwritten lexer/parser.
</div>
</header>
<section>
<h2>Motivation</h2>
<p>
ADR 0020 moved Silicon toward a bare, line-readable surface:
definitions are ordinary names, calls are parenthesized, and explicit
sigils are reserved for language forms. Requiring semicolons after every
complete item keeps the grammar simple, but it makes ordinary examples
look heavier than the language intends.
</p>
<p>
The goal is not JavaScript-style recovery. The goal is a narrow rule:
at statement boundaries, a newline may do the same job as a semicolon.
</p>
</section>
<section>
<h2>Final Insertion Rules</h2>
<ol>
<li>Explicit <code>;</code> stays valid everywhere it is valid today.</li>
<li>At the top level, a newline or EOF may terminate a complete item.</li>
<li>Inside a block, a newline may terminate a complete item only when the next token starts another item.</li>
<li>Do not insert before <code>}</code> by default; a trailing block expression remains the block value.</li>
<li>Do not insert after incomplete tokens: <code>:=</code>, <code>=</code>, binary operators, commas, <code>::</code>, open delimiters, <code>\\</code>, or signature modifiers.</li>
<li>Do not insert before continuation tokens: binary operators, commas, <code>.</code>, <code>::</code>, <code>)</code>, or <code>]</code>.</li>
<li>A call's <code>(</code> must stay on the same logical line as the callee.</li>
<li>Normal <code>\\</code> signature lines attach to the next definition. Bodyless <code>\\ @extern ...</code> signatures may terminate at line end.</li>
</ol>
</section>
<section>
<h2>Examples</h2>
<h3>Top-level items</h3>
<pre><code class="language-silicon">\\ add (Int, Int) -> Int
@fn add a, b := a + b
answer := add(20, 22)
print_int(answer)</code></pre>
<h3>Block values</h3>
<pre><code class="language-silicon">\\ score (Int) -> Int
@fn score n := {
doubled := n * 2
capped := @if(doubled > 100, { 100 }, { doubled })
capped
}</code></pre>
<p>
No semicolon is inserted before <code>}</code>, so <code>capped</code>
remains the block's value.
</p>
<h3>Multiline calls</h3>
<pre><code class="language-silicon">total := add(
subtotal,
tax
)
message := format_invoice(
customer,
total
)</code></pre>
<h3>Binary continuations</h3>
<pre><code class="language-silicon">total := subtotal
+ tax
+ shipping
ready := subtotal
+ tax
+ shipping</code></pre>
<h3>Signature lines</h3>
<pre><code class="language-silicon">\\ distance (Point, Point) -> Int
@fn distance a, b := abs(a.x - b.x) + abs(a.y - b.y)
\\ @extern puts (String) -> Int
puts("hello")</code></pre>
</section>
<section>
<h2>Non-examples</h2>
<div class="grid">
<div class="card">
<h3>Call opener moved to the next line</h3>
<pre><code class="language-silicon">print
("hello")</code></pre>
<p>The callee and call <code>(</code> are not on the same logical line.</p>
</div>
<div class="card">
<h3>Assignment split after <code>:=</code></h3>
<pre><code class="language-silicon">value :=
compute()</code></pre>
<p>No terminator is inserted after <code>:=</code>; the expression is incomplete.</p>
</div>
<div class="card">
<h3>Comma as a continuation</h3>
<pre><code class="language-silicon">pair := make_pair(first,
second)</code></pre>
<p>No terminator is inserted after a comma.</p>
</div>
<div class="card">
<h3>Namespace continuation</h3>
<pre><code class="language-silicon">value := math::
abs(n)</code></pre>
<p>No terminator is inserted after a namespace separator.</p>
</div>
</div>
</section>
<section>
<h2>Parser Implementation Shape</h2>
<p>
ASI should live at parser statement boundaries. A helper like
<code>consumeTerminator()</code> can accept either an explicit semicolon
or a virtual terminator when newline metadata says the current item is
complete and the following token is a valid follow token.
</p>
<pre><code>parseElement():
if next_is_signature_line():
parseSignatureLine()
consumeSignatureTerminatorIfExtern()
return
parseItem()
consumeTerminator(topLevelFollow)
parseBlockTail():
parseItem()
if next_token_is("}"):
keep_item_as_trailing_block_value()
else:
consumeTerminator(blockItemFollow)</code></pre>
<p>
The lexer can expose newline tokens or attach newline metadata to tokens.
It should not globally rewrite newlines into semicolons before the parser
knows whether it is at an <code>Element</code> or <code>BlockTail</code>
follow position.
</p>
</section>
<section>
<h2>Test Matrix</h2>
<table>
<thead>
<tr>
<th>Case</th>
<th>Expected result</th>
</tr>
</thead>
<tbody>
<tr>
<td>Two top-level definitions separated by newline</td>
<td>Parses as two items</td>
</tr>
<tr>
<td>EOF after a complete top-level item</td>
<td>Parses without a trailing semicolon</td>
</tr>
<tr>
<td>Block with final expression before <code>}</code></td>
<td>No virtual semicolon before <code>}</code>; expression is block value</td>
</tr>
<tr>
<td>Block with multiple newline-separated bindings</td>
<td>Virtual semicolons between item starts</td>
</tr>
<tr>
<td>Newline after binary operator, comma, <code>:=</code>, <code>=</code>, <code>::</code>, or open delimiter</td>
<td>No virtual semicolon</td>
</tr>
<tr>
<td>Newline before binary operator, comma, <code>.</code>, <code>::</code>, <code>)</code>, or <code>]</code></td>
<td>No virtual semicolon</td>
</tr>
<tr>
<td>Callee newline before call <code>(</code></td>
<td>Parse error</td>
</tr>
<tr>
<td>Normal <code>\\</code> signature followed by definition</td>
<td>Signature attaches to definition</td>
</tr>
<tr>
<td>Bodyless <code>\\ @extern ...</code> at line end</td>
<td>Signature line terminates without a following body</td>
</tr>
</tbody>
</table>
</section>
</main>
</body>
</html>