From ba22a7009e02d52d85670ad27a900de4e9b4cfa7 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Mon, 30 Mar 2015 18:15:32 -0400 Subject: [PATCH] fix #10677, better error for using a non-unary operator as unary --- src/julia-parser.scm | 8 ++++++-- test/parser.jl | 10 ++++++++++ test/show.jl | 6 +++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/julia-parser.scm b/src/julia-parser.scm index 7dfe56188d459..ff904d156c22f 100644 --- a/src/julia-parser.scm +++ b/src/julia-parser.scm @@ -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 '+)) @@ -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) diff --git a/test/parser.jl b/test/parser.jl index 1792182673b68..5d0332ed3adae 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -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)) diff --git a/test/show.jl b/test/show.jl index 2a8f85882e2d1..b9f576380a8b0 100644 --- a/test/show.jl +++ b/test/show.jl @@ -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