Skip to content

Commit 6c4e30e

Browse files
cdsousaJeffBezanson
authored andcommitted
Allow juxtaposition of macro and array literal. fixes #23519 (#23547)
1 parent b10833b commit 6c4e30e

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ New language features
1616
For example, `+̂ₐ″` is parsed as an infix operator with the same
1717
precedence as `+` ([#22089]).
1818

19+
* The macro call syntax `@macroname[args]` is now available and is parsed
20+
as `@macroname([args])` ([#23519]).
21+
1922
Language changes
2023
----------------
2124

doc/src/manual/metaprogramming.md

+7
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,13 @@ above; it passes the tuple `(expr1, expr2, ...)` as one argument to the macro:
583583
@name (expr1, expr2, ...)
584584
```
585585

586+
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`):
587+
588+
```julia
589+
@name[a b] * v
590+
@name([a b]) * v
591+
```
592+
586593
It is important to emphasize that macros receive their arguments as expressions, literals, or
587594
symbols. One way to explore macro arguments is to call the [`show`](@ref) function within the
588595
macro body:

src/julia-parser.scm

+12-10
Original file line numberDiff line numberDiff line change
@@ -1189,16 +1189,18 @@
11891189
;; ref(a,i) = x
11901190
(let* ((es end-symbol)
11911191
(al (with-end-symbol (parse-cat s #\] es))))
1192-
(if (null? al)
1193-
(loop (list 'ref ex))
1194-
(case (car al)
1195-
((vect) (loop (list* 'ref ex (cdr al))))
1196-
((hcat) (loop (list* 'typed_hcat ex (cdr al))))
1197-
((vcat)
1198-
(loop (list* 'typed_vcat ex (cdr al))))
1199-
((comprehension)
1200-
(loop (list* 'typed_comprehension ex (cdr al))))
1201-
(else (error "unknown parse-cat result (internal error)"))))))
1192+
(if macrocall?
1193+
(list 'call ex al)
1194+
(if (null? al)
1195+
(loop (list 'ref ex))
1196+
(case (car al)
1197+
((vect) (loop (list* 'ref ex (cdr al))))
1198+
((hcat) (loop (list* 'typed_hcat ex (cdr al))))
1199+
((vcat)
1200+
(loop (list* 'typed_vcat ex (cdr al))))
1201+
((comprehension)
1202+
(loop (list* 'typed_comprehension ex (cdr al))))
1203+
(else (error "unknown parse-cat result (internal error)")))))))
12021204
((|.|)
12031205
(if (ts:space? s) (disallowed-space ex t))
12041206
(take-token s)

test/parse.jl

+8
Original file line numberDiff line numberDiff line change
@@ -1366,3 +1366,11 @@ let xs = [:(1+2), :(3+4), :(5+6)]
13661366
ex2 = eval(ex)
13671367
@test ex2.args[2:end] == [3,7,11]
13681368
end
1369+
1370+
# issue #23519
1371+
@test parse("@foo[1]") == parse("@foo([1])")
1372+
@test parse("@foo[1 2; 3 4]") == parse("@foo([1 2; 3 4])")
1373+
@test parse("@foo[1] + [2]") == parse("@foo([1]) + [2]")
1374+
@test parse("@foo [1] + [2]") == parse("@foo([1] + [2])")
1375+
@test parse("@Mdl.foo[1] + [2]") == parse("@Mdl.foo([1]) + [2]")
1376+
@test parse("@Mdl.foo [1] + [2]") == parse("@Mdl.foo([1] + [2])")

0 commit comments

Comments
 (0)