-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathreader.cljd
562 lines (517 loc) · 23.8 KB
/
reader.cljd
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
(ns cljd.reader
(:require ["dart:io" :as io]
["dart:async" :as async]))
(deftype ReaderInput [^#/(async/Stream String) in
^:mutable ^#/(async/StreamSubscription? String) subscription
^:mutable ^#/(async/Completer? String?) completer
^:mutable ^String? buffer]
(^void init_stream_subscription [this]
(set! subscription
(doto (.listen in
(fn [^String s]
(assert (nil? buffer))
(when-not (== "" s)
(.complete (.-completer this) s)
(.pause (.-subscription this))))
.onDone
(fn []
(assert (nil? buffer))
(.complete (.-completer this) nil)
(set! (.-subscription this) nil)
nil))
(.pause)))
nil)
(^#/(async/FutureOr String?) read [this]
(when-not (nil? subscription)
(if-some [buf buffer]
(do (set! buffer nil)
(.value #/(async/Future String?) buf))
(do (set! completer (async/Completer.))
(.resume subscription)
(.-future completer)))))
(^void unread [this ^String s]
(assert (nil? buffer))
(assert (not (nil? subscription)))
(set! buffer (when-not (== "" s) s))
nil)
#_(^#/(Future void) ^:async close [this]
(when-some [sub subscription]
(await (.cancel sub))
(set! subscription nil)
nil)))
(defn ^ReaderInput make-reader-input [^#/(async/StreamController String) controller]
(doto (ReaderInput. (.-stream controller) nil nil nil) (.init_stream_subscription)))
(defn- saget
{:inline (fn [s idx] (list '. `^String ~s "[]" `^int ~idx))
:inline-arities #{2}}
([^String s ^int idx]
(. s "[]" idx))
([^String s ^int idx & idxs]
(apply saget (saget s idx) idxs)))
;; read-* functions
(declare ^:async ^:dart read)
(defn ^int cu0 [^String ch] (.codeUnitAt ch 0))
(defn ^#/(Future cljd.core/PersistentList) ^:async read-list [^ReaderInput rdr]
(let [result #dart[]]
(loop []
(let [val (await (read rdr (cu0 ")")))]
(if (== val rdr)
(-list-lit result)
(do (.add result val)
(recur)))))))
(defn ^#/(Future cljd.core/PersistentHashMap) ^:async read-hash-map [^ReaderInput rdr]
(let [result #dart[]]
(loop []
(let [val (await (read rdr (cu0 "}")))]
(if (== val rdr)
(if (zero? (bit-and 1 (.-length result)))
(-map-lit result)
(throw (FormatException. "Map literal must contain an even number of forms")))
(do (.add result val)
(recur)))))))
(defn ^#/(Future cljd.core/PersistentHashSet) ^:async read-hash-set [^ReaderInput rdr]
(let [result #dart[]]
(loop []
(let [val (await (read rdr (cu0 "}")))]
(if (== val rdr)
(set result)
(do (.add result val)
(recur)))))))
(defn ^#/(Future cljd.core/PersistentVector) ^:async read-vector [^ReaderInput rdr]
(let [result #dart[]]
(loop []
(let [val (await (read rdr (cu0 "]")))]
(if (== val rdr)
(if (< 32 (.-length result))
(vec result)
(-vec-owning result))
(do (.add result val)
(recur)))))))
(def ^RegExp COMMENT-CONTENT-REGEXP #"[^\r\n]*")
(defn ^:async read-comment [^ReaderInput rdr]
(loop []
(if-some [s (await (.read rdr))]
(let [index (or (some-> (.matchAsPrefix COMMENT-CONTENT-REGEXP s) .-end) 0)]
(if (< index (.-length s))
(doto rdr (.unread (.substring s index)))
(recur)))
rdr)))
(defn ^:async read-regexp [^ReaderInput rdr]
(let [sb (StringBuffer.)
string (await (.read rdr))]
(loop [index 0]
(let [ch (saget string index)]
(if (== "\"" ch)
(do (.unread rdr (.substring string (inc index)))
(RegExp. (.toString sb)))
(if (< (cu0 ch) 0)
(throw (FormatException. "EOF while reading regex"))
(do (.write sb ch)
(if (== "\\" ch)
(let [index (inc index)
ch (saget string index)]
(if (< (cu0 ch) 0)
(throw (FormatException. "EOF while reading regex"))
(do (.write sb ch)
(recur (inc index)))))
(recur (inc index))))))))))
(defn ^:async read-meta [^ReaderInput rdr]
(let [meta (await (read rdr -1))
meta (if (or (symbol? meta) (string? meta))
{:tag meta}
(if (keyword? meta)
{meta true}
(if (map? meta)
meta
(throw (FormatException. "Metadata must be Symbol,Keyword,String or Map")))))
obj (await (read rdr -1))]
(if (satisfies? cljd.core/IWithMeta obj)
(with-meta obj meta)
;;TODO handle IReference with reset-meta
(throw (FormatException. "Metadata can only be applied to IWithMeta")))))
(def ^RegExp STRING-CONTENT-REGEXP (RegExp. "(?:[^\"\\\\]|\\\\.)*"))
(def ^RegExp STRING-ESC-REGEXP (RegExp. "\\\\(?:u([0-9a-fA-F]{0,4})|([0-7]{1,3})|(.))"))
(defn ^:async read-string-content [^ReaderInput rdr]
(let [f ^:async (fn ^String [^ReaderInput rdr]
(let [sb (StringBuffer.)]
(loop []
(if-some [^String string (await (.read rdr))]
(let [^int i (or (some-> (.matchAsPrefix STRING-CONTENT-REGEXP string) .-end) 0)]
(.write sb (.substring string 0 i))
(if (< i (.-length string))
(do (.unread rdr (.substring string (inc i)))
(.toString sb))
(recur)))
(throw (FormatException. "Unexpected EOF while reading a string."))))))]
(.replaceAllMapped (await (f rdr))
STRING-ESC-REGEXP
(fn [^Match m]
(if-some [^String m1 (.group m 1)]
(if (< (.-length m1) 4)
(throw (FormatException. (str "Unsupported escape for character: \\u" m1 " \\u MUST be followed by 4 hexadecimal digits")))
(String/fromCharCode (int/parse m1 .radix 16)))
(if-some [^String m2 (.group m 2)]
(String/fromCharCode (int/parse m2 .radix 8))
(let [m3 (.group m 3)]
(case m3
"\"" (str m3)
"\\" (str m3)
"b" "\b"
"n" "\n"
"r" "\r"
"t" "\t"
"f" "\f"
(throw (FormatException. (str "Unsupported escape character: \\" m3)))))))))))
(def ^:dynamic *arg-env* nil)
(declare ^:dart interpret-token ^:dart ^:async read-token ^:dart terminating?)
(defn ^:async read-arg [^ReaderInput rdr]
;; funny case `(let [% 1] %)` > 1
(if (nil? *arg-env*)
(interpret-token (await (read-token rdr "%")))
(let [string (await (.read rdr))
ch (cu0 (saget string 0))]
(if (or (< ch 0) (terminating? ch))
(let [sym (symbol "param")]
(.unread rdr (.substring string 1))
(set! *arg-env* (assoc *arg-env* 1 sym))
sym)
(if-some [^Match m (.matchAsPrefix #"(?:([\d]+)|(&))([^\d&]).*$" string)]
(if-some [^String g3 (.group m 3)]
(if (terminating? (cu0 g3))
(let [g1 (.group m 1)
g2 (.group m 2)]
(cond
g1 (let [sym (symbol (str "param" g1))]
(.unread rdr (.substring string (.-length g1)))
(set! *arg-env* (assoc *arg-env* (int/parse g1) sym))
sym)
g2 (do (.unread rdr (.substring string 1))
(set! *arg-env* (assoc *arg-env* -1 'parammore))
'parammore)))
(throw (FormatException. "arg literal must be %, %& or %integer"))))
(throw (FormatException. "arg literal must be %, %& or %integer")))))))
(defn ^:async read-fn [^ReaderInput rdr]
(when-not (nil? *arg-env*)
(throw (FormatException. "Nested #()s are not allowed")))
(binding [*arg-env* {}]
(let [result (await (read-list rdr))
arg-count (cond-> (->> (keys *arg-env*) (apply max 0))
(*arg-env* -1) (+ 2))]
(list 'fn*
(list (reduce #(let [[n param] %2]
(if (< n 0)
(-> (assoc %1 (dec arg-count) param)
(assoc (- arg-count 2) '&))
(assoc %1 (dec n) param)))
(vec (repeat arg-count '_)) *arg-env*)
result)))))
(defn ^:async read-unquote [^ReaderInput rdr]
(let [string (await (.read rdr))
char (saget string 0)]
(if (< (cu0 char) 0)
(throw (FormatException. "EOF while reading character"))
(if (== char "@")
(do (.unread rdr (.substring string 1))
(list 'cljd.core/unquote-splicing (await (read rdr -1))))
(do (.unread rdr string)
(list 'cljd.core/unquote (await (read rdr -1))))))))
(declare ^:dart sq-expand-list)
(defprotocol IResolver
(currentNS [resolver])
(resolveClass [resolver sym])
(resolveAlias [resolver sym])
(resolveVar [resolver sym]))
(def ^:dynamic *resolver* nil)
(def ^:dynamic *gensym-env* nil)
(defn unquote? [form]
(and (seq? form) (= 'cljd.core/unquote (first form))))
(defn unquote-splicing? [form]
(and (seq? form) (= 'cljd.core/unquote-splicing (first form))))
;; TODO: should not be in reader.cljd
(defn special-form? [form]
(contains? '#{. set! dart/is? dart/await dart/assert throw new ns try case* quote do var let* loop* recur
if fn* letfn def reify* deftype* defprotocol* extend-type-protocol*} form))
(defn- syntax-quote [form]
(cond
(special-form? form) (list 'quote form)
(symbol? form)
(let [nsform (namespace form)
nameform (name form)]
(->> (cond
(and (nil? nsform) (.endsWith nameform "#"))
(if (nil? *gensym-env*)
;; Should never happen
(throw (FormatException. "Gensym literal not in syntax-quote"))
(or
(*gensym-env* form)
(let [gs (symbol nil
(str (gensym (str (.substring nameform 0 (dec (.-length nameform))) "__")) "__auto__"))]
(set! *gensym-env* (assoc *gensym-env* form gs))
gs)))
(and (nil? nsform) (.endsWith nameform "."))
(let [rc (resolveClass *resolver* (symbol nil (.substring nameform 0 (dec (.-length nameform)))))]
(symbol (namespace rc) (str (name rc) ".")))
(and (nil? nsform) (== (saget nameform 0) "."))
form
:else
(let [nsym (let [alias (some->> nsform (symbol nil))
nsym (some->> alias (resolveClass *resolver*))]
(if (nil? nsym) (resolveAlias *resolver* alias) nsym))]
(if (not (nil? nsym))
(symbol (name nsym) nameform)
(if (nil? nsform)
(let [rsym (resolveClass *resolver* form)
rsym (if (nil? rsym) (resolveVar *resolver* form) rsym)]
(if (not (nil? rsym))
rsym
(symbol (name (currentNS *resolver*)) nameform)))
form))))
(list 'quote)))
(unquote? form) (second form)
(unquote-splicing? form) (throw (FormatException. "splice not in list"))
(number? form) form
(string? form) form
(keyword? form) form
(coll? form)
(cond
;; TODO records
(vector? form)
(list 'cljd.core/apply 'cljd.core/vector (list 'cljd.core/seq (cons 'cljd.core/concat (sq-expand-list form))))
(set? form)
(list 'cljd.core/apply 'cljd.core/hash-set (list 'cljd.core/seq (cons 'cljd.core/concat (sq-expand-list form))))
(map? form)
(list 'cljd.core/apply 'cljd.core/hash-map (list 'cljd.core/seq (cons 'cljd.core/concat (sq-expand-list (apply concat form)))))
(or (list? form) (seq? form))
(list 'cljd.core/seq (cons 'cljd.core/concat (sq-expand-list form)))
:else (throw (FormatException. "Unknown Collection type")))
:else (list 'quote form)))
(defn- sq-expand-list [s]
(doall (map #(cond
(unquote? %) (list 'cljd.core/list (second %))
(unquote-splicing? %) (second %)
:else (list 'cljd.core/list (syntax-quote %))) s)))
(defn ^:async read-syntax-quote [^ReaderInput rdr]
(binding [*gensym-env* {}]
(syntax-quote (await (read rdr -1)))))
(defn ^:async read-namespace-map [^ReaderInput rdr]
(let [string (await (.read rdr))
auto (== ":" (saget string 0))
key (if (== -1 (.indexOf (if auto (.substring string 1) string) #"^[\s,{]"))
(await (read (doto rdr (.unread (str ":" string))) -1))
(if auto
(do
(.unread rdr (.substring string 1))
(currentNS *resolver*))
(throw (FormatException. "Namespaced map must specify a namespace")))) ]
(when-not (nil? (namespace key))
(throw (FormatException. (str "Namespaced map must specify a valid namespace: " (namespace key) "/" (name key)))))
(let [m (await (read rdr -1))]
(when-not (map? m)
(throw (FormatException. "Namespaced map must specify a map")))
(into {} (for [[k v] m
:let [k' (cond
(keyword? k)
(cond-> k
(== "_" (namespace k)) (->> name (keyword nil))
(nil? (namespace k)) (->> name (keyword (name key))))
(symbol? k)
(cond-> k
(== "_" (namespace k)) (->> name (symbol nil))
(nil? (namespace k)) (->> name (symbol (name key))))
:else k)]]
[k' v])))))
(def ^:dynamic *top-level-form* nil)
(def ^:dynamic *features* #{:cljd :clj})
(defn ^:async read-conditional
[^ReaderInput rdr]
;; TODO : manage EOF
;; TODO: should check if reader conditional are allowed
;; TODO: handle preserve read-cond
(let [string (await (.read rdr))
splicing (if (== "@" (saget string 0))
(do (.unread rdr (.substring string 1)) true)
(do (.unread rdr string) false))
[_ expr :as body] (await (read rdr -1))
;; TODO : in clj java.util.List but in Dart, PV are List and PL are also List...
is-list? (list? body)]
(when-not is-list?
(throw (FormatException. "read-cond body must be a list")))
(when-not (or (dart/is? expr List) (not splicing))
(throw (FormatException. "Spliced form list in read-cond-splicing must implement dart:core/List")))
(when-not (or (not splicing) (not *top-level-form*))
(throw (FormatException. "Reader conditional splicing not allowed at the top level.")))
(let [discard1 ^:unique (Object.)
discard ^:unique (Object.)
hold ^:unique (Object.)
check-feature (fn [feature]
(when-not (keyword? feature)
(throw (FormatException. (str "Feature should be a keyword: " feature))))
(when (#{:none :else} feature)
(throw (FormatException. (str "Feature name " feature " is reserved."))))
(if (contains? *features* feature)
hold
discard1))
result
(reduce (fn [state form]
(cond
(identical? hold state) (reduced form)
(identical? discard1 state) discard
(identical? discard state) (check-feature form)))
discard body)]
(when (identical? result hold)
(throw (FormatException. "read-cond requires an even number of forms.")))
(when (or (identical? result discard1) (identical? result discard))
(throw (FormatException. "EOF while reading")))
(if splicing
(let [post (await (.read rdr))]
(doto rdr (.unread (str (apply pr-str result) post))))
result))))
(defn read-tagged [^ReaderInput rdr ^cljd.core/Symbol tag form]
(if-some [dr-fn (or (some-> cljd.core/*data-readers* tag) (some-> cljd.core/default-data-readers tag))]
(dr-fn form)
(if-some [default-fn (some-> cljd.core/*default-data-reader-fn* tag)]
(default-fn tag form)
(throw (FormatException. (str "No reader function for tag " tag))))))
(defn ^:async read-record [^ReaderInput rdr]
(throw (Exception. "read-reacord NOT IMPLEMENTED YET")))
(defn ^:async ctor-reader [^ReaderInput rdr]
(let [tag (await (read rdr -1))]
(when-not (symbol? tag)
(throw (FormatException. "Reader tag must be a symbol")))
(let [form (await (read rdr -1))]
;; TODO: handle preserve read-cond
(if (.contains (name tag) ".")
(read-record rdr)
(read-tagged rdr tag form)))))
(def dispatch-macros
{"_" ^:async (fn [^ReaderInput rdr] (await (read rdr -1)) rdr)
"^" ^:async (fn [^ReaderInput rdr] (await (read-meta rdr)))
"{" ^:async (fn [^ReaderInput rdr] (await (read-hash-set rdr)))
"(" ^:async (fn [^ReaderInput rdr] (await (read-fn rdr)))
"!" ^:async (fn [^ReaderInput rdr] (await (read-comment rdr)))
"\"" ^:async (fn [^ReaderInput rdr] (await (read-regexp rdr)))
"'" ^:async (fn [^ReaderInput rdr] (list 'var (await (read rdr -1))))
"<" ^:async (fn [^ReaderInput rdr] (throw (FormatException. "Unreadable form")))
":" ^:async (fn [^ReaderInput rdr] (await (read-namespace-map rdr)))
"?" ^:async (fn [^ReaderInput rdr] (await (read-conditional rdr)))})
(def macros
{"(" ^:async (fn [^ReaderInput rdr] (await (read-list rdr)))
")" ^:async (fn [_] (throw (FormatException. "EOF while reading, starting at line")))
"{" ^:async (fn [^ReaderInput rdr] (await (read-hash-map rdr)))
"}" ^:async (fn [_] (throw (FormatException. "EOF while reading, starting at line")))
"[" ^:async (fn [^ReaderInput rdr] (await (read-vector rdr)))
"]" ^:async (fn [_] (throw (FormatException. "EOF while reading, starting at line")))
"'" ^:async (fn [^ReaderInput rdr] (list (symbol nil "quote") (await (read rdr -1))))
"`" ^:async (fn [^ReaderInput rdr] (await (read-syntax-quote rdr)))
"@" ^:async (fn [^ReaderInput rdr] (list 'cljd.core/deref (await (read rdr -1))))
";" ^:async (fn [^ReaderInput rdr] (await (read-comment rdr)))
"^" ^:async (fn [^ReaderInput rdr] (await (read-meta rdr)))
"\"" ^:async (fn [^ReaderInput rdr] (await (read-string-content rdr)))
"%" ^:async (fn [^ReaderInput rdr] (await (read-arg rdr)))
"~" ^:async (fn [^ReaderInput rdr] (await (read-unquote rdr)))
"#" ^:async (fn [^ReaderInput rdr]
(if-some [string (await (.read rdr))]
(if-some [macroreader (dispatch-macros (saget string 0))]
(do (.unread rdr (.substring string 1))
(await (macroreader rdr)))
(if-some [result (do (.unread rdr string)
(await (ctor-reader rdr)))]
result
(throw (FormatException. (str "Unepxected dispatch sequence: #" (saget string 0))))))
(throw (FormatException. "EOF while reading dispatch sequence.")))) })
(def ^RegExp SPACE-REGEXP #"[\s,]*")
(defn ^bool terminating? [^int code-unit]
(let [ch (String/fromCharCode code-unit)]
(cond
(< -1 (.indexOf "'#%" ch)) false
(macros ch) true
(< 0 (or (some-> (.matchAsPrefix SPACE-REGEXP ch) .-end) 0)) true
:else false)))
(defn ^#/(Future String) ^:async read-token [^ReaderInput rdr ^String init]
(let [sb (StringBuffer. init)]
(loop [^int index 0
^String string ""]
(if (== index (.-length string))
(do (.write sb string)
(when-some [s (await (.read rdr))]
(recur 0 ^String s)))
(let [cu (.codeUnitAt string index)]
(if (terminating? cu)
(do (.write sb (.substring string 0 index))
(.unread rdr (.substring string index)))
(recur (inc index) string)))))
(.toString sb)))
(def ^RegExp INT-REGEXP (RegExp. "([-+]?)(?:(0)|([1-9][0-9]*)|0[xX]([0-9A-Fa-f]+)|0([0-7]+)|([1-9][0-9]?)[rR]([0-9A-Za-z]+)|0[0-9]+)(N)?$"))
(def ^RegExp DOUBLE-REGEXP (RegExp. "([-+]?[0-9]+([.][0-9]*)?([eE][-+]?[0-9]+)?)(M)?$"))
(def ^RegExp SYMBOL-REGEXP (RegExp. "(?:([:]{2})|([:]))?(?:([^0-9/:].*)/)?(/|[^0-9/][^/]*)$"))
(defn interpret-token [^String token]
(case token
"nil" nil
"true" true
"false" false
(if-some [m (.matchAsPrefix INT-REGEXP token)]
(let [parse (if ^some (.group m 8)
(if (== "-" (.group m 1))
(fn ^BigInt [^String s ^int radix] (- (BigInt/parse s .radix radix)))
(fn ^BigInt [^String s ^int radix] (BigInt/parse s .radix radix)))
(if (== "-" (.group m 1))
(fn ^int [^String s ^int radix] (- (int/parse s .radix radix)))
(fn ^int [^String s ^int radix] (int/parse s .radix radix))))]
(cond
(not (nil? (.group m 2))) 0
(not (nil? (.group m 3))) (parse (.group m 3) 10)
(not (nil? (.group m 4))) (parse (.group m 4) 16)
(not (nil? (.group m 5))) (parse (.group m 5) 8)
(not (nil? (.group m 7))) (parse (.group m 7) (int/parse ^String (.group m 6)))
:else (throw (FormatException. (str "Invalid number: " token ".")))))
(if-some [m (.matchAsPrefix DOUBLE-REGEXP token)]
(if (.group m 4)
(throw (FormatException. "BigDecimal not supported yet."))
(double/parse token))
(if-some [^Match m (.matchAsPrefix SYMBOL-REGEXP token)]
(let [g1 (.group m 1)
g2 (.group m 2)
g3 (.group m 3)
g4 (.group m 4)]
(when (or
(and g3 (.endsWith g3 ":/"))
(.endsWith g4 ":")
(not= -1 (.indexOf token "::" 1)))
(FormatException. (str "Invalid token: " token)))
(if g1
(if-some [nsym (if g3 (resolveAlias *resolver* (symbol nil g3)) (currentNS *resolver*))]
(keyword (name nsym) g4)
(FormatException. (str "Invalid token: " token)))
(if g2
(keyword g3 g4)
(symbol g3 g4))))
(throw (FormatException. (str "Invalid token: " token))))))))
(defn ^#/(Future dynamic) ^:async read
[^ReaderInput rdr ^int delim]
(binding [*top-level-form* (nil? *top-level-form*)]
(loop []
(if-some [string (await (.read rdr))]
(let [index (or (some-> (.matchAsPrefix SPACE-REGEXP string) .-end) 0)]
(if (== index (.-length string))
(recur)
(let [ch (.codeUnitAt string index)]
(if (== delim ch)
(doto rdr (.unread (.substring string (inc index))))
(if-some [macro-reader (macros (saget string index))]
(do (.unread rdr (.substring string (inc index)))
(let [val (await (macro-reader rdr))]
(if (== val rdr)
(recur)
val)))
(do (.unread rdr (.substring string index))
(-> (await (read-token rdr "")) interpret-token)))))))
(if (< delim 0)
rdr
(throw (FormatException. (str "Unexpected EOF, expected " (String/fromCharCode delim)))))))))
(defn ^#/(Future dynamic) ^:async read-string [^String s]
(let [controller (new #/(async/StreamController String))
rdr (make-reader-input controller)]
(.add controller s)
(let [res (read rdr -1)]
(.close controller)
(await res))))