#!/usr/bin/guile \
-e "(scripts make-twerp2texi-index)" -s
!#
;;; make-twerp2texi-index --- Cache doc source pointers

;;	Copyright (C) 2003 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 2, 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 software; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;
;; As a special exception, the Free Software Foundation gives permission
;; for additional uses of the text contained in its release of GUILE.
;;
;; The exception is that, if you link the GUILE library with other files
;; to produce an executable, this does not by itself cause the
;; resulting executable to be covered by the GNU General Public License.
;; Your use of that executable is in no way restricted on account of
;; linking the GUILE library code into it.
;;
;; This exception does not however invalidate any other reasons why
;; the executable file might be covered by the GNU General Public License.
;;
;; This exception applies only to the code released by the
;; Free Software Foundation under the name GUILE.  If you copy
;; code from other Free Software Foundation releases into a copy of
;; GUILE, as the General Public License permits, the exception does
;; not apply to the code that you add in this way.  To avoid misleading
;; anyone as to the status of such modified files, you must delete
;; this exception notice from them.
;;
;; If you write modifications of your own for GUILE, it is your choice
;; whether to permit this exception to apply to your modifications.
;; If you do not wish that, delete this exception notice.

;;; Author: Thien-Thi Nguyen <ttn@gnu.org>

;;; Commentary:

;; Usage: make-twerp2texi-index [-o DOT-DOC-INDEX] [DOT-DOC ...]
;;
;; Scan the DOT-DOC files and write DOT-DOC-INDEX, an index file for use
;; by (later) twerp2texi invocation, q.v.  DOT-DOC-INDEX should be in the
;; current directory, and each DOT-DOC file should be under the current
;; directory (subdirectories ok).  Typically, you would add to Makefile.am
;; something like:
;;
;;   DOT_DOC_FILES = foo.doc bar.doc sub/foo.doc sub/bar.doc ...
;;   .doc-index: $(DOT_DOC_FILES)
;;          guile-tools make-twerp2texi-index -o $@ $(DOT_DOC_FILES)
;;
;; This writes .doc-index whenever any of the .doc files change.  See
;; guile-tool "doc-snarf" for creating .doc files.
;;
;;
;; Usage from a Scheme program:
;;   (make-twerp2texi-index files) => index
;;
;; Return a nested list representing the index of FILES.
;; FILES is a list of filenames, typically of the type produced by
;; "guile-tools doc-snarf" (.doc) or "guile-tools c2doc" (.cdoc).
;;
;; The return value is an assoc list, the keys of which are the FILES,
;; and the associated value a list of zero or more elements describing
;; functions found in each file, having the form:
;;
;;   (NAME "ARGLIST" START END)
;;
;; NAME is a symbol; ARGLIST is a string that may contain spaces, square
;; brackets and other strange characters; and both START and END are
;; byte positions (integers) in FILE between which NAME's docstring can
;; be found.

;;; Code:

(define-module (scripts make-twerp2texi-index)
  #:autoload (scripts PROGRAM) (HVQC-MAIN)
  #:autoload (scripts slurp) (slurp)
  #:autoload (ice-9 regex) (match:substring match:suffix)
  #:export (make-twerp2texi-index))

(define (make-twerp2texi-index files)
  (let ((rx (make-regexp "\f\n.([^( )]+)( (.+))*.$" regexp/newline))
        (ctl-A-rx (make-regexp (make-string 1 #\soh)))) ; control-A
    (map (lambda (file)
           (let* ((all '())
                  (s (slurp file))
                  (len (string-length s)))
             (let loop ((m (regexp-exec rx s 0)))
               (and m (let* ((start (1+ (cdr (array-ref m 1))))
                             (end (cond ((regexp-exec ctl-A-rx s start)
                                         => (lambda (e-m)
                                              (car
                                               (array-ref e-m 1)))))))
                        (set! all (cons (list
                                         (string->symbol
                                          (match:substring m 1))
                                         (or (match:substring m 3) "")
                                         start end)
                                        all))
                        (loop (regexp-exec rx s end)))))
             (cons file (reverse all))))
         files)))

(define (make-twerp2texi-index/qop qop)
  (write (make-twerp2texi-index (qop '()))
         (or (qop 'output-file open-output-file)
             (current-output-port))))

(define (main args)
  (HVQC-MAIN args make-twerp2texi-index/qop
             '(usage . commentary)
             '(package . "Guile")
             '(option-spec (output-file (single-char #\o)
                                        (value #t)))))

;;; make-twerp2texi-index ends here
