Giter Site home page Giter Site logo

fluxml / macrotools.jl Goto Github PK

View Code? Open in Web Editor NEW
310.0 15.0 79.0 784 KB

MacroTools provides a library of tools for working with Julia code and expressions.

Home Page: https://fluxml.ai/MacroTools.jl/stable/

License: Other

Julia 100.00%
julia macro

macrotools.jl's People

Contributors

aviatesk avatar bramtayl avatar cstjean avatar fingolfin avatar iblislin avatar jay-sanjay avatar jiahao avatar jkroso avatar jlapeyre avatar kmsquire avatar krastanov avatar kristofferc avatar lassepe avatar logankilpatrick avatar lopezm94 avatar marius311 avatar mikeinnes avatar musm avatar oxinabox avatar pfitzseb avatar ranjanan avatar rdeits avatar roger-luo avatar simeonschaub avatar stevengj avatar t-bltg avatar timholy avatar tkelman avatar willow-ahrens avatar yuyichao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

macrotools.jl's Issues

Reassigning Variables within @capture

@capture introduces assignments to variables indicated in its second argument. When one of these assignments is to a variable with the same name as the first argument, things seem to go wrong โ€” the match fails and the value of that variable is set to nothing. I think the better behavior in this case would be for the match to succeed, and for ex and b in the second example to have the values of a and b in the first.

julia> ex = :(1 + 2)
:(1 + 2)

julia> @capture(ex, a_ + b_)
true

julia> @capture(ex, ex_ + b_)
false

julia> ex

Alternative to the animal names in alias_gensyms?

I guess I'd like to have the option of having gensyms be converted to identifiers using some sort of deterministic naming scheme with minimal possibility for name collisions, like:

sym_id_1, sym_id_2, sym_id_3... etc. There are only so many animal names in animals.txt and I also worry that when the number of gensyms exceeds the number of animal names that this will lead to trouble.

Could not capture import expression

The following failed. Is there a way to support this capture pattern?

julia> ex=:(Base:cat,hcat,vcat)                            
:((Base:cat, hcat, vcat))                                  

julia> @capture(ex, M_:fs__)                               
false                                                      

Lacking documentation for `@forward`

While reading the Flux sources I see @forward a lot. I've tracked it to this package but as I'm new to Julia I can't tell if it's obtuse or I can't read macros yet. Either way some documentation, in the readme or 'proper', would be nice.

Change in `let` expressions for julia v0.7/master breaks match expressions

I'm relatively confident that the PR JuliaLang/julia#23553 is the reason the following example breaks when trying to use julia compiled from master:

julia> using MacroTools; using MacroTools: @q
...
julia> @match :(function tryme(x); x; end) begin
         function f_(args__) body_ end => @q $f($args...) = $body
       end
ERROR: UndefVarError: f not defined

I've put together a patch to the internal function bindinglet which seems to fix the issue โ€” I could probably work up a pull request for it. (I'm mostly a github lurker, so rather unfamiliar with the process...)

Add splitdef for lambda functions

It would be useful for manipulating anonymous functions. Currently it shows:

julia> splitdef(:(x->x))
ERROR: AssertionError: Not a function definition: x->begin
        #= REPL[30]:1 =#
        x
    end

Documents and demos

Hi Mike,

do you want to set up a Documenter.jl doc? I can make a PR for this.

Bests,
Roger

`shortdef` adds redundant block

julia> ex = :(f(x,y) = x+y) |> striplines;

julia> for i=1:10
         global ex = @> ex longdef shortdef
         end

julia> ex
:(f(x, y) = begin
          begin
              begin
                  begin
                      begin
                          begin
                              begin
                                  begin
                                      begin
                                          begin
                                              begin
                                                  x + y
                                              end
                                          end
                                      end
                                  end
                              end
                          end
                      end
                  end
              end
          end
      end)

expression with | operator

I'm looking into using the @formula macro from the StatsModels package, which allows using the | operator. This is used e.g. by MixedModels for random effect in groups.

However, I can't seem to parse it. Is it interfering with the usage of | for MacroTools optionality?

MWE

