[plt-scheme] inspecting modules

From: John Clements (clements at brinckerhoff.org)
Date: Mon Nov 3 15:11:08 EST 2003

On Monday, November 3, 2003, at 11:52  AM, MicheleSimionato at libero.it 

>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> Is there a command to retrieve the list of variables
> exported by a module? Something working like this:
>  (variables-exported-from mymod) => list of symbols

Eli Barzilay recently posted a function that would do this, given a 
file containing a module definition.  That's different from what you 
asked for, but might suffice:

(define (module-walk file)
   (let ((s (expand (parameterize ((port-count-lines-enabled #t))
                      (with-input-from-file file
                        (lambda () (read-syntax file))))))
         (name     #f)
         (lang     #f)
         (requires  (list 'requires))
         (srequires (list 'requires-for-syntax))
         (provides  (list 'provides))
         (variables (list 'bindings))
         (syntaxes  (list 'syntaxes)))
     (define (add-to l1 l2)
       (set! l1 (append! l1 (map (lambda (x) x) (syntax->list l2)))))
     (define (inspect stxs)
       (unless (null? stxs)
         (syntax-case (car stxs)
             (require require-for-syntax provide define-values 
           ((require . stuff)            (add-to requires  #'stuff))
           ((require-for-syntax . stuff) (add-to srequires #'stuff))
           ((provide . stuff)            (add-to provides  #'stuff))
           ((define-values   ids _)      (add-to variables #'ids))
           ((define-syntaxes ids _)      (add-to syntaxes  #'ids))
           (_ (error 'module-walk "unknown module expression")))
         (inspect (cdr stxs))))
     (syntax-case s (module #%plain-module-begin)
       ((module mname mlang (#%plain-module-begin . mbody))
        (begin (set! name #'mname) (set! lang #'mlang)
               (inspect (syntax->list #'mbody))))
       (_ (error 'module-walk "weird module body")))
     (let* ((* (lambda (x)
                 (list (syntax-object->datum x)
                       (syntax-source x)
                       (syntax-position x)
                       (syntax-line x)
                       (syntax-column x))))
            (** (lambda (l) (cons (car l) (map * (cdr l))))))
       (list (* name) (* lang) (** requires) (** srequires) (** provides)
             (** variables) (** syntaxes)))))

Actually, looking at this harder, I'm surprised that there's no 'begin' 
clause in the 'inspect' loop.  I believe this would be needed.


Posted on the users mailing list.