summaryrefslogtreecommitdiff
path: root/scheme/racket/geiser/autodoc.rkt
blob: 6e73271bd203260cbbfd96f5e6e7cd1fce0b23a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
;;; autodoc.rkt -- suport for autodoc echo

;; Copyright (C) 2009, 2010 Jose Antonio Ortega Ruiz

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the Modified BSD License. You should
;; have received a copy of the license along with this program. If
;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.

;; Start date: Sun May 03, 2009 14:45

#lang racket

(provide autodoc
         symbol-documentation
         module-exports
         update-signature-cache
         get-help)

(require racket/help
         geiser/utils
         geiser/modules
         geiser/locations)

(define (get-help symbol mod)
  (if (eq? symbol mod)
      (get-mod-help mod)
      (with-handlers ([exn? (lambda (_)
                              (eval `(help ,symbol)))])
        (eval `(help ,symbol #:from ,(ensure-module-spec mod))))))

(define (get-mod-help mod)
  (let-values ([(ids syns) (module-identifiers mod)])
    (let ([sym (cond [(not (null? syns)) (car syns)]
                     [(not (null? ids)) (car ids)]
                     [else #f])])
      (and sym (get-help sym mod)))))

(define (symbol-documentation sym)
  (let* ([val (value sym (symbol-module sym))]
         [sign (autodoc* sym)])
    (and sign
         (list (cons "signature" (autodoc* sym #f))
               (cons "docstring" (docstring sym val sign))))))

(define (docstring sym val sign)
  (let* ([mod (assoc "module" (cdr sign))]
         [mod (if mod (cdr mod) "<unknown>")]
         [id (namespace-symbol->identifier sym)]
         [desc (if (identifier? id) (format "~%~%~a" (describe id sym)) "")])
    (if val
        (format "A ~a in module ~a.~a~a~a"
                (if (procedure? val) "procedure" "variable")
                mod
                (if (procedure? val)
                    ""
                    (format "~%~%Value:~%~%  ~a" val))
                (if (has-contract? val)
                    (format "~%~%Contract:~%~%  ~a"
                            (contract-name (value-contract val)))
                    "")
                desc)
        (format "An identifier in module ~a.~a" mod desc))))

;; Lifted from Eli's interactive.rkt
(define (describe id s)
  (define b (identifier-binding id))
  (cond
   [(not b) (format "`~s' is a toplevel (or unbound) identifier." s)]
   [(eq? b 'lexical) (format "`~s' is a lexical identifier." s)]
   [(or (not (list? b)) (not (= 7 (length b))))
    "*** internal error, racket changed ***"]
   [else
    (let-values ([(source-mod source-id
                   nominal-source-mod nominal-source-id
                   source-phase import-phase
                   nominal-export-phase)
                  (apply values b)])
      (let ([aliased (not (eq? s source-id))]
            [for-syn (eqv? source-phase 1)]
            [amod (not (equal? source-mod nominal-source-mod))]
            [aid (not (eq? s nominal-source-id))])
        (if (or aliased for-syn amod aid)
            (string-append
             "Defined"
             (if for-syn " for syntax" "")
             (if aliased (format " as `~s' " source-id) "")
             (if amod
                 (format " in module ~a\nand required~a in module ~a"
                         (module-path-index->name source-mod)
                         (if (eqv? import-phase 1) "-for-syntax" "")
                         (module-path-index->name nominal-source-mod))
                 "")
             (if aid
                 (format ",\nwhere it is defined as `~s'" nominal-source-id)
                 "")
             ".")
            "")))]))

(define (value id mod)
  (with-handlers ([exn? (const #f)])
    (dynamic-require mod id (const #f))))

(define (autodoc ids)
  (if (not (list? ids))
      '()
      (map (lambda (id) (or (autodoc* id) (list id))) ids)))

(define (autodoc* id (extra #t))
  (define (val)
    (with-handlers ([exn? (const "")])
      (parameterize ([error-print-width 60])
        (format "~.a" (namespace-variable-value id)))))
  (and
   (symbol? id)
   (let* ([loc (symbol-location* id)]
          [name (car loc)]
          [path (cdr loc)]
          [sgns (and path (find-signatures path name id))]
          [value (if (and extra sgns (not (list? sgns)))
                     (list (cons "value" (val)))
                     '())]
          [mod (if (and extra sgns path)
                   (list (cons "module"
                               (module-path-name->name path)))
                   '())])
     (and sgns
          `(,id
            ("name" . ,name)
            ("args" ,@(if (list? sgns) (map format-signature sgns) '()))
            ,@value
            ,@mod)))))

(define (format-signature sign)
  (if (signature? sign)
      `(("required" ,@(signature-required sign))
        ("optional" ,@(signature-optional sign)
         ,@(let ((rest (signature-rest sign)))
             (if rest (list "...") '())))
        ("key" ,@(signature-keys sign)))
      '()))

(define signatures (make-hash))

(struct signature (required optional keys rest))

(define (find-signatures path name local-name)
  (let ([path (if (path? path) (path->string path) path)])
    (hash-ref! (hash-ref! signatures
                          path
                          (lambda () (parse-signatures path)))
               name
               (lambda () (infer-signatures local-name)))))

(define (parse-signatures path)
  (let ([result (make-hasheq)])
    (with-handlers ([exn? (lambda (e) result)])
      (with-input-from-file path
        (lambda ()
          (parameterize ([read-accept-reader #t])
            (let loop ([stx (read-syntax path)])
              (cond [(eof-object? stx) void]
                    [(syntax->datum stx) =>
                     (lambda (datum)
                       (parse-datum! datum result)
                       (loop (read-syntax path)))]
                    [else void]))))))
    result))

(define (parse-datum! datum store)
  (with-handlers ([exn? (lambda (_) void)])
    (match datum
      [`(module ,name ,lang (#%module-begin . ,forms))
       (for-each (lambda (f) (parse-datum! f store)) forms)]
      [`(module ,name ,lang . ,forms)
       (for-each (lambda (f) (parse-datum! f store)) forms)]
      [`(define ((,name . ,formals) . ,_) . ,_)
       (add-signature! name formals store)]
      [`(define (,name . ,formals) . ,_)
       (add-signature! name formals store)]
      [`(define ,name (lambda ,formals . ,_))
       (add-signature! name formals store)]
      [`(define ,name (case-lambda ,clauses ...))
       (for-each (lambda (c) (add-signature! name (car c) store))
                 (reverse clauses))]
      [`(,(or 'struct 'define-struct) ,name ,(? symbol? _)
         ,(list formals ...) . ,_)
       (add-signature! name formals store)]
      [`(,(or 'struct 'define-struct) ,name ,(list formals ...) . ,_)
       (add-signature! name formals store)]
      [`(define-for-syntax (,name . ,formals) . ,_)
       (add-signature! name formals store)]
      [`(define-for-syntax ,name (lambda ,formals . ,_))
       (add-signature! name formals store)]
      [`(define-syntax-rule (,name . ,formals) . ,_)
       (add-signature! name formals store)]
      [`(define-syntax ,name (syntax-rules ,specials . ,clauses))
       (for-each (lambda (c) (add-syntax-signature! name (cdar c) store))
                 (reverse clauses))]
      [`(define-syntax ,name (lambda ,_ (syntax-case ,_ . ,clauses)))
       (for-each (lambda (c) (add-syntax-signature! name (cdar c) store))
                 (reverse clauses))]
      [_ void])))

(define (add-signature! name formals store)
  (when (symbol? name)
    (hash-set! store
               name
               (cons (parse-formals formals)
                     (hash-ref store name '())))))

(define (add-syntax-signature! name formals store)
  (when (symbol? name)
    (hash-set! store
               name
               (cons (signature formals '() '() #f)
                     (hash-ref store name '())))))

(define (parse-formals formals)
  (let loop ([formals formals] [req '()] [opt '()] [keys '()])
    (cond [(null? formals)
           (signature (reverse req) (reverse opt) (reverse keys) #f)]
          [(symbol? formals)
           (signature (reverse req) (reverse opt) (reverse keys) formals)]
          [(pair? (car formals)) (loop (cdr formals)
                                       req
                                       (cons (car formals) opt)
                                       keys)]
          [(keyword? (car formals)) (let* ((kname (car formals))
                                           (arg-id (cadr formals))
                                           (name (if (pair? arg-id)
                                                     (list kname
                                                           (cadr arg-id))
                                                     (list kname))))
                                      (loop (cddr formals)
                                            req
                                            opt
                                            (cons name keys)))]
          [else (loop (cdr formals) (cons (car formals) req) opt keys)])))

(define (infer-signatures name)
  (with-handlers ([exn:fail:syntax? (const `(,(signature '(...) '() '() #f)))]
                  [exn:fail:contract:variable? (const #f)])
    (let ([v (namespace-variable-value name)])
      (if (procedure? v)
          (arity->signatures (procedure-arity v))
          'variable))))

(define (arity->signatures arity)
  (define (args count) (build-list count (const '_)))
  (define (arity->signature arity)
    (cond [(number? arity)
           (signature (args arity) '() '() #f)]
          [(arity-at-least? arity)
           (signature (args (arity-at-least-value arity)) '() '() 'rest)]))
  (define (conseq? lst)
    (cond [(< (length lst) 2) (number? (car lst))]
          [(and (number? (car lst))
                (number? (cadr lst))
                (eqv? (+ 1 (car lst)) (cadr lst)))
           (conseq? (cdr lst))]
          [else #f]))
  (cond [(and (list? arity) (conseq? arity))
         (let ((mi (apply min arity))
               (ma (apply max arity)))
           (list (signature (args mi) (args (- ma mi)) '() #f)))]
        [(list? arity) (map arity->signature arity)]
        [else (list (arity->signature arity))]))

(define (update-signature-cache path (form #f))
  (when (and (string? path)
             (or (not form)
                 (and (list? form)
                      (not (null? form))
                      (memq (car form)
                            '(define-syntax-rule struct
                               define-syntax define set! define-struct)))))
    (hash-remove! signatures path)))

(define (module-exports mod)
  (define (contracted id)
    (let ([v (value id mod)])
      (if (has-contract? v)
          (list id (cons "info" (contract-name (value-contract v))))
          (entry id))))
  (define (entry id)
    (let ((sign (eval `(,autodoc* ',id #f)
                      (module-spec->namespace mod #f #f))))
      (if sign (list id (cons "signature" sign)) (list id))))
  (define (classify-ids ids)
    (let loop ([ids ids] [procs '()] [vars '()])
      (cond [(null? ids)
             `(("procs" ,@(map entry (reverse procs)))
               ("vars" ,@(map list (reverse vars))))]
            [(procedure? (value (car ids) mod))
             (loop (cdr ids) (cons (car ids) procs) vars)]
            [else (loop (cdr ids) procs (cons (car ids) vars))])))
  (let-values ([(ids syn) (module-identifiers mod)])
    `(,@(classify-ids ids)
      ("syntax" ,@(map contracted syn))
      ("modules" ,@(map list (or (submodules mod) '()))))))