snarf-check-and-output-texi.scm 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. ;;; snarf-check-and-output-texi --- called by the doc snarfer.
  2. ;; Copyright (C) 2001, 2002, 2006, 2011, 2014 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: Michael Livshin
  19. ;;; Code:
  20. (define-module (scripts snarf-check-and-output-texi)
  21. :use-module (ice-9 streams)
  22. :use-module (ice-9 match)
  23. :export (snarf-check-and-output-texi))
  24. (define %include-in-guild-list #f)
  25. (define %summary "Transform snarfed .doc files into texinfo documentation.")
  26. ;;; why aren't these in some module?
  27. (define-macro (when cond . body)
  28. `(if ,cond (begin ,@body)))
  29. (define-macro (unless cond . body)
  30. `(if (not ,cond) (begin ,@body)))
  31. (define *manual-flag* #f)
  32. (define (snarf-check-and-output-texi . flags)
  33. (if (member "--manual" flags)
  34. (set! *manual-flag* #t))
  35. (process-stream (current-input-port)))
  36. (define (process-stream port)
  37. (let loop ((input (stream-map (match-lambda
  38. (('id . s)
  39. (cons 'id (string->symbol s)))
  40. (('int_dec . s)
  41. (cons 'int (string->number s)))
  42. (('int_oct . s)
  43. (cons 'int (string->number s 8)))
  44. (('int_hex . s)
  45. (cons 'int (string->number s 16)))
  46. ((and x (? symbol?))
  47. (cons x x))
  48. ((and x (? string?))
  49. (cons 'string x))
  50. (x x))
  51. (make-stream (lambda (s)
  52. (let loop ((s s))
  53. (cond
  54. ((stream-null? s) #t)
  55. ((memq (stream-car s) '(eol hash))
  56. (loop (stream-cdr s)))
  57. (else (cons (stream-car s) (stream-cdr s))))))
  58. (port->stream port read)))))
  59. (unless (stream-null? input)
  60. (let ((token (stream-car input)))
  61. (if (eq? (car token) 'snarf_cookie)
  62. (dispatch-top-cookie (stream-cdr input)
  63. loop)
  64. (loop (stream-cdr input)))))))
  65. (define (dispatch-top-cookie input cont)
  66. (when (stream-null? input)
  67. (error 'syntax "premature end of file"))
  68. (let ((token (stream-car input)))
  69. (cond
  70. ((eq? (car token) 'brace_open)
  71. (consume-multiline (stream-cdr input)
  72. cont))
  73. (else
  74. (consume-upto-cookie process-singleline
  75. input
  76. cont)))))
  77. (define (consume-upto-cookie process input cont)
  78. (let loop ((acc '()) (input input))
  79. (when (stream-null? input)
  80. (error 'syntax "premature end of file in directive context"))
  81. (let ((token (stream-car input)))
  82. (cond
  83. ((eq? (car token) 'snarf_cookie)
  84. (process (reverse! acc))
  85. (cont (stream-cdr input)))
  86. (else (loop (cons token acc) (stream-cdr input)))))))
  87. (define (consume-multiline input cont)
  88. (begin-multiline)
  89. (let loop ((input input))
  90. (when (stream-null? input)
  91. (error 'syntax "premature end of file in multiline context"))
  92. (let ((token (stream-car input)))
  93. (cond
  94. ((eq? (car token) 'brace_close)
  95. (end-multiline)
  96. (cont (stream-cdr input)))
  97. (else (consume-upto-cookie process-multiline-directive
  98. input
  99. loop))))))
  100. (define *file* #f)
  101. (define *line* #f)
  102. (define *c-function-name* #f)
  103. (define *function-name* #f)
  104. (define *snarf-type* #f)
  105. (define *args* #f)
  106. (define *sig* #f)
  107. (define *docstring* #f)
  108. (define (begin-multiline)
  109. (set! *file* #f)
  110. (set! *line* #f)
  111. (set! *c-function-name* #f)
  112. (set! *function-name* #f)
  113. (set! *snarf-type* #f)
  114. (set! *args* #f)
  115. (set! *sig* #f)
  116. (set! *docstring* #f))
  117. (define *primitive-deffnx-signature* "@deffnx {Scheme Procedure} ")
  118. (define *primitive-deffnx-sig-length* (string-length *primitive-deffnx-signature*))
  119. (define (end-multiline)
  120. (let* ((req (car *sig*))
  121. (opt (cadr *sig*))
  122. (var (caddr *sig*))
  123. (all (+ req opt var)))
  124. (if (and (not (eqv? *snarf-type* 'register))
  125. (not (= (length *args*) all)))
  126. (error (format #f "~A:~A: ~A's C implementation takes ~A args (should take ~A)"
  127. *file* *line* *function-name* (length *args*) all)))
  128. (let ((nice-sig
  129. (if (eq? *snarf-type* 'register)
  130. *function-name*
  131. (with-output-to-string
  132. (lambda ()
  133. (format #t "~A" *function-name*)
  134. (let loop-req ((args *args*) (r 0))
  135. (if (< r req)
  136. (begin
  137. (format #t " ~A" (car args))
  138. (loop-req (cdr args) (+ 1 r)))
  139. (let loop-opt ((o 0) (args args) (tail '()))
  140. (if (< o opt)
  141. (begin
  142. (format #t " [~A" (car args))
  143. (loop-opt (+ 1 o) (cdr args) (cons #\] tail)))
  144. (begin
  145. (if (> var 0)
  146. (format #t " . ~A"
  147. (car args)))
  148. (let loop-tail ((tail tail))
  149. (if (not (null? tail))
  150. (begin
  151. (format #t "~A" (car tail))
  152. (loop-tail (cdr tail))))))))))))))
  153. (scm-deffnx
  154. (if (and *manual-flag* (eq? *snarf-type* 'primitive))
  155. (with-output-to-string
  156. (lambda ()
  157. (format #t "@deffnx {C Function} ~A (" *c-function-name*)
  158. (unless (null? *args*)
  159. (format #t "~A" (car *args*))
  160. (let loop ((args (cdr *args*)))
  161. (unless (null? args)
  162. (format #t ", ~A" (car args))
  163. (loop (cdr args)))))
  164. (format #t ")\n")))
  165. #f)))
  166. (format #t "\n ~A\n" *function-name*)
  167. (format #t "@c snarfed from ~A:~A\n" *file* *line*)
  168. (format #t "@deffn {Scheme Procedure} ~A\n" nice-sig)
  169. (let loop ((strings *docstring*) (scm-deffnx scm-deffnx))
  170. (cond ((null? strings))
  171. ((or (not scm-deffnx)
  172. (and (>= (string-length (car strings))
  173. *primitive-deffnx-sig-length*)
  174. (string=? (substring (car strings)
  175. 0 *primitive-deffnx-sig-length*)
  176. *primitive-deffnx-signature*)))
  177. (display (car strings))
  178. (loop (cdr strings) scm-deffnx))
  179. (else (display scm-deffnx)
  180. (loop strings #f))))
  181. (display "\n")
  182. (display "@end deffn\n"))))
  183. (define (texi-quote s)
  184. (let rec ((i 0))
  185. (if (= i (string-length s))
  186. ""
  187. (string-append (let ((ss (substring s i (+ i 1))))
  188. (if (string=? ss "@")
  189. "@@"
  190. ss))
  191. (rec (+ i 1))))))
  192. (define (process-multiline-directive l)
  193. (define do-args
  194. (match-lambda
  195. (('(paren_close . paren_close))
  196. '())
  197. (('(comma . comma) rest ...)
  198. (do-args rest))
  199. (('(id . SCM) ('id . name) rest ...)
  200. (cons name (do-args rest)))
  201. (x (error (format #f "invalid argument syntax: ~A" (map cdr x))))))
  202. (define do-arglist
  203. (match-lambda
  204. (('(paren_open . paren_open) '(id . void) '(paren_close . paren_close))
  205. '())
  206. (('(paren_open . paren_open) rest ...)
  207. (do-args rest))
  208. (x (error (format #f "invalid arglist syntax: ~A" (map cdr x))))))
  209. (define do-command
  210. (match-lambda
  211. (('cname ('id . name))
  212. (set! *c-function-name* (texi-quote (symbol->string name))))
  213. (('fname ('string . name) ...)
  214. (set! *function-name* (texi-quote (apply string-append name))))
  215. (('type ('id . type))
  216. (set! *snarf-type* type))
  217. (('type ('int . num))
  218. (set! *snarf-type* num))
  219. (('location ('string . file) ('int . line))
  220. (set! *file* file)
  221. (set! *line* line))
  222. (('arglist rest ...)
  223. (set! *args* (do-arglist rest)))
  224. (('argsig ('int . req) ('int . opt) ('int . var))
  225. (set! *sig* (list req opt var)))
  226. (x (error (format #f "unknown doc attribute: ~A" x)))))
  227. (define do-directive
  228. (match-lambda
  229. ((('id . command) rest ...)
  230. (do-command (cons command rest)))
  231. ((('string . string) ...)
  232. (set! *docstring* string))
  233. (x (error (format #f "unknown doc attribute syntax: ~A" x)))))
  234. (do-directive l))
  235. (define (process-singleline l)
  236. (define do-argpos
  237. (match-lambda
  238. ((('id . name) ('int . pos) ('int . line))
  239. (let ((idx (list-index *args* name)))
  240. (when idx
  241. (unless (= (+ idx 1) pos)
  242. (display (format #f "~A:~A: wrong position for argument ~A: ~A (should be ~A)\n"
  243. *file* line name pos (+ idx 1))
  244. (current-error-port))))))
  245. (x #f)))
  246. (define do-command
  247. (match-lambda
  248. (('(id . argpos) rest ...)
  249. (do-argpos rest))
  250. (x (error (format #f "unknown check: ~A" x)))))
  251. (when *function-name*
  252. (do-command l)))
  253. (define main snarf-check-and-output-texi)