2 # aside from this initial boilerplate, this is actually -*- scheme -*- code
3 main
='(module-ref (resolve-module '\''(scripts doc-snarf)) '\'main
')'
4 exec ${GUILE-guile} -l $0 -c "(apply $main (cdr (command-line)))" "$@"
6 ;;; doc-snarf
--- Extract documentation from
source files
8 ;; Copyright
(C
) 2001 Free Software Foundation
, Inc.
10 ;; This program is free software
; you can redistribute it and
/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation
; either version
2, or
13 ;; (at your option
) any later version.
15 ;; This program is distributed
in the hope that it will be useful
,
16 ;; but WITHOUT ANY WARRANTY
; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License
for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this software
; see the
file COPYING. If not
, write to
22 ;; the Free Software Foundation
, Inc.
, 59 Temple Place
, Suite
330,
23 ;; Boston
, MA
02111-1307 USA
25 ;;; Author
: Martin Grabmueller
29 ;; Usage
: doc-snarf FILE
31 ;; This program reads
in a Scheme
source file and extracts docstrings
32 ;; in the format specified below. Additionally
, a procedure protoype
33 ;; is infered from the procedure definition line starting with
36 ;; Currently
, two output modi are implemented
: texinfo and plaintext.
37 ;; Default is plaintext
, texinfo can be switched on with the
38 ;; `--texinfo, -t' command line option.
40 ;; Format: A docstring can span multiple lines and a docstring line
41 ;; begins with `;; ' (two semicoli and a space). A docstring is ended
42 ;; by either a line beginning with (define ...) or one or more lines
43 ;; beginning with `;;-' (two semicoli and a dash
). These lines are
44 ;; called
`options' and begin with a keyword, followed by a colon and
47 ;; Additionally, "standard internal docstrings" (for Scheme source) are
48 ;; recognized and output as "options". The output formatting is likely
49 ;; to change in the future.
53 ;; This procedure foos, or bars, depending on the argument @var{braz}.
54 ;;-Author: Martin Grabmueller
55 (define (foo/bar braz)
58 ;;; Which results in the following docstring if texinfo output is
62 @deffn procedure foo/bar braz
63 This procedure foos, or bars, depending on the argument @var{braz}.
64 @c Author: Martin Grabmueller
68 ;;; Or in this if plaintext output is used:
70 Procedure: foo/bar braz
71 This procedure foos, or bars, depending on the argument @var{braz}.
72 ;; Author: Martin Grabmueller
76 ;; TODO: Convert option lines to alist.
77 ;; More parameterization.
78 ;; ../libguile/guile-doc-snarf emulation
80 (define doc-snarf-version "0.0.2") ; please update before publishing!
84 (define-module (scripts doc-snarf)
85 :use-module (ice-9 getopt-long)
86 :use-module (ice-9 regex)
87 :use-module (ice-9 string-fun)
88 :use-module (ice-9 rdelim)
91 (define command-synopsis
92 '((version (single-char #\v) (value #f))
93 (help (single-char #\h) (value #f))
94 (output (single-char #\o) (value #t))
95 (texinfo (single-char #\t) (value #f))
96 (lang (single-char #\l) (value #t))))
98 ;; Display version information and exit.
100 (define (display-version)
101 (display "doc-snarf ") (display doc-snarf-version) (newline))
103 ;; Display the usage help message and exit.
104 ;;-ttn-mod: change option "source" to "lang"
105 (define (display-help)
106 (display "Usage: doc-snarf [options...] inputfile\n")
107 (display " --help, -h Show this usage information\n")
108 (display " --version, -v Show version information\n")
110 " --output=FILE, -o Specify output file [default=stdout]\n")
111 (display " --texinfo, -t Format output as texinfo\n")
112 (display " --lang=[c,scheme], -l Specify the input language\n"))
115 ;;-ttn-mod: canonicalize lang
116 (define (doc-snarf . args)
117 (let ((options (getopt-long (cons "doc-snarf" args) command-synopsis)))
118 (let ((help-wanted (option-ref options 'help #f))
119 (version-wanted (option-ref options 'version #f))
120 (texinfo-wanted (option-ref options 'texinfo #f))
121 (lang (string->symbol
122 (string-downcase (option-ref options 'lang "scheme")))))
124 (version-wanted (display-version))
125 (help-wanted (display-help))
127 (let ((input (option-ref options '() #f))
128 (output (option-ref options 'output #f)))
130 ;; Bonard B. Timmons III says `(pair? input
)' alone is sufficient.
131 ;; (and input (pair? input))
133 (snarf-file (car input) output texinfo-wanted lang)
134 (display-help))))))))
136 (define main doc-snarf)
138 ;; Supported languages and their parameters. Each element has form:
139 ;; (LANG DOC-START DOC-END DOC-PREFIX OPT-PREFIX SIG-START STD-INT-DOC?)
140 ;; LANG is a symbol, STD-INT-DOC? is a boolean indicating whether or not
141 ;; LANG supports "standard internal docstring" (a string after the formals),
142 ;; everything else is a string specifying a regexp.
144 (define supported-languages
150 "NOTHING AT THIS TIME!!!"
162 ;; Get @var
{lang
}'s @var{parameter}. Both args are symbols.
164 (define (lang-parm lang parm)
165 (list-ref (assq-ref supported-languages lang)
167 ((docstring-start) 0)
169 ((docstring-prefix) 2)
171 ((signature-start) 4)
172 ((std-int-doc?) 5))))
174 ;; Snarf all docstrings from the file @var{input} and write them to
175 ;; file @var{output}. Use texinfo format for the output if
176 ;; @var{texinfo?} is true.
177 ;;-ttn-mod: don't use string comparison
, consult table instead
178 (define
(snarf-file input output texinfo? lang
)
179 (or
(memq lang
(map car supported-languages
))
180 (error
"doc-snarf: input language must be c or scheme."))
181 (write-output
(snarf input lang
) output
182 (if texinfo? format-texinfo format-plain
)))
184 ;; fixme
: this comment is required to trigger standard internal
185 ;; docstring snarfing... ideally
, it wouldn
't be necessary.
186 ;;-ttn-mod: new proc, from snarf-docs (aren't these names fun?
)
187 (define
(find-std-int-doc line input-port
)
188 "Unread @var{line} from @var{input-port}, then read in the entire form and
189 return the standard internal docstring if found. Return #f if not."
190 (unread-string line input-port
) ; ugh
191 (let ((form
(read input-port
)))
192 (cond
((and
(list? form
) ; (define
(PROC ARGS
) "DOC" ...
)
194 (eq?
'define (car form))
196 (symbol? (caadr form))
197 (string? (caddr form)))
199 ((and (list? form) ; (define VAR (lambda ARGS "DOC" ...))
201 (eq? 'define
(car form
))
202 (symbol?
(cadr form
))
204 (< 3 (length
(caddr form
)))
205 (eq?
'lambda (car (caddr form)))
206 (string? (caddr (caddr form))))
207 (caddr (caddr form)))
210 ;; Split @var{string} into lines, adding @var{prefix} to each.
212 (define (split-prefixed string prefix)
213 (separate-fields-discarding-char
217 (string-append prefix line))
220 ;; snarf input-file output-file
221 ;; Extract docstrings from the input file @var{input}, presumed
222 ;; to be written in language @var{lang}.
223 ;;-Author: Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
224 ;;-Created: 2001-02-17
225 ;;-ttn-mod: regluarize lang parm lookup, add "std int doc" snarfing (2 places)
226 (define (snarf input-file lang)
227 (let* ((i-p (open-input-file input-file))
228 (parm-regexp (lambda (parm) (make-regexp (lang-parm lang parm))))
229 (docstring-start (parm-regexp 'docstring-start
))
230 (docstring-end
(parm-regexp
'docstring-end))
231 (docstring-prefix (parm-regexp 'docstring-prefix
))
232 (option-prefix
(parm-regexp
'option-prefix))
233 (signature-start (parm-regexp 'signature-start
))
235 (lambda
(line i-p options
)
236 (let ((int-doc
(and
(lang-parm lang
'std-int-doc?)
237 (let ((d (find-std-int-doc line i-p)))
238 (and d (split-prefixed d "internal: "))))))
240 (append (reverse int-doc) options)
243 (let lp ((line (read-line i-p)) (state 'neutral
) (doc-strings
'())
244 (options '()) (entries
'()) (lno 0))
247 (close-input-port i-p)
250 ;; State 'neutral
: we
're currently not within a docstring or
252 ((eq? state 'neutral
)
253 (let ((m
(regexp-exec docstring-start line
)))
255 (lp (read-line i-p
) 'doc-string
256 (list (match:substring m 1)) '() entries
(+ lno
1))
257 (lp (read-line i-p
) state
'() '() entries
(+ lno
1)))))
259 ;; State
'doc-string: we have started reading a docstring and
260 ;; are waiting for more, for options or for a define.
261 ((eq? state 'doc-string
)
262 (let ((m0
(regexp-exec docstring-prefix line
))
263 (m1
(regexp-exec option-prefix line
))
264 (m2
(regexp-exec signature-start line
))
265 (m3
(regexp-exec docstring-end line
)))
268 (lp (read-line i-p
) 'doc-string
269 (cons (match:substring m0 1) doc-strings) '() entries
272 (lp (read-line i-p
) 'options
273 doc-strings (cons (match:substring m1 1) options) entries
276 (let ((options (augmented-options line i-p options))) ; ttn-mod
277 (lp (read-line i-p) 'neutral
'() '()
278 (cons
(parse-entry doc-strings options line input-file lno
)
282 (lp (read-line i-p
) 'neutral '() '()
283 (cons (parse-entry doc-strings options #f input-file lno)
287 (lp (read-line i-p) 'neutral
'() '() entries
(+ lno
1))))))
289 ;; State
'options: We're waiting
for more options or
for a
291 ((eq? state
'options)
292 (let ((m1 (regexp-exec option-prefix line))
293 (m2 (regexp-exec signature-start line))
294 (m3 (regexp-exec docstring-end line)))
297 (lp (read-line i-p) 'options
298 doc-strings
(cons
(match
:substring m1
1) options
) entries
301 (let ((options
(augmented-options line i-p options
))) ; ttn-mod
302 (lp (read-line i-p
) 'neutral '() '()
303 (cons (parse-entry doc-strings options line input-file lno)
307 (lp (read-line i-p) 'neutral
'() '()
308 (cons
(parse-entry doc-strings options
#f input-file lno)
312 (lp (read-line i-p
) 'neutral '() '() entries (+ lno 1))))))))))
314 (define (make-entry symbol signature docstrings options filename line)
315 (vector 'entry symbol signature docstrings options filename line
))
316 (define
(entry-symbol e
)
318 (define
(entry-signature e
)
320 (define
(entry-docstrings e
)
322 (define
(entry-options e
)
324 (define
(entry-filename e
)
326 (define
(entry-line e
)
327 "This docstring will not be snarfed, unfortunately..."
330 ;; Create a docstring entry from the docstring line list
331 ;; @var
{doc-strings
}, the option line list @var
{options
} and the
332 ;; define line @var
{def-line
}
333 (define
(parse-entry docstrings options def-line filename line-no
)
334 ; (write-line docstrings
)
337 (make-entry
(get-symbol def-line
)
338 (make-prototype def-line
) (reverse docstrings
)
339 (reverse options
) filename
340 (+ (- line-no
(length docstrings
) (length options
)) 1)))
341 ((> (length docstrings
) 0)
342 (make-entry
(string-
>symbol
(car
(reverse docstrings
)))
343 (car
(reverse docstrings
))
344 (cdr
(reverse docstrings
))
345 (reverse options
) filename
346 (+ (- line-no
(length docstrings
) (length options
)) 1)))
348 (make-entry
'foo "" (reverse docstrings) (reverse options) filename
349 (+ (- line-no (length docstrings) (length options)) 1)))))
351 ;; Create a string which is a procedure prototype. The necessary
352 ;; information for constructing the prototype is taken from the line
353 ;; @var{def-line}, which is a line starting with @code{(define...}.
354 (define (make-prototype def-line)
355 (call-with-input-string
358 (let* ((paren (read-char s-p))
365 (symbol->string tmp))
369 (define (get-symbol def-line)
370 (call-with-input-string
373 (let* ((paren (read-char s-p))
384 ;; Append the symbols
in the string list @var
{s
}, separated with a
386 (define
(join-symbols s
)
390 (string-append
". " (symbol-
>string s
)))
392 (symbol-
>string
(car s
)))
394 (string-append
(symbol-
>string
(car s
)) " " (join-symbols
(cdr s
))))))
396 ;; Write @var
{entries
} to @var
{output-file
} using @var
{writer
}.
397 ;; @var
{writer
} is a proc that takes one entry.
398 ;; If @var
{output-file
} is
#f, write to stdout.
400 (define
(write-output entries output-file writer
)
401 (with-output-to-port
(cond
(output-file
(open-output-file output-file
))
402 (else (current-output-port
)))
403 (lambda
() (for-each writer entries
))))
405 ;; Write an @var
{entry
} using texinfo format.
406 ;;-ttn-mod: renamed from
`texinfo-output', distilled
407 (define (format-texinfo entry)
409 (display (entry-symbol entry))
411 (display "@c snarfed from ")
412 (display (entry-filename entry))
414 (display (entry-line entry))
416 (display "@deffn procedure ")
417 (display (entry-signature entry))
419 (for-each (lambda (s) (write-line s))
420 (entry-docstrings entry))
421 (for-each (lambda (s) (display "@c ") (write-line s))
422 (entry-options entry))
423 (write-line "@end deffn"))
425 ;; Write an @var{entry} using plain format.
426 ;;-ttn-mod: renamed from `texinfo-output
', distilled
427 (define (format-plain entry)
428 (display "Procedure: (")
429 (display (entry-signature entry))
432 (for-each (lambda (s) (write-line s))
433 (entry-docstrings entry))
434 (for-each (lambda (s) (display ";; ") (write-line s))
435 (entry-options entry))
437 (display (entry-filename entry))
439 (display (entry-line entry))
444 ;;; doc-snarf ends here