Skip to content

Commit

Permalink
parse { } expressions as braces and bracescat
Browse files Browse the repository at this point in the history
part of #8470
  • Loading branch information
JeffBezanson committed Jul 26, 2017
1 parent 8ebedd6 commit c80c7f8
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Language changes
* The parsing of `1<<2*3` as `1<<(2*3)` is deprecated, and will change to
`(1<<2)*3` in a future version ([#13079]).

* `{ }` expressions now use `braces` and `bracescat` as expression heads instead
of `cell1d` and `cell2d`, and parse similarly to `vect` and `vcat` ([#8470]).

Breaking changes
----------------

Expand Down
5 changes: 3 additions & 2 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ const all_ops = union(quoted_syms, uni_ops, expr_infix_any)
const expr_calls = Dict(:call => ('(',')'), :calldecl => ('(',')'),
:ref => ('[',']'), :curly => ('{','}'), :(.) => ('(',')'))
const expr_parens = Dict(:tuple=>('(',')'), :vcat=>('[',']'),
:hcat =>('[',']'), :row =>('[',']'), :vect=>('[',']'))
:hcat =>('[',']'), :row =>('[',']'), :vect=>('[',']'),
:braces=>('{','}'), :bracescat=>('{','}'))

## AST decoding helpers ##

Expand Down Expand Up @@ -751,7 +752,7 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
# list (i.e. "(1, 2, 3)" or "[1, 2, 3]")
elseif haskey(expr_parens, head) # :tuple/:vcat
op, cl = expr_parens[head]
if head === :vcat
if head === :vcat || head === :bracescat
sep = "; "
elseif head === :hcat || head === :row
sep = " "
Expand Down
6 changes: 4 additions & 2 deletions src/ast.scm
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
(string #\( (deparse-arglist (cdr e))
(if (length= e 2) #\, "")
#\)))
((cell1d) (string #\{ (deparse-arglist (cdr e)) #\}))
((call) (string (deparse (cadr e)) #\( (deparse-arglist (cddr e)) #\)))
((ref) (string (deparse (cadr e)) #\[ (deparse-arglist (cddr e)) #\]))
((curly) (string (deparse (cadr e)) #\{ (deparse-arglist (cddr e)) #\}))
Expand All @@ -63,6 +62,9 @@
((vect) (string #\[ (deparse-arglist (cdr e)) #\]))
((vcat) (string #\[ (deparse-arglist (cdr e) ";") #\]))
((hcat) (string #\[ (deparse-arglist (cdr e) " ") #\]))
((row) (deparse-arglist (cdr e) " "))
((braces) (string #\{ (deparse-arglist (cdr e)) #\}))
((bracescat) (string #\{ (deparse-arglist (cdr e) ";") #\}))
((const) (string "const " (deparse (cadr e))))
((global local)
(string (car e) " " (string.join (map deparse (cdr e)) ", ")))
Expand Down Expand Up @@ -91,7 +93,7 @@
(string (deparse (cadr e)) " where "
(if (length= e 3)
(deparse (caddr e))
(deparse (cons 'cell1d (cddr e))))))
(deparse (cons 'braces (cddr e))))))
((function for while)
(deparse-block (string (car e) " " (deparse (cadr e)))
(block-stmts (caddr e))))
Expand Down
22 changes: 6 additions & 16 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@
(if (eq? t 'where)
(begin (take-token s)
(let ((var (parse-comparison s)))
(loop (if (and (pair? var) (eq? (car var) 'cell1d))
(loop (if (and (pair? var) (eq? (car var) 'braces))
(list* 'where ex (cdr var)) ;; form `x where {T,S}`
(list 'where ex var))
(peek-token s))))
Expand Down Expand Up @@ -2190,25 +2190,15 @@
(take-token s)
(if (eqv? (require-token s) #\})
(begin (take-token s)
'(cell1d))
'(braces))
(let ((vex (parse-cat s #\} end-symbol)))
(if (null? vex)
'(cell1d)
'(braces)
(case (car vex)
((vect) `(cell1d ,@(cdr vex)))
((hcat) `(cell2d 1 ,(length (cdr vex)) ,@(cdr vex)))
((vect) `(braces ,@(cdr vex)))
((hcat) `(bracescat (row ,@(cdr vex))))
((comprehension) (error "{a for a in b} syntax is discontinued"))
(else
(if (and (pair? (cadr vex)) (eq? (caadr vex) 'row))
(let ((nr (length (cdr vex)))
(nc (length (cdadr vex))))
(begin
`(cell2d ,nr ,nc
,@(apply append
;; transpose to storage order
(apply map list
(map cdr (cdr vex)))))))
`(cell1d ,@(cdr vex)))))))))
(else `(bracescat ,@(cdr vex))))))))

;; string literal
((eqv? t #\")
Expand Down
4 changes: 2 additions & 2 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2181,8 +2181,8 @@
(syntax-deprecation #f "Expr(:(=>), ...)" "Expr(:call, :(=>), ...)")
`(call => ,(expand-forms (cadr e)) ,(expand-forms (caddr e))))

'cell1d (lambda (e) (error "{ } vector syntax is discontinued"))
'cell2d (lambda (e) (error "{ } matrix syntax is discontinued"))
'braces (lambda (e) (error "{ } vector syntax is discontinued"))
'bracescat (lambda (e) (error "{ } matrix syntax is discontinued"))

'string
(lambda (e) (expand-forms `(call (top string) ,@(cdr e))))
Expand Down
16 changes: 16 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,22 @@ test_mt(show_f5, "show_f5(A::AbstractArray{T,N}, indexes::Vararg{$Int,N})")
@test_repr "[a;]"
@test_repr "[a; b]"

# other brackets and braces
@test_repr "[a]"
@test_repr "[a,b]"
@test_repr "[a;b;c]"
@test_repr "[a b]"
@test_repr "[a b;]"
@test_repr "[a b c]"
@test_repr "[a b; c d]"
@test_repr "{a}"
@test_repr "{a,b}"
@test_repr "{a;b;c}"
@test_repr "{a b}"
@test_repr "{a b;}"
@test_repr "{a b c}"
@test_repr "{a b; c d}"

# Printing of :(function f end)
@test sprint(show, :(function f end)) == ":(function f end)"
@test_repr "function g end"
Expand Down

0 comments on commit c80c7f8

Please sign in to comment.