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

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