Skip to content

Commit 6ddb1dc

Browse files
committed
parse { } expressions as braces and bracescat
part of #8470
1 parent 8ebedd6 commit 6ddb1dc

File tree

7 files changed

+43
-30
lines changed

7 files changed

+43
-30
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Language changes
2626
* The parsing of `1<<2*3` as `1<<(2*3)` is deprecated, and will change to
2727
`(1<<2)*3` in a future version ([#13079]).
2828

29+
* `{ }` expressions now use `braces` and `bracescat` as expression heads instead
30+
of `cell1d` and `cell2d`, and parse similarly to `vect` and `vcat` ([#8470]).
31+
2932
Breaking changes
3033
----------------
3134

base/show.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,8 @@ const all_ops = union(quoted_syms, uni_ops, expr_infix_any)
473473
const expr_calls = Dict(:call => ('(',')'), :calldecl => ('(',')'),
474474
:ref => ('[',']'), :curly => ('{','}'), :(.) => ('(',')'))
475475
const expr_parens = Dict(:tuple=>('(',')'), :vcat=>('[',']'),
476-
:hcat =>('[',']'), :row =>('[',']'), :vect=>('[',']'))
476+
:hcat =>('[',']'), :row =>('[',']'), :vect=>('[',']'),
477+
:braces=>('{','}'), :bracescat=>('{','}'))
477478

478479
## AST decoding helpers ##
479480

@@ -751,7 +752,7 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
751752
# list (i.e. "(1, 2, 3)" or "[1, 2, 3]")
752753
elseif haskey(expr_parens, head) # :tuple/:vcat
753754
op, cl = expr_parens[head]
754-
if head === :vcat
755+
if head === :vcat || head === :bracescat
755756
sep = "; "
756757
elseif head === :hcat || head === :row
757758
sep = " "

src/ast.scm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
(string #\( (deparse-arglist (cdr e))
5151
(if (length= e 2) #\, "")
5252
#\)))
53-
((cell1d) (string #\{ (deparse-arglist (cdr e)) #\}))
5453
((call) (string (deparse (cadr e)) #\( (deparse-arglist (cddr e)) #\)))
5554
((ref) (string (deparse (cadr e)) #\[ (deparse-arglist (cddr e)) #\]))
5655
((curly) (string (deparse (cadr e)) #\{ (deparse-arglist (cddr e)) #\}))
@@ -63,6 +62,9 @@
6362
((vect) (string #\[ (deparse-arglist (cdr e)) #\]))
6463
((vcat) (string #\[ (deparse-arglist (cdr e) ";") #\]))
6564
((hcat) (string #\[ (deparse-arglist (cdr e) " ") #\]))
65+
((row) (deparse-arglist (cdr e) " "))
66+
((braces) (string #\{ (deparse-arglist (cdr e)) #\}))
67+
((bracescat) (string #\{ (deparse-arglist (cdr e) ";") #\}))
6668
((const) (string "const " (deparse (cadr e))))
6769
((global local)
6870
(string (car e) " " (string.join (map deparse (cdr e)) ", ")))
@@ -91,7 +93,7 @@
9193
(string (deparse (cadr e)) " where "
9294
(if (length= e 3)
9395
(deparse (caddr e))
94-
(deparse (cons 'cell1d (cddr e))))))
96+
(deparse (cons 'braces (cddr e))))))
9597
((function for while)
9698
(deparse-block (string (car e) " " (deparse (cadr e)))
9799
(block-stmts (caddr e))))

src/julia-parser.scm

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@
945945
(if (eq? t 'where)
946946
(begin (take-token s)
947947
(let ((var (parse-comparison s)))
948-
(loop (if (and (pair? var) (eq? (car var) 'cell1d))
948+
(loop (if (and (pair? var) (eq? (car var) 'braces))
949949
(list* 'where ex (cdr var)) ;; form `x where {T,S}`
950950
(list 'where ex var))
951951
(peek-token s))))
@@ -2190,25 +2190,15 @@
21902190
(take-token s)
21912191
(if (eqv? (require-token s) #\})
21922192
(begin (take-token s)
2193-
'(cell1d))
2193+
'(braces))
21942194
(let ((vex (parse-cat s #\} end-symbol)))
21952195
(if (null? vex)
2196-
'(cell1d)
2196+
'(braces)
21972197
(case (car vex)
2198-
((vect) `(cell1d ,@(cdr vex)))
2199-
((hcat) `(cell2d 1 ,(length (cdr vex)) ,@(cdr vex)))
2200-
((comprehension) (error "{a for a in b} syntax is discontinued"))
2201-
(else
2202-
(if (and (pair? (cadr vex)) (eq? (caadr vex) 'row))
2203-
(let ((nr (length (cdr vex)))
2204-
(nc (length (cdadr vex))))
2205-
(begin
2206-
`(cell2d ,nr ,nc
2207-
,@(apply append
2208-
;; transpose to storage order
2209-
(apply map list
2210-
(map cdr (cdr vex)))))))
2211-
`(cell1d ,@(cdr vex)))))))))
2198+
((vect) `(braces ,@(cdr vex)))
2199+
((hcat) `(bracescat (row ,@(cdr vex))))
2200+
((comprehension) `(braces ,@(cdr vex)))
2201+
(else `(bracescat ,@(cdr vex))))))))
22122202

22132203
;; string literal
22142204
((eqv? t #\")

src/julia-syntax.scm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,8 +2181,8 @@
21812181
(syntax-deprecation #f "Expr(:(=>), ...)" "Expr(:call, :(=>), ...)")
21822182
`(call => ,(expand-forms (cadr e)) ,(expand-forms (caddr e))))
21832183

2184-
'cell1d (lambda (e) (error "{ } vector syntax is discontinued"))
2185-
'cell2d (lambda (e) (error "{ } matrix syntax is discontinued"))
2184+
'braces (lambda (e) (error "{ } vector syntax is discontinued"))
2185+
'bracescat (lambda (e) (error "{ } matrix syntax is discontinued"))
21862186

21872187
'string
21882188
(lambda (e) (expand-forms `(call (top string) ,@(cdr e))))

test/parse.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -619,15 +619,16 @@ end
619619
@test_throws ParseError parse("--x")
620620
@test_throws ParseError parse("stagedfunction foo(x); end")
621621

622-
#@test_throws ParseError parse("{1,2,3}")
623-
#@test_throws ParseError parse("{1 2 3 4}")
624-
#@test_throws ParseError parse("{1,2; 3,4}")
625-
@test_throws ParseError parse("{x for x in 1:10}")
626-
@test_throws ParseError parse("{x=>y for (x,y) in zip([1,2,3],[4,5,6])}")
627-
#@test_throws ParseError parse("{:a=>1, :b=>2}")
628-
629622
@test parse("A=>B") == Expr(:call, :(=>), :A, :B)
630623

624+
@test parse("{1,2,3}") == Expr(:braces, 1, 2, 3)
625+
@test parse("{1 2 3 4}") == Expr(:bracescat, Expr(:row, 1, 2, 3, 4))
626+
@test parse("{1 2; 3 4}") == Expr(:bracescat, Expr(:row, 1, 2), Expr(:row, 3, 4))
627+
@test parse("{x for x in 1:10}") == Expr(:braces, :(x for x in 1:10))
628+
@test parse("{x=>y for (x,y) in zip([1,2,3],[4,5,6])}") == Expr(:braces, :(x=>y for (x,y) in zip([1,2,3],[4,5,6])))
629+
@test parse("{:a=>1, :b=>2}") == Expr(:braces, Expr(:call, :(=>), QuoteNode(:a), 1),
630+
Expr(:call, :(=>), QuoteNode(:b), 2))
631+
631632
# this now is parsed as getindex(Pair{Any,Any}, ...)
632633
@test_throws MethodError eval(parse("(Any=>Any)[]"))
633634
@test_throws MethodError eval(parse("(Any=>Any)[:a=>1,:b=>2]"))

test/show.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,22 @@ test_mt(show_f5, "show_f5(A::AbstractArray{T,N}, indexes::Vararg{$Int,N})")
591591
@test_repr "[a;]"
592592
@test_repr "[a; b]"
593593

594+
# other brackets and braces
595+
@test_repr "[a]"
596+
@test_repr "[a,b]"
597+
@test_repr "[a;b;c]"
598+
@test_repr "[a b]"
599+
@test_repr "[a b;]"
600+
@test_repr "[a b c]"
601+
@test_repr "[a b; c d]"
602+
@test_repr "{a}"
603+
@test_repr "{a,b}"
604+
@test_repr "{a;b;c}"
605+
@test_repr "{a b}"
606+
@test_repr "{a b;}"
607+
@test_repr "{a b c}"
608+
@test_repr "{a b; c d}"
609+
594610
# Printing of :(function f end)
595611
@test sprint(show, :(function f end)) == ":(function f end)"
596612
@test_repr "function g end"

0 commit comments

Comments
 (0)