Skip to content

Commit c5b6cfc

Browse files
author
Yannick Scherer
committed
make reader macro node constructors more flexible.
1 parent 18bddd1 commit c5b6cfc

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/rewrite_clj/node/reader_macro.clj

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
(ns rewrite-clj.node.reader-macro
2-
(:require [rewrite-clj.node.protocols :as node]))
2+
(:require [rewrite-clj.node
3+
[protocols :as node]
4+
[whitespace :as ws]]))
35

46
;; ## Node
57

@@ -98,28 +100,48 @@
98100
children))
99101

100102
(defn var-node
103+
"Create node representing a var.
104+
Takes either a seq of nodes or a single one."
101105
[children]
102-
(->node :var "'" "" #(list* 'var %) 1 children))
106+
(if (sequential? children)
107+
(->node :var "'" "" #(list* 'var %) 1 children)
108+
(recur [children])))
103109

104110
(defn fn-node
111+
"Create node representing an anonymous function."
105112
[children]
106113
(->node :fn "(" ")" nil nil children))
107114

108115
(defn eval-node
116+
"Create node representing an inline evaluation. (`#=...`)
117+
Takes either a seq of nodes or a single one."
109118
[children]
110-
(->node
111-
:eval "=" ""
112-
#(list 'eval (list* 'quote %))
113-
1 children))
119+
(if (sequential? children)
120+
(->node
121+
:eval "=" ""
122+
#(list 'eval (list* 'quote %))
123+
1 children)
124+
(recur [children])))
114125

115126
(defn uneval-node
127+
"Create node representing a form ignored by the reader. (`#_...`)
128+
Takes either a seq of nodes or a single one."
116129
[children]
117-
(->node :uneval "_" "" nil 1 children))
130+
(if (sequential? children)
131+
(->node :uneval "_" "" nil 1 children)
132+
(recur [children])))
118133

119134
(defn reader-macro-node
120-
[children]
121-
(->ReaderMacroNode children))
135+
"Create node representing a reader macro. (`#... ...`)"
136+
([children]
137+
(->ReaderMacroNode children))
138+
([macro-node form-node]
139+
(->ReaderMacroNode [macro-node (ws/spaces 1) form-node])))
122140

123141
(defn deref-node
142+
"Create node representing the dereferencing of a form. (`@...`)
143+
Takes either a seq of nodes or a single one."
124144
[children]
125-
(->DerefNode children))
145+
(if (sequential? children)
146+
(->DerefNode children)
147+
(->DerefNode [children])))

0 commit comments

Comments
 (0)