summaryrefslogtreecommitdiff
path: root/guile/modules/system/documentation/output.scm
blob: 4f996ffeb29cf4bc7f63bf3e81c552f56dc68741 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
;;; output.scm  --  Output documentation "snarffed" from C files in Texi/GDF.
;;;
;;; Copyright 2006, 2007, 2010 Free Software Foundation, Inc.
;;;
;;;
;;; 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 3 of the License, or
;;; (at your option) any later version.
;;;
;;; 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.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

(define-module (system documentation output)
  :use-module (srfi srfi-1)
  :use-module (srfi srfi-13)
  :use-module (srfi srfi-39)
  :autoload   (system documentation c-snarf) (run-cpp-and-extract-snarfing)

  :export (schemify-name scheme-procedure-texi-line
           procedure-gdf-string procedure-texi-documentation
           output-procedure-texi-documentation-from-c-file
           *document-c-functions?*))

;;; Author:  Ludovic Courtès
;;;
;;; Commentary:
;;;
;;; This module provides support function to issue Texinfo or GDF (Guile
;;; Documentation Format) documentation from "snarffed" C files.
;;;
;;; Code:


;;;
;;; Utility.
;;;

(define (schemify-name str)
  "Turn @var{str}, a C variable or function name, into a more ``Schemey''
form, e.g., one with dashed instead of underscores, etc."
  (string-map (lambda (chr)
                (if (eq? chr #\_)
                    #\-
                    chr))
              (if (string-suffix? "_p" str)
                  (string-append (substring str 0
                                            (- (string-length str) 2))
                                 "?")
                  str)))


;;;
;;; Issuing Texinfo and GDF-formatted doc (i.e., `guile-procedures.texi').
;;; GDF = Guile Documentation Format
;;;

(define *document-c-functions?*
  ;; Whether to mention C function names along with Scheme procedure names.
  (make-parameter #t))

(define (scheme-procedure-texi-line proc-name args
                                    required-args optional-args
                                    rest-arg?)
  "Return a Texinfo string describing the Scheme procedure named
@var{proc-name}, whose arguments are listed in @var{args} (a list of strings)
and whose signature is defined by @var{required-args}, @var{optional-args}
and @var{rest-arg?}."
  (string-append "@deffn {Scheme Procedure} " proc-name " "
                 (string-join (take args required-args) " ")
                 (string-join (take (drop args required-args)
                                    (+ optional-args
                                       (if rest-arg? 1 0)))
                              " [" 'prefix)
                 (if rest-arg? "...]" "")
                 (make-string optional-args #\])))

(define (procedure-gdf-string proc-doc)
  "Issue a Texinfo/GDF docstring corresponding to @var{proc-doc}, a
documentation alist as returned by @code{parse-snarfed-line}.  To produce
actual GDF-formatted doc, the resulting string must be processed by
@code{makeinfo}."
  (let* ((proc-name     (assq-ref proc-doc 'scheme-name))
         (args          (assq-ref proc-doc 'arguments))
         (signature     (assq-ref proc-doc 'signature))
         (required-args (assq-ref signature 'required))
         (optional-args (assq-ref signature 'optional))
         (rest-arg?     (assq-ref signature 'rest?))
         (location      (assq-ref proc-doc 'location))
         (file-name     (car location))
         (line          (cadr location))
         (documentation (assq-ref proc-doc 'documentation)))
    (string-append "" ;; form feed
                   proc-name (string #\newline)
                   (format #f "@c snarfed from ~a:~a~%"
                           file-name line)

                   (scheme-procedure-texi-line proc-name
                                               (map schemify-name args)
                                               required-args optional-args
                                               rest-arg?)

                   (string #\newline)
                   documentation (string #\newline)
                   "@end deffn" (string #\newline))))

(define (procedure-texi-documentation proc-doc)
  "Issue a Texinfo docstring corresponding to @var{proc-doc}, a documentation
alist as returned by @var{parse-snarfed-line}.  The resulting Texinfo string
is meant for use in a manual since it also documents the corresponding C
function."
  (let* ((proc-name     (assq-ref proc-doc 'scheme-name))
         (c-name        (assq-ref proc-doc 'c-name))
         (args          (assq-ref proc-doc 'arguments))
         (signature     (assq-ref proc-doc 'signature))
         (required-args (assq-ref signature 'required))
         (optional-args (assq-ref signature 'optional))
         (rest-arg?     (assq-ref signature 'rest?))
         (location      (assq-ref proc-doc 'location))
         (file-name     (car location))
         (line          (cadr location))
         (documentation (assq-ref proc-doc 'documentation)))
  (string-append (string #\newline)
		 (format #f "@c snarfed from ~a:~a~%"
			 file-name line)

                 ;; document the Scheme procedure
                 (scheme-procedure-texi-line proc-name
                                             (map schemify-name args)
                                             required-args optional-args
                                             rest-arg?)
                 (string #\newline)

                 (if (*document-c-functions?*)
                     (string-append
                      ;; document the C function
                      "@deffnx {C Function} " c-name " ("
                      (if (null? args)
                          "void"
                          (string-join (map (lambda (arg)
                                              (string-append "SCM " arg))
                                            args)
                                       ", "))
                      ")" (string #\newline))
                     "")

		 documentation (string #\newline)
                 "@end deffn" (string #\newline))))


;;;
;;; Very high-level interface.
;;;

(define (output-procedure-texi-documentation-from-c-file c-file cpp cflags
                                                         port)
  (for-each (lambda (texi-string)
              (display texi-string port))
            (map procedure-texi-documentation
                 (run-cpp-and-extract-snarfing cpp c-file cflags))))


;;; output.scm ends here

;;; Local Variables:
;;; mode: scheme
;;; coding: latin-1
;;; End:

;;; arch-tag: 20ca493a-6f1a-4d7f-9d24-ccce0d32df49