doc-snarf 15 KB

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