1 ;;; output.scm -- Output documentation "snarffed" from C files in Texi/GDF.
3 ;;; Copyright 2006, 2007, 2010 Free Software Foundation, Inc.
6 ;;; This program is free software; you can redistribute it and/or modify
7 ;;; it under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or
9 ;;; (at your option) any later version.
11 ;;; This program is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with this program; if not, write to the Free Software
18 ;;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 (define-module (system documentation output)
21 :use-module (srfi srfi-1)
22 :use-module (srfi srfi-13)
23 :use-module (srfi srfi-39)
24 :autoload (system documentation c-snarf) (run-cpp-and-extract-snarfing)
26 :export (schemify-name scheme-procedure-texi-line
27 procedure-gdf-string procedure-texi-documentation
28 output-procedure-texi-documentation-from-c-file
29 *document-c-functions?*))
31 ;;; Author: Ludovic Courtès
35 ;;; This module provides support function to issue Texinfo or GDF (Guile
36 ;;; Documentation Format) documentation from "snarffed" C files.
45 (define (schemify-name str)
46 "Turn @var{str}, a C variable or function name, into a more ``Schemey''
47 form, e.g., one with dashed instead of underscores, etc."
48 (string-map (lambda (chr)
52 (if (string-suffix? "_p" str)
53 (string-append (substring str 0
54 (- (string-length str) 2))
60 ;;; Issuing Texinfo and GDF-formatted doc (i.e., `guile-procedures.texi').
61 ;;; GDF = Guile Documentation Format
64 (define *document-c-functions?*
65 ;; Whether to mention C function names along with Scheme procedure names.
68 (define (scheme-procedure-texi-line proc-name args
69 required-args optional-args
71 "Return a Texinfo string describing the Scheme procedure named
72 @var{proc-name}, whose arguments are listed in @var{args} (a list of strings)
73 and whose signature is defined by @var{required-args}, @var{optional-args}
75 (string-append "@deffn {Scheme Procedure} " proc-name " "
76 (string-join (take args required-args) " ")
77 (string-join (take (drop args required-args)
81 (if rest-arg? "...]" "")
82 (make-string optional-args #\])))
84 (define (procedure-gdf-string proc-doc)
85 "Issue a Texinfo/GDF docstring corresponding to @var{proc-doc}, a
86 documentation alist as returned by @code{parse-snarfed-line}. To produce
87 actual GDF-formatted doc, the resulting string must be processed by
89 (let* ((proc-name (assq-ref proc-doc 'scheme-name))
90 (args (assq-ref proc-doc 'arguments))
91 (signature (assq-ref proc-doc 'signature))
92 (required-args (assq-ref signature 'required))
93 (optional-args (assq-ref signature 'optional))
94 (rest-arg? (assq-ref signature 'rest?))
95 (location (assq-ref proc-doc 'location))
96 (file-name (car location))
97 (line (cadr location))
98 (documentation (assq-ref proc-doc 'documentation)))
99 (string-append "
\f" ;; form feed
100 proc-name (string #\newline)
101 (format #f "@c snarfed from ~a:~a~%"
104 (scheme-procedure-texi-line proc-name
105 (map schemify-name args)
106 required-args optional-args
110 documentation (string #\newline)
111 "@end deffn" (string #\newline))))
113 (define (procedure-texi-documentation proc-doc)
114 "Issue a Texinfo docstring corresponding to @var{proc-doc}, a documentation
115 alist as returned by @var{parse-snarfed-line}. The resulting Texinfo string
116 is meant for use in a manual since it also documents the corresponding C
118 (let* ((proc-name (assq-ref proc-doc 'scheme-name))
119 (c-name (assq-ref proc-doc 'c-name))
120 (args (assq-ref proc-doc 'arguments))
121 (signature (assq-ref proc-doc 'signature))
122 (required-args (assq-ref signature 'required))
123 (optional-args (assq-ref signature 'optional))
124 (rest-arg? (assq-ref signature 'rest?))
125 (location (assq-ref proc-doc 'location))
126 (file-name (car location))
127 (line (cadr location))
128 (documentation (assq-ref proc-doc 'documentation)))
129 (string-append (string #\newline)
130 (format #f "@c snarfed from ~a:~a~%"
133 ;; document the Scheme procedure
134 (scheme-procedure-texi-line proc-name
135 (map schemify-name args)
136 required-args optional-args
140 (if (*document-c-functions?*)
142 ;; document the C function
143 "@deffnx {C Function} " c-name " ("
146 (string-join (map (lambda (arg)
147 (string-append "SCM " arg))
150 ")" (string #\newline))
153 documentation (string #\newline)
154 "@end deffn" (string #\newline))))
158 ;;; Very high-level interface.
161 (define (output-procedure-texi-documentation-from-c-file c-file cpp cflags
163 (for-each (lambda (texi-string)
164 (display texi-string port))
165 (map procedure-texi-documentation
166 (run-cpp-and-extract-snarfing cpp c-file cflags))))
169 ;;; output.scm ends here
176 ;;; arch-tag: 20ca493a-6f1a-4d7f-9d24-ccce0d32df49