@capture :(y~x|e~z) (left_|right_)

where the variable right comes out empty.

Thanks!

uniquely cheeky and loveable macroexpand

Is now provided by base (causing method overwrite and stack overflow on master).

And macro expand should now splice in $__module__ as the first argument of the call to macroexpand.

Matching types is half-broken

In Julia 0.4, the first example from the README yields:

ex = quote
  type Foo
    x::Int
    y
  end
end

@match ex begin
         type T_
           fields__
         end => (T, fields)
       end

> (:Foo,Any[quote  # In[65], line 3:
>    x::Int # In[65], line 4:
>    y
> end])

The second value in the tuple is a one-element array,

Array(Any,(1,))
  1: Expr 
    head: Symbol block
    args: Array(Any,(4,))
      1: LineNumberNode 
        file: Symbol In[66]
        line: Int64 3
      2: Expr 
        head: Symbol ::
        args: Array(Any,(2,))
          1: Symbol x
          2: Symbol Int
        typ: Any
      3: LineNumberNode 
        file: Symbol In[66]
        line: Int64 4
      4: Symbol y
    typ: Any

Whereas the README promises

(:Foo,{:(x::Int),:y})

Problems expanding tricky $ macro

julia> using MacroTools

julia> macro foo(ex)
          isa(ex,Expr) && ex.head == :$ ? 7 : ex
       end
@foo (macro with 1 method)

julia> @foo $bar
7

julia> @expand @foo $bar
:bar

See also here

Union not working with simple comparisons

Just discovered this very nice package, thanks!
The following works:

julia> using MacroTools

julia> ex = :(x <= 1)
:(x <= 1)

julia> @match ex begin
       a_ <= b_  => (a,b)
       end
(:x,1)

but this union doesn't:

julia> @match ex begin
       (a_ <= b_ | a_ < b_) => (a,b)
       end 

(returns nothing).
Am I misunderstanding something?

Newest version fails to precompile

> using MacroTools
ERROR: LoadError: LoadError: UndefVarError: readstring not defined
 in include at /usr/local/lib/julia/sys.dylib
 in include_from_node1 at /usr/local/lib/julia/sys.dylib
 in include at /usr/local/lib/julia/sys.dylib
 in include_from_node1 at /usr/local/lib/julia/sys.dylib
 in require at /usr/local/lib/julia/sys.dylib
while loading /Users/michael/.julia/v0.4/MacroTools/src/utils.jl, in expression starting on line 77
while loading /Users/michael/.julia/v0.4/MacroTools/src/MacroTools.jl, in expression starting on line 9


> Pkg.status()
46 required packages:
 - Atom                          0.4.2+             master
 - Bio                           0.1.0+             phyloplot
 - Blink                         0.3.4+             master
 - Bootstrap                     0.3.3
 - Cairo                         0.2.31
 - CodeTools                     0.3.0+             master
 - Color                         0.2.11
 - ColorBrewer                   0.3.0
 - Colors                        0.6.3
 - Compose                       0.4.2
 - DataFrames                    0.7.0
 - DataFramesMeta                0.1.1
 - DataStructures                0.4.4
 - Dates                         0.4.4
 - Distributions                 0.8.10
 - EcologicalNetwork             1.0.1
 - FactCheck                     0.4.2
 - FunctionalData                0.1.0
 - GLM                           0.5.1
 - GLPlot                        0.0.5
 - Gadfly                        0.4.2
 - GeoInterface                  0.0.1
 - Geodesy                       0.0.1
 - GreatCircle                   0.0.1
 - IJulia                        1.1.9
 - ImageView                     0.1.19
 - Images                        0.5.4
 - JLD                           0.5.9
 - Lint                          0.2.3
 - MAT                           0.2.14
 - MixedModels                   0.4.5
 - NamedArrays                   0.4.7
 - OpenStreetMap                 0.8.2
 - Pandas                        0.2.0
 - Phylogenetics                 0.0.2
 - PlotlyJS                      0.1.4
 - Plots                         0.5.4+             master
 - QuartzImageIO                 0.1.2
 - RCall                         0.4.0
 - RDatasets                     0.1.3
 - Roots                         0.1.26             master
 - SFML                          0.1.0
 - Shapefile                     0.0.3
 - Stats                         0.1.0
 - TestImages                    0.1.1
 - Yeppp                         0.0.10
