|
1 | 1 | """
|
2 |
| - substitute_underscores(expr, var) -> new, changed |
| 2 | + substitute_underscores(expr, var, right=false) -> new, changed |
3 | 3 |
|
4 | 4 | Replace all occurrences of `_` in `expr` with `var` and return the new expression and a Bool
|
5 | 5 | indicating whether the expression was changed.
|
| 6 | +
|
| 7 | +If `right` is `true`, then expr is in an approximation of r-value position |
6 | 8 | """
|
7 |
| -substitute_underscores(f::Symbol, var::Symbol) = f === :_ ? (var, true) : (f, false) |
8 |
| -substitute_underscores(f, ::Symbol) = f, false |
9 |
| -function substitute_underscores(ex::Expr, var::Symbol) |
| 9 | +substitute_underscores(ex, var::Symbol) = substitute_underscores(ex, var, true) |
| 10 | +substitute_underscores(s::Symbol, var::Symbol, right) = right && s === :_ ? (var, true) : (s, false) |
| 11 | +substitute_underscores(s, ::Symbol, _) = s, false |
| 12 | +function substitute_underscores(ex::Expr, var::Symbol, right) |
10 | 13 | changed = false
|
11 | 14 | args = similar(ex.args)
|
12 |
| - i = firstindex(args) |
13 |
| - if ex.head in (:(=), :->, :function) |
14 |
| - args[i] = ex.args[i] |
15 |
| - i += 1 |
16 |
| - end |
17 |
| - for i in i:lastindex(args) |
18 |
| - args[i], c = substitute_underscores(ex.args[i], var) |
| 15 | + for i in eachindex(args) |
| 16 | + force_left = (i == 1 && ex.head in (:(=), :->, :function)) |
| 17 | + force_right = ex.head == :(::) && i == 2 || ex.head in (:ref, :.) |
| 18 | + args[i], c = substitute_underscores(ex.args[i], var, force_right || right && !force_left) |
19 | 19 | changed |= c
|
20 | 20 | end
|
21 | 21 | changed ? exprarray(ex.head, args) : ex, changed
|
|
0 commit comments