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

Commit

Permalink
injecting line/column into read objs
Browse files Browse the repository at this point in the history
  • Loading branch information
rksm committed May 3, 2015
1 parent 7b85f47 commit d3fd4e8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject org.rksm/cloxp-source-reader "0.1.4"
(defproject org.rksm/cloxp-source-reader "0.1.5"
:description "Source reading, parsing, and querying for cloxp."
:license {:name "MIT License"
:url "http://opensource.org/licenses/MIT"}
Expand Down
46 changes: 28 additions & 18 deletions src/rksm/cloxp_source_reader/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,34 @@
(first (drop 1 (filter symbol? form))))

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

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

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

(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)))
(if (seq? 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]
Expand All @@ -115,12 +118,14 @@
(defn read-objs
"Reads sexps from source and returns them as a {:form :source :line
:column} map. Note: this is more that the typical reader gives us."
[source & [{:keys [cljx?] :or {cljx? true} :as opts}]]
[source & [{:keys [cljx? line-offset column-offset] :or {cljx? true} :as opts}]]
; FIXME this is hacked...
(let [source (if-not (.endsWith source "\n") (str source "\n") source)
tfm-source (if cljx? (cljx.core/transform source cljx.rules/clj-rules) source)
get-src-fn (line-column-access source)
rdr (make-reader tfm-source)]
rdr (make-reader tfm-source)
line-offset (or line-offset 0)
column-offset (or column-offset 0)]
(loop [result []]
(let [start-line (trt/get-line-number rdr)
start-column (trt/get-column-number rdr)]
Expand All @@ -145,12 +150,17 @@
(trt/read-char rdr)
(purge-string! rdr))
(let [start {:line line :column column}
start-for-meta {:line (+ line line-offset)
:column (if (= 1 start-line) (+ column column-offset) column)}
end-line (trt/get-line-number rdr)
end-column (trt/get-column-number rdr)
source (get-src-fn start {:line end-line :column end-column})]
end {:line end-line :column end-column}
end-for-meta {:end-line (+ end-line line-offset)
:end-column (if (= 1 start-line) (+ end-column column-offset) end-column)}
source (get-src-fn start end)]
(recur (conj result
(merge start
{:end-line end-line :end-column end-column}
(merge start-for-meta
end-for-meta
{:form o, :source source}
(if def?
{:form (with-meta o (assoc meta :source src)),
Expand Down

0 comments on commit d3fd4e8

Please sign in to comment.