| 1 |
;;; |
| 2 |
;;; tools/make-export-list |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2023 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(use gauche.parseopt) |
| 35 |
(use util.match) |
| 36 |
|
| 37 |
;; List toplevel bindings in the given module---useful to create an export list. |
| 38 |
;; EXCLUDE-PREDICATES is a list of predicates to exclude names. Each predicate |
| 39 |
;; takes a symbol. You can pass a regexp as a predicate, though, in that |
| 40 |
;; case the symbol is converted to a string to match the predicate. |
| 41 |
|
| 42 |
(define (list-toplevel-bindings mod-or-name :optional (exclude-predicates '())) |
| 43 |
(define mod |
| 44 |
(cond [(module? mod-or-name) mod] |
| 45 |
[(symbol? mod-or-name) |
| 46 |
(or (find-module mod-or-name) |
| 47 |
(error "Don't know such module:" mod-or-name))] |
| 48 |
[else (error "Module or a symbol module name is expected, but got:" |
| 49 |
mod-or-name)])) |
| 50 |
(define (make-pred pred) |
| 51 |
(if (regexp? pred) |
| 52 |
(^y (rxmatch pred (x->string y))) |
| 53 |
pred)) |
| 54 |
|
| 55 |
($ sort $ remove (apply any-pred (map make-pred exclude-predicates)) |
| 56 |
$ hash-table-keys $ module-table mod)) |
| 57 |
|
| 58 |
(define (usage) |
| 59 |
(print "Usage: gosh tools/make-export-list [-x regexp] module\ |
| 60 |
\n Loads the given module and lists its toplevel bindings, excluding \ |
| 61 |
\n the ones that matches REGEXP. \ |
| 62 |
\n For example, -x '^%' excludes symbols beginning with '%'.\ |
| 63 |
\n Note that the specified module is actually loaded into the process.") |
| 64 |
(exit 1)) |
| 65 |
|
| 66 |
(define (main args) |
| 67 |
(define rxs '()) |
| 68 |
(let-args (cdr args) ([#f "x=s" => (^x (push! rxs (string->regexp x)))] |
| 69 |
[else => (^ _ (usage))] |
| 70 |
. rest) |
| 71 |
(match rest |
| 72 |
[() (usage)] |
| 73 |
[(module) |
| 74 |
(let1 modsym (string->symbol module) |
| 75 |
(eval `(require ,(module-name->path modsym)) |
| 76 |
(find-module 'user)) |
| 77 |
;; We limit width to 65, so that it can be easily cut&pasted |
| 78 |
;; to the 'export' list. |
| 79 |
(pprint (list-toplevel-bindings modsym rxs) :width 65))] |
| 80 |
[_ (usage)])) |
| 81 |
0) |
| 82 |
|
| 83 |
;; Local variables: |
| 84 |
;; mode: scheme |
| 85 |
;; end: |