| 1 |
#!/usr/bin/env ikarus --script |
| 2 |
(define get-comments |
| 3 |
(lambda (file-name) |
| 4 |
(define comments '()) |
| 5 |
(parameterize ([comment-handler |
| 6 |
(lambda (str) |
| 7 |
(set! comments (cons str comments)))]) |
| 8 |
(load file-name (lambda (x) (void)))) |
| 9 |
(reverse comments))) |
| 10 |
|
| 11 |
(define filter |
| 12 |
(lambda (f ls) |
| 13 |
(cond |
| 14 |
[(null? ls) '()] |
| 15 |
[(f (car ls)) (cons (car ls) (filter f (cdr ls)))] |
| 16 |
[else (filter f (cdr ls))]))) |
| 17 |
|
| 18 |
(define find-end |
| 19 |
(lambda (str i) |
| 20 |
(cond |
| 21 |
[(fx= i (string-length str)) i] |
| 22 |
[(memv (string-ref str i) '(#\newline #\space #\tab)) i] |
| 23 |
[(char=? (string-ref str i) #\:) i] |
| 24 |
[else (find-end str (fxadd1 i))]))) |
| 25 |
|
| 26 |
(define matcher |
| 27 |
(lambda (tok) |
| 28 |
(let ([n (string-length tok)]) |
| 29 |
(lambda (str) |
| 30 |
(let f ([i 0]) |
| 31 |
(let ([j (find-end str i)]) |
| 32 |
(or (string=? tok (substring str i j)) |
| 33 |
(and (fx< j (string-length str)) |
| 34 |
(char=? (string-ref str j) #\:) |
| 35 |
(f (fxadd1 j)))))))))) |
| 36 |
|
| 37 |
(define get-matching |
| 38 |
(lambda (token file-name) |
| 39 |
(filter (matcher token) (get-comments file-name)))) |
| 40 |
|
| 41 |
#|Usage: |
| 42 |
find.ss token files ... |
| 43 |
Prints all the comments matching Hash-Bar-token that are found |
| 44 |
in the files. |# |
| 45 |
(define usage |
| 46 |
(lambda () |
| 47 |
(printf "Usage: ikarusdoc token files ...\n") |
| 48 |
(printf " Prints all multiline comments matching #|token<comment>|# from the files\n") |
| 49 |
(exit 0))) |
| 50 |
|
| 51 |
(define print-comments |
| 52 |
(lambda (file ls) |
| 53 |
(unless (null? ls) |
| 54 |
(printf "From ~a:\n" file) |
| 55 |
(for-each |
| 56 |
(lambda (x) |
| 57 |
(display x) |
| 58 |
(newline)) |
| 59 |
ls)))) |
| 60 |
|
| 61 |
(let ([args (command-line-arguments)]) |
| 62 |
(when (null? args) (usage)) |
| 63 |
(let ([me (car args)] [args (cdr args)]) |
| 64 |
(when (null? args) (usage)) |
| 65 |
(let ([tok (car args)] [files (cdr args)]) |
| 66 |
(for-each |
| 67 |
(lambda (file) |
| 68 |
(print-comments file (get-matching tok file))) |
| 69 |
files)))) |
| 70 |
|
| 71 |
;;; vim:syntax=scheme |