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

Commit

Permalink
reading cljx source
Browse files Browse the repository at this point in the history
  • Loading branch information
rksm committed Mar 21, 2015
1 parent 187432f commit a90c7cd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
38 changes: 23 additions & 15 deletions src/rksm/cloxp_source_reader/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
(let [start (nth lines (dec start-line))
start (.substring start (dec start-column) (count start))
end (if (= start-line end-line)
start (nth lines (min (dec (count lines)) (dec end-line))))
end (.substring end 0 (dec end-column))
start
(nth lines (min (dec (count lines)) (dec end-line))))
end (.substring end 0 (if (= start-line end-line) (- (dec end-column) (dec start-column)) (dec end-column)))
inbetween (->> lines (drop start-line) (take (dec (- end-line start-line))))]
(s/join "\n"
(if (= start-line end-line)
Expand All @@ -31,6 +32,7 @@
(comment
(def s "hello\nworld\n\nfoo\nbar\n\n")
((line-column-access s) {:line 3 :column 1} {:line 2 :column 3})
((line-column-access s) {:line 1 :column 5} {:line 1 :column 4})
((line-column-access s) {:line 2 :column 3} {:line 3 :column 1})
((line-column-access s) {:line 1 :column 1} {:line 3 :column 1}))

Expand Down Expand Up @@ -59,13 +61,15 @@
(tr/read rdr)))

(defn read-objs
"Reads sexps from rdr-or-src and returns them as a {:form :source :line
"Reads sexps from source and returns them as a {:form :source :line
:column} map. Note: this is more that the typical reader gives us."
[rdr-or-src]
[source & [{:keys [cljx?] :or {cljx? true} :as opts}]]
; FIXME this is hacked...
(let [rdr (trt/indexing-push-back-reader
(let [tfm-source (if cljx? (cljx.core/transform source cljx.rules/clj-rules) source)
get-src-fn (line-column-access source)
rdr (trt/indexing-push-back-reader
(trt/source-logging-push-back-reader
rdr-or-src))]
tfm-source))]
(loop [result []]
(let [start-line (trt/get-line-number rdr)
start-column (trt/get-column-number rdr)]
Expand All @@ -85,14 +89,17 @@
(when (= \newline (trt/peek-char rdr))
(trt/read-char rdr)
(purge-string! rdr))
(recur (conj result
(merge {:form o, :source src,
:line line, :column column,
:end-line (trt/get-line-number rdr),
:end-column (trt/get-column-number rdr)}
(if def?
{:form (with-meta o (assoc meta :source src)),
:name name})))))
(let [start {:line line :column 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})]
(recur (conj result
(merge start
{:end-line end-line :end-column end-column}
{:form o, :source source}
(if def?
{:form (with-meta o (assoc meta :source src)),
:name name}))))))
result)))))

(defn add-source-to-interns-with-reader
Expand All @@ -102,6 +109,7 @@
{:pre [every? #((or (contains? (:line %))
(contains? (:name %)))) interns]}
(let [file-source (slurp rdr)
get-src-fn (line-column-access file-source)
clj-source (if cljx?
(cljx.core/transform file-source cljx.rules/clj-rules)
file-source)
Expand All @@ -118,7 +126,7 @@
first))]
; (assoc meta-entity :source source)
(assoc meta-entity :source
((line-column-access file-source)
(get-src-fn
{:line line :column column}
{:line end-line :column end-column}))))
interns))))
Expand Down
11 changes: 11 additions & 0 deletions test/rksm/cloxp_source_reader/cljx_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
"(def y #+clj 24 #+cljx 25)")]
(is (= expected sources))))

(deftest read-objs-cljx
(let [result (src-rdr/read-objs "(def y #+clj 24 #+cljx 25)")
expected '({:name y,
:form (def y 24),
:source "(def y #+clj 24 #+cljx 25)",
:line 1,
:column 1,
:end-line 1,
:end-column 27})]
(is (= expected result))))

; -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

(comment
Expand Down
8 changes: 4 additions & 4 deletions test/rksm/cloxp_source_reader/test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

(testing "simple read"
(is (= [{:form '(ns rksm.cloxp-source-reader.test.dummy-3),
:source "(ns rksm.cloxp-source-reader.test.dummy-3)",
:source "(ns rksm.cloxp-source-reader.test.dummy-3)\n",
:line 1, :column 1
:end-line 2, :end-column 1}
{:form '(def x 23), :source "(def x 23)",
{:form '(def x 23), :source "(def x 23)\n",
:name 'x
:line 2, :column 3,
:end-line 3, :end-column 1}]
Expand All @@ -39,13 +39,13 @@
(let [src "(ns rksm.cloxp-source-reader.test.dummy-3)\n (defmacro b [] `~23)\n(+ 2 3)\n(defn foo [] `~23)\n"
expected [{:ns 'rksm.cloxp-source-reader.test.dummy-3,
:name 'foo,
:source "(defn foo [] `~23)",
:source "(defn foo [] `~23)\n",
:line 4
; :column 1 :end-line 3, :end-column 1
}
{:ns 'rksm.cloxp-source-reader.test.dummy-3,
:name 'b,
:source "(defmacro b [] `~23)"
:source "(defmacro b [] `~23)\n"
:line 2,
; :column 1, :end-line 3, :end-column 1
}]]
Expand Down

0 comments on commit a90c7cd

Please sign in to comment.