[doc] precedence of modifier-rescue
The docs state that modifier-rescue has higher precedence than assignments which have higher precedence than modifier-if. This is true for
v = expr rescue $! if condition but not for
v = expr if condition rescue $! which is treated as
(v = expr if condition) rescue $! rather than
v = expr if (condition rescue $!)
This goes similarly for
defined? expr rescue $!
not expr rescue $!
expr1 and expr2 rescue $!
expr1 or expr2 rescue $!
So maybe the documentation should state that modifier-rescue has equal precedence to modifier-if & others, with an exception made for assignments? I'm not entirely sure how to describe that exception though.
Updated by mame (Yusuke Endoh) about 1 month ago
- Status changed from Open to Rejected
The current doc about precedence is correct. The behavior you showed is not caused by precedence, but by the grammer itself.
The point is, that
<stmt> rescue <stmt> is a statement, not an expression. The right side of modifier-if must be an expression, so
<stmt> rescue <stmt> cannot be a right side of modifier-if. So,
<stmt> if <stmt> rescue <stmt> can parse only as
(<stmt> if <stmt>) rescue <stmt>.
defined? also requires an expression as its argument. So
defined? expr rescue $! can parse only as
defined? (expr rescue $!).
You can see the precedence by the following code:
stmt if v = condition rescue $!. It can parse as both
(stmt if v = condition) rescue $! and
stmt if v = (condition rescue $!) but the second one is chosen because modifier-rescue has higher precedence than modifier-if.
Updated by Dan0042 (Daniel DeLorme) about 1 month ago
- Status changed from Rejected to Feedback
- Description updated (diff)
- File modifier-statements.patch modifier-statements.patch added
Ok, I'm starting to see. The difference between statements and expressions is why we get this
puts( 1 if 2 ) #=> SyntaxError puts((1 if 2)) #=> 1 puts(if 2;1;end) #=> 1 ...interesting puts( 1 rescue 2 ) #=> SyntaxError puts((1 rescue 2)) #=> 1 defined?(1 rescue 2) #=> SyntaxError defined? (1 rescue 2) #=> "expression"
a if b rescue c is parsed as
‹a if b› rescue c because
a if ‹b rescue c› would be a SyntaxError.
In any case, even if the issue is not precedence itself, I think the documentation should be updated somehow, because it leads one to think that
a if b rescue c is equivalent to
a if (b rescue c)
Now that I understand the nature of the issue I've tried writing a documentation patch.