doc-snarf.scm 15 KB

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