Browse files

Renamed ast functions so they line up with clojure.core.

  • Loading branch information...
1 parent b7aceb7 commit d93dde9b34ebdcd54c75a465de4a0f035f6933a1 @tcrayford tcrayford committed Sep 11, 2010
View
18 src/clojure_refactoring/destructuring.clj
@@ -9,7 +9,7 @@
(defn parsley-map-lookup? [ast]
(let [content (ast/relevant-content ast)]
(and (ast/tag= :list ast)
- (count= (filter ast/parsley-keyword? content) 1)
+ (count= (filter ast/keyword? content) 1)
(count= content 2))))
(defn parsley-key->sym [kw-node]
@@ -20,7 +20,7 @@
(defn parsley-find-lookups [node]
"Returns all the map lookups in a node as a set of parsley asts"
- (->> (ast/parsley-sub-nodes node)
+ (->> (ast/sub-nodes node)
(filter parsley-map-lookup?)
set))
@@ -36,7 +36,7 @@
(defn parsley-lookup-to-canoninical-form [lookup-ast]
(let [[maybe-keyword] (ast/relevant-content lookup-ast)]
- (if (ast/parsley-keyword? maybe-keyword)
+ (if (ast/keyword? maybe-keyword)
lookup-ast
(parsley-swap-first-with-last lookup-ast))))
@@ -46,9 +46,9 @@
(ast/replace-content m
`("{"
~key
- ~ast/parsley-whitespace
+ ~ast/whitespace
~val
- ~ast/parsley-whitespace
+ ~ast/whitespace
~@(drop 1 (:content m)))))
(def relevant-content-from-canoninical-form
@@ -59,7 +59,7 @@
(let [[key m] (relevant-content-from-canoninical-form lookup)]
(assoc binding-map m
(add-to-parsley-map
- (get binding-map m ast/parsley-empty-map)
+ (get binding-map m ast/empty-map)
(parsley-key->sym key) key))))
(defn lookups-to-binding-map [lookups]
@@ -78,7 +78,7 @@
;;replacing stuff all over the place
(reduce
(fn [new-ast lookup]
- (ast/parsley-tree-replace
+ (ast/tree-replace
lookup
(parsley-key->sym (first (ast/relevant-content (parsley-lookup-to-canoninical-form lookup))))
new-ast))
@@ -88,12 +88,12 @@
(defn- add-destructured-maps-to-args [lookups root-ast]
(let [args (ast/parsley-fn-args root-ast)
new-args (destructured-binding-vec args lookups)]
- (ast/parsley-tree-replace args new-args root-ast)))
+ (ast/tree-replace args new-args root-ast)))
(defparsed-fn destructure-map [root-ast]
"Destructures all calls to maps"
(let [lookups (parsley-find-lookups root-ast)]
- (ast/parsley-to-string
+ (ast/ast->string
(replace-lookups-with-destructured-symbols
lookups
(add-destructured-maps-to-args lookups root-ast)))))
View
12 src/clojure_refactoring/extract_method.clj
@@ -8,14 +8,14 @@
(defn- find-occurences [args node]
"Looks for any occurence of each element of args in the node"
- (seq (intersection (set args) (set (ast/parsley-sub-nodes node)))))
+ (seq (intersection (set args) (set (ast/sub-nodes node)))))
(defn fn-name [fn-node]
(second (ast/relevant-content fn-node)))
(defn fn-call [fn-node]
"Uses the arguments of a function node to return a call to it."
- (ast/parsley-list `(~(fn-name fn-node) ~@(ast/parsley-bindings fn-node))))
+ (ast/list `(~(fn-name fn-node) ~@(ast/parsley-bindings fn-node))))
(defn- arg-occurences [f-node extracted-node]
"Finds the bindings from f-node that are also in the extracted node."
@@ -28,20 +28,20 @@
(ast/list-without-whitespace
(ast/symbol 'defn)
name
- (ast/parsley-vector args)
+ (ast/vector args)
(ast/strip-whitespace body))))
(defn call-extracted [body toplevel extracted]
- (ast/parsley-tree-replace
+ (ast/tree-replace
body
(fn-call extracted)
toplevel))
(defn- nodes-to-string [extract-node fn-node new-fun]
"Formats the output for extract-method to print"
- (str (ast/parsley-to-string new-fun)
+ (str (ast/ast->string new-fun)
"\n\n"
- (ast/parsley-to-string
+ (ast/ast->string
(call-extracted extract-node fn-node new-fun))))
(defparsed-fn extract-method [function-node extract-node new-name]
View
20 src/clojure_refactoring/local_binding.clj
@@ -13,44 +13,44 @@
(ast/replace-content binding-node
`(~(first content)
~@(butlast (drop 1 content))
- ~ast/parsley-whitespace
+ ~ast/whitespace
~var-name
- ~ast/parsley-whitespace
+ ~ast/whitespace
~value
~(last content))))
(defn is-node-the-binding-form [top-level ast]
(= ast (ast/parsley-fn-args top-level)))
(defn- modify-existing-let-block [form value var-name]
- (ast/parsley-walk
+ (ast/walk
(fn [ast]
(if (is-node-the-binding-form form ast)
(add-to-binding ast value var-name)
ast))
form))
(defn let-wrap [form value var-name]
- (if (ast/parsley-binding-node? form)
+ (if (ast/binding-node? form)
(modify-existing-let-block form value var-name)
(ast/list-without-whitespace
(ast/symbol 'let)
- ast/parsley-whitespace
- (ast/parsley-vector
+ ast/whitespace
+ (ast/vector
[var-name value])
- ast/parsley-whitespace
+ ast/whitespace
form)))
(defn- wrap-function-body-with-let [defn-form value var-name]
(let [fn-def (get-function-definition defn-form)]
- (ast/parsley-tree-replace
+ (ast/tree-replace
fn-def (let-wrap fn-def value var-name)
defn-form)))
(defparsed-fn local-wrap [top value var-name]
"Extracts a value as a local variable inside top"
(-> (wrap-function-body-with-let
- (ast/parsley-tree-replace
+ (ast/tree-replace
value var-name top)
value var-name)
- ast/parsley-to-string))
+ ast/ast->string))
View
2 src/clojure_refactoring/rename.clj
@@ -8,7 +8,7 @@
(let [ast (parser/parse node)
old (symbol old-name)
new (symbol new-name)]
- (ast/parsley-to-string
+ (ast/ast->string
(ast/replace-symbol-in-ast-node
old
new
View
10 src/clojure_refactoring/support/find_bindings_above_node.clj
@@ -10,15 +10,15 @@
(defn extract-symbols-from-binding-node [ast]
(->> (extract-binding-syms ast)
- ast/parsley-sub-nodes
- (filter ast/parsley-symbol?)))
+ ast/sub-nodes
+ (filter ast/symbol?)))
(defn binding-node-that-contains? [node expr]
- (and (ast/parsley-binding-node? node)
- (ast/parsley-tree-contains node expr)))
+ (and (ast/binding-node? node)
+ (ast/tree-contains node expr)))
(defn find-bindings-above-node [node expr]
- (->> (ast/parsley-sub-nodes node)
+ (->> (ast/sub-nodes node)
(filter #(binding-node-that-contains? % expr))
(mapcat extract-symbols-from-binding-node)
distinct))
View
4 src/clojure_refactoring/support/formatter.clj
@@ -11,7 +11,7 @@
ast))
(defn add-whitespace-to-lists [ast]
- (ast/parsley-walk
+ (ast/walk
#(replace-content-by ast/add-whitespace %)
ast))
@@ -25,7 +25,7 @@
(= ast (last (ast/relevant-content toplevel))))))
(def threading-spacing
- [ast/parsley-newline ast/parsley-whitespace ast/parsley-whitespace])
+ [ast/newline ast/whitespace ast/whitespace])
(defn format-threaded [ast]
(let [with-whitespace
View
73 src/clojure_refactoring/support/parsley.clj
@@ -1,24 +1,23 @@
(ns clojure-refactoring.support.parsley
(:require [net.cgrand.parsley.glr :as glr])
- (:use clojure.walk
- clojure-refactoring.support.core
- net.cgrand.parsley
+ (:use net.cgrand.parsley
[clojure.contrib.def :only [defonce-]]
[clojure.contrib.seq-utils :only [find-first]]
[clojure.contrib.str-utils :only [str-join]])
- (:refer-clojure :exclude [symbol])
+ (:refer-clojure :exclude [symbol symbol? keyword? list vector newline conj])
(:require [clojure.core :as core])
+ (:use [clojure-refactoring.support.core :exclude [sub-nodes tree-contains?]])
(:require [clojure-refactoring.support.parser :as parser]))
(defn make-node [tag content]
{:tag tag :content content})
(defn symbol [sym]
- (make-node :atom (list (name sym))))
+ (make-node :atom (core/list (name sym))))
-(def parsley-empty-map (make-node :map (list "{" "}")))
+(def empty-map (make-node :map (core/list "{" "}")))
-(def parsley-whitespace (make-node :whitespace '(" ")))
+(def whitespace (make-node :whitespace '(" ")))
(def composite-tag? (complement
#{:atom :regex :space :var :char :string}))
@@ -27,12 +26,11 @@
(assoc ast
:content
new-content))
-
-(declare parsley-walk)
+(declare walk)
(defn- replacement-for-composite [tag f]
(if (composite-tag? tag)
- #(parsley-walk f %)
+ #(walk f %)
f))
(defn- replacement-for-content [tag f content]
@@ -47,37 +45,37 @@
ast
(replacement-for-content tag f content))))
-(defn parsley-walk [f ast]
+(defn walk [f ast]
(if (map? ast)
(f
(walk-replace-content f ast))
- (vec (map #(parsley-walk f %) ast))))
+ (vec (map #(walk f %) ast))))
-(defn- expand-ast-nodes [ast]
+(defn- expand-nodes [ast]
(if (sequential? ast)
(seq ast)
(:content ast)))
-(defn parsley-sub-nodes [ast]
+(defn sub-nodes [ast]
(tree-seq (any-of? sequential? composite-tag?)
- expand-ast-nodes
+ expand-nodes
ast))
-(defn parsley-tree-contains [ast obj]
- (some #{obj} (parsley-sub-nodes ast)))
+(defn tree-contains [ast obj]
+ (some #{obj} (sub-nodes ast)))
-(defn parsley-to-string [ast]
- (str-join "" (filter string? (parsley-sub-nodes ast))))
+(defn ast->string [ast]
+ (str-join "" (filter string? (sub-nodes ast))))
(def sexp->parsley (comp parser/parse1 format-code))
-(defn parsley-tree-replace [old new ast]
- (parsley-walk
+(defn tree-replace [old new ast]
+ (walk
(fn [node] (if (= node old) new node))
ast))
(defn replace-symbol-in-ast-node [old new ast]
- (parsley-tree-replace (symbol old) (symbol new) ast))
+ (tree-replace (symbol old) (symbol new) ast))
(defn- parsley-get-first-node [ast]
(if (map? ast) ast (first ast)))
@@ -95,13 +93,12 @@
(defn- ast-content [ast]
(str-join "" (:content ast)))
-;;symbol?
-(def parsley-symbol?
+(def symbol?
(all-of? map? parsley-atom?
- (comp symbol? read-string ast-content)))
+ (comp core/symbol? read-string ast-content)))
;;keyword?
-(def parsley-keyword?
+(def keyword?
(all-of? parsley-atom?
#(first= (ast-content %) \:)))
@@ -117,17 +114,15 @@
(interleave coll (repeat item)))
(defn add-whitespace [coll]
- (butlast (intersperse coll parsley-whitespace)))
+ (butlast (intersperse coll whitespace)))
(defn- coll-fn [tag start end elems]
(make-node tag `(~start ~@elems ~end)))
-;; list
-(defn parsley-list [coll]
+(defn list [coll]
(coll-fn :list "(" ")" (add-whitespace coll)))
-;;vector
-(defn parsley-vector [coll]
+(defn vector [coll]
(coll-fn :vector "[" "]" (add-whitespace coll)))
(defn list-without-whitespace [& elems]
@@ -136,8 +131,7 @@
(defn vector-without-whitespace [& elems]
(coll-fn :vector "[" "]" elems))
-;;newline
-(def parsley-newline (make-node :whitespace '("\n")))
+(def newline (make-node :whitespace '("\n")))
(defn first-vector [ast]
(find-first (tag= :vector) (:content ast)))
@@ -148,32 +142,31 @@
(def parsley-bindings
(comp relevant-content parsley-fn-args))
-;; conj
-(defn content-conj [{content :content :as ast} & xs]
+(defn conj [{content :content :as ast} & xs]
(replace-content ast
`(~(first content)
~@xs ~@(butlast (drop 1 content))
~(last content))))
(defn strip-whitespace [ast]
- (parsley-walk
+ (walk
(fn [node]
(if (composite-tag? (:tag node))
(replace-content node
(remove (tag= :whitespace) (:content node)))
node))
ast))
-(def empty-parsley-list (parsley-list nil))
+(def empty-list (list nil))
(def drop-first-and-last (comp rest butlast))
(def first-content (comp first :content))
(defn first-symbol [ast]
- (first-content (first (relevant-content ast))))
+ (first-content (first (relevant-content ast))))
-(def parsley-binding-node?
+(def binding-node?
(all-of? map?
(tag= :list)
(comp binding-forms core/symbol
@@ -182,7 +175,7 @@
(defn- expand-args-with-parse1 [args]
"Takes arguments from a function and returns a vector that
(in a let form) rebinds them by parsing them."
- (->> (mapcat #(list % (list 'clojure-refactoring.support.parser/parse1 %)) args) vec))
+ (->> (mapcat #(core/list % (core/list 'clojure-refactoring.support.parser/parse1 %)) args) vec))
(defmacro defparsed-fn [name args docstring & body]
"Defines a function in which all of the args are rebound by parsing them using parse1."
View
2 src/clojure_refactoring/support/replace.clj
@@ -15,7 +15,7 @@
(if (= (parsley-from-cache ns) replacement)
nil
{:file (filename-from-ns ns)
- :new-source (ast/parsley-to-string
+ :new-source (ast/ast->string
replacement)})))
(defn replace-namespaces [namespaces f]
View
8 src/clojure_refactoring/thread_expression.clj
@@ -48,18 +48,18 @@ based on what type of threading is going to be"
(let [{:keys [position-f all-but-position-f]}
(threading-fns-from-type thread-type)
useful-content (ast/relevant-content node)]
- (ast/content-conj
+ (ast/conj
new-ast
(position-f node)
(apply ast/list-without-whitespace (all-but-position-f node)))))
(defn thread-with-type [thread-type ast]
(let [{:keys [position-f all-but-position-f]}
(threading-fns-from-type thread-type)]
- (loop [node ast new-node ast/empty-parsley-list]
+ (loop [node ast new-node ast/empty-list]
(if (not-last-threading-node? node position-f)
(recur (position-f node)
- (ast/content-conj new-node (all-but-position-f node)))
+ (ast/conj new-node (all-but-position-f node)))
(finish-threading node new-node thread-type)))))
(defn thread-ast [thread-type ast]
@@ -72,7 +72,7 @@ based on what type of threading is going to be"
(->> (ast/strip-whitespace (parser/parse1 code))
(thread-ast thread-type)
format-ast
- ast/parsley-to-string))
+ ast/ast->string))
(defn thread-last [code]
(construct-threaded '->> code))
View
6 test/clojure_refactoring/destructuring_test.clj
@@ -28,17 +28,17 @@
'{:tag :atom :content ("a")})))
(deftest parsley_find_map_lookups
- (is (= (map ast/parsley-to-string
+ (is (= (map ast/ast->string
(parsley-find-lookups (parser/parse "(defn a [b] (:a b))")))
'("(:a b)"))))
(deftest parsley_lookup_to_proper_form
- (is (= (ast/parsley-to-string (parsley-lookup-to-canoninical-form
+ (is (= (ast/ast->string (parsley-lookup-to-canoninical-form
(first (parser/parse "(a :a)"))))
"(:a a)")))
(deftest add_to_parsley_map
- (is (= (ast/parsley-to-string
+ (is (= (ast/ast->string
(add-to-parsley-map '{:tag :map :content ("{" "}")}
'{:tag :atom :content ("a")}
'{:tag :atom :content ("b")})))))
View
4 test/clojure_refactoring/extract_method_test.clj
@@ -8,7 +8,7 @@
(use-fixtures :once #(time (%)))
(defn fn-call-sexp [sexp]
- (read-string (ast/parsley-to-string (fn-call (ast/sexp->parsley sexp)))))
+ (read-string (ast/ast->string (fn-call (ast/sexp->parsley sexp)))))
(defn fn-name-sexp [sexp]
(symbol (first (:content (fn-name (ast/sexp->parsley sexp))))))
@@ -20,7 +20,7 @@
(is (= (fn-call-sexp '(defn a [b] (+ 1 2))) '(a b))))
(defn remove-extracted-function-sexp [extracted toplevel new-fn]
- (ast/parsley-to-string
+ (ast/ast->string
(call-extracted
(parser/parse1 extracted)
(parser/parse1 toplevel)
View
2 test/clojure_refactoring/rename_test.clj
@@ -21,7 +21,7 @@
(deftest renaming_fn
(testing "it replaces occurences of the var name"
- (is (= (ast/parsley-to-string
+ (is (= (ast/ast->string
((renaming-fn #'a 'z)
(parser/parse
"(defn b [c] (a 1 2))")))
View
4 test/clojure_refactoring/support/formatter_test.clj
@@ -8,10 +8,10 @@
(->> (ast/sexp->parsley s)
ast/strip-whitespace
format-ast
- ast/parsley-to-string))
+ ast/ast->string))
(deftest strip-whitespace-test
- (is (= (ast/parsley-to-string
+ (is (= (ast/ast->string
(ast/strip-whitespace (ast/sexp->parsley '(+ 1 2))))
"(+12)")))
View
20 test/clojure_refactoring/support/parsley_test.clj
@@ -8,23 +8,23 @@
(use-fixtures :once #(time (%)))
(deftest parlsey_keyword
- (is (ast/parsley-keyword? (first (parser/parse ":a")))))
+ (is (ast/keyword? (first (parser/parse ":a")))))
(deftest parsley_to_string
(prop "parsley to string composed with parse is identity"
[s random-sexp-from-core]
- (is (= (ast/parsley-to-string (parser/parse s))
+ (is (= (ast/ast->string (parser/parse s))
s)))
(testing "parsley to string for each file in this project"
(doseq [file (map filename-from-ns (find-ns-in-user-dir))]
(let [slurped (memo-slurp file)]
- (is (= (ast/parsley-to-string (parser/parse slurped))
+ (is (= (ast/ast->string (parser/parse slurped))
slurped))))))
(defn parsley-rec-contains [obj ast]
"Works out if a parsley-ast contains a sexp object"
- (-> (ast/parsley-to-string ast)
+ (-> (ast/ast->string ast)
read-string
(tree-contains? obj)))
@@ -57,26 +57,26 @@
(ast/replace-symbol-in-ast-node new old))
parsed))))
- (is (= (ast/parsley-to-string
+ (is (= (ast/ast->string
(ast/replace-symbol-in-ast-node 'a 'z
(parser/parse "(defn b [c] (a 1 2))")))
"(defn b [c] (z 1 2))")))
(deftest parsley_walk
- (prop "parsley-walk with identity returns the same ast it was passe"
+ (prop "ast/walk with identity returns the same ast it was passe"
[s random-sexp-from-core]
(let [parsed (parser/parse s)]
- (is (= (ast/parsley-walk identity parsed)
+ (is (= (ast/walk identity parsed)
parsed)))))
(deftest parsley_list
- (is (= (ast/parsley-list [1 2 3])
+ (is (= (ast/list [1 2 3])
'{:tag :list, :content ("(" 1 {:tag :whitespace, :content (" ")} 2 {:tag :whitespace, :content (" ")} 3 ")")})))
(deftest parsley_vector
- (is (= (ast/parsley-vector [1 2 3])
+ (is (= (ast/vector [1 2 3])
'{:tag :vector, :content ("[" 1 {:tag :whitespace, :content (" ")} 2 {:tag :whitespace, :content (" ")} 3 "]")})))
(deftest parsley_binding_node?
- (is (ast/parsley-binding-node? (parser/parse1 "(let [a 1] a)"))))
+ (is (ast/binding-node? (parser/parse1 "(let [a 1] a)"))))

0 comments on commit d93dde9

Please sign in to comment.