Skip to content

Commit

Permalink
fix #10677, better error for using a non-unary operator as unary
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Mar 30, 2015
1 parent 9a53902 commit ba22a70
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,8 @@
(let ((t (require-token s)))
(if (closing-token? t)
(error (string "unexpected " t)))
(cond ((memq t unary-ops)
;; TODO: ? should probably not be listed here except for the syntax hack in osutils.jl
(cond ((and (operator? t) (not (memq t '(: |'| ?))) (not (syntactic-unary-op? t)))
(let* ((op (take-token s))
(nch (peek-char (ts:port s))))
(if (and (or (eq? op '-) (eq? op '+))
Expand All @@ -851,11 +852,14 @@
(list 'call op (parse-factor s)))
num))
(let ((next (peek-token s)))
(cond ((or (closing-token? next) (newline? next))
(cond ((or (closing-token? next) (newline? next) (eq? next '=))
op) ; return operator by itself, as in (+)
((eqv? next #\{) ;; this case is +{T}(x::T) = ...
(ts:put-back! s op)
(parse-factor s))
((and (not (memq op unary-ops))
(not (eqv? next #\( )))
(error (string "\"" op "\" is not a unary operator")))
(else
(let ((arg (parse-unary s)))
(if (and (pair? arg)
Expand Down
10 changes: 10 additions & 0 deletions test/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,13 @@ macro test999_str(args...); args; end

# issue #8301
@test_throws ParseError parse("&*s")

# issue #10677
@test_throws ParseError parse("/1")
@test_throws ParseError parse("/pi")
@test parse("- = 2") == Expr(:(=), :(-), 2)
@test parse("/ = 2") == Expr(:(=), :(/), 2)
@test_throws ParseError parse("< : 2")
@test_throws ParseError parse("+ : 2")
@test_throws ParseError parse("< :2")
@test parse("+ :2") == Expr(:call, :(+), QuoteNode(2))
6 changes: 3 additions & 3 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ end"

# issue #8994
@test_repr "get! => 2"
@test_repr "< : 2"
@test_repr "< :: T"
@test_repr "S{< <: T}"
@test_repr "(<) : 2"
@test_repr "(<) :: T"
@test_repr "S{(<) <: T}"
@test_repr "+ + +"

# issue #9474
Expand Down

0 comments on commit ba22a70

Please sign in to comment.