Skip to content
This repository has been archived by the owner on Apr 3, 2022. It is now read-only.

Commit

Permalink
multimethod-reading
Browse files Browse the repository at this point in the history
  • Loading branch information
rksm committed May 2, 2015
1 parent 0d8c885 commit 7b85f47
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/rksm/cloxp_source_reader/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,35 @@
(first (drop 1 (filter symbol? form))))

(defn def?
[form]
[[a & _ :as form]]
(and (seq? form)
(->> form first str (re-find #"(^|\/)def") boolean)))
(boolean (re-find #"(^|\/)def" (str a)))))

(defn defmethod?
[[a & _ :as form]]
(= a 'defmethod))

(defn defmulti?
[[a & _ :as form]]
(= a 'defmulti))

(defn defmethod-qualifier
"takes a defmethod form and extract the match args from it, like
'(defmethod ^{:dynamic true}foo-method String [::foo \"Bar\"] ([x] (.toUpperCase x)))
=>
'(String [:user/foo \"Bar\"])"
[form]
(let [ex-form (macroexpand form)
[_ _ _ match-1 fn-def] ex-form
rest-matches (if (= (->> fn-def last (map type))
[clojure.lang.PersistentVector clojure.lang.PersistentList])
(->> fn-def (drop 1) (drop-last))
(->> fn-def (drop 1) (drop-last 2)))]
(cons match-1 rest-matches)))

(defn defmethod-qualifier-string
[form]
(clojure.string/join "-" (defmethod-qualifier form)))

(defn purge-string!
[rdr]
Expand Down Expand Up @@ -112,7 +138,9 @@
column (+ (if (> (count ws-lines) 0) 1 start-column) (count leading-ws))
meta (meta o)
def? (def? o)
name (if def? (name-of-def o))]
defmethod? (defmethod? o)
name (if def? (name-of-def o))
defmethod-name (defmethod-qualifier-string o)]
(when (= \newline (trt/peek-char rdr))
(trt/read-char rdr)
(purge-string! rdr))
Expand All @@ -126,7 +154,9 @@
{:form o, :source source}
(if def?
{:form (with-meta o (assoc meta :source src)),
:name name}))))))
:name name})
(if defmethod?
{:defmethod-qualifier defmethod-name}))))))
result)))))

(defn add-source-to-interns-with-reader
Expand Down
11 changes: 11 additions & 0 deletions test/rksm/cloxp_source_reader/test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
[rksm.cloxp-source-reader.ast-reader :as ast-rdr]
(rksm.cloxp-source-reader.test dummy-1 dummy-3)))

(defmacro codify
[& body]
`(clojure.string/join "\n" (map str '~body)))

(deftest source-reader-matches-interns
(remove-ns 'rksm.cloxp-source-reader.test.dummy-3)
(require 'rksm.cloxp-source-reader.test.dummy-3 :reload)
Expand Down Expand Up @@ -89,6 +93,13 @@
source (java.io.StringReader. "(def x 23)\n\n(def y 24)")]
(src-rdr/add-source-to-interns-with-reader source entities)))))))

(deftest multimethod-reading
(is (= '(nil nil ":a")
(map :defmethod-qualifier
(src-rdr/read-objs (codify (ns multi-test-1)
(defmulti multi-f (fn [x & _] x))
(defmethod multi-f :a [_ x] (+ x 3))))))))

(deftest ns-decl-read-test
(is (= 'bar (src-rdr/read-ns-sym "foo\n(ns ^{:doc \"baz\"} bar)"))))

Expand Down

0 comments on commit 7b85f47

Please sign in to comment.