Skip to content

Commit

Permalink
[#50] Support parsing Clojure code in tagged literals
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Jun 20, 2020
1 parent 11e5bd5 commit 1357bca
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion resources/EDAMAME_VERSION
@@ -1 +1 @@
0.0.11-alpha.12
0.0.11-alpha.13
20 changes: 13 additions & 7 deletions src/edamame/impl/parser.cljc
Expand Up @@ -13,6 +13,7 @@
:cljs [cljs.tools.reader.impl.utils :refer [reader-conditional desugar-meta namespace-keys]])
#?(:clj [clojure.tools.reader.impl.commons :as commons]
:cljs [cljs.tools.reader.impl.commons :as commons])
#?(:cljs [cljs.tagged-literals :as cljs-tags])
[clojure.string :as str]
[edamame.impl.read-fn :refer [read-fn]]
[edamame.impl.syntax-quote :refer [syntax-quote]])
Expand Down Expand Up @@ -322,8 +323,16 @@
(parse-next ctx reader)
;; read form
(parse-next ctx reader))
(do (r/unread reader \#)
(edn-read ctx reader))))))))
(let [sym (parse-next ctx reader)
data (parse-next ctx reader)
f (or (get (:readers ctx) sym)
#?(:clj (default-data-readers sym)
:cljs (cljs-tags/*cljs-data-readers* sym)))]
(if f (f data)
(throw (new #?(:clj Exception :cljs js/Error)
(str "No reader function for tag " sym)))))
#_(do (r/unread reader \#)
(edn-read ctx reader))))))))

(defn throw-odd-map
[#?(:cljs ^not-native reader :default reader) loc elements]
Expand Down Expand Up @@ -385,7 +394,7 @@
(let [next-val (parse-next ctx reader)]
(if (ifn? v)
(v next-val)
(list 'deref next-val))))
(list 'clojure.core/deref next-val))))
(throw-reader
reader
(str "Deref not allowed. Use the `:deref` option")))
Expand Down Expand Up @@ -541,10 +550,7 @@
(not (:row-key opts)) (assoc :row-key :row)
(not (:end-row-key opts)) (assoc :end-row-key :end-row)
(not (:col-key opts)) (assoc :col-key :col)
(not (:end-col-key opts)) (assoc :end-col-key :end-col))
opts (if-let [readers (:readers opts)]
(update-in opts [:tools.reader/opts :readers] merge readers)
opts)]
(not (:end-col-key opts)) (assoc :end-col-key :end-col))]
(map->Options opts)))

(defn parse-string [s opts]
Expand Down
11 changes: 8 additions & 3 deletions test/edamame/core_test.cljc
Expand Up @@ -40,7 +40,7 @@
(let [foo-sym (second (p/parse-string "(defn foo [])"))]
(is (= {:row 1 :col 7 :end-row 1, :end-col 10} (meta foo-sym))))
(is (= '(do (+ 1 2 3)) (p/parse-string "(do (+ 1 2 3)\n)")))
(is (= "[1 2 3]" (p/parse-string "#foo/bar [1 2 3]" {:tools.reader/opts {:readers {'foo/bar (fn [v] (str v))}}})))
(is (= "[1 2 3]" (p/parse-string "#foo/bar [1 2 3]" {:readers {'foo/bar (fn [v] (str v))}})))
(is (= [1 2 3] (p/parse-string-all "1 2 3")))
(is (= '({:row 1, :col 1, :end-row 1, :end-col 23}
{:row 1, :col 5, :end-row 1, :end-col 22}
Expand Down Expand Up @@ -208,10 +208,15 @@
:cljs (str (readFileSync (join "test-resources" "clojure" "core.cljs"))))
{:all true
:auto-resolve '{:current cljs.core}
:tools.reader/opts {:readers cljs-tags/*cljs-data-readers*}}))))))
#?@(:clj [:readers cljs-tags/*cljs-data-readers*])}))))))

(deftest readers-test
(is (= '(js [1 2 3]) (p/parse-string "#js [1 2 3]" {:readers {'js (fn [v] (list 'js v))}}))))
(is (= '(foo [1 2 3]) (p/parse-string "#foo [1 2 3]" {:readers {'foo (fn [v] (list 'foo v))}})))
(is (= '(foo @(atom 1)) (p/parse-string "#foo @(atom 1)" {:readers {'foo (fn [v] (list 'foo v))}
:all true})))
(is (= '(js [1 2 3]) (p/parse-string "#js [1 2 3]" {:readers {'js (fn [v] (list 'js v))}})))
;; TODO: should we "eval" the JSValue here, or in sci?
#?(:cljs (is (p/parse-string "#js [1 2 3]"))))

(deftest namespaced-map-test
;; TODO: fix locations of namespaced maps
Expand Down

0 comments on commit 1357bca

Please sign in to comment.