From ca8fac3ecbac1865364dac3923067eb2d09648fb Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Fri, 11 Mar 2022 15:23:23 +0100 Subject: [PATCH] Support block syntax: at-enumx begin; ...; end. --- src/EnumX.jl | 12 +++++++++--- test/runtests.jl | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/EnumX.jl b/src/EnumX.jl index 1c54447..9330dfd 100644 --- a/src/EnumX.jl +++ b/src/EnumX.jl @@ -21,11 +21,17 @@ function enumx(_module_, name, args...) throw(ArgumentError("invalid EnumX.@enumx type specification: $(name)")) end name = modname + if length(args) == 1 && args[1] isa Expr && args[1].head === :block + syms = args[1].args + else + syms = args + end namemap = Dict{baseT,Symbol}() next = 0 - for arg in args - @assert arg isa Symbol # TODO - namemap[next] = arg + for s in syms + s isa LineNumberNode && continue + s isa Symbol || throw(ArgumentError("invalid member expression: $(s)")) + namemap[next] = s next += 1 end module_block = quote diff --git a/test/runtests.jl b/test/runtests.jl index 6f92726..9a03220 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -96,4 +96,22 @@ catch err @test err.msg == "invalid EnumX.@enumx type specification: Fr + uit" end + +# Block syntax +@enumx FruitBlock begin + Apple + Banana +end +@test FruitBlock.Type <: EnumX.Enum{Int32} <: Base.Enum{Int32} +@test FruitBlock.Apple === FruitBlock.Type(0) +@test FruitBlock.Banana === FruitBlock.Type(1) + +@enumx FruitBlock8::Int8 begin + Apple + Banana +end +@test FruitBlock8.Type <: EnumX.Enum{Int8} <: Base.Enum{Int8} +@test FruitBlock8.Apple === FruitBlock8.Type(0) +@test FruitBlock8.Banana === FruitBlock8.Type(1) + end # testset