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

Matching Exprs (vararg constructor) #85

Open
gafter opened this issue May 17, 2023 · 0 comments
Open

Matching Exprs (vararg constructor) #85

gafter opened this issue May 17, 2023 · 0 comments

Comments

@gafter
Copy link
Member

gafter commented May 17, 2023

From the Match.jl documentation we have:

The following function is a nice example of matching expressions. It is used in VideoIO.jl to extract the names of expressions generated by Clang.jl, for later filtering and rewriting.:

extract_name(x) = string(x)
function extract_name(e::Expr)
    @match e begin
        Expr(:type,      [_, name, _], _)     => name
        Expr(:typealias, [name, _], _)        => name
        Expr(:call,      [name, _...], _)     => name
        Expr(:function,  [sig, _...], _)      => extract_name(sig)
        Expr(:const,     [assn, _...], _)     => extract_name(assn)
        Expr(:(=),       [fn, body, _...], _) => extract_name(fn)
        Expr(expr_type,  _...)                => error("Can't extract name from ",
                                                        expr_type, " expression:\n",
                                                        "    $e\n")
    end
end

However, this doesn't work in Match, Rematch, or Rematch2. It would be nice if it did. Doing so would require recognizing treating Expr specially:

julia> methods(Expr)
# 1 method for type constructor:
[1] Expr(args...) in Core at boot.jl:263

julia> fieldnames(Expr)
(:head, :args)

julia> fieldtypes(Expr)
(Symbol, Vector{Any})
@gafter gafter transferred this issue from JuliaServices/Rematch2.jl Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant