Skip to content

Instantly share code, notes, and snippets.

@frenchy64 frenchy64/taj.clj
Created Feb 15, 2014

Embed
What would you like to do?
(ns ^:skip-wiki clojure.core.typed.analyze-clj
(:require [clojure.jvm.tools.analyzer :as analyze]
[clojure.jvm.tools.analyzer.hygienic :as hygienic]
[clojure.tools.analyzer :as ta]
[clojure.tools.analyzer.jvm :as taj]
[clojure.tools.reader :as tr]
[clojure.tools.reader.reader-types :as readers]
[clojure.string :as str]
[clojure.java.io :as io]
[clojure.core.typed.utils :as u]))
(defn ast-for-ns
"Returns a vector of AST nodes contained
in the given namespace symbol nsym"
[nsym]
{:pre [(symbol? nsym)]}
(u/p :analyze/ast-for-ns
;copied basic approach from tools.emitter.jvm
(let [res nsym
p (str (str/replace res #"\." "/") ".clj")
eof (reify)
p (if (.startsWith p "/") (subs p 1) p)
file (-> p io/resource io/reader slurp)
reader (readers/indexing-push-back-reader file)]
(binding [*ns* (or (find-ns nsym)
*ns*)]
(loop [asts []]
(let [form (tr/read reader false eof)]
(if (not= eof form)
(recur (conj asts (taj/analyze form (taj/empty-env))))
asts)))))))
; (do (ast-for-ns 'clojure.tools.reader) nil)
user=> (doc clojure.tools.reader/read)
-------------------------
(quote ([] [reader] [reader eof-error? sentinel] [reader eof-error? sentinel recursive?]))
Reads the first object from an IPushbackReader or a java.io.PushbackReader.
Returns the object read. If EOF, throws if eof-error? is true.
Otherwise returns sentinel. If no stream is providen, *in* will be used.
***WARNING***
Note that read can execute code (controlled by *read-eval*),
and as such should be used only with trusted sources.
To read data structures only, use clojure.tools.reader.edn/read
Note that the function signature of clojure.tools.reader/read and
clojure.tools.reader.edn/read is not the same for eof-handling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.