Skip to content

Commit

Permalink
Handling Inf -Inf and NaN in reader.cljd
Browse files Browse the repository at this point in the history
  • Loading branch information
dupuchba committed Nov 6, 2023
1 parent b9a7588 commit 6a1ef20
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
28 changes: 25 additions & 3 deletions clj/src/cljd/reader.cljd
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,26 @@
from
(post-regexp state s from to))))

(def post-symbolic-value
(post-read (fn [x]
(case x
Inf double/infinity
-Inf double/negativeInfinity
NaN double/nan
(if (symbol? x)
(throw (Exception (str "Unknown symbolic value: ##" x)))
(throw (Exception (str "Unknown token: ##" x))))))))

(defn read-symbolic-value
[^ReaderState {:flds [q stack] :as state} ^String s ^int from ^int to]
(doto stack
(.add post-symbolic-value)
(.add do-read))
(let [from (do-read state s from to)]
(if (neg? from)
from
(post-symbolic-value state s from to))))

(def post-char-token
(post-read
(fn [^String token]
Expand Down Expand Up @@ -567,8 +587,9 @@

;; 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))
(contains? '#{. set! dart/is? dart/await dart/async-barrier dart/run-zoned dart/assert dart/type-like
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
Expand Down Expand Up @@ -769,7 +790,8 @@
(aset (int "{") (mk-read-coll "}" set))
(aset (int \") read-regexp)
(aset (int \') (mk-read-wrapped 'var))
(aset (int \:) read-namespaced-map)))
(aset (int \:) read-namespaced-map)
(aset (int \#) read-symbolic-value)))

(defn read-string
"Synchronous, takes a string and returns the value of the first form."
Expand Down
10 changes: 6 additions & 4 deletions clj/test/cljd/test_reader/reader_test.cljd
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@
(is (= 'abc:def/ghi:jkl.mno (r/read-string "abc:def/ghi:jkl.mno")))
(is (dart/is? (r/read-string "alphabet") cljd.core/Symbol))
(is (= "foo//" (str (r/read-string "foo//"))))
;; TODO : think about it
#_(is (await (js/isNaN (r/read-string "##NaN"))))
#_(is (= js/Number.POSITIVE_INFINITY (r/read-string "##Inf")))
#_(is (= js/Number.NEGATIVE_INFINITY (r/read-string "##-Inf"))))
(is (true? (-> (r/read-string "##NaN") .-isNaN)))
(is (= double/infinity (r/read-string "##Inf")))
(is (= double/negativeInfinity (r/read-string "##-Inf")))
(is (dart/is? (r/read-string "#inst \"2023-01-01T00:00:00.000-00:00\"") DateTime)))



(deftest read-specials
(is (= 'nil (read-string "nil")))
Expand Down

0 comments on commit 6a1ef20

Please sign in to comment.