90 additional packages:
 - ArrayViews                    0.6.4
 - BinDeps                       0.3.21
 - Blosc                         0.1.4
 - BufferedStreams               0.0.3
 - Calculus                      0.1.14
 - Codecs                        0.1.5
 - ColorTypes                    0.2.2
 - ColorVectorSpace              0.1.3
 - Combinatorics                 0.2.1
 - Compat                        0.7.14
 - Conda                         0.1.9
 - Contour                       0.1.0
 - DataArrays                    0.2.20
 - Distances                     0.3.0
 - Docile                        0.5.23
 - DualNumbers                   0.2.2
 - FileIO                        0.0.5
 - FixedPointNumbers             0.1.3
 - FixedSizeArrays               0.1.0
 - ForwardDiff                   0.1.6
 - GLAbstraction                 0.0.12
 - GLFW                          1.1.2
 - GLText                        0.0.4
 - GLWindow                      0.1.2
 - GMT                           0.0.0-             master (unregistered)
 - GZip                          0.2.18
 - GeometryTypes                 0.1.4
 - Graphics                      0.1.3
 - Graphs                        0.6.0
 - Grid                          0.4.0
 - HDF5                          0.5.8
 - Hexagons                      0.0.4
 - Hiccup                        0.0.2
 - Homebrew                      0.2.0
 - HttpCommon                    0.2.4
 - HttpParser                    0.1.1
 - HttpServer                    0.1.5
 - ImmutableArrays               0.0.11
 - IniFile                       0.2.5
 - IntervalTrees                 0.0.5
 - Iterators                     0.1.9
 - JSON                          0.5.0
 - JuliaParser                   0.6.4
 - KernelDensity                 0.1.2
 - LNR                           0.0.2
 - LaTeXStrings                  0.1.6
 - Lazy                          0.10.1
 - Lexicon                       0.1.18
 - LibExpat                      0.1.2
 - Libz                          0.0.2
 - LightXML                      0.2.1
 - Loess                         0.0.6
 - Logging                       0.2.0
 - MacroTools                    0.3.0+             master
 - MathProgBase                  0.4.3
 - MbedTLS                       0.2.2
 - Measures                      0.0.2
 - Media                         0.1.2
 - ModernGL                      0.0.7
 - Mustache                      0.0.14
 - Mux                           0.2.0
 - NLopt                         0.3.1
 - NaNMath                       0.2.1
 - Nettle                        0.2.3
 - Optim                         0.4.4
 - PDMats                        0.4.1
 - Polynomials                   0.0.5
 - PyCall                        1.4.0
 - PyPlot                        2.1.1
 - Quaternions                   0.1.0
 - RasterIO                      0.0.0-             master (unregistered)
 - Reactive                      0.3.2
 - Reexport                      0.0.3
 - Requires                      0.2.2
 - SHA                           0.1.2
 - SIUnits                       0.0.6
 - Showoff                       0.0.6
 - SortingAlgorithms             0.0.6
 - StatsBase                     0.8.0
 - StatsFuns                     0.2.0
 - Switch                        0.0.1
 - TexExtensions                 0.0.3
 - Tk                            0.3.7
 - URIParser                     0.1.3
 - WebSockets                    0.1.2
 - Winston                       0.11.13
 - WoodburyMatrices              0.1.5
 - ZMQ                           0.3.1
 - ZipFile                       0.2.6
 - Zlib                          0.1.12

Pattern matching a namespaced type

It seems the pattern-matching syntax gets confused by the . in the type:

julia> using MacroTools, Markdown

julia> @capture(md"_hello_", x_Markdown.MD)
false

julia> using Markdown: MD

julia> @capture(md"_hello_", x_MD)
true

@q doesn't work with macros

