extract-doc.stk 4.43 KB
Newer Older
eg's avatar
eg committed
1 2
;;;;
;;;; extract-doc.stk	-- Extrcat Documentation from STklos source files
Erick's avatar
Erick committed
3 4 5 6
;;;;
;;;; Copyright © 2000-2011 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
;;;;
;;;;
eg's avatar
eg committed
7 8 9 10
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
Erick's avatar
Erick committed
11
;;;;
eg's avatar
eg committed
12 13 14 15
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
Erick's avatar
Erick committed
16
;;;;
eg's avatar
eg committed
17 18
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
Erick's avatar
Erick committed
19
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
eg's avatar
eg committed
20
;;;; USA.
Erick's avatar
Erick committed
21
;;;;
eg's avatar
eg committed
22 23
;;;;           Author: Erick Gallesio [eg@unice.fr]
;;;;    Creation date: 24-Nov-2000 11:08 (eg)
Erick's avatar
Erick committed
24
;;;; Last file update: 20-Apr-2011 10:24 (eg)
eg's avatar
eg committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
;;;;

(define start-doc-rgxp   	(string->regexp "^<doc "))
(define end-synopsys-rgxp 	(string->regexp "^ \\* *$"))
(define end-doc-rgxp		(string->regexp "^doc>"))

;======================================================================
;
;			parse-until-regexp
;
;======================================================================
(define (end-of-string s from)
  (let ((len (string-length s)))
    (substring s (min from len) len)))

(define (parse-until in proc)
  (let ((result '()))
    (do ((l (read-line in) (read-line in)))
	((or (eof-object? l) (proc l)))
      (set! result (if (null? result)
		       (list (end-of-string l 3))
		       `(,(end-of-string l 3) "\n" ,@result))))
    (apply string-append (reverse! result))))

;======================================================================
;
; 			parse-documentation-item
;
;======================================================================
(define (parse-documentation-item first-line in)

  (define (analyse-documentation-header first-line)
    (let ((in 	(open-input-string first-line))
	  (type #f)
	  (l    '()))
      (read in) ;; to skip "<doc"

      ;; Determine the type (procedure, syntax, ..) of this header
      (let ((item (read in)))
	(case  item
Erick's avatar
Erick committed
65 66 67 68 69
	  ((ext EXT R7RS) 			(set! type 'extended))
	  ((ext-syntax EXT-SYNTAX SYNTAX-R7RS)	(set! type 'extended-syntax))
	  ((syntax SYNTAX)			(set! type 'syntax))
	  (else					(set! type 'procedure)
						(set! l (list item)))))
eg's avatar
eg committed
70 71 72 73 74 75 76 77 78 79
      ;; Read all the item on the line
      (do ((proc (read in) (read in)))
	  ((eof-object? proc))
	(set! l (cons proc l)))

      ;; return a pair with the type in the car and the names in cdr.
      (cons type l)))

  (define (parse-synopsys in)
    (parse-until in (lambda (l) (regexp-match end-synopsys-rgxp l))))
Erick's avatar
Erick committed
80 81

  (define (parse-description in)
eg's avatar
eg committed
82 83 84
    (parse-until in (lambda (l) (regexp-match end-doc-rgxp l))))

  ;;
Erick's avatar
Erick committed
85
  ;; parse-proc-starts here
eg's avatar
eg committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  ;;
  (let* ((infos (analyse-documentation-header first-line))
	 (type  (car infos))
	 (procs (cdr infos))
	 (syn   (parse-synopsys in))
	 (descr (parse-description in)))
    ;; Generate an entry for the first procedure of the list
    (format #t "(~S :type ~S :synopsis ~S :description ~S :similar ~S)\n\n"
	    (car procs) type syn descr (cdr procs))
    ;; Other procedures will use a :see entry
    (for-each (lambda(p) (format #t "(~S :see ~S)\n" p (car procs)))
	      (cdr procs))))

;======================================================================
;
; 			extract-doc-from-file
;
;======================================================================
(define (extract-doc-from-file f)
  (let ((in (open-input-file f)))
    (do ((l (read-line in) (read-line in)))
	((eof-object? l))
      (when (regexp-match start-doc-rgxp l)
	(parse-documentation-item l in)))
    (close-input-port in)))

;======================================================================
;
Erick's avatar
Erick committed
114
; 			Program starts here
eg's avatar
eg committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
;
;======================================================================
(when (null? (argv))
  (error "Usage: extract-doc file ...")
  (exit 1))

;;
;; "Create" the file on standard output (will be redirected in the Makefile)
;;
(format #t ";; **DO NOT EDIT**\t\t\t-*- scheme -*-\n")
(format #t ";; File generated automatically on ~A\n" (exec "date"))


(for-each (lambda (f)
	    (format (current-error-port) "Extracting documentation from ~A\n" f)
	    (format #t "\n;; Source file ~S\n\n" f)
	    (extract-doc-from-file f))
	  (argv))
(flush)
(exit 0)