File: srfi-30.html

package info (click to toggle)
drscheme 1%3A352-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 71,608 kB
  • ctags: 55,284
  • sloc: ansic: 278,966; cpp: 63,318; sh: 32,265; lisp: 14,530; asm: 7,327; makefile: 4,846; pascal: 4,363; perl: 2,920; java: 1,632; yacc: 755; lex: 258; sed: 93; xml: 12
file content (210 lines) | stat: -rw-r--r-- 8,189 bytes parent folder | download | duplicates (11)
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>SRFI 30: Nested Multi-line Comments</title>
  </head>

  <body>

<H1>Title</H1>

SRFI 30: Nested Multi-line Comments

<H1>Author</H1>

Martin Gasbichler
    
<H1>Status</H1>

This SRFI is currently in ``final'' 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>.  It will
remain in draft until 2002-04-06, or as amended.  to provide input on
this SRFI, please
<a href="mailto:srfi-30@srfi.schemers.org">
mail to <code>srfi-30@srfi.schemers.org</code></a>.
See <a href="http://srfi.schemers.org/srfi-list-subscribe.html">
instructions here</a> to subscribe to the list.  You can access
previous messages via
<a href="http://srfi.schemers.org/srfi-30/mail-archive/maillist.html">
the archive of the mailing list</a>.
You can access
post-finalization messages via
<a href="http://srfi.schemers.org/srfi-30/post-mail-archive/maillist.html">
the archive of the mailing list</a>.

<UL>
      <LI>Draft: 2002/04/12-2002/06/10</LI>
      <li>Revised: 2002/06/05</li>
      <li>Final: 2002/08/07</li>
</UL>

    <h1>Related SRFIs</h1>
    
    <p><a href="http://srfi.schemers.org/srfi-22/">SRFI 22</a> defines
    a multi line comment that may only appear at the beginning of a
    file.</p>

    <p><a href="http://srfi.schemers.org/srfi-10/">SRFI 10</a>
    proposes the notation

    <PRE> "#" &lt;discriminator&gt; &lt;other-char&gt;*</PRE> for further
    SRFIs that introduce values which may be read and written.</p>

    <H1>Abstract</H1>

    <p>This SRFI extends R5RS by possibly nested, multi-line
    comments. Multi-line comments start with <code>#|</code> and end
    with <code>|#</code>.</p>
    
    <H1>Rationale</H1>
    
    <p>Multi-line comments are common to many programming languages. They
    provide a convenient mean for adding blocks of text inside a program and
    support development by fading out incomplete code or alternative
    implementations.</p>
    
    <p>The syntax <code>#|</code> ... <code>|#</code> was chosen
    because it is already widely used (Chez Scheme, Chicken, Gambit-C,
    Kawa, MIT Scheme, MzScheme and RScheme) and since it fits smoothly
    with R5RS. Nested comments are an important feature for
    incrementally commenting out code.</p>
    
    <H1>Specification</H1>

    <p>This SRFI extends the specification of comments -- <a
    href="http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-5.html#%_sec_2.2">R5RS
    section 2.2</a> -- as follows:</p>

    <a name="spec"></a>
    <p>The sequence <code>#|</code> indicates the start of a
    multi-line comment. The multi-line comment continues until
    <code>|#</code> appears. If the closing <code>|#</code> is
    omitted, an error is signaled. Multi-line comments are visible to
    Scheme as a single whitespace. Multi-line comments may be nested:
    Every <code>#|</code> within the comment starts a new multi-line
    comment, which has to be terminated by a matching
    <code>|#</code>.</p>
    
    <p>This SRFI furthermore extends the production for
    <code>&lt;comment&gt;</code> in the specification of lexical
    structure -- <a
    href="http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-10.html#%_sec_7.1.1">R5RS
    section 7.1.1</a> -- to:
    </p>
    <pre>
    &lt;comment&gt; ---&gt; ; &lt;all subsequent characters up to a line break&gt;
     	    |         #| &lt;comment-text&gt; (&lt;comment&gt; &lt;comment-text&gt;)* |#

    &lt;comment-text&gt; ---&gt; &lt;character sequence not containing #| or |#&gt;

    </pre>


    <h1><code>&lt;delimiter&gt;</code> and <code>#</code></h1> 

    <p>The SRFI does not extend the specification of
    <code>&lt;delimiter&gt;</code> from <a
    href="http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-10.html#%_sec_7.1.1">R5RS
    section 7.1.1</a>. It is therefore required to separate tokens
    which require implicit termination (identifiers, numbers,
    characters, and dot) from multi-line comments by a
    <code>&lt;delimiter&gt;</code> from R5RS.</p>

    <p><em>Rationale</em> An extension of the
    <code>&lt;delimiter&gt;</code> to <code>#|</code> would be
    incompatible with existing implementations which allow
    <code>#</code> as legal character within identifiers.</p>

    <H1>Implementation</H1>
    
    <p>The following procedure <code>skip-comment</code> deletes a
    leading multi-line comment from the current input port. Its
    optional argument specifies whether the caller has already removed
    leading characters of the comment from the current input port. The
    symbol <code>start</code> means that the caller has removed
    nothing, <code>read-sharp</code> means that it has removed a sharp
    and finally <code>read-bar</code> indicates that the caller has
    removed <code>#|</code>. </p>

    <pre>
