Skip to content

Commit c80c7f8

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

File tree

6 files changed

+34
-22
lines changed

6 files changed

+34
-22
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: 6 additions & 16 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)))
2198+
((vect) `(braces ,@(cdr vex)))
2199+
((hcat) `(bracescat (row ,@(cdr vex))))
22002200
((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)))))))))
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/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)