Add copyright notice to acinclude.m4.
[bpt/guile.git] / module / scripts / doc-snarf.scm
CommitLineData
28c31342
TTN
1;;; doc-snarf --- Extract documentation from source files
2
6e7d5622 3;; Copyright (C) 2001, 2006 Free Software Foundation, Inc.
28c31342
TTN
4;;
5;; This program is free software; you can redistribute it and/or
83ba2d37
NJ
6;; modify it under the terms of the GNU Lesser General Public License
7;; as published by the Free Software Foundation; either version 3, or
28c31342
TTN
8;; (at your option) any later version.
9;;
10;; This program is distributed in the hope that it will be useful,
11;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
83ba2d37 13;; Lesser General Public License for more details.
28c31342 14;;
83ba2d37
NJ
15;; You should have received a copy of the GNU Lesser General Public
16;; License along with this software; see the file COPYING.LESSER. If
17;; not, write to the Free Software Foundation, Inc., 51 Franklin
18;; Street, Fifth Floor, Boston, MA 02110-1301 USA
28c31342 19
61897afe
TTN
20;;; Author: Martin Grabmueller
21
28c31342
TTN
22;;; Commentary:
23
24;; Usage: doc-snarf FILE
25;;
26;; This program reads in a Scheme source file and extracts docstrings
27;; in the format specified below. Additionally, a procedure protoype
28;; is infered from the procedure definition line starting with
29;; (define... ).
30;;
31;; Currently, two output modi are implemented: texinfo and plaintext.
32;; Default is plaintext, texinfo can be switched on with the
33;; `--texinfo, -t' command line option.
34;;
35;; Format: A docstring can span multiple lines and a docstring line
36;; begins with `;; ' (two semicoli and a space). A docstring is ended
37;; by either a line beginning with (define ...) or one or more lines
38;; beginning with `;;-' (two semicoli and a dash). These lines are
39;; called `options' and begin with a keyword, followed by a colon and
40;; a string.
41;;
42;; Additionally, "standard internal docstrings" (for Scheme source) are
43;; recognized and output as "options". The output formatting is likely
44;; to change in the future.
45;;
46;; Example:
47
48;; This procedure foos, or bars, depending on the argument @var{braz}.
49;;-Author: Martin Grabmueller
50(define (foo/bar braz)
51 (if braz 'foo 'bar))
52
53;;; Which results in the following docstring if texinfo output is
54;;; enabled:
55#!
56\ffoo/bar
57@deffn procedure foo/bar braz
58This procedure foos, or bars, depending on the argument @var{braz}.
59@c Author: Martin Grabmueller
60@end deffn
61!#
62
63;;; Or in this if plaintext output is used:
64#!
65Procedure: foo/bar braz
66This procedure foos, or bars, depending on the argument @var{braz}.
67;; Author: Martin Grabmueller
68^L
69!#
70
71;; TODO: Convert option lines to alist.
72;; More parameterization.
73;; ../libguile/guile-doc-snarf emulation
74
28c31342
TTN
75(define doc-snarf-version "0.0.2") ; please update before publishing!
76
77;;; Code:
78
79(define-module (scripts doc-snarf)
80 :use-module (ice-9 getopt-long)
81 :use-module (ice-9 regex)
82 :use-module (ice-9 string-fun)
83 :use-module (ice-9 rdelim)
84 :export (doc-snarf))
85
86(define command-synopsis
87 '((version (single-char #\v) (value #f))
88 (help (single-char #\h) (value #f))
89 (output (single-char #\o) (value #t))
90 (texinfo (single-char #\t) (value #f))
91 (lang (single-char #\l) (value #t))))
92
93;; Display version information and exit.
94;;-ttn-mod: use var
95(define (display-version)
96 (display "doc-snarf ") (display doc-snarf-version) (newline))
97
98;; Display the usage help message and exit.
99;;-ttn-mod: change option "source" to "lang"
100(define (display-help)
101 (display "Usage: doc-snarf [options...] inputfile\n")
102 (display " --help, -h Show this usage information\n")
103 (display " --version, -v Show version information\n")
104 (display
105 " --output=FILE, -o Specify output file [default=stdout]\n")
106 (display " --texinfo, -t Format output as texinfo\n")
107 (display " --lang=[c,scheme], -l Specify the input language\n"))
108
109;; Main program.
110;;-ttn-mod: canonicalize lang
111(define (doc-snarf . args)
112 (let ((options (getopt-long (cons "doc-snarf" args) command-synopsis)))
113 (let ((help-wanted (option-ref options 'help #f))
114 (version-wanted (option-ref options 'version #f))
115 (texinfo-wanted (option-ref options 'texinfo #f))
116 (lang (string->symbol
117 (string-downcase (option-ref options 'lang "scheme")))))
118 (cond
119 (version-wanted (display-version))
120 (help-wanted (display-help))
121 (else
122 (let ((input (option-ref options '() #f))
123 (output (option-ref options 'output #f)))
124 (if
125 ;; Bonard B. Timmons III says `(pair? input)' alone is sufficient.
126 ;; (and input (pair? input))
127 (pair? input)
128 (snarf-file (car input) output texinfo-wanted lang)
129 (display-help))))))))
130
131(define main doc-snarf)
132
133;; Supported languages and their parameters. Each element has form:
134;; (LANG DOC-START DOC-END DOC-PREFIX OPT-PREFIX SIG-START STD-INT-DOC?)
135;; LANG is a symbol, STD-INT-DOC? is a boolean indicating whether or not
136;; LANG supports "standard internal docstring" (a string after the formals),
137;; everything else is a string specifying a regexp.
138;;-ttn-mod: new var
139(define supported-languages
140 '((c
141 "^/\\*(.*)"
142 "^ \\*/"
143 "^ \\* (.*)"
144 "^ \\*-(.*)"
145 "NOTHING AT THIS TIME!!!"
146 #f
147 )
148 (scheme
149 "^;; (.*)"
150 "^;;\\."
151 "^;; (.*)"
152 "^;;-(.*)"
153 "^\\(define"
154 #t
155 )))
156
157;; Get @var{lang}'s @var{parameter}. Both args are symbols.
158;;-ttn-mod: new proc
159(define (lang-parm lang parm)
160 (list-ref (assq-ref supported-languages lang)
161 (case parm
162 ((docstring-start) 0)
163 ((docstring-end) 1)
164 ((docstring-prefix) 2)
165 ((option-prefix) 3)
166 ((signature-start) 4)
167 ((std-int-doc?) 5))))
168
169;; Snarf all docstrings from the file @var{input} and write them to
170;; file @var{output}. Use texinfo format for the output if
171;; @var{texinfo?} is true.
172;;-ttn-mod: don't use string comparison, consult table instead
173(define (snarf-file input output texinfo? lang)
174 (or (memq lang (map car supported-languages))
175 (error "doc-snarf: input language must be c or scheme."))
176 (write-output (snarf input lang) output
177 (if texinfo? format-texinfo format-plain)))
178
179;; fixme: this comment is required to trigger standard internal
180;; docstring snarfing... ideally, it wouldn't be necessary.
181;;-ttn-mod: new proc, from snarf-docs (aren't these names fun?)
182(define (find-std-int-doc line input-port)
183 "Unread @var{line} from @var{input-port}, then read in the entire form and
184return the standard internal docstring if found. Return #f if not."
185 (unread-string line input-port) ; ugh
186 (let ((form (read input-port)))
187 (cond ((and (list? form) ; (define (PROC ARGS) "DOC" ...)
188 (< 3 (length form))
189 (eq? 'define (car form))
190 (pair? (cadr form))
191 (symbol? (caadr form))
192 (string? (caddr form)))
193 (caddr form))
194 ((and (list? form) ; (define VAR (lambda ARGS "DOC" ...))
195 (< 2 (length form))
196 (eq? 'define (car form))
197 (symbol? (cadr form))
198 (list? (caddr form))
199 (< 3 (length (caddr form)))
200 (eq? 'lambda (car (caddr form)))
201 (string? (caddr (caddr form))))
202 (caddr (caddr form)))
203 (else #f))))
204
205;; Split @var{string} into lines, adding @var{prefix} to each.
206;;-ttn-mod: new proc
207(define (split-prefixed string prefix)
208 (separate-fields-discarding-char
209 #\newline string
210 (lambda lines
211 (map (lambda (line)
212 (string-append prefix line))
213 lines))))
214
215;; snarf input-file output-file
216;; Extract docstrings from the input file @var{input}, presumed
217;; to be written in language @var{lang}.
218;;-Author: Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
219;;-Created: 2001-02-17
220;;-ttn-mod: regluarize lang parm lookup, add "std int doc" snarfing (2 places)
221(define (snarf input-file lang)
222 (let* ((i-p (open-input-file input-file))
223 (parm-regexp (lambda (parm) (make-regexp (lang-parm lang parm))))
224 (docstring-start (parm-regexp 'docstring-start))
225 (docstring-end (parm-regexp 'docstring-end))
226 (docstring-prefix (parm-regexp 'docstring-prefix))
227 (option-prefix (parm-regexp 'option-prefix))
228 (signature-start (parm-regexp 'signature-start))
229 (augmented-options
230 (lambda (line i-p options)
231 (let ((int-doc (and (lang-parm lang 'std-int-doc?)
232 (let ((d (find-std-int-doc line i-p)))
233 (and d (split-prefixed d "internal: "))))))
234 (if int-doc
235 (append (reverse int-doc) options)
236 options)))))
237
238 (let lp ((line (read-line i-p)) (state 'neutral) (doc-strings '())
239 (options '()) (entries '()) (lno 0))
240 (cond
241 ((eof-object? line)
242 (close-input-port i-p)
243 (reverse entries))
244
245 ;; State 'neutral: we're currently not within a docstring or
246 ;; option section
247 ((eq? state 'neutral)
248 (let ((m (regexp-exec docstring-start line)))
249 (if m
250 (lp (read-line i-p) 'doc-string
251 (list (match:substring m 1)) '() entries (+ lno 1))
252 (lp (read-line i-p) state '() '() entries (+ lno 1)))))
253
254 ;; State 'doc-string: we have started reading a docstring and
255 ;; are waiting for more, for options or for a define.
256 ((eq? state 'doc-string)
257 (let ((m0 (regexp-exec docstring-prefix line))
258 (m1 (regexp-exec option-prefix line))
259 (m2 (regexp-exec signature-start line))
260 (m3 (regexp-exec docstring-end line)))
261 (cond
262 (m0
263 (lp (read-line i-p) 'doc-string
264 (cons (match:substring m0 1) doc-strings) '() entries
265 (+ lno 1)))
266 (m1
267 (lp (read-line i-p) 'options
268 doc-strings (cons (match:substring m1 1) options) entries
269 (+ lno 1)))
270 (m2
271 (let ((options (augmented-options line i-p options))) ; ttn-mod
272 (lp (read-line i-p) 'neutral '() '()
273 (cons (parse-entry doc-strings options line input-file lno)
274 entries)
275 (+ lno 1))))
276 (m3
277 (lp (read-line i-p) 'neutral '() '()
278 (cons (parse-entry doc-strings options #f input-file lno)
279 entries)
280 (+ lno 1)))
281 (else
282 (lp (read-line i-p) 'neutral '() '() entries (+ lno 1))))))
283
284 ;; State 'options: We're waiting for more options or for a
285 ;; define.
286 ((eq? state 'options)
287 (let ((m1 (regexp-exec option-prefix line))
288 (m2 (regexp-exec signature-start line))
289 (m3 (regexp-exec docstring-end line)))
290 (cond
291 (m1
292 (lp (read-line i-p) 'options
293 doc-strings (cons (match:substring m1 1) options) entries
294 (+ lno 1)))
295 (m2
296 (let ((options (augmented-options line i-p options))) ; ttn-mod
297 (lp (read-line i-p) 'neutral '() '()
298 (cons (parse-entry doc-strings options line input-file lno)
299 entries)
300 (+ lno 1))))
301 (m3
302 (lp (read-line i-p) 'neutral '() '()
303 (cons (parse-entry doc-strings options #f input-file lno)
304 entries)
305 (+ lno 1)))
306 (else
307 (lp (read-line i-p) 'neutral '() '() entries (+ lno 1))))))))))
308
309(define (make-entry symbol signature docstrings options filename line)
310 (vector 'entry symbol signature docstrings options filename line))
311(define (entry-symbol e)
312 (vector-ref e 1))
313(define (entry-signature e)
314 (vector-ref e 2))
315(define (entry-docstrings e)
316 (vector-ref e 3))
317(define (entry-options e)
318 (vector-ref e 4))
319(define (entry-filename e)
320 (vector-ref e 5))
321(define (entry-line e)
322 "This docstring will not be snarfed, unfortunately..."
323 (vector-ref e 6))
324
325;; Create a docstring entry from the docstring line list
326;; @var{doc-strings}, the option line list @var{options} and the
327;; define line @var{def-line}
328(define (parse-entry docstrings options def-line filename line-no)
329; (write-line docstrings)
330 (cond
331 (def-line
332 (make-entry (get-symbol def-line)
333 (make-prototype def-line) (reverse docstrings)
334 (reverse options) filename
335 (+ (- line-no (length docstrings) (length options)) 1)))
336 ((> (length docstrings) 0)
337 (make-entry (string->symbol (car (reverse docstrings)))
338 (car (reverse docstrings))
339 (cdr (reverse docstrings))
340 (reverse options) filename
341 (+ (- line-no (length docstrings) (length options)) 1)))
342 (else
343 (make-entry 'foo "" (reverse docstrings) (reverse options) filename
344 (+ (- line-no (length docstrings) (length options)) 1)))))
345
346;; Create a string which is a procedure prototype. The necessary
347;; information for constructing the prototype is taken from the line
348;; @var{def-line}, which is a line starting with @code{(define...}.
349(define (make-prototype def-line)
350 (call-with-input-string
351 def-line
352 (lambda (s-p)
353 (let* ((paren (read-char s-p))
354 (keyword (read s-p))
355 (tmp (read s-p)))
356 (cond
357 ((pair? tmp)
358 (join-symbols tmp))
359 ((symbol? tmp)
360 (symbol->string tmp))
361 (else
362 ""))))))
363
364(define (get-symbol def-line)
365 (call-with-input-string
366 def-line
367 (lambda (s-p)
368 (let* ((paren (read-char s-p))
369 (keyword (read s-p))
370 (tmp (read s-p)))
371 (cond
372 ((pair? tmp)
373 (car tmp))
374 ((symbol? tmp)
375 tmp)
376 (else
377 'foo))))))
378
379;; Append the symbols in the string list @var{s}, separated with a
380;; space character.
381(define (join-symbols s)
382 (cond ((null? s)
383 "")
384 ((symbol? s)
385 (string-append ". " (symbol->string s)))
386 ((null? (cdr s))
387 (symbol->string (car s)))
388 (else
389 (string-append (symbol->string (car s)) " " (join-symbols (cdr s))))))
390
391;; Write @var{entries} to @var{output-file} using @var{writer}.
392;; @var{writer} is a proc that takes one entry.
393;; If @var{output-file} is #f, write to stdout.
394;;-ttn-mod: new proc
395(define (write-output entries output-file writer)
396 (with-output-to-port (cond (output-file (open-output-file output-file))
397 (else (current-output-port)))
398 (lambda () (for-each writer entries))))
399
400;; Write an @var{entry} using texinfo format.
401;;-ttn-mod: renamed from `texinfo-output', distilled
402(define (format-texinfo entry)
403 (display "\n\f")
404 (display (entry-symbol entry))
405 (newline)
406 (display "@c snarfed from ")
407 (display (entry-filename entry))
408 (display ":")
409 (display (entry-line entry))
410 (newline)
411 (display "@deffn procedure ")
412 (display (entry-signature entry))
413 (newline)
414 (for-each (lambda (s) (write-line s))
415 (entry-docstrings entry))
416 (for-each (lambda (s) (display "@c ") (write-line s))
417 (entry-options entry))
418 (write-line "@end deffn"))
419
420;; Write an @var{entry} using plain format.
421;;-ttn-mod: renamed from `texinfo-output', distilled
422(define (format-plain entry)
423 (display "Procedure: ")
424 (display (entry-signature entry))
425 (newline)
426 (for-each (lambda (s) (write-line s))
427 (entry-docstrings entry))
428 (for-each (lambda (s) (display ";; ") (write-line s))
429 (entry-options entry))
430 (display "Snarfed from ")
431 (display (entry-filename entry))
432 (display ":")
433 (display (entry-line entry))
434 (newline)
435 (write-line "\f"))
436
437;;; doc-snarf ends here