doc-snarf.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. ;;; doc-snarf --- Extract documentation from source files
  2. ;; Copyright (C) 2001, 2006, 2011 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 %summary "Snarf out documentation from a file.")
  75. (define command-synopsis
  76. '((version (single-char #\v) (value #f))
  77. (help (single-char #\h) (value #f))
  78. (output (single-char #\o) (value #t))
  79. (texinfo (single-char #\t) (value #f))
  80. (lang (single-char #\l) (value #t))))
  81. ;; Display version information and exit.
  82. ;;-ttn-mod: use var
  83. (define (display-version)
  84. (display "doc-snarf ") (display doc-snarf-version) (newline))
  85. ;; Display the usage help message and exit.
  86. ;;-ttn-mod: change option "source" to "lang"
  87. (define (display-help)
  88. (display "Usage: doc-snarf [options...] inputfile\n")
  89. (display " --help, -h Show this usage information\n")
  90. (display " --version, -v Show version information\n")
  91. (display
  92. " --output=FILE, -o Specify output file [default=stdout]\n")
  93. (display " --texinfo, -t Format output as texinfo\n")
  94. (display " --lang=[c,scheme], -l Specify the input language\n"))
  95. ;; Main program.
  96. ;;-ttn-mod: canonicalize lang
  97. (define (doc-snarf . args)
  98. (let ((options (getopt-long (cons "doc-snarf" args) command-synopsis)))
  99. (let ((help-wanted (option-ref options 'help #f))
  100. (version-wanted (option-ref options 'version #f))
  101. (texinfo-wanted (option-ref options 'texinfo #f))
  102. (lang (string->symbol
  103. (string-downcase (option-ref options 'lang "scheme")))))
  104. (cond
  105. (version-wanted (display-version))
  106. (help-wanted (display-help))
  107. (else
  108. (let ((input (option-ref options '() #f))
  109. (output (option-ref options 'output #f)))
  110. (if
  111. ;; Bonard B. Timmons III says `(pair? input)' alone is sufficient.
  112. ;; (and input (pair? input))
  113. (pair? input)
  114. (snarf-file (car input) output texinfo-wanted lang)
  115. (display-help))))))))
  116. (define main doc-snarf)
  117. ;; Supported languages and their parameters. Each element has form:
  118. ;; (LANG DOC-START DOC-END DOC-PREFIX OPT-PREFIX SIG-START STD-INT-DOC?)
  119. ;; LANG is a symbol, STD-INT-DOC? is a boolean indicating whether or not
  120. ;; LANG supports "standard internal docstring" (a string after the formals),
  121. ;; everything else is a string specifying a regexp.
  122. ;;-ttn-mod: new var
  123. (define supported-languages
  124. '((c
  125. "^/\\*(.*)"
  126. "^ \\*/"
  127. "^ \\* (.*)"
  128. "^ \\*-(.*)"
  129. "NOTHING AT THIS TIME!!!"
  130. #f
  131. )
  132. (scheme
  133. "^;; (.*)"
  134. "^;;\\."
  135. "^;; (.*)"
  136. "^;;-(.*)"
  137. "^\\(define"
  138. #t
  139. )))
  140. ;; Get @var{lang}'s @var{parameter}. Both args are symbols.
  141. ;;-ttn-mod: new proc
  142. (define (lang-parm lang parm)
  143. (list-ref (assq-ref supported-languages lang)
  144. (case parm
  145. ((docstring-start) 0)
  146. ((docstring-end) 1)
  147. ((docstring-prefix) 2)
  148. ((option-prefix) 3)
  149. ((signature-start) 4)
  150. ((std-int-doc?) 5))))
  151. ;; Snarf all docstrings from the file @var{input} and write them to
  152. ;; file @var{output}. Use texinfo format for the output if
  153. ;; @var{texinfo?} is true.
  154. ;;-ttn-mod: don't use string comparison, consult table instead
  155. (define (snarf-file input output texinfo? lang)
  156. (or (memq lang (map car supported-languages))
  157. (error "doc-snarf: input language must be c or scheme."))
  158. (write-output (snarf input lang) output
  159. (if texinfo? format-texinfo format-plain)))
  160. ;; fixme: this comment is required to trigger standard internal
  161. ;; docstring snarfing... ideally, it wouldn't be necessary.
  162. ;;-ttn-mod: new proc, from snarf-docs (aren't these names fun?)
  163. (define (find-std-int-doc line input-port)
  164. "Unread @var{line} from @var{input-port}, then read in the entire form and
  165. return the standard internal docstring if found. Return #f if not."
  166. (unread-string line input-port) ; ugh
  167. (let ((form (read input-port)))
  168. (cond ((and (list? form) ; (define (PROC ARGS) "DOC" ...)
  169. (< 3 (length form))
  170. (eq? 'define (car form))
  171. (pair? (cadr form))
  172. (symbol? (caadr form))
  173. (string? (caddr form)))
  174. (caddr form))
  175. ((and (list? form) ; (define VAR (lambda ARGS "DOC" ...))
  176. (< 2 (length form))
  177. (eq? 'define (car form))
  178. (symbol? (cadr form))
  179. (list? (caddr form))
  180. (< 3 (length (caddr form)))
  181. (eq? 'lambda (car (caddr form)))
  182. (string? (caddr (caddr form))))
  183. (caddr (caddr form)))
  184. (else #f))))
  185. ;; Split @var{string} into lines, adding @var{prefix} to each.
  186. ;;-ttn-mod: new proc
  187. (define (split-prefixed string prefix)
  188. (separate-fields-discarding-char
  189. #\newline string
  190. (lambda lines
  191. (map (lambda (line)
  192. (string-append prefix line))
  193. lines))))
  194. ;; snarf input-file output-file
  195. ;; Extract docstrings from the input file @var{input}, presumed
  196. ;; to be written in language @var{lang}.
  197. ;;-Author: Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
  198. ;;-Created: 2001-02-17
  199. ;;-ttn-mod: regluarize lang parm lookup, add "std int doc" snarfing (2 places)
  200. (define (snarf input-file lang)
  201. (let* ((i-p (open-input-file input-file))
  202. (parm-regexp (lambda (parm) (make-regexp (lang-parm lang parm))))
  203. (docstring-start (parm-regexp 'docstring-start))
  204. (docstring-end (parm-regexp 'docstring-end))
  205. (docstring-prefix (parm-regexp 'docstring-prefix))
  206. (option-prefix (parm-regexp 'option-prefix))
  207. (signature-start (parm-regexp 'signature-start))
  208. (augmented-options
  209. (lambda (line i-p options)
  210. (let ((int-doc (and (lang-parm lang 'std-int-doc?)
  211. (let ((d (find-std-int-doc line i-p)))
  212. (and d (split-prefixed d "internal: "))))))
  213. (if int-doc
  214. (append (reverse int-doc) options)
  215. options)))))
  216. (let lp ((line (read-line i-p)) (state 'neutral) (doc-strings '())
  217. (options '()) (entries '()) (lno 0))
  218. (cond
  219. ((eof-object? line)
  220. (close-input-port i-p)
  221. (reverse entries))
  222. ;; State 'neutral: we're currently not within a docstring or
  223. ;; option section
  224. ((eq? state 'neutral)
  225. (let ((m (regexp-exec docstring-start line)))
  226. (if m
  227. (lp (read-line i-p) 'doc-string
  228. (list (match:substring m 1)) '() entries (+ lno 1))
  229. (lp (read-line i-p) state '() '() entries (+ lno 1)))))
  230. ;; State 'doc-string: we have started reading a docstring and
  231. ;; are waiting for more, for options or for a define.
  232. ((eq? state 'doc-string)
  233. (let ((m0 (regexp-exec docstring-prefix line))
  234. (m1 (regexp-exec option-prefix line))
  235. (m2 (regexp-exec signature-start line))
  236. (m3 (regexp-exec docstring-end line)))
  237. (cond
  238. (m0
  239. (lp (read-line i-p) 'doc-string
  240. (cons (match:substring m0 1) doc-strings) '() entries
  241. (+ lno 1)))
  242. (m1
  243. (lp (read-line i-p) 'options
  244. doc-strings (cons (match:substring m1 1) options) entries
  245. (+ lno 1)))
  246. (m2
  247. (let ((options (augmented-options line i-p options))) ; ttn-mod
  248. (lp (read-line i-p) 'neutral '() '()
  249. (cons (parse-entry doc-strings options line input-file lno)
  250. entries)
  251. (+ lno 1))))
  252. (m3
  253. (lp (read-line i-p) 'neutral '() '()
  254. (cons (parse-entry doc-strings options #f input-file lno)
  255. entries)
  256. (+ lno 1)))
  257. (else
  258. (lp (read-line i-p) 'neutral '() '() entries (+ lno 1))))))
  259. ;; State 'options: We're waiting for more options or for a
  260. ;; define.
  261. ((eq? state 'options)
  262. (let ((m1 (regexp-exec option-prefix line))
  263. (m2 (regexp-exec signature-start line))
  264. (m3 (regexp-exec docstring-end line)))
  265. (cond
  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. (define (make-entry symbol signature docstrings options filename line)
  284. (vector 'entry symbol signature docstrings options filename line))
  285. (define (entry-symbol e)
  286. (vector-ref e 1))
  287. (define (entry-signature e)
  288. (vector-ref e 2))
  289. (define (entry-docstrings e)
  290. (vector-ref e 3))
  291. (define (entry-options e)
  292. (vector-ref e 4))
  293. (define (entry-filename e)
  294. (vector-ref e 5))
  295. (define (entry-line e)
  296. "This docstring will not be snarfed, unfortunately..."
  297. (vector-ref e 6))
  298. ;; Create a docstring entry from the docstring line list
  299. ;; @var{doc-strings}, the option line list @var{options} and the
  300. ;; define line @var{def-line}
  301. (define (parse-entry docstrings options def-line filename line-no)
  302. ; (write-line docstrings)
  303. (cond
  304. (def-line
  305. (make-entry (get-symbol def-line)
  306. (make-prototype def-line) (reverse docstrings)
  307. (reverse options) filename
  308. (+ (- line-no (length docstrings) (length options)) 1)))
  309. ((> (length docstrings) 0)
  310. (make-entry (string->symbol (car (reverse docstrings)))
  311. (car (reverse docstrings))
  312. (cdr (reverse docstrings))
  313. (reverse options) filename
  314. (+ (- line-no (length docstrings) (length options)) 1)))
  315. (else
  316. (make-entry 'foo "" (reverse docstrings) (reverse options) filename
  317. (+ (- line-no (length docstrings) (length options)) 1)))))
  318. ;; Create a string which is a procedure prototype. The necessary
  319. ;; information for constructing the prototype is taken from the line
  320. ;; @var{def-line}, which is a line starting with @code{(define...}.
  321. (define (make-prototype def-line)
  322. (call-with-input-string
  323. def-line
  324. (lambda (s-p)
  325. (let* ((paren (read-char s-p))
  326. (keyword (read s-p))
  327. (tmp (read s-p)))
  328. (cond
  329. ((pair? tmp)
  330. (join-symbols tmp))
  331. ((symbol? tmp)
  332. (symbol->string tmp))
  333. (else
  334. ""))))))
  335. (define (get-symbol def-line)
  336. (call-with-input-string
  337. def-line
  338. (lambda (s-p)
  339. (let* ((paren (read-char s-p))
  340. (keyword (read s-p))
  341. (tmp (read s-p)))
  342. (cond
  343. ((pair? tmp)
  344. (car tmp))
  345. ((symbol? tmp)
  346. tmp)
  347. (else
  348. 'foo))))))
  349. ;; Append the symbols in the string list @var{s}, separated with a
  350. ;; space character.
  351. (define (join-symbols s)
  352. (cond ((null? s)
  353. "")
  354. ((symbol? s)
  355. (string-append ". " (symbol->string s)))
  356. ((null? (cdr s))
  357. (symbol->string (car s)))
  358. (else
  359. (string-append (symbol->string (car s)) " " (join-symbols (cdr s))))))
  360. ;; Write @var{entries} to @var{output-file} using @var{writer}.
  361. ;; @var{writer} is a proc that takes one entry.
  362. ;; If @var{output-file} is #f, write to stdout.
  363. ;;-ttn-mod: new proc
  364. (define (write-output entries output-file writer)
  365. (with-output-to-port (cond (output-file (open-output-file output-file))
  366. (else (current-output-port)))
  367. (lambda () (for-each writer entries))))
  368. ;; Write an @var{entry} using texinfo format.
  369. ;;-ttn-mod: renamed from `texinfo-output', distilled
  370. (define (format-texinfo entry)
  371. (display "\n\f")
  372. (display (entry-symbol entry))
  373. (newline)
  374. (display "@c snarfed from ")
  375. (display (entry-filename entry))
  376. (display ":")
  377. (display (entry-line entry))
  378. (newline)
  379. (display "@deffn procedure ")
  380. (display (entry-signature entry))
  381. (newline)
  382. (for-each (lambda (s) (write-line s))
  383. (entry-docstrings entry))
  384. (for-each (lambda (s) (display "@c ") (write-line s))
  385. (entry-options entry))
  386. (write-line "@end deffn"))
  387. ;; Write an @var{entry} using plain format.
  388. ;;-ttn-mod: renamed from `texinfo-output', distilled
  389. (define (format-plain entry)
  390. (display "Procedure: ")
  391. (display (entry-signature entry))
  392. (newline)
  393. (for-each (lambda (s) (write-line s))
  394. (entry-docstrings entry))
  395. (for-each (lambda (s) (display ";; ") (write-line s))
  396. (entry-options entry))
  397. (display "Snarfed from ")
  398. (display (entry-filename entry))
  399. (display ":")
  400. (display (entry-line entry))
  401. (newline)
  402. (write-line "\f"))
  403. ;;; doc-snarf ends here