From 6ddb1dc194e5325d683f1d90bb9a297a3c0900cb Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Mon, 24 Jul 2017 15:25:51 -0400 Subject: [PATCH] parse `{ }` expressions as `braces` and `bracescat` part of #8470 --- NEWS.md | 3 +++ base/show.jl | 5 +++-- src/ast.scm | 6 ++++-- src/julia-parser.scm | 24 +++++++----------------- src/julia-syntax.scm | 4 ++-- test/parse.jl | 15 ++++++++------- test/show.jl | 16 ++++++++++++++++ 7 files changed, 43 insertions(+), 30 deletions(-) diff --git a/NEWS.md b/NEWS.md index c90cb93b30f16..837bde590f84a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 ---------------- diff --git a/base/show.jl b/base/show.jl index cfda889c54228..86e10440f4395 100644 --- a/base/show.jl +++ b/base/show.jl @@ -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 ## @@ -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 = " " diff --git a/src/ast.scm b/src/ast.scm index 0023531f04337..66f28ceffd915 100644 --- a/src/ast.scm +++ b/src/ast.scm @@ -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)) #\})) @@ -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)) ", "))) @@ -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)))) diff --git a/src/julia-parser.scm b/src/julia-parser.scm index 4c45085815b14..b192147daeaa2 100644 --- a/src/julia-parser.scm +++ b/src/julia-parser.scm @@ -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)))) @@ -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 #\") diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm index 09fff6029571a..4f04712e8dc83 100644 --- a/src/julia-syntax.scm +++ b/src/julia-syntax.scm @@ -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)))) diff --git a/test/parse.jl b/test/parse.jl index 26d42d52a76b7..686c56a574962 100644 --- a/test/parse.jl +++ b/test/parse.jl @@ -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]")) diff --git a/test/show.jl b/test/show.jl index 02334eaab612a..04fa06872cb85 100644 --- a/test/show.jl +++ b/test/show.jl @@ -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"