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

sourcewalk introduces blocks #123

Open
oxinabox opened this issue Aug 30, 2019 · 3 comments
Open

sourcewalk introduces blocks #123

oxinabox opened this issue Aug 30, 2019 · 3 comments

Comments

@oxinabox
Copy link
Member

MWE:

using MacroTools
using MacroTools: textwalk

code = """
Rule(x -> -sin(x))
Rule(x -> 1 + tan(x)^2)
""";

after = textwalk(code) do expr
    @capture(expr, Rule(v_)) && return v
    return expr
end

println(after)

Output:

(x->begin
        -(sin(x))
    end)
(x->begin
        1 + tan(x) ^ 2
    end)
@oxinabox
Copy link
Member Author

oxinabox commented Aug 30, 2019

Workaround:

 textwalk(code) do expr
    @capture(expr, Rule(v_)) && return MacroTools.postwalk(MacroTools.unblock, v)
    return expr
end

Though that still changes spacing,
and adds extra backets arround sin(x) and arraound the whole expression

Output

(x->-(sin(x)))
(x->1 + tan(x) ^ 2)

@MikeInnes
Copy link
Member

MikeInnes commented Sep 2, 2019

This is unfortunately due to Base's default parsing/printing of lambdas:

julia> :(x->1)
:(x->begin
          #= REPL[1]:1 =#
          1
      end)

Would be great to fix, but unfortunately might require a fork of the Expr printing code (though there are likely several reasons to do this).

@MikeInnes
Copy link
Member

Alternatively, since Expr(:->, :x, 1) seems to do the right thing, maybe we can just tweak CSTParser to not insert the block / line number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants