codedthinking / Kezdi.jl

An umbrella of Julia packages for data analysis, in loving memory of Gábor Kézdi
MIT License
7 stars 0 forks source link

Check Julia parsing for stata-like syntax #17

Open korenmiklos opened 2 weeks ago

korenmiklos commented 2 weeks ago
@keep a b @where d == 1, cluster(z) whatever

now looks


❔
Tuple{Symbol, Symbol, Expr}
  1: Symbol a
  2: Symbol b
  3: Expr
    head: Symbol macrocall
    args: Array{Any}((4,))
      1: Symbol @where
      2: LineNumberNode
        line: Int64 1
        file: Symbol /Users/koren/Tresorit/Mac/code/julia/github-data/src/stats.jl#==#25eefd78-67a2-413d-9abb-201d0ccfc094
      3: Expr
        head: Symbol tuple
        args: Array{Any}((2,))
          1: Expr
            head: Symbol call
            args: Array{Any}((3,))
              1: Symbol ==
              2: Symbol d
              3: Int64 1
          2: Expr
            head: Symbol call
            args: Array{Any}((2,))
              1: Symbol cluster
              2: Symbol z
      4: Symbol whatever

We want this to be parsed in our own AST as

Command(:keep,
  Tuple(:a, :b),
Where(Expr(:d == 1)),
Options(Expr(cluster(z)), :whatever)
korenmiklos commented 2 weeks ago

Check this trick in Chain.jl. Does not actually parse aside macro, but checks whether there is a aside macro call within your list of expressions.

https://github.com/jkrumbiegel/Chain.jl/blob/df30824b1db5321ae29c6b2e02ff1147d4ba0fb3/src/Chain.jl#L6

korenmiklos commented 2 weeks ago

With this trick, it is actually possible to use @if like

@replace y = 0 @if y < 0

There is no macro if, which is a reserved word. But @replace can parse the above, including a macrocall to symbol @if.

This is a hacky solution, and we run the risk of Julia removing this functionality in later parsers and compilers. I would introduce and document the keyword @where, but mention that @if is also available for better Stata compatibility.