-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsrfi-110-1.5.html
5073 lines (4717 loc) · 172 KB
/
srfi-110-1.5.html
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
<!--
SPDX-FileCopyrightText: 2012 - 2013 Alan Manuel K. Gloria
SPDX-FileCopyrightText: 2012 - 2013 David A. Wheeler
SPDX-License-Identifier: MIT
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>SRFI 110: Sweet-expressions (t-expressions)</title>
<meta content="text/html; charset=us-ascii" http-equiv="content-type">
<!-- This commented out text is for the brittle SRFI tools -->
<!--
</head>
<body>
<H1>Title</H1>
Sweet-expressions (t-expressions)
<H1>Author</H1>
David A. Wheeler, Alan Manuel K. Gloria
<H1>Status</H1>
This SRFI is currently in ``draft'' status.
-->
<meta name="description" content="This defines sweet-expressions (t-expressions) for Scheme, building on neoteric-expressions (n-expressions) and curly-infix-expressions (c-expressions). This defines an approach to making Scheme more 'readable' by adding syntactically-relevant indentation, as well as supporting infix and functions whose names precede the opening parenthesis.">
<meta name="keywords" content="sweet, sweet-expression, sweet-expressions, t-expression, Scheme, Lisp, Common Lisp, neoteric-expression, n-expression, readable, notation, s-expression, s-expr, M-expressions, SRFI, implementation, David Wheeler, David A. Wheeler, Alan Manuel Gloria, Alan Manuel K. Gloria">
<meta name="generator" content="vim">
<!-- Copy CSS style of SRFI-64; credits to Per Bothner. -->
<!-- Note: "style" isn't in HTML 3.2, but SRFI-64 set a precedent
for allowing this: -->
<style type="text/css">
div.title h1 { font-size: small; color: blue }
div.title { font-size: xx-large; color: blue; font-weight: bold }
h1 { font-size: x-large; color: blue }
h2 { font-size: large; color: blue }
h3 { color: blue ; font-style: italic }
/* So var inside pre gets same font as var in paragraphs. */
var { font-family: monospace; }
</style>
</head>
<body>
<div class="title">
<h1><a name="title">Title</a></h1>
<p>Sweet-expressions (t-expressions)</p>
</div>
<!-- Some old browsers have problem with empty names. Work around here: -->
<h1><a name="authors">Authors</a><a name="author"> </a></h1>
<p><a href="http://www.dwheeler.com">David A. Wheeler</a></p>
<p>Alan Manuel K. Gloria</p>
<h1 id="status">Status</h1>
<p>
This SRFI is currently in “draft” status. To see an explanation of
each status that a SRFI can hold, see <a
href="http://srfi.schemers.org/srfi-process.html">here</a>.
To provide input on this SRFI, please
<a href="mailto:srfi minus 110 at srfi dot schemers dot org">mail to
<code><srfi minus 110 at srfi dot schemers dot org></code></a>. See
<a href="../../srfi-list-subscribe.html">instructions here</a> to
subscribe to the list. You can access previous messages via
<a href="mail-archive/maillist.html">the archive of the mailing list</a>.
</p>
<ul>
<li>
<li>Received: <a href="http://srfi.schemers.org/srfi-110/srfi-110-1.1.html">2013/03/05</a></li>
<li>Revised: <a href="http://srfi.schemers.org/srfi-110/srfi-110-1.2.html">2013/03/07</a></li>
<li>Revised: <a href="http://srfi.schemers.org/srfi-110/srfi-110-1.3.html">2013/03/10</a></li>
<li>Revised: <a href="http://srfi.schemers.org/srfi-110/srfi-110-1.4.html">2013/03/14</a></li>
<li>Revised: <a href="http://srfi.schemers.org/srfi-110/srfi-110-1.5.html">2013/03/22</a></li>
<li>Draft: 2013/03/06-2013/05/06</li>
</ul>
<p>
This SRFI contains all the required sections, including
an <a href="#abstract">abstract</a>,
<a href="#rationale">rationale</a>,
<a href="#specification">specification</a>,
<a href="#design-rationale">design rationale</a>,
and
<a href="#reference-implementation">reference implementation</a>.
</p>
<h1><a name="abstract">Abstract</a></h1>
<p>
This SRFI describes a new extended syntax for Scheme, called sweet-expressions
(t-expressions), that has the same descriptive power as s-expressions
but is designed to be easier for humans to read.
The sweet-expression syntax enables the use of syntactically-meaningful
indentation to group expressions (similar to Python),
and it builds on the infix and traditional function notation defined in
<a href="http://srfi.schemers.org/srfi-105/">SRFI-105 (curly-infix-expressions)</a>.
Unlike nearly all past efforts to improve s-expression readability,
sweet-expressions are
general (the notation is independent from any underlying semantic)
and homoiconic (the underlying data structure is clear from the syntax).
Sweet-expressions can be used both for program and data input.
This notation was developed by the
“<a href="http://readable.sourceforge.net/">Readable Lisp S-expressions Project</a>”.
</p>
<p>
Sweet-expressions can be considered simply
a set of some additional abbreviations.
Sweet-expressions and traditionally formatted s-expressions
can be freely mixed, allowing the developer
to easily transition and maximize readability when laying out code.
For example, a sweet-expression reader would accept
<i>either</i> the sweet-expression or s-expression format shown below.
Here is an example:
</p>
<table border="1" cellpadding="4">
<tr><th>sweet-expression</th><th>s-expression</th></tr>
<tr>
<td>
<pre>
define factorial(n)
if {n <= 1}
1
{n * factorial{n - 1}}
</pre>
</td>
<td>
<pre>
(define (factorial n)
(if (<= n 1)
1
(* n (factorial (- n 1)))))
</pre>
</td>
</tr>
</table>
<!-- SRFI-97 has a TOC; we think a TOC would be helpful here too. -->
<h1><a name="toc">Table of Contents</a></h1>
<ul>
<li><a href="#related-srfis">Related SRFIs</a></li>
<li><a href="#rationale">Rationale</a></li>
<li><a href="#specification">Specification</a>
<ul>
<li><a href="#basic-specification">Basic specification</a></li>
<li><a href="#related-tools">Related tools</a></li>
<li><a href="#bnf">Backus-Naur Form (BNF)</a></li>
</ul></li>
<li><a href="#examples">Examples</a></li>
<li><a href="#design-rationale">Design Rationale</a>
<ul>
<li><a href="#basic">Basic approach</a>
<ul>
<li><a href="#general-and-homoiconic">General and homoiconic formats</a></li>
<li><a href="#cant-improve">Is it impossible to improve on s-expression notation?</a></li>
<li><a href="#why-indent">Why should indentation be syntactically relevant?</a></li>
<li><a href="#srfi-49">What is the relationship between sweet-expressions and SRFI-49 (I-expressions)?</a></li>
<li><a href="#separate-105">Why are sweet-expression separate from curly-infix and neoteric-expressions as defined in SRFI-105?</a></li>
<li><a href="#writing-out-results">Writing out results</a></li>
<li><a href="#backwards-compatibility">Backwards compatibility (well-formatted s-expressions)</a></li>
</ul>
</li>
<li><a href="#whitespace-indentation-comment">Whitespace, indentation, and comment handling</a>
<ul>
<li><a href="#blank-lines">Blank lines</a></li>
<li><a href="#trailing-hspace">Trailing horizontal spaces are ignored</a></li>
<li><a href="#indentation-characters">Indentation characters (! as indent)</a></li>
<li><a href="#disabling-indentation-processing-with-paired-characters">Disabling indentation processing with paired characters</a></li>
<li><a href="#disabling-indentation-processing-with-an-initial-indent">Disabling indentation processing with an initial indent</a></li>
<li><a href="#block-comment-indent-significant">Why are the indentations of block comments and datum comments significant?</a></li>
<li><a href="#eol">End-of-line (EOL) handling</a></li>
<li><a href="#eof">End-of-file (EOF) handling</a></li>
<li><a href="#semicolon">Special semicolon values for an unsweetener</a></li>
</ul>
</li>
<li><a href="#specific-constructs">Other specific sweet-expression constructs</a>
<ul>
<li><a href="#sweet">The #!sweet marker</a></li>
<li><a href="#grouping-and-splitting">Grouping and splitting (\\)</a></li>
<li><a href="#initial-group-mean-nothing">Why does initial \\ mean nothing if there are datums afterwards on the same line?</a></li>
<li><a href="#traditional-abbreviations">Traditional abbreviations</a></li>
<li><a href="#sublist">Sublist ($)</a></li>
<li><a href="#single-item-sublist">Why is <code>a $ b</code> equivalent to <code>(a b)</code> rather than <code>(a (b))</code>?</a></li>
<li><a href="#collecting-lists">Collecting lists (<* ... *>)</a></li>
<li><a href="#reserved">Reserved marker ($$$)</a></li>
</ul>
</li>
<li><a href="#comparisons">Comparisons to other notations</a>
<ul>
<li><a href="#m-expressions">Comparison to M-expressions</a></li>
<li><a href="#honu">Comparison to Honu</a></li>
<li><a href="#q2">Comparison to Q2</a></li>
<li><a href="#p4p">Comparison to P4P</a></li>
<li><a href="#z">Comparison to Z</a></li>
<li><a href="#genyris">Comparison to Genyris</a></li>
<li><a href="#arne">Comparison to “Arne formulation”</a></li>
<li><a href="#closing-sublist-unmatched-dedent">Closing SUBLIST by unmatched dedent (“Beni Formulation of SUBLIST”)</a></li>
<li><a href="#closing-ending-sublist-results">Variation: Closing end-of-line SUBLIST by unmatched dedent (“Beni-Lite”)</a></li>
</ul>
</li>
<li><a href="#experience">Experience using and implementing sweet-expressions</a></li>
<li><a href="#style">Style guide</a></li>
</ul></li>
<li><a href="#reference-implementation">Reference implementation</a></li>
<li><a href="#references">References</a></li>
<li><a href="#acknowledgments">Acknowledgments</a></li>
<li><a href="#copyright">Copyright</a></li>
</ul>
<h1><a name="related-srfis">Related SRFIs</a></h1>
<p>
<a href="http://srfi.schemers.org/srfi-49/">SRFI-49
(Indentation-sensitive syntax)</a> (superceded by this SRFI),
<a href="http://srfi.schemers.org/srfi-105/">SRFI-105
(Curly-infix-expressions)</a> (incorporated by this SRFI),
<a href="http://srfi.schemers.org/srfi-22/">SRFI-22
(Running Scheme Scripts on Unix)</a> (some interactions),
<a href="http://srfi.schemers.org/srfi-30/">SRFI-30
(Nested Multi-line comments)</a> (some interactions),
and
<a href="http://srfi.schemers.org/srfi-62/">SRFI-62
(S-expression comments)</a> (some interactions)
</p>
<h1><a name="rationale">Rationale</a></h1>
<p>
Many software developers find Lisp s-expression notation inconvenient and
unpleasant to read.
In fact, the large number of parentheses required by traditional
Lisp s-expression syntax is the butt
of many jokes in the software development community.
The <a href="http://www.catb.org/jargon/html/L/LISP.html">Jargon File</a>
says that Lisp is “mythically from
‘Lots of Irritating Superfluous Parentheses’”.
<a href="http://fortunes.cat-v.org/kernelnewbies/">Linus Torvalds</a>
commented about some parentheses-rich C code,
“don’t ask me about the extraneous parenthesis. I bet some
LISP programmer felt alone and decided to make it a bit more homey.”
<a href="http://www.linuxjournal.com/article/2070">
Larry Wall, the creator of Perl</a>, says that,
“Lisp has all the visual appeal of oatmeal
with fingernail clippings mixed in.
(Other than that, it’s quite a nice language.)”.
<a href="http://shriram.github.com/p4p/">Shriram Krishnamurthi</a> says,
“Racket [(a Scheme implementation)] has an excellent language design,
a great implementation, a superb programming environment, and terrific tools.
Mainstream adoption will, however, always be curtailed by the syntax.
Racket could benefit from [reducing]
the layers of parenthetical adipose that [needlessly] engird it.”
</p>
<p>
Even <a href="http://paulgraham.com/popular.html">Lisp advocate
Paul Graham says</a>, regarding Lisp syntax,
“A more serious problem [in Lisp] is the diffuseness of prefix notation...
We can get rid of (or make optional) a lot of parentheses by making
indentation significant.
That’s how programmers read code anyway: when indentation says
one thing and delimiters say another, we go by the indentation.
Treating indentation as significant would eliminate this
common source of bugs as well as making programs shorter.
Sometimes infix syntax is easier to read. This is especially true for
math expressions. I’ve used Lisp my whole programming life and I still
don’t find prefix math expressions natural...
I don’t think we should be religiously opposed to introducing syntax
into Lisp, as long as it translates in a well-understood
way into underlying s-expressions.
There is already a good deal of syntax in Lisp.
It’s not necessarily bad to introduce more,
as long as no one is forced to use it.”
</p>
<p>
Many new syntaxes have been invented for various Lisp dialects,
including <a href="#m-expressions">McCarthy’s
original M-expression notation for Lisp</a>.
However, nearly all of these past notations fail to be
general (i.e., the notation is independent of an underlying semantic) or
homoiconic (i.e., the underlying data structure is clear from the syntax).
We believe a Lisp-based notation <i>needs</i> to be general and homoiconic.
For example, Lisp-based languages can trivially create new semantic constructs
(e.g., with macros) or be used to process other constructs;
a Lisp notation that is not general will always lag behind and lack
the “full” power of s-expressions.
</p>
<p>
Recently, using indentation as the sole grouping construct of a
language has become popular (in particular
with the advent of the Python programming language).
This approach solves the problem of indentation going out of sync
with the native grouping construct of the language, and exploits
the fact that most programmers indent larger programs and expect
reasonable indentation by others.
Unfortunately, the Python syntax uses special constructs
for the various semantic
constructs of the language, and the syntaxes of file input and
interactive input differ slightly.
</p>
<p>
<a href="http://srfi.schemers.org/srfi-49/">SRFI-49</a>
defined a promising indentation-sensitive syntax for Scheme.
Unfortunately,
<a href="#srfi-49">SRFI-49 had some awkward usage issues</a>,
and by itself it lacks
support for infix notation (e.g., <samp>{a + b}</samp>)
and prefix formats (e.g., <samp>f(x)</samp>).
Sweet-expressions build on and refine SRFI-49 by addressing these issues.
Real programs by different authors have been written using sweet-expressions,
demonstrating that sweet-expressions are a practical notation.
See the <a href="#design-rationale">design rationale</a> for a detailed
discussion on how and why it is designed this way.
</p>
<p>
Sweet-expressions <i>are</i> general and homoiconic,
and thus can be easily used with other constructs
such as quasiquoting and macros.
In short, if a capability can be accessed using s-expressions, then they
can be accessed using sweet-expressions.
Unlike Python, the notation is exactly the same in a REPL and a file,
so people can switch between a REPL and files without issues.
Fundamentally, sweet-expressions define a few additional abbreviations
for s-expressions, in much the same way that
<samp>'x</samp> is an abbreviation for <samp>(quote x)</samp>.
</p>
<h1><a name="specification">Specification</a></h1>
<p>
The key words
“<em>MUST</em>”,
“<em>MUST NOT</em>”,
“<em>REQUIRED</em>”,
“<em>SHALL</em>”,
“<em>SHALL NOT</em>”,
“<em>SHOULD</em>”,
“<em>SHOULD NOT</em>”,
“<em>RECOMMENDED</em>”,
“<em>MAY</em>”,
and “<em>OPTIONAL</em>” in this
document are to be interpreted as described in
<a href="http://www.ietf.org/rfc/rfc2119.txt">RFC 2119</a>.
</p>
<h3 id="basic-specification">Basic specification</h3>
<p>“<dfn>Sweet-expressions</dfn>” (aka “<dfn>t-expressions</dfn>”) deduce parentheses from indentation.
A sweet-expression reader <em>MUST</em> interpret its input
as follows when indentation processing is active:
</p>
<ol>
<li>An indented line is a parameter of its parent.</li>
<li>Later terms on a line are parameters of the first term.</li>
<li>A line with exactly one term, and no child lines, is simply that term; multiple terms are wrapped into a list.</li>
<li>An empty line ends the expression; empty lines before expressions are ignored.</li>
<li>Terms are neoteric-expressions as defined in
<a href="http://srfi.schemers.org/srfi-105/">SRFI-105</a>.
Thus <samp>{a + b}</samp> maps to <samp>(+ a b)</samp>,
<samp>f(...)</samp> maps to <samp>(f ...)</samp>, and
<samp>f{...}</samp> with non-empty content
maps to <samp>(f {...})</samp>.</li>
<li>When reading begins, indentation processing is active, but indentation processing is disabled inside ( ), [ ], and { }, whether they are prefixed or not
(inside they’re a sequence of
whitespace-separated neoteric-expressions).</li>
</ol>
<p>
A sweet-expression reader <em>MUST</em> apply these rule clarifications:
</p>
<ol>
<li>You can indent using one or more of the indent characters,
which are space, tab, and exclamation point (!).
Except for lines with initial indents and the first line of a stream,
every line <em>MUST</em> be <dfn>consistently indented</dfn>
when indentation processing is active.
A line is consistently indented if
the indent character sequence of that line, when compared to the
indent character sequence of the preceding line,
is equal or one is a prefix of the other.
</li>
<li>An unescaped “;” not in a string (still) introduces comments
that end at the end of the line.</li>
<li>Lines with only a ;-comment (preceded by 0 or more indent characters)
are completely ignored - even their indentation (if any) is irrelevant.</li>
<li>A line with only indentation is an empty line.</li>
<li>An expression that starts indented enables “indented-compatibility” mode,
where indentation is completely ignored.
Instead, a sequence of white-space separated neoteric-expressions is read
until the first end of line.</li>
<li>Scheme’s <code>#;</code> datum comment comments out the next neoteric expression,
not the next sweet expression.
Datum comments ignore intervening whitespace, including spaces, tabs, and newlines.</li>
<li>Block comments (<samp>#|</samp>...<samp>|#</samp>) are removed.</li>
<li>For all <code>#</code>-based comments
(i.e. datum comments <code>#;</code>,
block comments <code>#|</code>...<code>|#</code>,
the markers <code>#!fold-case</code> <code>#!no-fold-case</code> <code>#!sweet</code> <code>#!curly-infix</code>,
and anything else an implementation can read
but does not return a datum),
if they
begin immediately after the indent (if any),
the indentation at the beginning of the comment is used.
</li>
<li>
A sweet-expression reader <em>MUST</em> accept, as an
an end-of-line (EOL) sequence, either
a newline <i>or</i> a carriage return followed by newline.
A sweet-expression reader <em>SHOULD</em> also accept
a carriage return without a following newline as an end-of-line sequence.
</li>
<li>
Portable non-empty files <em>MUST</em> end with an unescaped end-of-line
sequence before the end-of-file.
A sweet-expression reader <em>MAY</em> treat non-empty files that do
not end in an unescaped
end-of-line as though an end-of-line sequence had been added.
</li>
</ol>
<p>
A sweet-expression reader <em>MUST</em> implement these
sweet-expression “advanced features”:
<ol>
<li>
The marker <code>\\</code> is specially interpreted.
If any terms precede it on the line, it is called SPLIT,
and it <em>MUST</em> be interpreted
as if it started a new line, at the current line’s indentation.
If no terms precede <code>\\</code> on the line,
it is called GROUP,
and it represents no symbol at all,
located at that indentation (GROUP is useful for lists of lists).</li>
<li>
The marker <code>$</code> (aka SUBLIST) <em>MUST</em> restart list processing.
If <code>$</code> is preceded by any terms on the line,
the right-hand-side (including its sub-blocks)
is the last parameter of the left-hand side
(of just that line).
If there’s no left-hand-side,
the right-hand-side is put in a list.
</li>
<li>
A leading traditional abbreviation
(quote, comma, backquote, or comma-at),
located after indentation,
and followed by space or tab,
<em>MUST</em> be interpreted as that operator applied to the entire sweet-expression that follows.
</li>
<li>
The markers “<*” and “*>” surround a
<i>collecting list</i>, and <em>MUST</em> accept
a list of 0 or more un-indented sweet-expressions.
</li>
<li>
The marker “$$$” <em>MUST</em> be reserved for future use.
</li>
</ol>
<p>
The markers for the advanced sweet-expression features <em>MUST</em>
only be accepted as such when indentation processing is active.
A character sequence <em>MUST NOT</em> be considered one of those
markers (or as the dot operator) if it
does not begin with exactly the marker or operator’s first character.
For example, <samp><tt>{$}</tt></samp>
<em>MUST NOT</em> be interpreted as the SUBLIST marker; instead, it
<em>MUST</em> be interpreted as the symbol <samp>$</samp>.
</p>
<p>A <dfn>sweet-expression reader</dfn> is a datum reader
that can correctly read and map sweet-expressions as defined above
(including the advanced sweet-expression features).
An implementation of this SRFI <em>MUST</em> accept
the directive <code>#!sweet</code> followed by a whitespace character
in its standard datum readers (e.g., <code>read</code> and, if applicable,
the default implementation REPL).
This directive <em>MUST</em> be consumed and considered whitespace.
After reading this directive, the reader <em>MUST</em> accept
sweet-expressions in subsequent datums read from the same port,
until some other conflicting directive is given.
Once a sweet-expression reader is enabled,
the <code>#!sweet</code> directive <em>MUST</em> be accepted and ignored.
</p>
<p>
A <code>#!curly-infix</code>
<em>SHOULD</em> cause the current port to switch to SRFI-105
semantics (e.g., sweet-expression indentation processing is disabled).
A <code>#!no-sweet</code>
<em>SHOULD</em> cause the current port to
disable sweet-expression indentation processing and
<em>MAY</em> also disable curly-infix expression processing.
</p>
<p>
A sweet-expression reader <em>SHOULD</em> support
<a href="http://srfi.schemers.org/srfi-30/">SRFI-30
(Nested Multi-line comments)</a> (<tt>#|</tt> ... <tt>|#</tt>)
and
<a href="http://srfi.schemers.org/srfi-62/">SRFI-62
(S-expression comments)</a> (<tt>#;</tt><var>datum</var>).
A sweet-expression reader <em>SHOULD</em> support
<a href="http://srfi.schemers.org/srfi-22/">SRFI-22
(Running Scheme Scripts on Unix)</a> (where #!+space ignores
to the end of the line),
<tt>#!</tt> followed by a letter as a directive
(such as <tt>#!fold-case</tt>) that is delimited by a whitespace character
or end-of-file,
and the formats
<tt>#!/</tt> ... <tt>!#</tt> and
<tt>#!.</tt> ... <tt>!#</tt> as multi-line non-nesting comments.
</p>
<p>
A sweet-expression reader <em>MAY</em> implement datum labels
with syntax <code>#<i>number</i>=<i>datum</i></code>.
If the first character after the equal sign is not whitespace,
such a reader <em>SHOULD</em> read it as a neoteric-expression.
If the first character after the equal sign is whitespace,
a datum reader <em>MAY</em> reject it.
A reader <em>MAY</em> also accept a datum label
that is an initial expression of a <i>head</i> production (see the BNF below),
with a trailing space or tab,
as labelling the rest of the sweet-expression.
</p>
<p>
A <i>well-formatted</i> s-expression is an expression interpreted
identically by both traditional s-expressions and by sweet-expressions.
A well-formatted file is a file interpreted identically
by both traditional s-expressions and sweet-expressions.
(In practice,
<a href="#backwards-compatibility">most s-expressions used in
real programs are well-formatted</a>.)
</p>
<p>
Implementations of this SRFI <em>MAY</em>
implement sweet-expressions in their datum readers by default,
even when the <code>#!sweet</code> directive is not (yet) received.
Portable applications <em>SHOULD</em> include the <code>#!sweet</code>
directive before using sweet-expressions, typically near the top of a file.
Portable applications <em>SHOULD NOT</em>
use this directive as the very first characters of a file
because they might be misinterpreted on some platforms
as an executable script header; preceding this directive with a newline
avoids this problem.
</p>
<p>
Implementations <em>MAY</em> provide the procedures
<var>sweet-read</var> as a sweet-expression reader and/or
<var>neoteric-read</var> as a neoteric-expression reader.
If provided, these procedures
<em>SHOULD</em> support an optional port parameter.
</p>
<p>
Implementations <em>SHOULD</em> enable a sweet-expression reader when
reading a file whose name ends in “.sscm” (Sweet Scheme).
Application authors <em>SHOULD</em> use the
filename extension “.sscm”
when writing portable Scheme programs using sweet-expressions.
</p>
<p>Note that, by definition, this SRFI modifies lexical syntax.</p>
<h2><a name="related-tools">Related tools</a></h2>
<p>
Implementations <em>MAY</em> provide a tool,
called an “unsweetener”,
that reads sweet-expressions and writes out s-expressions.
An unsweetener <em>SHOULD</em> specially treat
lines that begin with a semicolon
when they are not currently reading an expression (e.g., no expression has
been read, or the last expression read has been completed with a blank line).
Such a tool <em>SHOULD</em>
(when outside an expression) copy exactly
any line beginning with semicolon followed by a whitespace or semicolon.
Such a tool <em>SHOULD</em>
(when outside an expression) also
copy lines beginning with “;#” or “;!”
without the leading semicolon,
and copy lines beginning with “;_”
without either of those first two characters.
Application authors <em>SHOULD</em>
follow a semicolon in the first column with a whitespace character
or semicolon if they mean for it to be a comment.
</p>
<p>
A program editor <em>MAY</em> usefully highlight
blank lines (as they separate expressions) and lines beginning at the
left column (as these start new expressions).
We <em>RECOMMEND</em> that program editors highlight
expressions whose first line is indented,
to reduce the risk of their accidental use.
</p>
<h2><a name="bnf">Backus-Naur Form (BNF)</a></h2>
<p>
The following BNF rules define sweet-expressions;
a sweet-expression reader <em>MUST</em> implement the productions
below unless otherwise noted.
The BNF is intended to capture the specification above;
in case of (unintentional)
conflict, the specification text above governs.
The BNF is an LL(1) grammar, written using
<a href="http://www.antlr.org/">ANTLR version 3</a>.
</p>
<p>
In the summarized BNF below,
the action rules inside {...} are in Scheme syntax.
You can also separately view the
<a href="sweet.g">full ANTLR BNF definition of sweet-expressions
with Java action rules</a>, along with a support Java class
<a href="Pair.java">Pair.java</a>.
</p>
<p>
As with SRFI-49, we model input as being preprocessed and having
INDENT and DEDENT tokens inserted to represent the addition or
removal of indentation; a single end-of-line may translate to a single
EOL followed by multiple DEDENT tokens.
(The indent and dedent non-terminals just refer to INDENT and DEDENT
respectively.)
If the indentation is invalid, BADDENT is generated which is not
matched by the grammar.
</p>
<p>
A sweet-expression reader <em>MUST</em>
support three modes: indentation processing,
enclosed (when inside pairs of parentheses, brackets, or curly braces,
recursively), and initial indent.
On initialization a sweet-expression reader <em>MUST</em> be
in indentation processing mode.
An initial indent <em>MUST</em> enter indentation processing mode,
which <em>MUST</em> end on an end-of-line sequence.
The markers <tt>\\</tt>, <tt>$</tt>, <tt><*</tt>, <tt>*></tt>,
and the abbreviations followed by horizontal space
<em>MUST</em> only have their
special meaning in indentation processing mode.
</p>
<p>
There are a few special non-terminals that act essentially as comments
and are used to clarify the grammar; each matches an empty sequence:
</p>
<ol>
<li>empty : Identifies an empty branch</li>
<li>same : Emphasizes where neither indent nor dedent has occurred</li>
<li>error : Specifically identifies an error branch.</li>
</ol>
<p>
The error non-terminal makes it clear where a sequence is
not defined by this specification, and thus
recommends where a parser might specifically check for errors.
It also also acts as a check on the grammar itself (to help warn the
BNF developers of unintended interpretation).
Note that errors can occur elsewhere, and an implementation
MAY include an extension where an error is noted in this grammar.
</p>
<p>
The BNF productions below are intentionally written so that they can
be easily implemented using a recursive descent parser that
corresponds to the given rules.
In particular, the rules are given so that it would be easy to implement
a parser that does not consume characters unless
necessary and to not require multi-character unread-char
(this makes it easy to reuse an underlying <var>read</var> procedure).
However, no particular implementation approach is required.
Unlike the SRFI-49 BNF, this BNF makes comment and whitespace
processing explicit, to make comment and whitespace processing requirements
clear.
</p>
<p>
A sweet-expression reader <em>MUST</em> read n-expression tails greedily.
That is, if a potential tail begins with an opening parenthesis,
bracket, or brace, it <em>MUST</em> be considered a tail; otherwise,
it <em>MUST NOT</em> be considered a tail.
</p>
<p>
The BNF depends on this utility function:
</p>
<pre>
; If x is a 1-element list, return (car x), else return x
(define (monify x)
(cond
((not (pair? x)) x)
((null? (cdr x)) (car x))
(#t x)))
</pre>
<p>
Here is the actual BNF:
</p>
<!-- Between start and end pre, insert the results of ./to-srfi < sweet.g -->
<pre>
SPACE : ' ';
TAB : '\t';
PERIOD : '.';
// Special markers, which only have meaning outside (), [], {}.
GROUP_SPLIT : {(indent_processing)}? => '\\' '\\'; // GROUP/split symbol.
SUBLIST : {(indent_processing)}? =>'$';
COLLECTING : {(indent_processing)}? => '<*' { restart_indent_level()} ;
// This generates EOL + (any DEDENTs ) + COLLECTING_END, and restores indents:
COLLECTING_END : {(indent_processing)}? => t='*>' {process_collecting_end($t)};
RESERVED_TRIPLE_DOLLAR : {(indent_processing)}? => '$$$'; // Reserved.
// Abbreviations followed by certain whitespace are special:
APOSW : {(indent_processing)}? => '\'' (SPACE | TAB) ;
QUASIQUOTEW : {(indent_processing)}? => '\`' (SPACE | TAB) ;
UNQUOTE_SPLICEW : {(indent_processing)}? => ',@' (SPACE | TAB) ;
UNQUOTEW : {(indent_processing)}? => ',' (SPACE | TAB) ;
// Abbreviations followed by EOL also generate abbrevW:
APOS_EOL : {(indent_processing)}? => '\'' EOL_SEQUENCE
SPECIAL_IGNORED_LINE* i=INDENT_CHARS_PLUS
{emit_type(APOSW); emit_type(EOL);
process_indent($i.text $i)};
QUASIQUOTE_EOL : {(indent_processing)}? => '\`' EOL_SEQUENCE
SPECIAL_IGNORED_LINE* i=INDENT_CHARS_PLUS
{emit_type(QUASIQUOTEW); emit_type(EOL);
process_indent($i.text $i)};
UNQUOTE_SPLICE_EOL: {(indent_processing)}? => ',@' EOL_SEQUENCE
SPECIAL_IGNORED_LINE* i=INDENT_CHARS_PLUS
{emit_type(UNQUOTE_SPLICEW); emit_type(EOL);
process_indent($i.text $i)};
UNQUOTE_EOL : {(indent_processing)}? => ',' EOL_SEQUENCE
SPECIAL_IGNORED_LINE* i=INDENT_CHARS_PLUS
{emit_type(UNQUOTEW); emit_type(EOL);
process_indent($i.text $i)};
// Abbreviations not followed by horizontal space are ordinary:
APOS : '\'';
QUASIQUOTE : '\`';
UNQUOTE_SPLICE : ',@';
UNQUOTE : ',';
// Special end-of-line character definitions.
fragment EOL_CHAR : '\n' | '\r' ;
fragment NOT_EOL_CHAR : (~ (EOL_CHAR));
fragment NOT_EOL_CHARS : NOT_EOL_CHAR*;
fragment EOL_SEQUENCE : ('\r' '\n'? | '\n');
// Comments. LCOMMENT=line comment, scomment=special comment.
LCOMMENT : ';' NOT_EOL_CHARS ; // Line comment - doesn't include EOL
BLOCK_COMMENT : '#|' // This is #| ... #|
(options {greedy=false;} : (BLOCK_COMMENT | .))* '|#' ;
DATUM_COMMENT_START : '#;' ;
// SRFI-105 notes that "implementations could trivially support
// (simultaneously) markers beginning with #! followed by a letter
// (such as the one to identify support for curly-infix-expressions),
// the SRFI-22 #!+space marker as an ignored line, and the
// format #!/ ... !# and #!. ... !# as a multi-line comment."
// We'll implement that approach for maximum flexibility.
SRFI_22_COMMENT : '#! ' NOT_EOL_CHARS ;
SHARP_BANG_FILE : '#!' ('/' | '.') (options {greedy=false;} : .)*
'!#' (SPACE|TAB)* ;
// These match #!fold-case, #!no-fold-case, #!sweet, and #!curly-infix;
// it also matches a lone "#!". The "#!"+space case is handled above,
// in SRFI_22_COMMENT, overriding this one:
SHARP_BANG_MARKER : '#!' (('a'..'z'|'A'..'Z'|'_')
('a'..'z'|'A'..'Z'|'_'|'0'..'9'|'-')*)? (SPACE|TAB)* ;
// IMPORTANT SUPPORTING PARSER DEFINITIONS for the BNF
hspace : SPACE | TAB ; // horizontal space
// Production "abbrevw" is an abbreviation with a following whitespace:
abbrevw returns [Object v]
: APOSW {'quote}
| QUASIQUOTEW {'quasiquote}
| UNQUOTE_SPLICEW {'unquote-splicing}
| UNQUOTEW {'unquote} ;
// Production "abbrev_no_w" is an abbreviation without a following whitespace:
abbrev_no_w returns [Object v]
: APOS {'quote}
| QUASIQUOTE {'quasiquote}
| UNQUOTE_SPLICE {'unquote-splicing}
| UNQUOTE {'unquote};
abbrev_all returns [Object v]
: abbrevw {$abbrevw}
| abbrev_no_w {$abbrev_no_w} ;
// Production "n_expr" is a full neoteric-expression as defined in SRFI-105.
// n_expr does *not* consume any following horizontal space.
// Uses "n_expr_noabbrev", an n-expression with no leading abbreviations:
n_expr returns [Object v]
: abbrev_all n1=n_expr {(list $abbrev_all $n1)}
| n_expr_noabbrev {$n_expr_noabbrev} ;
// Production "n_expr_first" is a neoteric-expression, but leading
// abbreviations cannot have an whitespace afterwards (used by "head"):
n_expr_first returns [Object v]
: abbrev_no_w n1=n_expr_first {(list $abbrev_no_w $n1)}
| n_expr_noabbrev {$n_expr_noabbrev} ;
// Production "scomment" (special comment) defines comments other than ";":
sharp_bang_comments : SRFI_22_COMMENT | SHARP_BANG_FILE | SHARP_BANG_MARKER ;
scomment : BLOCK_COMMENT
| DATUM_COMMENT_START (options : {greedy=true} hspace)* n_expr
| sharp_bang_comments ;
// Production "comment_eol" reads an optional ;-comment (if it exists),
// and then reads the end-of-line (EOL) sequence. EOL processing consumes
// additional comment-only lines (if any) which may be indented.
comment_eol : LCOMMENT? EOL;
// KEY BNF PRODUCTIONS for sweet-expressions:
// Production "collecting_tail" returns a collecting list's contents.
// Precondition: At beginning of line.
// Postcondition: Consumed the matching collecting_end.
// FF = formfeed (\f aka \u000c), VT = vertical tab (\v aka \u000b)
collecting_tail returns [Object v]
: it_expr more=collecting_tail {(cons $it_expr $more)}
| (initial_indent_no_bang | initial_indent_with_bang)?
comment_eol retry1=collecting_tail {$retry1}
| (FF | VT)+ EOL retry2=collecting_tail {$retry2}
| collecting_end {'()} ;
// Process line after ". hspace+" sequence. Does not go past current line.
post_period returns [Object v]
: scomment hspace* rpt=post_period {$rpt} // (scomment hspace*)*
| pn=n_expr hspace* (scomment hspace*)* (n_expr error)? {$pn}
| COLLECTING hspace* pc=collecting_tail hspace*
(scomment hspace*)* (n_expr error)? {$pc}
| /*empty*/ {"."} ;
// Production "head" reads 1+ n-expressions on one line; it will
// return the list of n-expressions on the line. If there is one n-expression
// on the line, it returns a list of exactly one item; this makes it
// easy to append to later (if appropriate). In some cases, we want
// single items to be themselves, not in a list; function monify does this.
// The "head" production never reads beyond the current line
// (except within a block comment), so it doesn't need to keep track
// of indentation, and indentation will NOT change within head.
// The "head" production only directly handles the first n-expression on the
// line, and then calls on "rest" to process the rest (if any); we do this
// because in a few cases it matters if an expression is the first one.
// Callers can depend on "head" and "rest" *not* changing indentation.
// On entry, all indentation/hspace must have already been read.
// On return, it will have consumed all hspace (spaces and tabs).
// Precondition: At beginning of line+indent
// Postcondition: At unconsumed EOL
head returns [Object v]
: PERIOD /* Leading ".": escape following datum like an n-expression. */
(hspace+ pp=post_period {(list $pp)}
| /*empty*/ {(list '.)} )
| COLLECTING hspace* collecting_tail hspace*
(rr=rest {(cons $collecting_tail $rr)}
| /*empty*/ {(list $collecting_tail)} )
| basic=n_expr_first /* Only match n_expr_first */
((hspace+ (br=rest {(cons $basic $br)}
| /*empty*/ {(list $basic)} ))
| /*empty*/ {(list $basic)} ) ;
// Production "rest" production reads the rest of the expressions on a line
// (the "rest of the head"), after the first expression of the line.
// Like head, it consumes any hspace before it returns.
// The "rest" production is written this way so a non-tokenizing
// implementation can read an expression specially. E.G., if it sees a period,
// read the expression directly and then see if it's just a period.
// Precondition: At beginning of non-first expression on line (past hspace)
// Postcondition: At unconsumed EOL
rest returns [Object v]
: PERIOD /* Improper list */
(hspace+ pp=post_period {$pp}
| /*empty*/ {(list '.)})
| scomment hspace* (sr=rest {$sr} | /*empty*/ {'()} )
| COLLECTING hspace* collecting_tail hspace*
(rr=rest {(cons $collecting_tail $rr)}
| /*empty*/ {(list $collecting_tail)} )
| basic=n_expr
((hspace+ (br=rest {(cons $basic $br)}
| /*empty*/ {(list $basic)} ))
| /*empty*/ {(list $basic)} ) ;
// Production "body" handles the sequence of 1+ child lines in an it_expr
// (e.g., after a "head"), each of which is itself an it_expr.
// It returns the list of expressions in the body.
// Note that an it-expr will consume any line comments or hspaces
// before it returns back to the "body" production.
// Since (list x) is simply (cons x '()), this production always does a
// cons of the first it_expr and another body [if it exists] or '() [if not].
body returns [Object v]
: i=it_expr
(same
( {isperiodp($i)}? => f=it_expr dedent
{$f} // Improper list final value
| {! isperiodp($i)}? => nxt=body
{(cons $i $nxt)} )
| dedent {(list $i)} ) ;
// Production "it_expr" (indented sweet-expressions)
// is the main production for sweet-expressions in the usual case.
// Precondition: At beginning of line+indent
// Postcondition: it-expr ended by consuming EOL + examining indent
// Note: This BNF presumes that "*>" generates multiple tokens,
// "EOL DEDENT* COLLECTING_END", and resets the indentation list.
// You can change the BNF below to allow "head /*empty*/", and handle dedents
// by directly comparing values; then "*>" only needs to generate
// COLLECTING_END. But this creates a bunch of ambiguities
// like a 'dangling else', which must all be disambiguated by accepting
// the first or the longer sequence first. Either approach is needed to
// support "*>" as the non-first element so that the "head" can end
// without a literal EOL, e.g., as in "let <* y 5 *>".
it_expr returns [Object v]
: head
(options {greedy=true} : (
GROUP_SPLIT hspace* /* Not initial; interpret as split */
(options {greedy=true} :
// To allow \\ EOL as line-continuation, instead do:
// comment_eol same more=it_expr {(append $head $more)}
comment_eol error
| /*empty*/ {(monify $head)} )
| SUBLIST hspace* /* head SUBLIST ... case */
(sub_i=it_expr {(append $head (list $sub_i))}
| comment_eol error )
| comment_eol // Normal case, handle child lines if any:
(indent children=body {(append $head $children)}
| /*empty*/ {(monify $head)} /* No child lines */ )
// If COLLECTING_END doesn't generate multiple tokens, can do:
// | /*empty*/ {(monify $head)}
))
| (GROUP_SPLIT | scomment) hspace* /* Initial; Interpet as group */
(group_i=it_expr {$group_i} /* Ignore initial GROUP/scomment */
| comment_eol
(indent g_body=body {$g_body} /* Normal GROUP use */
| same ( g_i=it_expr {$g_i} /* Plausible separator */
/* Handle #!sweet EOL EOL t_expr */
| comment_eol restart=t_expr {$restart} )
| dedent error ))
| SUBLIST hspace* /* "$" first on line */
(is_i=it_expr {(list $is_i)}
| comment_eol error )
| abbrevw hspace*
(comment_eol indent ab=body
{(append (list $abbrevw) $ab)}
| ai=it_expr
{(list $abbrevw $ai)} ) ;
// Production "t_expr" is the top-level production for sweet-expressions.
// This production handles special cases, then in the normal case
// drops to the it_expr production.
// Precondition: At beginning of line
// Postcondition: At beginning of line
// The rule for "indent processing disabled on initial top-level hspace"
// is a very simple (and clever) BNF construction by Alan Manuel K. Gloria.
// If there is an indent it simply reads a single n-expression and returns.
// If there is more than one on an initially-indented line, the later
// horizontal space will not have have been read, so this production will
// fire again on the next invocation, doing the right thing.
t_expr returns [Object v]
: comment_eol retry1=t_expr {$retry1}
| (FF | VT)+ EOL retry2=t_expr {$retry2}
| (initial_indent_no_bang | hspace+ )
(n_expr {$n_expr} /* indent processing disabled */
| ((scomment (options {greedy=true} : hspace)*
sretry=t_expr {$sretry}))
| comment_eol retry3=t_expr {$retry3} )
| initial_indent_with_bang error
| EOF {(generate_eof)} /* End of file */
| it_expr {$it_expr} /* Normal case */ ;
</pre>
<h1><a name="examples">Examples</a></h1>
<p>
Here are some examples and their mappings.
Note that a sweet-expression reader would accept either form in all cases,
since a sweet-expression reader is for the most part a
traditional s-expression reader with support for some additional abbreviations.
</p>