julia> @q @time(1+1)
:(#= REPL[5]:1 =# @time 1 + 1)

It could be fixed by postwalking and changing all the :macrocalls to have nothing for the line number, I believe?

Keyword-argument failure

Thank you for MacroTools, it's super useful! I think I found a bug

let ex = :(function foo(a, b, c; kw=1) a+b+c end)
    @capture(ex, function foo(arg1_, args__) body__ end)
    arg1
end
> :($(Expr(:parameters, :(x=1))))

It works fine without kw=1

Canonicalize function expression

When I write macros acting on function expressions I often write a lot of code such that the macro works for different types of declarations. E.g:

@some_macro a(x)=1
@some_macro function a(x)
  1
end

# or
@some_macro function a{T <: Float64}(x::T)
  1
end
@some_macro function a(x) where T <: Float64
  1
end

Since this leads to a lot of unnecessary macro code it would be nice if there was some sort of canonical form in which an expression could be converted to. MacroTools already has a function longdef that does part of what is needed, but does not rewrite where expressions. Do we want a function canonicaldef or something similar that rewrites functions with where syntax into the regular fn_name{targs...}(args...) syntax? If that is the case is another proposals for the naming beside canonicaldef? I would be happy to contribute that function in a pull request.

UndefVarError splitarg julia v0.7

Hi

MacroTools.splitarg(arg) returns an error in julia v0.7:

julia> arg=:(a::Int=0)
:(a::Int = 0)

julia> MacroTools.splitarg(arg)
ERROR: UndefVarError: name not defined
Stacktrace:
 [1] macro expansion at /Users/ben/.julia/v0.7/MacroTools/src/utils.jl:286 [inlined]
 [2] macro expansion at /Users/ben/.julia/v0.7/MacroTools/src/macro.jl:14 [inlined]
 [3] (::getfield(MacroTools, Symbol("#splitvar#28")))(::Expr) at /Users/ben/.julia/v0.7/MacroTools/src/utils.jl:283
 [4] splitarg(::Expr) at /Users/ben/.julia/v0.7/MacroTools/src/utils.jl:292

Match types when slurping

I couldn't work out how to match types when slurping with __:

block = quote
    begin
        north
        east
        south
        west
    end
end

Untyped slurping works:

symbols = @match block begin
    begin
        s__
    end => s
end

but I wanted to match only Symbols, so I tried this but didn't work:

symbols = @match block begin
    begin
        s__Symbol
    end => s
end

Slurp not working on single fields?

I'm trying to get all the fields in a type with

@match e begin
type typename_
fields__
end -> (typename, fields)

which works as expected if

e=quote
type T
x
y
end
end

but doesn't match if

e=quote
type T
x
end
end

minor clarification for matching pairs (documentation)

In retrospect it is pretty obvious, but I needed to look up the associativity of => to figure out how to match Pair(x,y) expressions, ie

@match :(a => b) begin
    (x_ => y_) => (x,y)         # need parentheses
    _ => :unmatched
end

where x_ => y_ => (x,y) won't work because it is parsed as x_ => (y_ => (x,y)). Thanks for the great package, if you consider this issue too trivial just close it.

Allow for determinmism in the alias_gensyms

We're having a heck of a time debugging ONNX.jl given that it uses MacroTools.alias_gensyms in generating it's output. The problem is that given the same inputs, we always get a different output (alias_gensyms replaces gensysms with animal names). This makes it very difficult to compare two different outputs from ONNX.jl.

I see where the "problem" is (from our perspective), it's in the init function in src/MacroTools.jl, specifically, this line:
Compat.Random.shuffle!(animals)

MacroTools not defined

module MM
using MacroTools: @capture
@capture(:(x=2), _lhs = _rhs)
end
> UndefVarError: MacroTools not defined

It works if I import MacroTools. In my code, I usually fix this by writing function calls like $MacroTools.foo(...), but I don't use hygiene so I'm not sure if it's applicable in your case.

This is still definitely one of the most useful Julia packages for me, thank you!

`isexpr` vs. `Meta.isexpr`

Is there a reason why MacroTools.isexpr is distinct from Base.Meta.isexpr? They have slightly different interfaces, but on the whole it looks like they are trying to do the same thing.

Tag a release

The currently tagged versions produces a very large number of deprecation warnings on julia 0.5, master seems fine. With the RC0 out for julia 0.5, it would be great if you could tag a version very soon so that we can clean up all the deprecation avalanche that is hitting users right now :)

