Skip to content

Instantly share code, notes, and snippets.

@frenchy64
Created February 15, 2014 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frenchy64/9014220 to your computer and use it in GitHub Desktop.
Save frenchy64/9014220 to your computer and use it in GitHub Desktop.
(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