Skip to content

Commit

Permalink
Allow juxtaposition of macro and array literal. fixes #23519 (#23547)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdsousa authored and JeffBezanson committed Oct 2, 2017
1 parent b10833b commit 6c4e30e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ New language features
For example, `+̂ₐ″` is parsed as an infix operator with the same
precedence as `+` ([#22089]).

* The macro call syntax `@macroname[args]` is now available and is parsed
as `@macroname([args])` ([#23519]).

Language changes
----------------

Expand Down
7 changes: 7 additions & 0 deletions doc/src/manual/metaprogramming.md
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,13 @@ above; it passes the tuple `(expr1, expr2, ...)` as one argument to the macro:
@name (expr1, expr2, ...)
```

An alternative way to invoke a macro over an array literal (or comprehension) is to juxtapose both without using parentheses. In this case, the array will be the only expression fed to the macro. The following syntax is equivalent (and different from `@name [a b] * v`):

```julia
@name[a b] * v
@name([a b]) * v
```

It is important to emphasize that macros receive their arguments as expressions, literals, or
symbols. One way to explore macro arguments is to call the [`show`](@ref) function within the
macro body:
Expand Down
22 changes: 12 additions & 10 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1189,16 +1189,18 @@
;; ref(a,i) = x
(let* ((es end-symbol)
(al (with-end-symbol (parse-cat s #\] es))))
(if (null? al)
(loop (list 'ref ex))
(case (car al)
((vect) (loop (list* 'ref ex (cdr al))))
((hcat) (loop (list* 'typed_hcat ex (cdr al))))
((vcat)
(loop (list* 'typed_vcat ex (cdr al))))
((comprehension)
(loop (list* 'typed_comprehension ex (cdr al))))
(else (error "unknown parse-cat result (internal error)"))))))
(if macrocall?
(list 'call ex al)
(if (null? al)
(loop (list 'ref ex))
(case (car al)
((vect) (loop (list* 'ref ex (cdr al))))
((hcat) (loop (list* 'typed_hcat ex (cdr al))))
((vcat)
(loop (list* 'typed_vcat ex (cdr al))))
((comprehension)
(loop (list* 'typed_comprehension ex (cdr al))))
(else (error "unknown parse-cat result (internal error)")))))))
((|.|)
(if (ts:space? s) (disallowed-space ex t))
(take-token s)
Expand Down
8 changes: 8 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1366,3 +1366,11 @@ let xs = [:(1+2), :(3+4), :(5+6)]
ex2 = eval(ex)
@test ex2.args[2:end] == [3,7,11]
end

# issue #23519
@test parse("@foo[1]") == parse("@foo([1])")
@test parse("@foo[1 2; 3 4]") == parse("@foo([1 2; 3 4])")
@test parse("@foo[1] + [2]") == parse("@foo([1]) + [2]")
@test parse("@foo [1] + [2]") == parse("@foo([1] + [2])")
@test parse("@Mdl.foo[1] + [2]") == parse("@Mdl.foo([1]) + [2]")
@test parse("@Mdl.foo [1] + [2]") == parse("@Mdl.foo([1] + [2])")

0 comments on commit 6c4e30e

Please sign in to comment.