-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
check-syntax.rkt
201 lines (192 loc) · 8.7 KB
/
check-syntax.rkt
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
#lang racket/base
(require drracket/check-syntax
racket/class
racket/contract/base
racket/match
racket/set
racket/logging
racket/list
racket/string
syntax/modread
racket/sandbox
setup/path-to-relative
"editor.rkt"
"responses.rkt"
"interfaces.rkt"
"autocomplete.rkt"
"doc-trace.rkt")
(define ((error-diagnostics doc-text) exn)
(define msg (exn-message exn))
(cond
;; typed racket support: don't report error summaries
[(string-prefix? msg "Type Checker: Summary") (list)]
[(exn:fail:resource? exn)
(list (Diagnostic #:range (Range #:start (Pos #:line 0 #:char 0)
#:end (Pos #:line 0 #:char 0))
#:severity Diag-Hint
#:source "Expander"
#:message "the expand time has exceeded the 90s limit.\
Check if your macro is infinitely expanding"))]
[(and (exn:fail:read? exn)
;; Looking for the pattern '... version mismatch ... .zo ... raco setup ...'
(regexp-match? #rx"version mismatch.*\\.zo.*raco setup" msg))
(define maybe-error-source-mtchs/f
(regexp-match #rx"in: (.*\\.zo)" msg))
(define maybe-lib-error-source/f
(and maybe-error-source-mtchs/f
(path->relative-string/library (list-ref maybe-error-source-mtchs/f 1))))
(define expanded-msg
(cond
[maybe-lib-error-source/f
(format (string-append
"Failed to load file because it is compiled by a different version of Racket.\n"
" file: ~a\n"
" suggestion: ~a\n"
" complete message:\n\n ~a")
maybe-lib-error-source/f
(cond
[(regexp-match? #rx"<[a-z-]+>/" maybe-lib-error-source/f)
"run `raco setup` to recompile the libraries."]
[(regexp-match #rx"^(.*)compiled/([^/]+)_([^_]+)\\.zo$" maybe-lib-error-source/f)
=> (match-lambda
[(list whole-str base-path filename file-extension)
(format "run `raco make '~a~a.~a'` to recompile it."
base-path
filename
file-extension)])]
[else
(format "try running `raco setup` or `raco make <file>`.")])
msg)]
[else msg]))
;; stub range -- see the comments in exn:missing-module?
(list (Diagnostic #:range (Range #:start (Pos #:line 0 #:char 0)
#:end (Pos #:line 0 #:char 0))
#:severity Diag-Error
#:source "Racket"
#:message expanded-msg))]
[(exn:srclocs? exn)
(define srclocs ((exn:srclocs-accessor exn) exn))
(for/list ([sl (in-list srclocs)])
(match-define (srcloc src line col pos span) sl)
(if (and (number? line) (number? col) (number? span))
(Diagnostic #:range (Range #:start (Pos #:line (sub1 line) #:char col)
#:end (Pos #:line (sub1 line) #:char (+ col span)))
#:severity Diag-Error
#:source "Racket"
#:message msg)
;; Some reader exceptions don't report a position
;; Use end of file as a reasonable guess
(let ([end-of-file (abs-pos->Pos doc-text (send doc-text end-pos))])
(Diagnostic #:range (Range #:start end-of-file
#:end end-of-file)
#:severity Diag-Error
#:source "Racket"
#:message msg))))]
[(exn:missing-module? exn)
;; Hack:
;; We do not have any source location for the offending `require`, but the language
;; server protocol requires a valid range object. So we punt and just highlight the
;; first character.
;; This is very close to DrRacket's behavior: it also has no source location to work with,
;; however it simply doesn't highlight any code.
(define silly-range
(Range #:start (Pos #:line 0 #:char 0) #:end (Pos #:line 0 #:char 0)))
(list (Diagnostic #:range silly-range
#:severity Diag-Error
#:source "Racket"
#:message msg))]
[else (error 'error-diagnostics "unexpected failure: ~a" exn)]))
(define (check-typed-racket-log doc-text log)
(match-define (vector _ msg data _) log)
(when (and (list? data) (not (empty? data)) (syntax? (car data)))
(define prop (syntax-property (car data) 'mouse-over-tooltips))
(when (and prop (list? prop) (not (empty? prop)))
(define-values (start end msg)
(match prop
[(list (vector _ start _ _) (vector _ _ end msg))
(values start end msg)]
[(list (vector _ start end msg))
(values start end msg)]
[else (values #f #f #f)]))
(when (string? msg)
(list (Diagnostic #:range (Range #:start (abs-pos->Pos doc-text start) #:end (abs-pos->Pos doc-text end))
#:severity Diag-Error
#:source "Typed Racket"
#:message msg))))))
(define (get-indenter doc-text)
(define text (send doc-text get-text))
(define lang-info
(with-handlers ([exn:fail:read? (lambda (e) 'missing)]
[exn:missing-module? (lambda (e) #f)])
(read-language (open-input-string text) (lambda () 'missing))))
(cond
[(procedure? lang-info)
(lang-info 'drracket:indentation #f)]
[(eq? lang-info 'missing)
; check for a #reader directive at start of file, ignoring comments
; the ^ anchor here matches start-of-string, not start-of-line
(if (regexp-match #rx"^(;[^\n]*\n)*#reader" text)
#f ; most likely a drracket file, use default indentation
; (https://github.com/jeapostrophe/racket-langserver/issues/86)
'missing)]
[else #f]))
(define-syntax-rule (timeout time-sec body)
(with-limits time-sec #f body))
(define (check-syntax src doc-text trace)
(define indenter (get-indenter doc-text))
(define ns (make-base-namespace))
(define new-trace (new build-trace% [src src] [doc-text doc-text] [indenter indenter]))
(match-define-values (src-dir _ #f)
(split-path src))
(define-values (add-syntax done)
(make-traversal ns src-dir))
;; Rewind input port and read syntax
(define text (send doc-text get-text))
(define in (open-input-string text))
(port-count-lines! in)
(when trace
(set-clear! (send trace get-warn-diags)))
(define valid #f)
(define lang-diag
(if (eq? indenter 'missing)
(list (Diagnostic #:range (Range #:start (Pos #:line 0 #:char 0) #:end (Pos #:line 0 #:char 0))
#:severity Diag-Error
#:source "Racket"
#:message "Missing or invalid #lang line"))
(list)))
(define diags (list))
(define err-diags
(parameterize ([current-annotations new-trace]
[current-namespace ns]
[current-load-relative-directory src-dir])
(with-intercepted-logging
(lambda (l)
(define result (check-typed-racket-log doc-text l))
(when (list? result) (set! diags (append result diags))))
(lambda ()
(with-handlers ([(or/c exn:fail:read?
exn:fail:syntax?
exn:fail:filesystem?
exn:fail:resource?)
(error-diagnostics doc-text)])
(define original-stx (with-module-reading-parameterization
(λ () (read-syntax src in))))
;; 90 seconds limit for possible infinity recursive macro expand
(define stx (timeout 90 (expand original-stx)))
(define completions (append (set->list (walk stx)) (set->list (walk-module stx))))
(send new-trace set-completions completions)
(when trace
(send trace set-completions completions))
(add-syntax stx)
(set! valid #t)
(done)
(list)))
'info)))
(define warn-diags (send new-trace get-warn-diags))
;; reuse old trace if check-syntax failed
(list (if valid new-trace (or trace new-trace))
(append err-diags (set->list warn-diags) lang-diag diags)))
(provide
(contract-out
[check-syntax (-> any/c (is-a?/c lsp-editor%) (or/c #f (is-a?/c build-trace%))
(list/c (is-a?/c build-trace%) any/c))]))