order in unions

Not sure if this is a documentation issue or a bug, but order matters:

julia> (@capture :(struct Foo{X} end) struct (T_{params__}|T_) fields__ end), T
(true, :Foo)

julia> (@capture :(struct Foo{X} end) struct (T_|T_{params__}) fields__ end), T
(true, :(Foo{X}))

Error while loading MacroTools.jl and TaylorSeries.jl

Hi,

In julia 0.5.0-rc4+0 (OS X Yosemite 10.10.5), I'm having some trouble while loading MacroTools and TaylorSeries. At the REPL, I tried to run

julia> using MacroTools, TaylorSeries

and everything worked fine; but I also tried to run

julia> using TaylorSeries, MacroTools

and then I'm getting the following error:

ERROR: TypeError: Tuple: in non-final parameter, expected Type{T}, got Type{Vararg{Int64,N}}
 in _include_from_serialized(::String) at ./loading.jl:150
 in _require_from_serialized(::Int64, ::Symbol, ::String, ::Bool) at ./loading.jl:187
 in _require_search_from_serialized(::Int64, ::Symbol, ::String, ::Bool) at ./loading.jl:217
 in _require_search_from_serialized(::Int64, ::Symbol, ::String, ::Bool) at /Users/Jorge/julia/julia-9c76c3e89a/lib/julia/sys.dylib:?
 in require(::Symbol) at ./loading.jl:371
 in require(::Symbol) at /Users/Jorge/julia/julia-9c76c3e89a/lib/julia/sys.dylib:?

I also tested for this issue in julia 0.4.7-pre+3 but found no error there.

Coverage

This definitely isn't a high priority issue, but rmlines used here (and also in Lazy.jl) messes up code coverage statistics. I've been struggling with this in ChainMap.jl. See

https://github.com/bramtayl/ChainMap.jl/blob/master/src/chain.jl