(define (skip-comment! . maybe-start-state)
  (let lp ((state (if (null? maybe-start-state) 'start (car maybe-start-state))) 
	   (nested-level 0))
    (define (next-char)
      (let ((c (read-char)))
	(if (eof-object? c)
	    (error "EOF inside block comment -- #| missing a closing |#")
	    c)))

    (case state
      ((start) (case (next-char)
		 ((#\|) (lp 'read-bar nested-level))
		 ((#\#) (lp 'read-sharp nested-level))
		 (else (lp 'start nested-level))))
      ((read-bar) (case (next-char)
		    ((#\#) (if (> nested-level 1)
			       (lp 'start (- nested-level 1))))
		    (else (lp 'start nested-level))))
      ((read-sharp) (case (next-char)
		      ((#\|) (lp 'start (+ nested-level 1)))
		      (else (lp 'start nested-level)))))))
    </pre>

    <p>A <a href="http://srfi.schemers.org/srfi-22/">SRFI 22</a> script to remove nested multi-line comments is
    available at 
      
      <a
	 href="http://srfi.schemers.org/srfi-30/remove-srfi30-comments-script.scm">http://srfi.schemers.org/srfi-30/remove-srfi30-comments-script.scm</a>.
      
      The script will read a Scheme program containing nested
      multi-line comments from standard input and emit the same
      programs with the comments removed to standard output. The
      script mimics the Scheme 48 reader and uses the
      <code>error</code> procedure from <a
      href="http://srfi.schemers.org/srfi-23/">SRFI 23</a>.</p>

     <p>A number of Scheme implemenations already support this SRFI:
     Chez Scheme, Chicken, Gambit-C, Kawa, MIT Scheme, MzScheme and
     RScheme. Bigloo supports multi-line comments via
     <code>#!</code>/<code>!#</code>. Among the major Scheme
     implementations, Scheme 48 and Guile do not support this SRFI
     yet.</p>

    <H1>Copyright</H1>
    <p>Copyright (C) Martin Gasbichler (2002). All Rights Reserved.</p>

    <p>
    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use, copy,
    modify, merge, publish, distribute, sublicense, and/or sell copies
    of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    </p>
    <p>
    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.
    </p>
    <p>
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.
    </p>

      
    <hr>
    <address>Editor: <a
    href="mailto:srfi-editors@srfi.schemers.org">Mike Sperber</a></address>
<!-- Created: Tue Sep 29 19:20:08 EDT 1998 -->
<!-- hhmts start -->
Last modified: Sun Sep  1 17:14:55 MST 2002
<!-- hhmts end -->
  </body>
</html>