Skip to content

Commit

Permalink
Unify cljs reader
Browse files Browse the repository at this point in the history
  • Loading branch information
dundalek committed Mar 7, 2020
1 parent d72ef91 commit 3cbb299
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 110 deletions.
39 changes: 28 additions & 11 deletions src/common/closh/zero/reader.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
(:refer-clojure :exclude [read read-string])
(:require [clojure.tools.reader.reader-types :as r]
[clojure.tools.reader.edn :as edn]
#?(:cljs [closh.zero.cljs-reader :as reader])))
#?(:cljs [cljs.tools.reader])
#?(:cljs [cljs.tools.reader.impl.utils :refer [ws-rx]]))
#?(:cljs (:import goog.string.StringBuffer)))

(set! *warn-on-reflection* true)
#?(:clj (set! *warn-on-reflection* true))

#?(:clj
(defmacro require-reader []
Expand All @@ -13,12 +15,21 @@
'(do (require 'clojure.tools.reader)
(def read-clojure clojure.tools.reader/read)))))

#?(:clj (require-reader))
#?(:clj (require-reader)
:cljs
(defn- ^:no-doc read-clojure
"This is a verbatim copy of `cljs.tools.reader/read`. We need a copy otherwise re-binding ends up in infinite loop."
{:arglists '([] [reader] [opts reader] [reader eof-error? eof-value])}
([reader] (read-clojure reader true nil))
([{eof :eof :as opts :or {eof :eofthrow}} reader] (cljs.tools.reader/read* reader (= eof :eofthrow) eof nil opts (to-array [])))
([reader eof-error? sentinel] (cljs.tools.reader/read* reader eof-error? sentinel nil {} (to-array [])))))

(defn whitespace?-custom
"Customizes `clojure.tools.reader.impl.utils/whitespace?` so that read-token splits token only on whitespace and does not split on comma."
[ch]
(and ch (Character/isWhitespace ^Character ch)))
(and (some? ch)
#?(:clj (Character/isWhitespace ^Character ch)
:cljs (.test ws-rx ch))))

(defn macro-terminating?
"Customizes `clojure.tools.reader/macro-terminating?` so that read-token is more permissive. For example it does not stop on curly braces but reads them in so they can be used for brace expansion."
Expand All @@ -37,7 +48,7 @@
(defn ^String read-token*
"Read in a single logical token from the reader"
[rdr]
(loop [sb (StringBuilder.) ch (r/read-char rdr)]
(loop [sb #?(:clj (StringBuilder.) :cljs (StringBuffer.)) ch (r/read-char rdr)]
(if (or (whitespace?-custom ch)
(macro-terminating? ch)
(nil? ch))
Expand All @@ -50,10 +61,16 @@
(if (= \" (r/peek-char rdr))
(edn/read rdr)
(let [token (read-token* rdr)]
(try
(Integer/parseInt token)
(catch Exception _
(symbol token))))))
#?(:clj
(try
(Integer/parseInt token)
(catch Exception _
(symbol token)))
:cljs
(let [number (js/Number token)]
(if (js/isNaN number)
(symbol token)
number))))))

(defn read* [opts reader]
(loop [coll (transient [])]
Expand Down Expand Up @@ -96,8 +113,8 @@
(recur (conj! coll token)))))))))

(defn read
([]
(read *in*))
#?(:clj ([]
(read *in*)))
([stream]
(read stream true nil))
([stream eof-error? eof-value]
Expand Down
99 changes: 0 additions & 99 deletions src/lumo/closh/zero/cljs_reader.cljs

This file was deleted.

0 comments on commit 3cbb299

Please sign in to comment.