Skip to content

Commit

Permalink
fix unary-related parsing regressions caused by #26154 (#26239)
Browse files Browse the repository at this point in the history
This affects chains of unary operators, calls, and juxtaposition,
as in `-(f)(x)`.
  • Loading branch information
JeffBezanson authored Feb 28, 2018
1 parent 1da3190 commit 4378408
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1039,11 +1039,13 @@
(if (cdr parens) ;; found an argument list
(if opspc
(disallowed-space op #\( )
(parse-factor-with-initial-ex
s
(fix-syntactic-unary (cons op (tuple-to-arglist (car parens))))))
(parse-juxtapose
(parse-factor-with-initial-ex
s
(fix-syntactic-unary (cons op (tuple-to-arglist (car parens)))))
s))
(fix-syntactic-unary
(list op (parse-factor-with-initial-ex s (car parens)))))))
(list op (parse-juxtapose (parse-factor-with-initial-ex s (car parens)) s))))))
((not un)
(error (string "\"" op "\" is not a unary operator")))
(else
Expand Down Expand Up @@ -1098,10 +1100,10 @@
;; -2^3 is parsed as -(2^3), so call parse-decl for the first argument,
;; and parse-unary from then on (to handle 2^-3)
(define (parse-factor s)
(parse-factor-with-initial-ex s (parse-call s)))
(parse-factor-with-initial-ex s (parse-unary-prefix s)))

(define (parse-factor-with-initial-ex s ex0)
(let* ((ex (parse-decl-with-initial-ex s ex0))
(let* ((ex (parse-decl-with-initial-ex s (parse-call-with-initial-ex s ex0)))
(t (peek-token s)))
(if (is-prec-power? t)
(begin (take-token s)
Expand Down Expand Up @@ -1130,10 +1132,12 @@
;; parse function call, indexing, dot, and transpose expressions
;; also handles looking for syntactic reserved words
(define (parse-call s)
(let ((ex (parse-unary-prefix s)))
(if (or (initial-reserved-word? ex) (eq? ex 'mutable) (eq? ex 'primitive) (eq? ex 'abstract))
(parse-resword s ex)
(parse-call-chain s ex #f))))
(parse-call-with-initial-ex s (parse-unary-prefix s)))

(define (parse-call-with-initial-ex s ex)
(if (or (initial-reserved-word? ex) (eq? ex 'mutable) (eq? ex 'primitive) (eq? ex 'abstract))
(parse-resword s ex)
(parse-call-chain s ex #f)))

(define (parse-unary-prefix s)
(let ((op (peek-token s)))
Expand Down
10 changes: 10 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1341,3 +1341,13 @@ end

@test Meta.parse("1 -+(a=1, b=2)") == Expr(:call, :-, 1,
Expr(:call, :+, Expr(:kw, :a, 1), Expr(:kw, :b, 2)))

@test Meta.parse("-(2)(x)") == Expr(:call, :-, Expr(:call, :*, 2, :x))
@test Meta.parse("-(x)y") == Expr(:call, :-, Expr(:call, :*, :x, :y))
@test Meta.parse("-(x,)y") == Expr(:call, :*, Expr(:call, :-, :x), :y)
@test Meta.parse("-(f)(x)") == Expr(:call, :-, Expr(:call, :f, :x))
@test Meta.parse("-(2)(x)^2") == Expr(:call, :-, Expr(:call, :*, 2, Expr(:call, :^, :x, 2)))
@test Meta.parse("Y <- (x->true)(X)") ==
Expr(:call, :<, :Y,
Expr(:call, :-, Expr(:call, Expr(:->, :x, Expr(:block, LineNumberNode(1,:none), true)),
:X)))

0 comments on commit 4378408

Please sign in to comment.