for one way to work around this when chaining (although I'm not sure whether or not a similar strategy could work for MacroTools).

Matching interpolation syntax

Is there any way to match on expressions with a :$ head? Like, replacing

if isexpr(expr, :$)
    @assert length(expr.args) == 1
    :(shift($(esc(expr.args[1]))))
end

by

@match expr begin
    ($(e)) => :(shift($(esc(e))))
end

where expr is, for example, defined as follows:

julia> expr = :(:($(sdf))).args[1]
:($(Expr(:$, :sdf)))

julia> dump(expr)
Expr
  head: Symbol $
  args: Array{Any}((1,))
    1: Symbol sdf

I also tried interpolating the $ itself, but it didn't help:

julia> @match expr begin
           ($:$(e_)) => :(shift($(esc(e))))
       end

(obviously, since the match expression of this is a Expr(:call, :$, :e_) and not an interpolation).

export @forward, deprecate Lazy.@forward

Sometimes I just depend on Lazy for Lazy.@forward, but the latter is already in MacroTools, although unexported.

Perhaps it would make sense to move it to this package "officially", deprecating Lazy.@forward. The functionality is not strongly tied to either package, but this would remove one dependency from those who just need this single macro.

rmlines does not remove embedded line numbers

I had generated code that had line numbers embedded at all levels.
Here's a recursive version to strip them out, though I expect you can rewrite it in a much simpler way:

function rmlines!(ex)

    n = length(ex.args)
    i = 1

    while i <= n
        if isa(ex.args[i], Expr)

            if ex.args[i].head == :line

                deleteat!(ex.args, i)
                n -= 1
                continue

            else
                rmlines!(ex.args[i])

            end
        end

        i += 1

    end
end

kw turns to =

I think there's a bug where a kw expression will turn into an assignment:

e = Expr(:kw, :a, 1)
enew = MacroTools.@match e begin
  a_ => a
  end

e.head != enew.head

Union problem in `@capture`

Hey! I was playing with the package and I spotted a problem while trying unions in @capture:

julia> using MacroTools

julia> versioninfo()
Julia Version 0.5.1
Commit 6445c82d00 (2017-03-05 13:25 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-3720QM CPU @ 2.60GHz
  WORD_SIZE: 64
  BLAS: libopenblas (NO_AFFINITY SANDYBRIDGE)
  LAPACK: liblapack
  LIBM: libm
  LLVM: libLLVM-3.7.1 (ORCJIT, ivybridge)

julia> macro m1(ex)
         @capture(ex, f_(args__) = body_ | function f_(args__) body_ end) || return ex
         @show f
         @show args
         @show body
         return ex
       end;

julia> macro m2(ex)
         @capture(ex, (f_{T_}|f_)(args__) = body_) || return ex
         @show f
         @show args
         @show body
         return ex
       end;

julia> macro m3(ex)
         @capture(ex, f_(args__) = body_) || @capture(ex, function f_(args__) body_ end) || return ex
         @show f
         @show args
         @show body
         return ex
       end;

julia> @m1 f(x) = 5x;
f = :f
args = Any[:x]
body = :(5x)

julia> @m1 function f(x) 5x end;
WARNING: Method definition f(Any) in module Main at REPL[6]:1 overwritten at REPL[7]:1.

julia> @m2 f(x) = 5x;
f = :f
args = Any[:x]
body = quote  # REPL[8], line 1:
    5x
end
WARNING: Method definition f(Any) in module Main at REPL[7]:1 overwritten at REPL[8]:1.

julia> @m3 f(x) = 5x;
f = :f
args = Any[:x]
body = quote  # REPL[9], line 1:
    5x
end
WARNING: Method definition f(Any) in module Main at REPL[8]:1 overwritten at REPL[9]:1.

julia> @m3 function f(x) 5x end;
f = :f
args = Any[:x]
body = quote  # REPL[10], line 1:
    5x
end
WARNING: Method definition f(Any) in module Main at REPL[9]:1 overwritten at REPL[10]:1.

Even though m1 and m2 are copies from README, they don't work properly. I hope I am not missing some stupid mistake while trying.

I'd appreciate if you could help. Thanks!

Weird match behaviour over function definition

On 0.6:

@capture(:(function foo(x) x+x end), function (fcall_ | fcall_) body_ end)  # true
@capture(:(function foo(x) x+x end), function (fcall_) body_ end)  # false
@capture(:(function foo(x) x+x end), function fcall_ body_ end)   # syntax error

I get that there isn't anything to be done about the last case, but it's weird that number one and number two aren't equivalent.

longdef1 strips lines

longdef1 gets rid of line numbers:

julia> fun = :(f(x) = x+2)
:(f(x) = begin
          #= REPL[37]:1 =#
          x + 2
      end)

julia> MacroTools.longdef1(fun)
:(function f(x)
      x + 2
  end)

And since it's relied upon by splitdef, we lose line numbers there too.

`prettify` fails on `do` notation

Call:

prettify(
    quote
        map(1:N) do n
            f(n)
        end
    end
)

Result:

:(map(1:N) do n
      f
      n
  end)

Expected:

:(map(1:N) do n
      f(n)
  end)

System info:
Manjaro Linux
Julia Version 0.7.0-rc2.0
Commit 78540cba4c (2018-08-02 19:14 UTC)
MacroTools v0.4.2

StackOverflow in v0.7

Just in case you didn't spot this on Travis nightly:

ERROR: LoadError: LoadError: StackOverflowError:
Stacktrace:
 [1] include_from_node1(::Module, ::String) at ./loading.jl:549
 [2] include(::Module, ::String) at ./sysimg.jl:14
 [3] include(::String) at /home/travis/.julia/v0.7/MacroTools/src/MacroTools.jl:2
 [4] include_from_node1(::Module, ::String) at ./loading.jl:549
 [5] include(::Module, ::String) at ./sysimg.jl:14
 [6] anonymous at ./<missing>:2
while loading /home/travis/.julia/v0.7/MacroTools/src/utils.jl, in expression starting on line 96
while loading /home/travis/.julia/v0.7/MacroTools/src/MacroTools.jl, in expression starting on line 12

I didn't know I was using this, but I've added a Juno.jl dependency so it looks like I am... :)

Match failure when combining Unions and expression types

It appears that attempting to create an @match that uses unions and expression types results in no matches being found. A few examples:

This works:

julia> @match :(x[1]) begin
           (v_ref) => (v)
       end
:(x[1])

But adding a union gives no match:

julia> @match :(x[1]) begin
           (v_ref |
           (v_ref <= ub_)) => (v)
       end

Removing the _ref tag fixes it:

julia> @match :(x[1]) begin
           (v_ |
           (v_ <= ub_)) => (v)
       end
:(x[1])

Similarly, with a _Symbol tag, this works:

julia> @match :(x) begin
           (v_Symbol) => (v)
       end
:x

and this doesn't:

@match :(x) begin
    (v_Symbol |
    (v_Symbol <= ub_)) => (v)
end

Do unions just not support type restrictions?

splitarg doesn't recognize keyword arguments after semicolon

I don't know if it is intended, but splitarg errors if there are keyword arguments after the ;, it'd be very useful if it could also recognize those (just like splitdefdoes):

julia> map(splitarg, (:(f(a=2, x::Int=nothing, y; z = 12))).args[2:end])
ERROR: TypeError: splitvar: in typeassert, expected Symbol, got Expr
Stacktrace:
 [1] macro expansion at /Users/pietro/.julia/v0.6/MacroTools/src/utils.jl:287 [inlined]
 [2] macro expansion at /Users/pietro/.julia/v0.6/MacroTools/src/macro.jl:14 [inlined]
 [3] (::MacroTools.#splitvar#28)(::Expr) at /Users/pietro/.julia/v0.6/MacroTools/src/utils.jl:283
 [4] splitarg(::Expr) at /Users/pietro/.julia/v0.6/MacroTools/src/utils.jl:294
 [5] _collect(::Array{Any,1}, ::Base.Generator{Array{Any,1},MacroTools.#splitarg}, ::Base.EltypeUnknown, ::Base.HasShape) at ./array.jl:454
 [6] map(::Function, ::Array{Any,1}) at ./abstractarray.jl:1865

bizzare match failure for macros in type assertions

MacroTools can't match macros when they appear in the type assertions in function calls. This seems like a crazy issue, but a key macro in my package depended on this functionality and now something has changed with the latest julia build and things fail. Any ideas as to why this match failure would happen?

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.7.0-DEV.5246 (2018-05-29 13:27 UTC)
 _/ |\__'_|_|_|\__'_|  |  Commit 082be9a (8 days old master)
|__/                   |  x86_64-apple-darwin17.5.0

(v0.7) pkg> up MacroTools
  Updating registry at `~/.julia/registries/Uncurated`
  Updating git-repo `https://github.com/JuliaRegistries/Uncurated.git`
 Resolving package versions...
  Updating `~/.julia/environments/v0.7/Project.toml`
 [no changes]
  Updating `~/.julia/environments/v0.7/Manifest.toml`
 [no changes]

julia> using MacroTools

julia> x = :(foo(x::@bar(qux))).args[2].args[2]
:(#= REPL[3]:1 =# @bar qux)

julia> :(@bar(qux))
:(#= REPL[4]:1 =# @bar qux)

julia> x.head
:macrocall

julia> :(@bar(qux)).head
:macrocall

julia> x.args
3-element Array{Any,1}:
 Symbol("@bar")
 :(#= REPL[3]:1 =#)
 :qux

julia> :(@bar(qux)).args
3-element Array{Any,1}:
 Symbol("@bar")
 :(#= REPL[8]:1 =#)
 :qux

julia> @capture(x, @bar(qux))
false

julia> @capture(:(@bar(qux)), @bar(qux))
true

EDIT: It appears that this has something to do with line number nodes getting passed as arguments to macros. I rolled a custom solution for myself, but I'll dig into this sometime when I have more time. I don't think this has anything to do with the type assertions at all now.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.