-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsrfi-110-1.6.html
5627 lines (5222 loc) · 189 KB
/
srfi-110-1.6.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 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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: -->
<!-- Using just this:
li li {list-style-type:lower-alpha}
would make the TOC look funny.
-->
<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>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>Revised: <a href="http://srfi.schemers.org/srfi-110/srfi-110-1.6.html">2013/03/28</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>,
and
<a href="#reference-implementation">reference implementation</a>.
It also includes a longer
<a href="#design-rationale">design rationale</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 fibfast(n) ; Typical function notation
if {n < 2} ; Indentation, infix {...}
n ; Single expr = no new list
fibup n 2 1 0 ; Simple function calls
</pre>
</td>
<td>
<pre>
(define (fibfast n)
(if (< n 2)
n
(fibup n 2 1 0)))
</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="#tutorial">Tutorial</a>
<ul>
<li><a href="#tutorial-basics">Basics</a></li>
<li><a href="#tutorial-clarifications">Clarifications</a></li>
<li><a href="#tutorial-advanced-features">Advanced features</a></li>
</ul></li>
<li><a href="#specification">Specification</a>
<ul>
<li><a href="#bnf-conventions">BNF conventions</a></li>
<li><a href="#bnf-supporting">Supporting BNF definitions</a></li>
<li><a href="#bnf-key">Key BNF productions</a></li>
<li><a href="#other-requirements">Other requirements</a></li>
<li><a href="#related-tools">Related tools</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>
<li><a href="#ease-of-implementation">Ease of implementation</a></li>
<li><a href="#simplicity">Simplicity</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>
It has often been said that the parentheses
“just disappear” after experience.
But as
<a href="http://www.gregslepak.com/on-lisps-readability">bhurt notes</a>,
“I’m always somewhat amazed by the claim that the
parens ‘just disappear’, as if this is a good thing.
Bugs live in the difference between the code in your head
and the code on the screen - and having the parens
in the wrong place causes bugs.
And autoindenting isn’t the answer -
I don’t want the indenting to follow the parens,
I want the parens to follow the indenting.
The indenting I can see, and can see is correct.”
</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="tutorial">Tutorial</a></h1>
<p>
This section provides a basic tutorial on sweet-expressions,
which should also make the
<a href="#specification">specification</a>
below easier to understand.
</p>
<h2><a name="tutorial-basics">Basics</a></h2>
<p>“<dfn>Sweet-expressions</dfn>”
(aka “<dfn>t-expressions</dfn>”)
build on neoteric-expressions (aka n-expressions) as defined in
<a href="http://srfi.schemers.org/srfi-105/">SRFI-105</a>.
N-expressions are a simple extension of traditional s-expression notation,
so valid n-expressions include
numbers, strings surrounded by double-quotes, and lists
(whitespace-separated n-expressions surrounded by parentheses).
N-expressions add support for
infix expressions surrounded by curly braces
(aka curly-infix lists), so
<samp>{a + b}</samp> maps to <samp>(+ a b)</samp>.
There is no precedence, but you can use braces in braces, e.g.,
<samp>{a + b + {x * y}}</samp>
maps to
<samp>(+ a b (* x y))</samp>.
A curly-infix list with two elements
<samp>{e1 e2}</samp> maps to
<samp>(e1 e2)</samp>, and a one-element curly-infix list
<samp>{e}</samp> maps to just that element <samp>e</samp>.
In addition,
<samp>f(...)</samp> maps to <samp>(f ...)</samp>, and
<samp>f{...}</samp> with non-whitespace content
maps to <samp>(f {...})</samp>.
For more details, see
<a href="http://srfi.schemers.org/srfi-105/">SRFI-105</a>.
</p>
<p>
Sweet-expressions add the ability to
deduce parentheses from indentation,
as well as adding a few additional abbreviations
to make the notation especially convenient to use.
Let’s first examine how parentheses are deduced.
</p>
<p>
In sweet-expressions, a line with content consists of one or more
n-expressions, separated by one or more spaces or tabs.
If a line is indented more than the previous line, that line is
a <i>child</i> line, and the previous line is a <i>parent</i> to that child.
Later lines with the same indentation as the child are also children
of that parent, until there is an intervening line with the parent’s
indentation or less.
A line with only one n-expression, and no child lines, represents itself.
Otherwise, the line represents a list; each n-expression on the line
is an element of the list, and each of its child lines represents
an element of the list (in order).
Here are some examples:
</p>
<blockquote>
<table border="1" cellpadding="4">
<tr><th>sweet-expression</th><th>s-expression</th></tr>
<tr>
<td>
<pre>
a b (c1 c2) d(1 2) e
</pre>
</td>
<td>
<pre>
(a b (c1 c2) (d 1 2) e)
</pre>
</td>
<tr>
<!-- Here's another trivial Scheme program, a greatest common divisor function straight from Carl A. Gunter's "Semantics of Programming Languages" page 2: -->
<td align="left" valign="top">
<pre>
define gcd(x y)
if {y = 0}
x
gcd y rem(x y)
</pre>
</td>
<td align="left" valign="top">
<pre>
(define (gcd x y)
(if (= y 0)
x
(gcd y (rem x y))))
</pre>
</td>
</tr>
</table>
</blockquote>
<p>
An empty line (a line containing only 0+ spaces and tabs)
ends an expression once one has begun.
This makes sweet-expressions easy to use interactively; just press
“Enter Enter” to end an expression.
Empty lines are ignored before an expression begins.
</p>
<p>
You can indent using one or more of the indent characters,
which are space, tab, and the exclamation point (!).
Lines after the first line
need to be <dfn>consistently indented</dfn>, that is,
the current line’s indentation, when compared to the
previous line’s, are equal or one is a prefix of the other.
Indentation is ignored inside ( ), [ ], and { },
whether they are prefixed or not.
This makes sweet-expressions backwards-compatible with traditional
s-expressions, and also provides an easy way to disable indentation
processing if it’s inconvenient.
</p>
<h2><a name="tutorial-clarifications">Clarifications</a></h2>
<p>
Here are a few clarifications:
</p>
<ol>
<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>An expression that starts
indented enables “initial-indent” mode,
a special compatibility mode where indentation is completely ignored.
Instead, that line is considered a sequence of
whitespace-separated neoteric-expressions that are each read separately.</li>
<li>Scheme’s datum comments (<code>#;</code><i>datum</i>)
comment out the next neoteric expression,
not the next sweet expression.
Datum comments ignore intervening whitespace,
including spaces, tabs, and newlines.</li>
<li>Special comments are non-whitespace sequences
other than ;-comments that do not return a datum; they include
datum comments (<code>#;</code><i>datum</i>),
block comments (<code>#|</code>...<code>|#</code>),
and directives
(such as <code>#!fold-case</code>, <code>#!no-fold-case</code>,
<code>#!sweet</code>, and <code>#!curly-infix</code>).
If a special comment
begins immediately after the indent,
the indentation of the special comment is used.
</li>
<li>
A single delimited period (.) still sets the value of the cdr field of a pair.
</li>
</ol>
<p>
Here are some examples:
</p>
<blockquote>
<table border="1" cellpadding="4">
<tr>
<th align="center">Sweet-expressions (t-expressions)</th>
<th align="center">s-expressions</th>
</tr>
<tr>
<td align="left" valign="top">
<pre>
aaa bbb
; Comment indent ignored
cc dd
</pre>
</td>
<td align="left" valign="top">
<pre>
(aaa bbb
(cc dd))
</pre>
</td>
</tr>
<tr>
<td align="left" valign="top">
<pre>
ff ; Demo special comments
#| qq |# t1 t2
t3 t4
t5 #| xyz |# t6
t7 #;t8(q) t9
</pre>
</td>
<td align="left" valign="top">
<pre>
(ff
(t1 t2)
(t3 t4
(t5 t6)
(t7 t9)))
</pre>
</td>
</tr>
<tr>
<td align="left" valign="top">
<pre>
f ; Demo improper lists
a . b
</pre>
</td>
<td align="left" valign="top">
<pre>
(f
(a . b))
</pre>
</td>
</tr>
<tr>
<td align="left" valign="top">
<pre>
f ; Demo vertical improper lists
x y
.
z
</pre>
</td>
<td align="left" valign="top">
<pre>
(f
(x y)
.
z)
</pre>
</td>
</tr>
</table>
</blockquote>
<h2><a name="tutorial-advanced-features">Advanced features</a></h2>
<p>
As mentioned above,
sweet-expressions add a few additional abbreviations,
which are sometimes called
sweet-expression “advanced features”.
These involve the marker “<code>\\</code>”
(called GROUP and SPLIT),
the marker “<code>$</code>” (SUBLIST),
leading traditional abbreviations
(quote, comma, backquote, or comma-at) with following whitespace,
and the pair of
markers “<*” and “*>”
(which surround a <i>collecting list</i>).
We will examine each in turn, including some examples.
</p>
<p>
The marker <code>\\</code> is specially interpreted.
If any n-expressions precede it on the line, it is called SPLIT, and
it is interpreted
the start of a new line at the current line’s indentation.
Otherwise it is called GROUP,
and it represents no symbol at all located at that indentation.
GROUP is useful for representing lists of lists.
Examples:
</p>
<blockquote>
<table border="1" cellpadding="4">
<tr>
<th align="center">Sweet-expressions (t-expressions)</th>
<th align="center">s-expressions</th>
</tr>
<tr>
<td align="left" valign="top">
<pre>
let ; Demo GROUP
\\
var1 cos(a)
var2 sin(a)
body...
</pre>
</td>
<td align="left" valign="top">
<pre>
(let
(
(var1 (cos a))
(var2 (sin a)))
body...)
</pre>
</td>
</tr>
<tr>
<td align="left" valign="top">
<pre>
myfunction ; Demo SPLIT
x: \\ xpos
y: \\ ypos
</pre>
</td>
<td align="left" valign="top">
<pre>
(myfunction
x: xpos
y: ypos)
</pre>
</td>
</tr>
<tr>
<td align="left" valign="top">
<pre>
sin 0 \\ cos 0
</pre>
</td>
<td align="left" valign="top">
<pre>
(sin 0)
(cos 0)
</pre>
</td>
</tr>
</table>
</blockquote>
<p>
The marker <code>$</code> is called SUBLIST.
If <code>$</code> is preceded by any n-expressions on the line,
the right-hand-side (including any child lines)
is the last element of the list described on the left-hand side
of just the line with the <code>$</code>.
(This is basically the same meaning it has in Haskell.)
If there’s no left-hand-side,
the right-hand-side is put in a list.
Examples:
</p>
<blockquote>
<table border="1" cellpadding="4">
<tr>
<th align="center">Sweet-expressions (t-expressions)</th>
<th align="center">s-expressions</th>
</tr>
<tr>
<td align="left" valign="top">
<pre>
a b $ c d
</pre>
</td>
<td align="left" valign="top">
<pre>
(a b (c d))
</pre>
</td>
</tr>
<tr>
<td align="left" valign="top">
<pre>
a b $ c d e f $ g
</pre>
</td>
<td align="left" valign="top">
<pre>
(a b (c d e f g))
</pre>
</td>
</tr>
<tr>
<td align="left" valign="top">
<pre>
let
$ x sqrt(a)
{2 * x}
</pre>
</td>
<td align="left" valign="top">
<pre>
(let
((x (sqrt a)))
(* 2 x))
</pre>
</td>
</tr>
<tr>
<td align="left" valign="top">
<pre>
run $ grep |-v| "xx.*zz" <(oldfile) >(newfile)
</pre>
</td>
<td align="left" valign="top">
<pre>
(run (grep |-v| "xx.*zz" (< oldfile) (> newfile)))
</pre>
</td>
</tr>
</table>
</blockquote>
<p>
A leading traditional abbreviation
(quote, comma, backquote, or comma-at)
located after indentation, and followed by space, tab, or the end-of-line,
is interpreted as that operator applied to
the entire sweet-expression that follows.
Examples:
</p>
<blockquote>
<table border="1" cellpadding="4">
<tr>
<th align="center">Sweet-expressions (t-expressions)</th>
<th align="center">s-expressions</th>
</tr>
<tr>
<td align="left" valign="top">
<pre>
' a b ; Demo abbreviations
' c d e \\ 'f g h
</pre>
</td>
<td align="left" valign="top">
<pre>
(quote (a b
(quote (c d e)) ((quote f) g h)))
</pre>
</td>
</tr>
</table>
</blockquote>
<p>
The markers “<*” and “*>” surround a
<i>collecting list</i>.
This represents a list, but unlike (...), indentation processing
continues to work, the indentation level is temporary restarted
at the left edge, and empty lines do not a collecting list.
These are useful for long lists (e.g., in modules), because they
shorten indentation and allow empty lines.
They are also useful in let expressions with short variable expressions.
The <a href="#examples">examples</a> section has longer examples;
here are short examples:
</p>
<blockquote>
<table border="1" cellpadding="4">
<tr>
<th align="center">Sweet-expressions (t-expressions)</th>
<th align="center">s-expressions</th>
</tr>
<tr>
<td align="left" valign="top">
<pre>
let <* x sqrt(a) *>
! g {x + 1} {x - 1}
</pre>
</td>
<td>
<pre>
(let ((x (sqrt a)))
(g (+ x 1) (- x 1)))
</pre>
</td>
</tr>
<tr>
<td align="left" valign="top">
<pre>
let <* x $ {oldx - 1} \\ y $ {oldy - 1} *>
! {{x * x} + {y * y}}
</pre>
</td>
<td>
<pre>
(let ((x (- oldx 1)) (y (- oldy 1)))
(+ (* x x) (* y y)))
</pre>
</td>
</tr>
</table>
</blockquote>
<p>
Your Scheme implementation may already provide these capabilities
if you simply enter the <code>#!sweet</code> directive.
If your preferred Scheme implementation does not yet support
sweet-expressions, encourage them to add it, or
consider trying out the
<a href="http://readable.sourceforge.net/">Readable Lisp S-expressions Project</a>
sample implementation and tools.
</p>
<p>
The next two sections provide a more rigorous
<a href="#specification">specification</a>
and many more <a href="#examples">examples</a>.
</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>
<p>
The following subsections provide the
<a href="#bnf-conventions">Backus-Naur Form (BNF) conventions</a>,
<a href="#bnf-supporting">supporting definitions in BNF format</a>
<a href="#bnf-key">key productions in BNF format</a>,
<a href="#other-requirements">other requirements</a>,
and specifications about
<a href="#related-tools">related tools</a>.
</p>
<h2 id="bnf-conventions">Backus-Naur Form (BNF) conventions</h2>
<p>
A sweet-expression (aka t-expression)
is an external representations of a Scheme object,
which may include other Scheme objects.
A sweet-expression reader converts a sweet-expression
into the objects the sweet-expression represents.
</p>
<p>
The BNF rules below define the syntax of sweet-expressions,
in particular, the production <code>t_expr</code> defines one sweet-expression.
A sweet-expression reader <em>MUST</em> implement the productions
below unless otherwise noted.
The BNF is an LL(1) grammar, written using
<a href="http://www.antlr.org/">ANTLR version 3</a>.
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>.
The non-terminal <code>same</code> emphasizes where there is a new
line with unchanged indentation; it matches nothing.
The non-terminal <code>error</code> emphasizes certain sequences that
are not defined by this BNF
(an implementation <em>MAY</em> implement an extension, but if it does
not, it <em>SHOULD</em> report an error).
The sequence <code>/*empty*/</code> identifies an empty branch.
</p>
<p>
A sweet-expression reader <em>MUST</em>
support three modes: indentation processing, enclosed, and initial indent.
A sweet-expression reader <em>MUST</em> start
in indentation processing mode before it begins to read a sweet-expression.
The reader temporarily switches to enclosed mode when it is reading inside
any unescaped pairs of parentheses, brackets, or curly braces.
</p>
<p>
To further understand sweet-expressions, we first need these definitions:
</p>
<ul>
<li>eol character: A character that is a carriage return
(U+000D) or linefeed (U+000A)
(eol is short for “end of line”).</li>
<li>eol sequence: A carriage return (U+000D)
optionally followed by linefeed (U+000A), or a linefeed (U+000A)
This is EOL_SEQUENCE in the BNF.
Implementations <em>MAY</em>
also recognize other end of line characters or sequences.</li>
<li>line: A sequence of non-eol characters terminated by an eol sequence.
A sweet-expression reader <em>MAY</em> also support a final line without
an eol sequence.
The line terminator is not considered part of the line contents.</li>
<li>hspace: A character that is a space (U+0020) or tab (U+0009)
(short for “horizontal space”).</li>
<li>indent character: A character that is a space (U+0020), tab (U+0009), or
exclamation point “!” (U+0021).</li>
<li>indentation: The set of all 0+ indent characters
at the beginning of a line.</li>
</ul>
<p>
Indentation is not directly represented in the following syntax definition.
Instead, a sweet-expression reader <em>MUST</em> act as if it preprocessed
its input as follows.
First, when the sweet-expression reader begins,
a stack called the “indentation stack”
is initialized to contain exactly one value, the empty string
(<tt>""</tt>).
Then, when a line is read and the current mode is not enclosed,
the line indentation is removed and possibly replaced by other generated
symbols as follows (where “top” is the
value of the top of the indentation stack):
</p>
<ol>
<li>If an end-of-line sequence immediately follows the indentation
and the indentation length is nonzero:
<ol>
<li type="a">If the indentation contains “!”, it is an error.</li>
<li type="a">If indentation does not contain “!”,
it is considered a line with no characters
(thus indentation is empty)
and the rest of these rules are applied.</li>
</ol>
</li>
<li>If top is the empty string and the indentation length is nonzero:
<ol>
<li type="a">
If the indentation does not contain “!”, then
symbol <code>initial_indent_no_bang</code>
is generated and the reader changes to initial indent mode.
When an end-of-line sequence is reached
the mode changes back to indentation processing.
</li>
<li type="a">If the indentation does contain “!”, then
symbol <code>initial_indent_with_bang</code> is generated.</li>
</ol>
</li>
<li>If top is not the empty string, and
“;” immediately follows the indentation, the line is skipped
— with the indentation completely ignored —
and the following line is examined instead.</li>
<li>If the indentation is equal to top, no extra symbol is generated
(this is called “same”).</li>
<li>If the indentation is longer than top, and top is a prefix of indentation,
the indentation is pushed onto the indentation stack
and the symbol <code>indent</code> is generated.</li>