Skip to content

Commit

Permalink
DEV: Add Matcher to hide clj/cljs implementation details
Browse files Browse the repository at this point in the history
  • Loading branch information
or committed Jun 18, 2022
1 parent be381cd commit d89925b
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions cljfmt/src/cljfmt/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -408,33 +408,30 @@
(some-> zloc z/up ns-form?)
(-> zloc z/sexpr first ns-reference-symbols)))

#?(:cljs
(defn- re-seq-matcher [re charmap coll]
{:pre (every? charmap coll)}
(let [s (apply str (map charmap coll))
v (vec coll)
m (js/RegExp. re.source "g")]
(letfn [(next-match []
(when-let [result (.exec m s)]
(let [start result.index
end m.lastIndex]
{:value (subvec v start end)
:start start
:end end})))]
(take-while some? (repeatedly next-match)))))

:clj
(defn- re-seq-matcher [re charmap coll]
{:pre (every? charmap coll)}
(let [s (apply str (map charmap coll))
v (vec coll)
m (re-matcher re s)]
(letfn [(next-match []
(when (.find m)
{:value (subvec v (.start m) (.end m))
:start (.start m)
:end (.end m)}))]
(take-while some? (repeatedly next-match))))))
(defprotocol Matcher
(find-next [this]))

(defn- matcher [re s]
(let [m #?(:clj (re-matcher re s)
:cljs (js/RegExp. re.source "g"))]
(reify Matcher
(find-next [_this]
#?(:clj (when (.find m)
{:start (.start m)
:end (.end m)})
:cljs (when-let [result (.exec m s)]
{:start result.index
:end m.lastIndex}))))))

(defn- re-seq-matcher [re charmap coll]
{:pre (every? charmap coll)}
(let [s (apply str (map charmap coll))
v (vec coll)
m (matcher re s)]
(letfn [(next-match []
(when-let [{:keys [start end] :as match} (find-next m)]
(assoc match :value (subvec v start end))))]
(take-while some? (repeatedly next-match)))))

(defn- find-elements-with-comments [nodes]
(re-seq-matcher #"(CNS*)*E(S*C)?"
Expand Down

0 comments on commit d89925b

Please sign in to comment.