Skip to content

Commit f0eecac

Browse files
committed
parse { } expressions as braces and bracescat
part of #8470
1 parent 04d76f3 commit f0eecac

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
@@ -22,6 +22,9 @@ Language changes
2222
* The parsing of `1<<2*3` as `1<<(2*3)` is deprecated, and will change to
2323
`(1<<2)*3` in a future version ([#13079]).
2424

25+
* `{ }` expressions now use `braces` and `bracescat` as expression heads instead
26+
of `cell1d` and `cell2d`, and parse similarly to `vect` and `vcat` ([#8470]).
27+
2528
Breaking changes
2629
----------------
2730

base/show.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,8 @@ const all_ops = union(quoted_syms, uni_ops, expr_infix_any)
466466
const expr_calls = Dict(:call => ('(',')'), :calldecl => ('(',')'),
467467
:ref => ('[',']'), :curly => ('{','}'), :(.) => ('(',')'))
468468
const expr_parens = Dict(:tuple=>('(',')'), :vcat=>('[',']'),
469-
:hcat =>('[',']'), :row =>('[',']'), :vect=>('[',']'))
469+
:hcat =>('[',']'), :row =>('[',']'), :vect=>('[',']'),
470+
:braces=>('{','}'), :bracescat=>('{','}'))
470471

471472
## AST decoding helpers ##
472473

@@ -744,7 +745,7 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
744745
# list (i.e. "(1, 2, 3)" or "[1, 2, 3]")
745746
elseif haskey(expr_parens, head) # :tuple/:vcat
746747
op, cl = expr_parens[head]
747-
if head === :vcat
748+
if head === :vcat || head === :bracescat
748749
sep = "; "
749750
elseif head === :hcat || head === :row
750751
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))))
@@ -2177,25 +2177,15 @@
21772177
(take-token s)
21782178
(if (eqv? (require-token s) #\})
21792179
(begin (take-token s)
2180-
'(cell1d))
2180+
'(braces))
21812181
(let ((vex (parse-cat s #\})))
21822182
(if (null? vex)
2183-
'(cell1d)
2183+
'(braces)
21842184
(case (car vex)
2185-
((vect) `(cell1d ,@(cdr vex)))
2186-
((hcat) `(cell2d 1 ,(length (cdr vex)) ,@(cdr vex)))
2185+
((vect) `(braces ,@(cdr vex)))
2186+
((hcat) `(bracescat (row ,@(cdr vex))))
21872187
((comprehension) (error "{a for a in b} syntax is discontinued"))
2188-
(else
2189-
(if (and (pair? (cadr vex)) (eq? (caadr vex) 'row))
2190-
(let ((nr (length (cdr vex)))
2191-
(nc (length (cdadr vex))))
2192-
(begin
2193-
`(cell2d ,nr ,nc
2194-
,@(apply append
2195-
;; transpose to storage order
2196-
(apply map list
2197-
(map cdr (cdr vex)))))))
2198-
`(cell1d ,@(cdr vex)))))))))
2188+
(else `(bracescat ,@(cdr vex))))))))
21992189

22002190
;; string literal
22012191
((eqv? t #\")

src/julia-syntax.scm

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

2192-
'cell1d (lambda (e) (error "{ } vector syntax is discontinued"))
2193-
'cell2d (lambda (e) (error "{ } matrix syntax is discontinued"))
2192+
'braces (lambda (e) (error "{ } vector syntax is discontinued"))
2193+
'bracescat (lambda (e) (error "{ } matrix syntax is discontinued"))
21942194

21952195
'string
21962196
(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
@@ -598,6 +598,22 @@ test_mt(show_f5, "show_f5(A::AbstractArray{T,N}, indexes::Vararg{$Int,N})")
598598
@test_repr "[a;]"
599599
@test_repr "[a; b]"
600600

601+
# other brackets and braces
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+
@test_repr "{a}"
610+
@test_repr "{a,b}"
611+
@test_repr "{a;b;c}"
612+
@test_repr "{a b}"
613+
@test_repr "{a b;}"
614+
@test_repr "{a b c}"
615+
@test_repr "{a b; c d}"
616+
601617
# Printing of :(function f end)
602618
@test sprint(show, :(function f end)) == ":(function f end)"
603619
@test_repr "function g end"

0 commit comments

Comments
 (0)