Skip to content

Commit

Permalink
Merge pull request #69 from TidierOrg/create-escaped-symbols-list
Browse files Browse the repository at this point in the history
Updated parsing engine to enable correct auto-escaping behavior for all reserved names in the Base and Core modules.
  • Loading branch information
Karandeep Singh authored Dec 12, 2023
2 parents 6a550d5 + 171d6af commit 2f7c9f2
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# TidierData.jl updates

## v0.14.0 - 2023-12-12
- Update parsing engine so that non-function reserved names from the Base and Core modules (like `missing`, `pi`, and `Real`) are auto-escaped now, with the exception of names in the not_escaped[] array, which are never escaped
- Add `collect()` to not_vectorized[] array

## v0.13.5 - 2023-12-05
- `@summarize()` and `@summarise()` now perform auto-vectorization in the same way as `@mutate()`, meaning that the top-level macros are now all consistent in their treatment of auto-vectorization.
- Update documentation to describe new auto-vectorization behavior and give an example of how to modify the `TidierData.not_vectorized[]` array.
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TidierData"
uuid = "fe2206b3-d496-4ee9-a338-6a095c4ece80"
authors = ["Karandeep Singh"]
version = "0.13.5"
version = "0.14.0"

[deps]
Chain = "8be319e6-bccf-4806-a6f7-6fae938471bc"
Expand Down
10 changes: 5 additions & 5 deletions docs/examples/UserGuide/interpolation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,21 @@ df = DataFrame(radius = 1:5)
@mutate(area = !!pi * radius^2)
end

# ## Alternative interpolation syntax

# While interpolation using `!!` is concise and handy, it's not required. You can also access user-defined globals and global constant variables using the following syntax:
# As of v0.14.0, global constants defined within the Base or Core modules (like `missing`, `pi`, and `Real` can be directly referenced without any `!!`)

@chain df begin
@mutate(area = esc(pi) * radius^2)
@mutate(area = pi * radius^2)
end

# ## Alternative interpolation syntax

# Since we know that `pi` is defined in the `Main` module, we can also access it using `Main.pi`.

@chain df begin
@mutate(area = Main.pi * radius^2)
end

# The key lesson with interpolation is that any bare unquoted variable is assumed to refer to a column name in the DataFrame. If you are referring to any variable outside of the DataFrame, you need to either use `!!variable`, `esc(variable)`, or `[Module_name_here].variable` syntax to refer to this variable.
# The key lesson with interpolation is that any bare unquoted variable is assumed to refer to a column name in the DataFrame. If you are referring to any variable outside of the DataFrame, you need to either use `!!variable` or `[Module_name_here].variable` syntax to refer to this variable.

# Note: You can use `!!` interpolation anywhere, including inside of functions and loops.

Expand Down
8 changes: 6 additions & 2 deletions src/TidierData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ export TidierData_set, across, desc, n, row_number, everything, starts_with, end
const code = Ref{Bool}(false) # output DataFrames.jl code?
const log = Ref{Bool}(false) # output tidylog output? (not yet implemented)

# Expose the global do-not-vectorize "list"
const not_vectorized = Ref{Vector{Symbol}}([:esc, :Ref, :Set, :Cols, :(:), :, :lag, :lead, :ntile, :repeat, :across, :desc, :mean, :std, :var, :median, :first, :last, :minimum, :maximum, :sum, :length, :skipmissing, :quantile, :passmissing, :cumsum, :cumprod, :accumulate, :is_float, :is_integer, :is_string, :cat_rev, :cat_relevel, :cat_infreq, :cat_lump, :cat_reorder, :cat_collapse, :cat_lump_min, :cat_lump_prop, :categorical, :as_categorical, :is_categorical])
# The global do-not-vectorize "list"
const not_vectorized = Ref{Vector{Symbol}}([:esc, :Ref, :Set, :Cols, :collect, :(:), :, :lag, :lead, :ntile, :repeat, :across, :desc, :mean, :std, :var, :median, :first, :last, :minimum, :maximum, :sum, :length, :skipmissing, :quantile, :passmissing, :cumsum, :cumprod, :accumulate, :is_float, :is_integer, :is_string, :cat_rev, :cat_relevel, :cat_infreq, :cat_lump, :cat_reorder, :cat_collapse, :cat_lump_min, :cat_lump_prop, :categorical, :as_categorical, :is_categorical])

