Skip to content

Commit

Permalink
fix #24153, make <| right associative
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Oct 20, 2017
1 parent db91082 commit e05b1d2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ 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]).

* The parsing of `<|` is now right associative. `|>` remains left associative ([#24153]).

* `{ }` expressions now use `braces` and `bracescat` as expression heads instead
of `cell1d` and `cell2d`, and parse similarly to `vect` and `vcat` ([#8470]).

Expand Down
3 changes: 2 additions & 1 deletion doc/src/manual/mathematical-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ Julia applies the following order and associativity of operations, from highest
| Multiplication | `* / % & \` | Left[^2] |
| Bitshifts | `<< >> >>>` | Left |
| Addition | `+ - \| ⊻` | Left[^2] |
| Syntax | `: ..` followed by `\|>` | Left |
| Syntax | `: ..` | Left |
| Syntax | `\|> <\|` | `\|>` Left, `<\|` Right |
| Comparisons | `> < >= <= == === != !== <:` | Non-associative |
| Control flow | `&&` followed by `\|\|` followed by `?` | Right |
| Assignments | `= += -= *= /= //= \= ^= ÷= %= \|= &= ⊻= <<= >>= >>>=` | Right |
Expand Down
11 changes: 10 additions & 1 deletion src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,16 @@
`(call ,op ,arg1 ,arg2))))
(else ex)))))

(define (parse-pipes s) (parse-LtoR s parse-range is-prec-pipe?))
(define (parse-pipes s)
(let loop ((ex (parse-range s))
(t (peek-token s)))
(cond ((or (eq? t '|\|>|) (eq? t '|.\|>|)) ; |> associates left
(begin (take-token s)
(loop (list 'call t ex (parse-range s)) (peek-token s))))
((or (eq? t '|<\||) (eq? t '|.<\||)) ; <| associates right
(begin (take-token s)
(list 'call t ex (parse-pipes s))))
(else ex))))

; parse ranges and postfix ...
; colon is strange; 3 arguments with 2 colons yields one call:
Expand Down
4 changes: 4 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ end

@test parse("1 == 2|>3") == Expr(:call, :(==), 1, Expr(:call, :(|>), 2, 3))

# issue #24153
@test parse("a|>b|>c|>d") == parse("((a|>b)|>c)|>d")
@test parse("a<|b<|c<|d") == parse("a<|(b<|(c<|d))")

# issue #12501 and pr #12502
parse("""
baremodule A
Expand Down

0 comments on commit e05b1d2

Please sign in to comment.