@@ -11,10 +11,11 @@ assoc!(d, k, v) = (d[k] = v; d)
11
11
"""
12
12
@esc x y
13
13
14
- is the same as
15
-
16
- x = esc(x)
17
- y = esc(y)
14
+ is the same as:
15
+ ```julia
16
+ x = esc(x)
17
+ y = esc(y)
18
+ ```
18
19
"""
19
20
macro esc (xs... )
20
21
:($ ([:($ x = esc ($ x)) for x in map (esc, xs)]. .. );)
26
27
Like the `quote` keyword but doesn't insert line numbers from the construction
27
28
site. e.g. compare `@q begin end` with `quote end`. Line numbers of interpolated
28
29
expressions are preserverd.
30
+
31
+ See also: [`rmlines`](@ref)
29
32
"""
30
33
macro q (ex)
31
34
Expr (:quote , striplines (ex))
@@ -65,6 +68,8 @@ To work with nested blocks:
65
68
```julia
66
69
prewalk(rmlines, ex)
67
70
```
71
+
72
+ See also: [`@q`](@ref)
68
73
"""
69
74
rmlines (x) = x
70
75
function rmlines (x:: Expr )
@@ -108,8 +113,9 @@ walk(x::Expr, inner, outer) = outer(Expr(x.head, map(inner, x.args)...))
108
113
postwalk(f, expr)
109
114
110
115
Applies `f` to each node in the given expression tree, returning the result.
111
- `f` sees expressions *after* they have been transformed by the walk. See also
112
- `prewalk`.
116
+ `f` sees expressions *after* they have been transformed by the walk.
117
+
118
+ See also: [`prewalk`](@ref).
113
119
"""
114
120
postwalk (f, x) = walk (x, x -> postwalk (f, x), f)
115
121
@@ -121,7 +127,7 @@ Applies `f` to each node in the given expression tree, returning the result.
121
127
walk will be applied to whatever `f` returns.
122
128
123
129
This makes `prewalk` somewhat prone to infinite loops; you probably want to try
124
- `postwalk` first.
130
+ [ `postwalk`](@ref) first.
125
131
"""
126
132
prewalk (f, x) = walk (f (x), x -> prewalk (f, x), identity)
127
133
@@ -133,7 +139,9 @@ replace(ex, s, s′) = prewalk(x -> x == s ? s′ : x, ex)
133
139
Simple expression match; will return `true` if the expression `x` can be found
134
140
inside `expr`.
135
141
136
- inexpr(:(2+2), 2) == true
142
+ ```julia
143
+ inexpr(:(2+2), 2) == true
144
+ ```
137
145
"""
138
146
function inexpr (ex, x)
139
147
result = false
@@ -162,11 +170,13 @@ end
162
170
163
171
Replaces gensyms with unique ids (deterministically).
164
172
165
- julia> x, y = gensym("x"), gensym("y")
166
- (Symbol("##x#363"), Symbol("##y#364"))
173
+ ```julia
174
+ julia> x, y = gensym("x"), gensym("y")
175
+ (Symbol("##x#363"), Symbol("##y#364"))
167
176
168
- julia> MacroTools.gensym_ids(:(\$ x+\$ y))
169
- :(x_1 + y_2)
177
+ julia> MacroTools.gensym_ids(:(\$ x+\$ y))
178
+ :(x_1 + y_2)
179
+ ```
170
180
"""
171
181
function gensym_ids (ex)
172
182
counter = 0
@@ -184,11 +194,13 @@ end
184
194
Replaces gensyms with animal names.
185
195
This makes gensym'd code far easier to follow.
186
196
187
- julia> x, y = gensym("x"), gensym("y")
188
- (Symbol("##x#363"), Symbol("##y#364"))
197
+ ```julia
198
+ julia> x, y = gensym("x"), gensym("y")
199
+ (Symbol("##x#363"), Symbol("##y#364"))
189
200
190
- julia> MacroTools.alias_gensyms(:(\$ x+\$ y))
191
- :(porcupine + gull)
201
+ julia> MacroTools.alias_gensyms(:(\$ x+\$ y))
202
+ :(porcupine + gull)
203
+ ```
192
204
"""
193
205
function alias_gensyms (ex)
194
206
left = copy (animals)
201
213
"""
202
214
More convenient macro expansion, e.g.
203
215
204
- @expand @time foo()
216
+ ```julia
217
+ @expand @time foo()
218
+ ```
205
219
"""
206
220
@static if VERSION <= v " 0.7.0-DEV.484"
207
221
macro expand (ex)
@@ -248,7 +262,10 @@ function shortdef1(ex)
248
262
end
249
263
shortdef (ex) = prewalk (shortdef1, ex)
250
264
251
- """ `gatherwheres(:(f(x::T, y::U) where T where U)) => (:(f(x::T, y::U)), (:U, :T))`
265
+ """
266
+ ```julia
267
+ gatherwheres(:(f(x::T, y::U) where T where U)) == (:(f(x::T, y::U)), (:U, :T))
268
+ ```
252
269
"""
253
270
function gatherwheres (ex)
254
271
if @capture (ex, (f_ where {params1__}))
@@ -272,14 +289,16 @@ end
272
289
and return `Dict(:name=>..., :args=>..., etc.)`. The definition can be rebuilt by
273
290
calling `MacroTools.combinedef(dict)`, or explicitly with
274
291
275
- ```
292
+ ```julia
276
293
rtype = get(dict, :rtype, :Any)
277
294
all_params = [get(dict, :params, [])..., get(dict, :whereparams, [])...]
278
295
:(function \$ (dict[:name]){\$ (all_params...)}(\$ (dict[:args]...);
279
296
\$ (dict[:kwargs]...))::\$ rtype
280
297
\$ (dict[:body])
281
298
end)
282
299
```
300
+
301
+ See also: [`combinedef`](@ref)
283
302
"""
284
303
function splitdef (fdef)
285
304
error_msg = " Not a function definition: $(repr (fdef)) "
321
340
"""
322
341
combinedef(dict::Dict)
323
342
324
- `combinedef` is the inverse of `splitdef`. It takes a splitdef-like Dict
325
- and returns a function definition. """
343
+ `combinedef` is the inverse of [`splitdef`](@ref). It takes a `splitdef`-like Dict
344
+ and returns a function definition.
345
+ """
326
346
function combinedef (dict:: Dict )
327
347
rtype = get (dict, :rtype , nothing )
328
348
params = get (dict, :params , [])
383
403
"""
384
404
combinearg(arg_name, arg_type, is_splat, default)
385
405
386
- `combinearg` is the inverse of `splitarg`. """
406
+ `combinearg` is the inverse of [`splitarg`](@ref).
407
+ """
387
408
function combinearg (arg_name, arg_type, is_splat, default)
388
409
a = arg_name=== nothing ? :(:: $arg_type ) : :($ arg_name:: $arg_type )
389
410
a2 = is_splat ? Expr (:... , a) : a
@@ -405,13 +426,15 @@ Match function arguments (whether from a definition or a function call) such as
405
426
`default` are `nothing` when they are absent. For example:
406
427
407
428
```julia
408
- > map(splitarg, (:(f(a=2, x::Int=nothing, y, args...))).args[2:end])
429
+ julia > map(splitarg, (:(f(a=2, x::Int=nothing, y, args...))).args[2:end])
409
430
4-element Array{Tuple{Symbol,Symbol,Bool,Any},1}:
410
431
(:a, :Any, false, 2)
411
432
(:x, :Int, false, :nothing)
412
433
(:y, :Any, false, nothing)
413
434
(:args, :Any, true, nothing)
414
435
```
436
+
437
+ See also: [`combinearg`](@ref)
415
438
"""
416
439
function splitarg (arg_expr)
417
440
splitvar (arg) =
0 commit comments