Skip to content

Commit

Permalink
fix unintentional change to parsing assignments inside tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jan 28, 2016
1 parent 98802a7 commit eb2ff4b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,9 @@
(else
(parse-matrix s first closer #f))))))))))

(define (kw-to-= e) (if (kwarg? e) (cons '= (cdr e)) e))
(define (=-to-kw e) (if (assignment? e) (cons 'kw (cdr e)) e))

;; translate nested (parameters ...) expressions to a statement block if possible
;; this allows us to first parse tuples using parse-arglist
(define (parameters-to-block e)
Expand All @@ -1615,9 +1618,7 @@
(cons (car snd) rec)))
#f)))
(else #f))
(list (if (kwarg? e)
(cons '= (cdr e))
e))))
(list (kw-to-= e))))

;; convert an arglist to a tuple or block expr
;; leading-semi? means we saw (; ...)
Expand All @@ -1634,8 +1635,8 @@
(and (null? first) (null? args) (not comma?)
`(block)) ;; this case is (;)
(if (and (pair? args) (pair? (car args)) (eq? (caar args) 'parameters))
`(tuple ,(car args) ,@first ,@(cdr args))
`(tuple ,@first ,@args)))))
`(tuple ,(car args) ,@first ,@(map kw-to-= (cdr args)))
`(tuple ,@first ,@(map kw-to-= args))))))

(define (not-eof-2 c)
(if (eof-object? c)
Expand Down
4 changes: 2 additions & 2 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@
(body (caddr e)))
(let ((argl (if (pair? a)
(if (eq? (car a) 'tuple)
(cdr a)
(map =-to-kw (cdr a))
(if (eq? (car a) 'block)
(cond ((length= a 1) '())
((length= a 2) (list (cadr a)))
Expand All @@ -975,7 +975,7 @@
`((parameters ,(caddr a)) ,(cadr a))))
(else
(error "more than one semicolon in argument list")))
(list a)))
(list (=-to-kw a))))
(list a)))
;; TODO: always use a specific special name like #anon# or _, then ignore
;; this as a local variable name.
Expand Down
5 changes: 5 additions & 0 deletions test/keywordargs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,8 @@ let f = (x;a=1,b=2)->(x, a, b)
@test f(0,b=88) === (0, 1, 88)
@test_throws ErrorException f(0,z=1)
end
@test ((a=2)->10a)(3) == 30
@test ((a=2)->10a)() == 20
@test ((a=1,b=2)->(a,b))() == (1,2)
@test ((a=1,b=2)->(a,b))(5) == (5,2)
@test ((a=1,b=2)->(a,b))(5,6) == (5,6)
1 change: 1 addition & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ macro f(args...) end; @f ""
@test parse("(x,;y=1)") == Expr(:tuple, Expr(:parameters, Expr(:kw, :y, 1)), :x)
@test parse("(x,a;y=1)") == Expr(:tuple, Expr(:parameters, Expr(:kw, :y, 1)), :x, :a)
@test parse("(x,a;y=1,z=2)") == Expr(:tuple, Expr(:parameters, Expr(:kw,:y,1), Expr(:kw,:z,2)), :x, :a)
@test parse("(a=1, b=2)") == Expr(:tuple, Expr(:(=), :a, 1), Expr(:(=), :b, 2))

# integer parsing
@test is(parse(Int32,"0",36),Int32(0))
Expand Down

0 comments on commit eb2ff4b

Please sign in to comment.