@@ -18,39 +18,60 @@ __Leiningen ([via Clojars](http://clojars.org/rewrite-clj))__
18
18
19
19
[ ![ Clojars Project] ( http://clojars.org/rewrite-clj/latest-version.svg )] ( http://clojars.org/rewrite-clj )
20
20
21
+ Auto-generated documentation can be found [ here] ( http://xsc.github.io/rewrite-clj/ ) .
22
+
21
23
### Parsing Data
22
24
23
- The parser relies on [ clojure.tools.reader] ( https://github.com/clojure/tools.reader ) when handling simple
24
- tokens. It generates a structure of nested vectors whose first elements represent the kind of data
25
- contained (` :token ` , ` :whitespace ` , ` :comment ` , ` :list ` , ...).
25
+ The parser relies on [ clojure.tools.reader] ( https://github.com/clojure/tools.reader ) when
26
+ handling simple tokens and generates a custom node type representing EDN forms:
26
27
27
28
``` clojure
28
29
(require '[rewrite-clj.parser :as p])
29
- (p/parse-string " (defn my-function [a]\n (* a 3))" )
30
- ; ; =>
31
- ; ; [:list
32
- ; ; [:token defn] [:whitespace " "] [:token my-function] [:whitespace " "]
33
- ; ; [:vector [:token a]] [:newline "\n"] [:whitespace " "]
34
- ; ; [:list
35
- ; ; [:token *] [:whitespace " "] [:token a] [:whitespace " "] [:token 3]]]
30
+
31
+ (p/parse-string " (defn my-function [a]\n (* a 3))"
32
+ ; ; => <list:
33
+ ; ; (defn my-function [a]
34
+ ; ; (* a 3))
35
+ ; ; >
36
36
```
37
37
38
- ### Printing Data
38
+ These nodes can be analysed using functions in `rewrite-clj.node`:
39
+
40
+ ```clojure
41
+ (require '[rewrite-clj.node :as n])
42
+
43
+ (n/tag form) ; ; => :list
44
+ (n/children form) ; ; => (<token: defn> <whitespace: " "> <token: my-function> ...)
45
+ (n/sexpr form) ; ; => (defn my-function [a] (* a 3))
46
+ (n/child-sexprs form) ; ; => (defn my-function [a] (* a 3))
47
+ ```
39
48
40
- The printer incorporates whitespaces and comments in its output.
49
+ To convert the structure back to a printable string, use:
41
50
42
51
```clojure
43
- (require '[rewrite-clj.printer :as prn])
44
- (prn/print-edn (p/parse-string " (defn my-function [a]\n (* a 3))" ))
45
- ; ; (defn my-function [a]
46
- ; ; (* a 3))
47
- ; ; => nil
52
+ (n/string form) ; ; => "(defn my-function [a]\n (* a 3))"
53
+ ```
54
+
55
+ You can create a node from nearly any value using `coerce`:
56
+
57
+ ```
58
+ (n/coerce '[a b c]) ; ; => <vector: [a b c]>
59
+ ```
60
+
61
+ Alternatively, by hand:
62
+
63
+ ```
64
+ (n/meta-node
65
+ (n/token-node :private )
66
+ (n/token-node 'sym))
67
+ ; ; => <meta: ^:private sym>
48
68
```
49
69
50
70
### Clojure Zipper
51
71
52
- To traverse/modify the generated structure you can use rewrite-clj's whitespace-/comment-/value-aware zipper
53
- operations, based on [ fast-zip] ( https://github.com/akhudek/fast-zip ) .
72
+ To traverse/modify the generated structure you can use rewrite-clj's
73
+ whitespace-/comment-/value-aware zipper operations, based on
74
+ [fast-zip](https://github.com/akhudek/fast-zip ).
54
75
55
76
```clojure
56
77
(require '[rewrite-clj.zip :as z])
@@ -61,7 +82,7 @@ operations, based on [fast-zip](https://github.com/akhudek/fast-zip).
61
82
(def data (z/of-string data-string))
62
83
63
84
(z/sexpr data) ; ; => (defn my-function [a] (* a 3))
64
- (-> data z/down z/right z/node) ; ; => [: token my-function]
85
+ (-> data z/down z/right z/node) ; ; => < token: my-function>
65
86
(-> data z/down z/right z/sexpr) ; ; => my-function
66
87
67
88
(-> data z/down z/right (z/edit (comp symbol str) " 2" ) z/up z/sexpr)
@@ -74,26 +95,28 @@ operations, based on [fast-zip](https://github.com/akhudek/fast-zip).
74
95
; ; => nil
75
96
```
76
97
77
- ` rewrite-clj.zip/edit ` and ` rewrite-clj.zip/replace ` try to facilitate their use by transparently converting
78
- between the node's internal representation (` [:token my-function] ` ) and its corresponding s-expression (` my-function ` ).
98
+ `rewrite-clj.zip/edit` and `rewrite-clj.zip/replace` try to facilitate their use
99
+ by transparently coercing between the node's internal representation (`<token: my-function>`)
100
+ and its corresponding s-expression (`my-function` ).
79
101
80
102
## Sweet Code Traversal
81
103
82
104
### Example
83
105
84
- ` rewrite-clj.zip ` offers a series of ` find ` operations that can be used to determine specific positions in the code.
85
- For example, you might want to modify a ` project.clj ` of the following form by replacing the ` :description ` placeholder
86
- text with something meaningful:
106
+ `rewrite-clj.zip` offers a series of `find` operations that can be used to determine specific
107
+ positions in the code. For example, you might want to modify a `project.clj` of the following
108
+ form by replacing the `:description` placeholder text with something meaningful:
87
109
88
110
```clojure
89
111
(defproject my-project " 0.1.0-SNAPSHOT"
90
112
:description " Enter description"
91
113
... )
92
114
```
93
115
94
- Most find operations take an optional movement function as parameter. If you wanted to perform a depth-first search you'd
95
- use ` rewrite-clj.zip/next ` , if you wanted to look for something on the same level as the current location, you'd employ
96
- ` rewrite-clj.zip/right ` (the default) or ` rewrite-clj.zip/left ` .
116
+ Most find operations take an optional movement function as parameter. If you wanted to perform
117
+ a depth-first search you'd use `rewrite-clj.zip/next`, if you wanted to look for something on
118
+ the same level as the current location, you'd employ `rewrite-clj.zip/right` (the default ) or
119
+ `rewrite-clj.zip/left`.
97
120
98
121
Now, to enter the project map, you'd look for the symbol `defproject` in a depth-first way:
99
122
@@ -119,25 +142,13 @@ Replace it, zip up and print the result:
119
142
; ; => nil
120
143
```
121
144
122
- ### Searching the Tree
123
-
124
- Search functions include:
125
-
126
- - ` (find zloc [f] p?) ` : find the first match for the given predicate by repeatedly applying ` f ` to the current zipper
127
- location (default movement: ` rewrite-clj.zip/right ` ). This might return ` zloc ` itself.
128
- - ` (find-next zloc [f] p?) ` : find the next match for the given predicate by repeatedly applying ` f ` to the current zipper
129
- location (default movement: ` rewrite-clj.zip/right ` ). This will not return ` zloc ` itself.
130
- - ` (find-tag zloc [f] t) ` : uses ` find ` to get the first node with the given tag.
131
- - ` (find-next-tag zloc [f] t) ` : uses ` find-next ` to get the first node with the given tag.
132
- - ` (find-token zloc [f] p?) ` : like ` find ` but will only check ` :token ` nodes. The predicate is applied to the node's value.
133
- - ` (find-next-token zloc [f] p?) ` : like ` find-next ` but will only check ` :token ` nodes.
134
- - ` (find-value zloc [f] v) ` : uses ` find ` to get the first ` :token ` node with the given value.
135
- - ` (find-next-value zloc [f] v) ` : uses ` find-next ` to get the first ` :token ` node with the given value.
145
+ See the [auto-generated documentation](http://xsc.github.io/rewrite-clj/ ) for more information.
136
146
137
147
### Handling Clojure Data Structures
138
148
139
- rewrite-clj aims at providing easy ways to work with Clojure data structures. It offers functions corresponding
140
- to the standard seq functions designed to work with zipper nodes containing said structures, e.g.:
149
+ rewrite-clj aims at providing easy ways to work with Clojure data structures. It offers
150
+ functions corresponding to the standard seq functions designed to work with zipper nodes
151
+ containing said structures, e.g.:
141
152
142
153
```clojure
143
154
(def data (z/of-string " [1\n 2\n 3]" ))
@@ -151,16 +162,7 @@ to the standard seq functions designed to work with zipper nodes containing said
151
162
; ; => "[5\n6\n7]"
152
163
```
153
164
154
- The following functions exist:
155
-
156
- - ` map ` : takes a function to be applied to the zipper nodes of the seq's values, has to return the
157
- modified zipper node. If a ` :map ` node is supplied, the value nodes will be iterated over. Returns
158
- the supplied node incorporating all changes.
159
- - ` map-keys ` : Iterate over the key nodes of a ` :map ` node.
160
- - ` get ` : can be applied to ` :map ` nodes (with a key) or ` :vector ` /` :list ` /` :set ` nodes (with a numerical index)
161
- and will return the desired zipper location.
162
- - ` assoc ` : will replace the value at the location obtained via ` get ` .
163
- - ` seq? ` , ` map? ` , ` vector? ` , ` list? ` , ` set? ` : check the type of the given zipper node.
165
+ See the [auto-generated documentation](http://xsc.github.io/rewrite-clj/ ) for more information.
164
166
165
167
## License
166
168
0 commit comments