Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define @qq #200

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1.0'
- '1.6'
- '1'
- 'nightly'
os: [ubuntu-latest, macOS-latest, windows-latest]
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name = "MacroTools"
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
version = "0.5.12"
version = "0.5.13"

[deps]
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
julia = "1"
julia = "1.6"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
1 change: 1 addition & 0 deletions docs/src/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ MacroTools.combinearg

```@docs
MacroTools.@q
MacroTools.@qq
MacroTools.isexpr
MacroTools.rmlines
MacroTools.unblock
Expand Down
23 changes: 23 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@ macro q(ex)
esc(Expr(:quote, striplines(ex)))
end

struct Flag end

to_flag(ex) = MacroTools.prewalk(x->x isa LineNumberNode ? Flag() : x, ex)
to_line(l::LineNumberNode, ex) = MacroTools.prewalk(x->x isa Flag ? l : x, ex)

"""
@qq [expression]

Like the `quote` keyword but replace construction site line numbers with __source__.
Line numbers of interpolated expressions are preserved. The result is that line numbers will be
attributed to the macro usage site, instead of the macro source code.

Only works inside of a macro definition.

See also: [`@q`](@ref)
"""
macro qq(ex)
# This was a difficult macro to write; the round-trip to Flag appears to be necessary.
# Crucially, we want interpolated expressions to preserve their line numbers.
esc(:($MacroTools.to_line(__source__, $(Expr(:quote, to_flag(ex))))))
end


"""
isexpr(x, ts...)

Expand Down
16 changes: 15 additions & 1 deletion test/utils.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MacroTools: isdef, flatten, striplines
using MacroTools: isdef, flatten, striplines, @qq

@testset "utils" begin
ex1 = :(function foo(a) return a; end)
Expand Down Expand Up @@ -45,3 +45,17 @@ end
@test flatten(ex) |> striplines == ex |> striplines
end
end

## Test for @qq

macro my_fff_def(a)
@qq function fff() $a end
end

@my_fff_def begin # line where fff() is defined
function g() # line where fff()() is defined
22
end
end

@test which(fff,()).line == which(fff(),()).line - 1
Loading