merge from 1.8 branch
[bpt/guile.git] / scripts / snarf-guile-m4-docs
CommitLineData
d928d47f
TTN
1#!/bin/sh
2# aside from this initial boilerplate, this is actually -*- scheme -*- code
3main='(module-ref (resolve-module '\''(scripts snarf-guile-m4-docs)) '\'main')'
4exec ${GUILE-guile} -l $0 -c "(apply $main (cdr (command-line)))" "$@"
5!#
6;;; snarf-guile-m4-docs --- Parse guile.m4 comments for texi documentation
7
6e7d5622 8;; Copyright (C) 2002, 2006 Free Software Foundation, Inc.
d928d47f
TTN
9;;
10;; This program is free software; you can redistribute it and/or
11;; modify it under the terms of the GNU General Public License as
12;; published by the Free Software Foundation; either version 2, or
13;; (at your option) any later version.
14;;
15;; This program is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18;; General Public License for more details.
19;;
20;; You should have received a copy of the GNU General Public License
21;; along with this software; see the file COPYING. If not, write to
92205699
MV
22;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301 USA
d928d47f
TTN
24
25;;; Author: Thien-Thi Nguyen <ttn@gnu.org>
26
27;;; Commentary:
28
29;; Usage: snarf-guile-m4-docs FILE
30;;
31;; Grep FILE for comments preceding macro definitions, massage
32;; them into valid texi, and display to stdout. For each comment,
33;; lines preceding "^# Usage:" are discarded.
34;;
35;; TODO: Generalize.
36
37;;; Code:
38
39(define-module (scripts snarf-guile-m4-docs)
40 :use-module (ice-9 rdelim)
41 :export (snarf-guile-m4-docs))
42
43(define (display-texi lines)
44 (display "@deffn {Autoconf Macro}")
45 (for-each (lambda (line)
27c54e0b
NJ
46 (display (cond ((and (>= (string-length line) 2)
47 (string=? "# " (substring line 0 2)))
48 (substring line 2))
49 ((string=? "#" (substring line 0 1))
50 (substring line 1))
51 (else line)))
d928d47f
TTN
52 (newline))
53 lines)
54 (display "@end deffn")
55 (newline) (newline))
56
57(define (prefix? line sub)
58 (false-if-exception
59 (string=? sub (substring line 0 (string-length sub)))))
60
61(define (massage-usage line)
62 (let loop ((line (string->list line)) (acc '()))
63 (if (null? line)
64 (list (list->string (reverse acc)))
65 (loop (cdr line)
66 (cons (case (car line)
67 ((#\( #\) #\,) #\space)
68 (else (car line)))
69 acc)))))
70
71(define (snarf-guile-m4-docs . args)
72 (let* ((p (open-file (car args) "r"))
73 (next (lambda () (read-line p))))
74 (let loop ((line (next)) (acc #f))
75 (or (eof-object? line)
76 (cond ((prefix? line "# Usage:")
77 (loop (next) (massage-usage (substring line 8))))
78 ((prefix? line "AC_DEFUN")
79 (display-texi (reverse acc))
80 (loop (next) #f))
81 ((and acc (prefix? line "#"))
82 (loop (next) (cons line acc)))
83 (else
84 (loop (next) #f)))))))
85
86(define main snarf-guile-m4-docs)
87
88;;; snarf-guile-m4-docs ends here