2014-01-18 10:43:56 +00:00
|
|
|
(ns reagentdemo.syntax
|
2017-05-19 17:25:53 +00:00
|
|
|
(:require [clojure.java.io :as io]
|
2019-12-17 12:32:45 +00:00
|
|
|
[clojure.string :as string]
|
|
|
|
[cljs.analyzer :as analyzer]))
|
2014-12-07 15:38:56 +00:00
|
|
|
|
2015-09-17 13:10:27 +00:00
|
|
|
;;; Source splitting
|
2014-12-07 15:38:56 +00:00
|
|
|
|
|
|
|
(defn src-parts [src]
|
|
|
|
(string/split src #"\n(?=[(])"))
|
|
|
|
|
|
|
|
(defn src-defs [parts]
|
2015-03-11 16:50:33 +00:00
|
|
|
(let [ws #"[^ \t\n]+"]
|
2014-12-07 15:38:56 +00:00
|
|
|
(into {} (for [x parts]
|
|
|
|
[(->> x (re-seq ws) second keyword) x]))))
|
|
|
|
|
|
|
|
(defn fun-map [src]
|
|
|
|
(-> src src-parts src-defs))
|
|
|
|
|
|
|
|
(defn src-for-names [srcmap names]
|
|
|
|
(string/join "\n" (map srcmap names)))
|
|
|
|
|
2015-02-09 20:02:31 +00:00
|
|
|
|
2014-12-07 15:38:56 +00:00
|
|
|
;;; Macros
|
|
|
|
|
|
|
|
(defmacro syntaxed [src]
|
2015-02-09 20:02:31 +00:00
|
|
|
`(reagentdemo.syntax/syntaxify ~src))
|
2014-12-07 15:38:56 +00:00
|
|
|
|
2015-09-17 13:10:27 +00:00
|
|
|
;; ;; A much simpler way to find source: currently broken with #js annotations
|
|
|
|
;; (defmacro src-for [& syms]
|
|
|
|
;; (let [s (map #(list 'with-out-str (list 'cljs.repl/source %)) syms)]
|
|
|
|
;; `(->> [~@s]
|
|
|
|
;; (string/join "\n")
|
|
|
|
;; syntaxed)))
|
|
|
|
|
|
|
|
;; (defmacro src-from-file [f]
|
|
|
|
;; (let [src (-> f io/resource slurp)]
|
|
|
|
;; `(syntaxed ~src)))
|
|
|
|
|
2014-12-07 15:38:56 +00:00
|
|
|
(defmacro src-of
|
|
|
|
([funs]
|
|
|
|
`(src-of ~funs nil))
|
|
|
|
([funs resource]
|
|
|
|
(assert (or (nil? funs)
|
|
|
|
(vector? funs)))
|
|
|
|
(assert (or (nil? resource)
|
|
|
|
(string? resource)))
|
|
|
|
(let [f (if (nil? resource)
|
2019-12-17 12:32:45 +00:00
|
|
|
(-> (name analyzer/*cljs-ns*)
|
2014-12-07 15:38:56 +00:00
|
|
|
(string/replace #"[.]" "/")
|
|
|
|
(str ".cljs"))
|
|
|
|
resource)
|
|
|
|
src (-> f io/resource slurp)
|
|
|
|
sel (if (nil? funs)
|
|
|
|
src
|
|
|
|
(-> src fun-map (src-for-names funs)))]
|
2015-09-17 13:10:27 +00:00
|
|
|
`(syntaxed ~sel))))
|