Forum: Ruby Parsing bug with 'return not' (was, Syntax bug)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Good Night M. (Guest)
on 2007-07-23 00:31
(Received via mailing list)
On Sun, 22 Jul 2007 03:50:01 -0400, Bil K. wrote:
>>   myrect.rb:8: syntax error, unexpected kNOT, expecting kEND
>>       return  not ((other.x1 < self.x0 or self.x1 < other.x0) and
>
> You need to use the '!' operator instead of the 'not' "conjunction".
> It's a precedence thing -- the parser is seeing 'return not' -- see
> parse.y in the source.

Thanks, maybe I'll look. It's not just precedence though, or else
'return' itself is part of it:

  def ok
    return (not true)
  end

  def fails
    return not true
  end

And note that within an 'if', it works as you'd expect:

  def with_parens_ok
    if (not true)
      puts "not true"
    end
  end

  def no_parens_ok
    if not true
      puts "not true"
    end
  end
Gregory B. (Guest)
on 2007-07-23 05:17
(Received via mailing list)
On 7/22/07, Good Night M. <removed_email_address@domain.invalid> wrote:
> On Sun, 22 Jul 2007 03:50:01 -0400, Bil K. wrote:
> >>   myrect.rb:8: syntax error, unexpected kNOT, expecting kEND
> >>       return  not ((other.x1 < self.x0 or self.x1 < other.x0) and
> >
> > You need to use the '!' operator instead of the 'not' "conjunction".
> > It's a precedence thing -- the parser is seeing 'return not' -- see
> > parse.y in the source.
>
> Thanks, maybe I'll look. It's not just precedence though, or else
> 'return' itself is part of it:

if binds differently than return.  It is precedence.
Rick D. (Guest)
on 2007-07-25 20:53
(Received via mailing list)
On 7/22/07, Gregory B. <removed_email_address@domain.invalid> wrote:
> > 'return' itself is part of it:
>
> if binds differently than return.  It is precedence.

Except that return isn't an operator.  As far as I can tell, looking
at parse.y for ruby1.8.6 the parse tree for the original statement
which raised the question

return not true

should be:

command_call
  kRETURN      expr
                         kNOT   expr
                                        primary
                                           var_ref
                                              variable
                                                 kTRUE

There definitely looks to be something strange here.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
This topic is locked and can not be replied to.