/
reader_macro.cljc
201 lines (171 loc) · 5.33 KB
/
reader_macro.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
(ns ^:no-doc rewrite-clj.node.reader-macro
(:require [rewrite-clj.node.protocols :as node]
[rewrite-clj.node.whitespace :as ws]))
#?(:clj (set! *warn-on-reflection* true))
;; ## Node
(defrecord ReaderNode [tag prefix suffix
sexpr-fn sexpr-count
children]
node/Node
(tag [_node] tag)
(node-type [_node] :reader)
(printable-only? [_node]
(not sexpr-fn))
(sexpr* [_node opts]
(if sexpr-fn
(sexpr-fn (node/sexprs children opts))
(throw (ex-info "unsupported operation" {}))))
(length [_node]
(-> (node/sum-lengths children)
(+ 1 (count prefix) (count suffix))))
(string [_node]
(str "#" prefix (node/concat-strings children) suffix))
node/InnerNode
(inner? [_node] true)
(children [_node] children)
(replace-children [this children']
(when sexpr-count
(node/assert-sexpr-count children' sexpr-count))
(assoc this :children children'))
(leader-length [_node]
(inc (count prefix)))
Object
(toString [node]
(node/string node)))
(defrecord ReaderMacroNode [children]
node/Node
(tag [_node] :reader-macro)
(node-type [_node] :reader-macro)
(printable-only?[_node] false)
(sexpr* [node _opts]
(list 'read-string (node/string node)))
(length [_node]
(inc (node/sum-lengths children)))
(string [_node]
(str "#" (node/concat-strings children)))
node/InnerNode
(inner? [_node] true)
(children [_node] children)
(replace-children [this children']
(node/assert-sexpr-count children' 2)
(assoc this :children children'))
(leader-length [_node] 1)
Object
(toString [node]
(node/string node)))
(defrecord DerefNode [children]
node/Node
(tag [_node] :deref)
(node-type [_node] :deref)
(printable-only?[_node] false)
(sexpr* [_node opts]
(list* 'deref (node/sexprs children opts)))
(length [_node]
(inc (node/sum-lengths children)))
(string [_node]
(str "@" (node/concat-strings children)))
node/InnerNode
(inner? [_node] true)
(children [_node] children)
(replace-children [this children']
(node/assert-sexpr-count children' 1)
(assoc this :children children'))
(leader-length [_node] 1)
Object
(toString [node]
(node/string node)))
(node/make-printable! ReaderNode)
(node/make-printable! ReaderMacroNode)
(node/make-printable! DerefNode)
;; ## Constructors
(defn- ->node
[tag prefix suffix sexpr-fn sexpr-count children]
(when sexpr-count
(node/assert-sexpr-count children sexpr-count))
(->ReaderNode
tag prefix suffix
sexpr-fn sexpr-count
children))
(defn var-node
"Create node representing a var where `children` is either a
sequence of nodes or a single node.
```Clojure
(require '[rewrite-clj.node :as n])
(-> (n/var-node (n/token-node 'my-var))
n/string)
;; => \"#'my-var\"
;; specifying a sequence allows for whitespace between the
;; prefix and the var
(-> (n/var-node [(n/spaces 2)
(n/token-node 'my-var)])
n/string)
;; => \"#' my-var\"
```"
[children]
(if (sequential? children)
(->node :var "'" "" #(list* 'var %) 1 children)
(recur [children])))
(defn eval-node
"Create node representing an inline evaluation
where `children` is either a sequence of nodes or a single node.
```Clojure
(require '[rewrite-clj.node :as n])
(-> (n/eval-node (n/list-node [(n/token-node 'inc)
(n/spaces 1)
(n/token-node 1)]))
n/string)
;; => \"#=(inc 1)\"
;; specifying a sequence allows for whitespace between the
;; prefix and the form
(-> (n/eval-node [(n/spaces 3)
(n/list-node [(n/token-node 'inc)
(n/spaces 1)
(n/token-node 1)])])
n/string)
;; => \"#= (inc 1)\"
```"
[children]
(if (sequential? children)
(->node
:eval "=" ""
#(list 'eval (list* 'quote %))
1 children)
(recur [children])))
(defn reader-macro-node
"Create node representing a reader macro with `macro-node` and `form-node` or `children`.
```Clojure
(require '[rewrite-clj.node :as n])
;; here we call with macro-node and form-node
(-> (n/reader-macro-node (n/token-node 'my-macro)
(n/token-node 42))
n/string)
;; => \"#my-macro 42\"
;; calling with a sequence of children gives us control over whitespace
(-> (n/reader-macro-node [(n/token-node 'my-macro)
(n/spaces 4)
(n/token-node 42)])
n/string)
;; => \"#my-macro 42\"
```"
([children]
(->ReaderMacroNode children))
([macro-node form-node]
(->ReaderMacroNode [macro-node (ws/spaces 1) form-node])))
(defn deref-node
"Create node representing the dereferencing of a form
where `children` is either a sequence of nodes or a single node.
```Clojure
(require '[rewrite-clj.node :as n])
(-> (n/deref-node (n/token-node 'my-var))
n/string)
;; => \"@my-var\"
;; specifying a sequence allows for whitespace between @ and form
(-> (n/deref-node [(n/spaces 2)
(n/token-node 'my-var)])
n/string)
;; => \"@ my-var\"
```"
[children]
(if (sequential? children)
(->DerefNode children)
(->DerefNode [children])))