From 3a637eb1caa2a1c3fb63fb071bec2e18048d563b Mon Sep 17 00:00:00 2001 From: Peter Deffebach Date: Sun, 20 Jun 2021 10:49:44 -0400 Subject: [PATCH 1/4] all done --- Project.toml | 1 + src/DataFramesMeta.jl | 2 ++ src/parsing.jl | 21 ++++++--------------- test/dataframes.jl | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/Project.toml b/Project.toml index 833dbe96..d82e1be9 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ version = "0.7.0" [deps] DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" +MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" [compat] diff --git a/src/DataFramesMeta.jl b/src/DataFramesMeta.jl index 321bd171..6b7299ce 100644 --- a/src/DataFramesMeta.jl +++ b/src/DataFramesMeta.jl @@ -2,6 +2,8 @@ module DataFramesMeta using Reexport +using MacroTools + @reexport using DataFrames # Basics: diff --git a/src/parsing.jl b/src/parsing.jl index 1042266a..df00b6ee 100644 --- a/src/parsing.jl +++ b/src/parsing.jl @@ -118,17 +118,15 @@ julia> MacroTools.prettify(fun) """ function get_source_fun(function_expr; wrap_byrow::Bool=false) + function_expr = MacroTools.unblock(function_expr) + # recursive step for begin :a + :b end if is_macro_head(function_expr, "@byrow") if wrap_byrow throw(ArgumentError("Redundant `@byrow` calls.")) end return get_source_fun(function_expr.args[3], wrap_byrow=true) - elseif function_expr isa Expr && - function_expr.head == :block && - length(function_expr.args) == 1 - return get_source_fun(function_expr.args[1]) elseif is_simple_non_broadcast_call(function_expr) source = args_to_selectors(function_expr.args[2:end]) fun_t = function_expr.args[1] @@ -230,13 +228,7 @@ function fun_to_vec(ex::Expr; nolhs::Bool = false, gensym_names::Bool = false, w if !nokw lhs = ex.args[1] - rhs_t = ex.args[2] - # if lhs is a cols(y) then the rhs gets parsed as a block - if onearg(lhs, :cols) && rhs_t.head === :block && length(rhs_t.args) == 1 - rhs = rhs_t.args[1] - else - rhs = rhs_t - end + rhs = MacroTools.unblock(ex.args[2]) else throw(ArgumentError("This path should not be reached")) end @@ -281,7 +273,6 @@ function fun_to_vec(ex::Expr; nolhs::Bool = false, gensym_names::Bool = false, w if onearg(lhs, :cols) && onearg(rhs, :cols) source = rhs.args[2] dest = lhs.args[2] - return quote $source => $dest end @@ -347,7 +338,7 @@ of expression-like object (`Expr`, `QuoteNode`, etc.), puts them into a single array, removing line numbers. """ function create_args_vector(args...) - Any[Base.remove_linenums!(arg) for arg in args], false + create_args_vector(Expr(:block, args...)) end """ @@ -380,9 +371,9 @@ function create_args_vector(arg) end if arg isa Expr && arg.head == :block - x = Base.remove_linenums!(arg).args + x = MacroTools.rmlines(arg).args else - x = Any[Base.remove_linenums!(arg)] + x = Any[arg] end if wrap_byrow && any(t -> is_macro_head(t, "@byrow"), x) diff --git a/test/dataframes.jl b/test/dataframes.jl index 2d69b9cb..d48b75d2 100644 --- a/test/dataframes.jl +++ b/test/dataframes.jl @@ -728,4 +728,47 @@ end @test @transform(df, cols("X Y Z")) == df end +macro linenums_macro(arg) + if arg isa Expr && arg.head == :block && length(arg.args) == 1 && arg.args[1] isa LineNumberNode + esc(:(true)) + else + esc(:(false)) + end +end + +@testset "removing lines" begin + df = DataFrame(a = [1], b = [2]) + # Can't use @test because @test remove line numbers + d = @transform(df, y = @linenums_macro begin end) + @test d.y == [true] + + d = @transform df begin + y = @linenums_macro begin end + end + + @test d.y == [true] + + d = @transform df @byrow begin + y = @linenums_macro begin end + end + + @test d.y == [true] + + d = @where(df, @linenums_macro begin end) + + @test nrow(d) == 1 + + d = @where df begin + @byrow @linenums_macro begin end + end + + @test nrow(d) == 1 + + d = @where df @byrow begin + @linenums_macro begin end + end + + @test nrow(d) == 1 +end + end # module From 6f894f5cd62a6fec872ba719c0d4a86be60df407 Mon Sep 17 00:00:00 2001 From: pdeffebach <23196228+pdeffebach@users.noreply.github.com> Date: Mon, 21 Jun 2021 13:42:38 -0400 Subject: [PATCH 2/4] Update test/dataframes.jl Co-authored-by: Milan Bouchet-Valat --- test/dataframes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dataframes.jl b/test/dataframes.jl index d48b75d2..571c4667 100644 --- a/test/dataframes.jl +++ b/test/dataframes.jl @@ -764,7 +764,7 @@ end @test nrow(d) == 1 - d = @where df @byrow begin + d = @where df @byrow begin @linenums_macro begin end end From 188dcbe98c876e21111565cf7eeed8aac44ce442 Mon Sep 17 00:00:00 2001 From: pdeffebach <23196228+pdeffebach@users.noreply.github.com> Date: Mon, 21 Jun 2021 13:42:44 -0400 Subject: [PATCH 3/4] Update src/parsing.jl Co-authored-by: Milan Bouchet-Valat --- src/parsing.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/parsing.jl b/src/parsing.jl index df00b6ee..fae1a909 100644 --- a/src/parsing.jl +++ b/src/parsing.jl @@ -126,7 +126,6 @@ function get_source_fun(function_expr; wrap_byrow::Bool=false) throw(ArgumentError("Redundant `@byrow` calls.")) end return get_source_fun(function_expr.args[3], wrap_byrow=true) - elseif is_simple_non_broadcast_call(function_expr) source = args_to_selectors(function_expr.args[2:end]) fun_t = function_expr.args[1] From 4e278155a4f2826d1114391473630f817ed1e139 Mon Sep 17 00:00:00 2001 From: Peter Deffebach Date: Mon, 21 Jun 2021 10:34:54 -0400 Subject: [PATCH 4/4] compat entry --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index d82e1be9..c8d1311a 100644 --- a/Project.toml +++ b/Project.toml @@ -11,6 +11,7 @@ Reexport = "189a3867-3050-52da-a836-e630ba90ab69" DataFrames = "0.21, 0.22, 1" Reexport = "0.2, 1" julia = "1" +MacroTools = "0.5" [extras] CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"