snarf-check-and-output-texi 10 KB

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