forked from coalton-lang/coalton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.lisp
273 lines (245 loc) · 12.1 KB
/
reader.lisp
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
(defpackage #:coalton-impl/reader
(:use
#:cl)
(:local-nicknames
(#:cst #:concrete-syntax-tree)
(#:settings #:coalton-impl/settings)
(#:util #:coalton-impl/util)
(#:error #:coalton-impl/error)
(#:parser #:coalton-impl/parser)
(#:tc #:coalton-impl/typechecker)
(#:entry #:coalton-impl/entry)))
(in-package #:coalton-impl/reader)
(defvar *coalton-reader-allowed* t
"Is the Coalton reader allowed to parse the current input?
Used to forbid reading while inside quasiquoted forms.")
(defun read-coalton-toplevel-open-paren (stream char)
(declare (optimize (debug 2)))
(unless *coalton-reader-allowed*
(return-from read-coalton-toplevel-open-paren
(funcall (get-macro-character #\( (named-readtables:ensure-readtable :standard)) stream char)))
(parser:with-reader-context stream
(let ((first-form
(multiple-value-bind (form presentp)
(parser:maybe-read-form stream)
(unless presentp
(return-from read-coalton-toplevel-open-paren
nil))
form)))
(case (cst:raw first-form)
(coalton:coalton-toplevel
(let ((opened-streams nil))
(unwind-protect
(let* ((pathname (or *compile-file-truename* *load-truename*))
(filename (if pathname (namestring pathname) "<unknown>"))
(file-input-stream
(cond
((or #+sbcl (sb-int:form-tracking-stream-p stream)
nil)
(let ((s (open (pathname stream))))
(push s opened-streams)
s))
(t
stream)))
(file (error:make-coalton-file :stream file-input-stream :name filename)))
(handler-bind
;; Render errors and set highlights
((error:coalton-base-error
(lambda (c)
(set-highlight-position-for-error stream (funcall (error:coalton-error-err c)))
(error:render-coalton-error c)))
(error:coalton-base-warning
(lambda (c)
(error:render-coalton-warning c))))
(multiple-value-bind (program env)
(entry:entry-point (parser:read-program stream file :mode :toplevel-macro))
(setf entry:*global-environment* env)
program)))
;; Clean up any opened file streams
(dolist (s opened-streams)
(close s)))))
(coalton:coalton-codegen
(let ((opened-streams nil))
(unwind-protect
(let* ((pathname (or *compile-file-truename* *load-truename*))
(filename (if pathname (namestring pathname) "<unknown>"))
(file-input-stream
(cond
((or #+sbcl (sb-int:form-tracking-stream-p stream)
nil)
(let ((s (open (pathname stream))))
(push s opened-streams)
s))
(t
stream)))
(file (error:make-coalton-file :stream file-input-stream :name filename)))
(handler-bind
;; Render errors and set highlights
((error:coalton-base-error
(lambda (c)
(set-highlight-position-for-error stream (funcall (error:coalton-error-err c)))
(error:render-coalton-error c)))
(error:coalton-base-warning
(lambda (c)
(error:render-coalton-warning c))))
(let ((settings:*coalton-skip-update* t)
(settings:*emit-type-annotations* nil))
(multiple-value-bind (program env)
(entry:entry-point (parser:read-program stream file :mode :toplevel-macro))
(declare (ignore env))
`',program))))
;; Clean up any opened file streams
(dolist (s opened-streams)
(close s)))))
(coalton:coalton-codegen-ast
(let ((opened-streams nil))
(unwind-protect
(let* ((pathname (or *compile-file-truename* *load-truename*))
(filename (if pathname (namestring pathname) "<unknown>"))
(file-input-stream
(cond
((or #+sbcl (sb-int:form-tracking-stream-p stream)
nil)
(let ((s (open (pathname stream))))
(push s opened-streams)
s))
(t
stream)))
(file (error:make-coalton-file :stream file-input-stream :name filename)))
(handler-bind
;; Render errors and set highlights
((error:coalton-base-error
(lambda (c)
(set-highlight-position-for-error stream (funcall (error:coalton-error-err c)))
(error:render-coalton-error c)))
(error:coalton-base-warning
(lambda (c)
(error:render-coalton-warning c))))
(let ((settings:*coalton-skip-update* t)
(settings:*emit-type-annotations* nil)
(settings:*coalton-dump-ast* t))
(multiple-value-bind (program env)
(entry:entry-point (parser:read-program stream file :mode :toplevel-macro))
(declare (ignore program env))
nil))))
;; Clean up any opened file streams
(dolist (s opened-streams)
(close s)))))
(coalton:coalton
(let ((opened-streams nil))
(unwind-protect
(let* ((pathname (or *compile-file-truename* *load-truename*))
(filename (if pathname (namestring pathname) "<unknown>"))
(file-input-stream
(cond
((or #+sbcl (sb-int:form-tracking-stream-p stream)
nil)
(let ((s (open (pathname stream))))
(push s opened-streams)
s))
(t
stream)))
(file (error:make-coalton-file :stream file-input-stream :name filename)))
(handler-bind
;; Render errors and set highlights
((error:coalton-base-error
(lambda (c)
(set-highlight-position-for-error stream (funcall (error:coalton-error-err c)))
(error:render-coalton-error c)))
(error:coalton-base-warning
(lambda (c)
(error:render-coalton-warning c))))
(entry:expression-entry-point (parser:read-expression stream file) file)))
;; Clean up any opened file streams
(dolist (s opened-streams)
(close s)))))
;; Fall back to reading the list manually
(t
(let ((collected-forms (list (cst:raw first-form)))
(dotted-context nil))
(loop :do
(handler-case
(multiple-value-bind (form presentp)
(parser:maybe-read-form stream)
(cond
((and (not presentp)
dotted-context)
(error "Invalid dotted list"))
((not presentp)
(return-from read-coalton-toplevel-open-paren
(nreverse collected-forms)))
(dotted-context
(when (nth-value 1 (parser:maybe-read-form stream))
(error "Invalid dotted list"))
(return-from read-coalton-toplevel-open-paren
(nreconc collected-forms (cst:raw form))))
(t
(push (cst:raw form) collected-forms))))
(eclector.reader:invalid-context-for-consing-dot (c)
(when dotted-context
(error "Invalid dotted list"))
(setf dotted-context t)
(eclector.reader:recover c))))))))))
(defun set-highlight-position-for-error (stream error)
"Set the highlight position within the editor using implementation specific magic."
#+sbcl
;; We need some way of setting FILE-POSITION so that
;; when Slime grabs the location of the error it
;; highlights the correct form.
;;
;; In SBCL, we can't unread more than ~512
;; characters due to limitations with ANSI streams,
;; which breaks our old method of unreading
;; characters until the file position is
;; correct. Instead, we now patch in our own
;; version of FILE-POSITION.
;;
;; This is a massive hack and might start breaking
;; with future changes in SBCL.
(when (typep stream 'sb-impl::form-tracking-stream)
(let* ((file-offset
(- (sb-impl::fd-stream-get-file-position stream)
(file-position stream)))
(loc (error:coalton-error-location error)))
(setf (sb-impl::fd-stream-misc stream)
(lambda (stream operation arg1)
(if (= (sb-impl::%stream-opcode :get-file-position) operation)
(+ file-offset loc 1)
(sb-impl::tracking-stream-misc stream operation arg1)))))))
(named-readtables:defreadtable coalton:coalton
(:merge :standard)
(:macro-char #\( #'read-coalton-toplevel-open-paren)
(:macro-char #\` (lambda (s c)
(let ((*coalton-reader-allowed* nil))
(funcall (get-macro-character #\` (named-readtables:ensure-readtable :standard)) s c))))
(:macro-char #\, (lambda (s c)
(let ((*coalton-reader-allowed* t))
(funcall (get-macro-character #\, (named-readtables:ensure-readtable :standard)) s c)))))
(defmacro coalton:coalton-toplevel (&body forms)
(let ((*readtable* (named-readtables:ensure-readtable 'coalton:coalton))
(*compile-file-truename*
(pathname (format nil "COALTON-TOPLEVEL (~A)" *compile-file-truename*)))
(*print-circle* t))
(with-input-from-string (stream (cl:format cl:nil "~S" (cons 'coalton:coalton-toplevel forms)))
(cl:read stream))))
(defmacro coalton:coalton-codegen (&body forms)
(let ((*readtable* (named-readtables:ensure-readtable 'coalton:coalton))
(*compile-file-truename*
(pathname (format nil "COALTON-TOPLEVEL (~A)" *compile-file-truename*)))
(*print-circle* t))
(with-input-from-string (stream (cl:format cl:nil "~S" (cons 'coalton:coalton-codegen forms)))
(cl:read stream))))
(defmacro coalton:coalton-codegen-ast (&body forms)
(let ((*readtable* (named-readtables:ensure-readtable 'coalton:coalton))
(*compile-file-truename*
(pathname (format nil "COALTON-TOPLEVEL (~A)" *compile-file-truename*)))
(*print-circle* t))
(with-input-from-string (stream (cl:format cl:nil "~S" (cons 'coalton:coalton-codegen-ast forms)))
(cl:read stream))))
(defmacro coalton:coalton (&rest forms)
(let ((*readtable* (named-readtables:ensure-readtable 'coalton:coalton))
(*compile-file-truename*
(pathname (format nil "COALTON (~A)" *compile-file-truename*)))
(*print-circle* t))
(with-input-from-string (stream (cl:format cl:nil "~S" (cons 'coalton:coalton forms)))
(cl:read stream))))