Skip to content

Commit

Permalink
parse { } expressions as braces and bracescat
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jul 28, 2017
1 parent 8ebedd6 commit 6ddb1dc
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 30 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
24 changes: 7 additions & 17 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)))
((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)))))))))
((vect) `(braces ,@(cdr vex)))
((hcat) `(bracescat (row ,@(cdr vex))))
((comprehension) `(braces ,@(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
15 changes: 8 additions & 7 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -619,15 +619,16 @@ end
@test_throws ParseError parse("--x")
@test_throws ParseError parse("stagedfunction foo(x); end")

#@test_throws ParseError parse("{1,2,3}")
#@test_throws ParseError parse("{1 2 3 4}")
#@test_throws ParseError parse("{1,2; 3,4}")
@test_throws ParseError parse("{x for x in 1:10}")
@test_throws ParseError parse("{x=>y for (x,y) in zip([1,2,3],[4,5,6])}")
#@test_throws ParseError parse("{:a=>1, :b=>2}")

@test parse("A=>B") == Expr(:call, :(=>), :A, :B)

@test parse("{1,2,3}") == Expr(:braces, 1, 2, 3)
@test parse("{1 2 3 4}") == Expr(:bracescat, Expr(:row, 1, 2, 3, 4))
@test parse("{1 2; 3 4}") == Expr(:bracescat, Expr(:row, 1, 2), Expr(:row, 3, 4))
@test parse("{x for x in 1:10}") == Expr(:braces, :(x for x in 1:10))
@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])))
@test parse("{:a=>1, :b=>2}") == Expr(:braces, Expr(:call, :(=>), QuoteNode(:a), 1),
Expr(:call, :(=>), QuoteNode(:b), 2))

# this now is parsed as getindex(Pair{Any,Any}, ...)
@test_throws MethodError eval(parse("(Any=>Any)[]"))
@test_throws MethodError eval(parse("(Any=>Any)[:a=>1,:b=>2]"))
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 6ddb1dc

Please sign in to comment.