Skip to content

Commit ecfa190

Browse files
authored
add support for :bracescat literals (#6)
1 parent dea0bdd commit ecfa190

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ julia> 2.6 - 0.7 - 1.9
196196

197197
Few more literals can be substituted: arrays and tuples, and the `{}` vector
198198
syntax, which are specified respectively as `:vect`, `:tuple`, `:braces`.
199+
Vectors entered with `{}` delimiters but with elements separated with a newline
200+
instead of `,` can be specified as `:bracescat`.
201+
199202
For example:
200203
```julia
201204
julia> swapliterals!(:vect => :Set)
@@ -222,13 +225,19 @@ which is used to transform the Julia AST:
222225
```julia
223226
julia> makeset(ex) = Expr(:call, :Set, Expr(:vect, ex.args...));
224227

225-
julia> swapliterals!(:braces => makeset)
228+
julia> swapliterals!(:braces => makeset, :bracescat => makeset)
226229

227230
julia> {1, 2, 3}
228231
Set{Int64} with 3 elements:
229232
2
230233
3
231234
1
235+
236+
julia> { 1
237+
2 }
238+
Set{Int64} with 2 elements:
239+
2
240+
1
232241
```
233242

234243
For types which are stored directly in the AST, using a symbol or

SwapLiterals/src/SwapLiterals.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function literals_swapper(swaps)
4747
foreach(swaps) do kv
4848
kv[1] Any[Float32,Float64,Int32,Int64,String,Char,
4949
Base.BitUnsigned64_types...,Int128,UInt128,BigInt,
50-
:braces, :tuple, :vect, :(:=)] ||
50+
:braces, :bracescat, :tuple, :vect, :(:=)] ||
5151
throw(ArgumentError("type $(kv[1]) cannot be replaced"))
5252
end
5353

@@ -104,7 +104,7 @@ function literals_swapper(swaps)
104104
swap(ex)
105105
end
106106
end
107-
elseif h (:braces, :tuple, :vect, :(:=))
107+
elseif h (:braces, :bracescat, :tuple, :vect, :(:=))
108108
ex = recswap(ex)
109109
swap = get(swaps, h, nothing)
110110
if swap === nothing

SwapLiterals/test/runtests.jl

+6
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ makecoloneq(ex) = Expr(:(=),
291291

292292
# :braces, :tuple, :vect
293293
@swapliterals [:braces => makeset,
294+
:bracescat => makeset,
294295
:tuple => makeset,
295296
:vect => makeset
296297
] begin
@@ -304,6 +305,11 @@ makecoloneq(ex) = Expr(:(=),
304305
s = (1, 2, 3)
305306
@test s isa Set{Int}
306307
@test s == r
308+
s = { 1
309+
2
310+
3 }
311+
@test s isa Set{Int}
312+
@test s == r
307313
end
308314
@swapliterals :tuple => :collect begin
309315
v = (1, 2, 3)

0 commit comments

Comments
 (0)