Skip to content
This repository was archived by the owner on Sep 13, 2019. It is now read-only.

Commit 32b274d

Browse files
committed
make syntax/moddep more useful
This tool seems misplaced, but improve it a little to be practical for larger module hierarchies.
1 parent 11e81f8 commit 32b274d

File tree

2 files changed

+61
-23
lines changed

2 files changed

+61
-23
lines changed

pkgs/racket-doc/syntax/scribblings/moddep.scrbl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,20 @@ Re-exports @racketmodname[syntax/modread],
99
@racketmodname[syntax/modcode], @racketmodname[syntax/modcollapse],
1010
and @racketmodname[syntax/modresolve], in addition to the following:
1111

12-
@defproc[(show-import-tree [module-path-v module-path?]) void?]{
12+
@defproc[(show-import-tree [module-path-v module-path?]
13+
[#:dag? dag? any/c #f]
14+
[#:path-to path-to-module-path-v (or/c #f module-path?) #f])
15+
void?]{
1316

1417
A debugging aid that prints the import hierarchy starting from a given
15-
module path.}
18+
module path.
19+
20+
If @racket[dag?] is true, then a module is printed only the first time
21+
is encountered in the hierarchy.
22+
23+
If @racket[path-to-module-path-v] is a module path, then only the
24+
spines of the tree that reach @racket[path-to-module-path-v] are
25+
shown.
26+
27+
@history[#:changed "6.12.0.4" @elem{Added the @racket[#:dag?] and
28+
@racket[#:path-to] arguments.}]}

racket/collects/syntax/moddep.rkt

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,49 @@
1010
(all-from-out "modresolve.rkt")
1111
show-import-tree)
1212

13-
(define (show-import-tree module-path)
14-
(let loop ([path (resolve-module-path module-path #f)][indent ""][fs ""])
15-
(printf "~a~a~a\n" indent path fs)
16-
(let ([code (get-module-code path)])
17-
(let ([imports (module-compiled-imports code)])
18-
(define ((mk-loop fs) i)
19-
(let ([p (resolve-module-path-index i path)])
20-
(unless (symbol? p)
21-
(loop p
22-
(format " ~a" indent)
23-
fs))))
24-
(for-each (lambda (i)
25-
(for-each
26-
(mk-loop (case (car i)
27-
[(0) ""]
28-
[(1) " [for-syntax]"]
29-
[(-1) " [for-syntax]"]
30-
[(#f) " [for-label]"]
31-
[else (format " [for-meta ~a]" (car i))]))
32-
(cdr i)))
33-
imports))))))
13+
(define (show-import-tree module-path
14+
#:dag? [dag? #f]
15+
#:path-to [given-path-to #f])
16+
(define path-to (and given-path-to (simplify-path (resolve-module-path given-path-to #f))))
17+
(define seen (and dag? (make-hash)))
18+
(let loop ([path (resolve-module-path module-path #f)] [indent ""] [fs ""] [phase 0] [accum '()])
19+
(unless path-to
20+
(printf "~a~a~a ~a\n" indent path fs phase))
21+
(when (equal? path-to path)
22+
(let ([accum (let loop ([accum (cons (list indent path fs phase) accum)])
23+
(cond
24+
[(null? accum) null]
25+
[(hash-ref seen accum #f) null]
26+
[else
27+
(hash-set! seen accum #t)
28+
(cons (car accum) (loop (cdr accum)))]))])
29+
(for ([i (in-list (reverse accum))])
30+
(apply printf "~a~a~a ~a\n" i))))
31+
(unless (and seen (hash-ref seen (cons path phase) #f))
32+
(when seen (hash-set! seen (cons path phase) #t))
33+
(define plain-path (if (pair? path) (cadr path) path))
34+
(let ([code (get-module-code plain-path
35+
#:submodule-path (if (pair? path) (cddr path) '()))])
36+
(let ([imports (module-compiled-imports code)]
37+
[accum (cons (list indent path fs phase) accum)])
38+
(define ((mk-loop phase-shift fs) i)
39+
(let ([p (resolve-module-path-index i plain-path)])
40+
(unless (symbol? p)
41+
(loop (if (path? p)
42+
(simplify-path p)
43+
(list* 'submod (simplify-path (cadr p)) (cddr p)))
44+
(format " ~a" indent)
45+
fs
46+
(and phase phase-shift (+ phase phase-shift))
47+
accum))))
48+
(for-each (lambda (i)
49+
(for-each
50+
(mk-loop (car i)
51+
(case (car i)
52+
[(0) ""]
53+
[(1) " [for-syntax]"]
54+
[(-1) " [for-template]"]
55+
[(#f) " [for-label]"]
56+
[else (format " [for-meta ~a]" (car i))]))
57+
(cdr i)))
58+
imports)))))))

0 commit comments

Comments
 (0)