# The global do-not-escape "list"
# `in`, `∈`, and `∉` should be vectorized in auto-vec but not escaped
const not_escaped = Ref{Vector{Symbol}}([:esc, :in, :, :, :Ref, :Set, :Cols, :collect, :(:), :, :(=>), :across, :desc, :mean, :std, :var, :median, :first, :last, :minimum, :maximum, :sum, :length, :skipmissing, :quantile, :passmissing, :startswith, :contains, :endswith])

# Includes
include("docstrings.jl")
Expand Down
45 changes: 41 additions & 4 deletions src/parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,30 @@ end
# Not exported
function parse_escape_function(rhs_expr::Union{Expr,Symbol})
rhs_expr = MacroTools.postwalk(rhs_expr) do x
if @capture(x, fn_(args__))

# `in`, `∈`, and `∉` should be vectorized in auto-vec but not escaped
if fn in [:esc :in : : :Ref :Set :Cols :(:) : :across :desc :mean :std :var :median :first :last :minimum :maximum :sum :length :skipmissing :quantile :passmissing :startswith :contains :endswith]
# If it's already escaped, make sure it needs to remain escaped
if @capture(x, esc(variable_Symbol))
if hasproperty(Base, variable) && !(typeof(getproperty(Base, variable)) <: Function)
# Remove the escaping if referring to a constant value like Base.pi
return variable
elseif @capture(x, variable_Symbol) && hasproperty(Core, variable) && !(typeof(getproperty(Core, variable)) <: Function)
# Remove the escaping if referring to a data type like Core.Int64
return variable
elseif variable in not_escaped[]
return variable
elseif contains(string(variable), r"[^\W0-9]\w*$") # valid variable name
return esc(variable)
else
return variable
end
elseif @capture(x, fn_(args__))
if hasproperty(Base, fn) && typeof(getproperty(Base, fn)) <: Function
return x
elseif hasproperty(Core, fn) && typeof(getproperty(Core, fn)) <: Function
return x
elseif hasproperty(Statistics, fn) && typeof(getproperty(Statistics, fn)) <: Function
return x
elseif fn in not_escaped[]
return x
elseif contains(string(fn), r"[^\W0-9]\w*$") # valid variable name
return :($(esc(fn))($(args...)))
Expand Down Expand Up @@ -366,6 +386,10 @@ function parse_interpolation(var_expr::Union{Expr,Symbol,Number,String}; summari
var_expr = MacroTools.postwalk(var_expr) do x
if @capture(x, !!variable_Symbol)
return esc(variable)
# If a variable has already been escaped and marked with a `!!` (e.g., `!!pi`),
# then it won't be re-escaped.
elseif @capture(x, !!expr_)
return expr
# `hello` in Julia is converted to Core.@cmd("hello")
# Since MacroTools is unable to match this pattern, we can directly
# evaluate the expression to see if it matches. If it does, the 3rd argument
Expand All @@ -389,7 +413,20 @@ function parse_interpolation(var_expr::Union{Expr,Symbol,Number,String}; summari
return :($fn())
end
elseif @capture(x, esc(variable_))
return esc(variable)
return esc(variable)
# Escape any native Julia symbols that come from the Base or Core packages
# This includes :missing but also includes all data types (e.g., :Real, :String, etc.)
# To refer to a column named String, you can use `String` (in backticks)
elseif @capture(x, variable_Symbol)
if variable in not_escaped[]
return variable
elseif hasproperty(Base, variable) && !(typeof(getproperty(Base, variable)) <: Function)
return esc(variable)
elseif @capture(x, variable_Symbol) && hasproperty(Core, variable) && !(typeof(getproperty(Core, variable)) <: Function)
return esc(variable)
else
return variable
end
end
return x
end
Expand Down

2 comments on commit 2f7c9f2

@kdpsingh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/96942

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.14.0 -m "<description of version>" 2f7c9f2220cb29ff674d528b35fc5f5eab070698
git push origin v0.14.0

Please sign in to comment.