Subject: Re: bug#22241: 25.0.50; etags Ruby parser problems

From: Dmitry Gutov <[email-masked]>
Date: Sat, 23 Jan 2016 22:29:02 +0300

On 01/23/2016 09:59 PM, Eli Zaretskii wrote:

> So I guess I will leave constants out for now: etags has no notion of
> expressions.

That would be a noticeable omission. Can't you just look for

^[ \t]([A-Z][a-z0-9_])[ \t]*=[ \t]*

? Then record the first group, and simply don't look at what's being 
assigned. The right hand value is an expression, and you need to skip 
over paired {} and do-end's, but the etags parser has to do that anyway, 
right?

> Is the telltale part "<<" or "self" (or both)?  If it's "<<", then are
> there other such tokens that "invalidate" a class?

It's "class << self" as a whole. Instead of self, there could be a 
variable, or a class name, but let's ignore those cases for now.

If we see "class <<" - it's not a class definition. If it's followed by 
"self", record the methods inside the scope as class methods. If it's 
followed by something other than "self"... maybe even skip the following 
scope altogether.

> Looks complicated, but I will look into this.  I hope no identifier
> can be named "end", as in
>
>    def foo
>      bar = end
>    end
>
> ?

No variable can be named 'end'. But 'end' can be a method name (so 
foo.end is valid syntax). You should also be on the lookout for :end or 
end:, that's an :end Symbol, not a keyword.

In practice, the 'end' keyword is almost always either preceded by ^[ 
\t]* or by ;[ \t]*.

>> It's common to use '#' in the qualified names of instance methods
>
> What part of the source makes 'foo!' an instance method?

An instance method is a "normal" method, i.e. a method you can call on 
an "instance" of a class. Example:

class C
   def foo
   end
end

(That's C#foo).

c = C.new # instantiate
c.foo # call

>> Because 'attr_writer :bar' effectively expands to
>>
>> def bar=(val)
>>     @bar = val
>> end
>>
>> and 'attr_accessor :tee' expands into
>>
>> def tee
>>     @tee
>> end
>>
>> def tee=(val)
>>     @tee = val
>> end
>
> So you are saying that attr_writer and attr_accessor cause the '=' to
> be appended?

They generate a method with the name bar=, yes.

To clarify the meaning of this: you can't have '=' in a name of a 
variable, only at the end of a method name. And if you have 'bar=(val)' 
defined in class C, it gets called during assignment:

class C
   def bar=(val)
     @bar = val
   end

   def foo
     @bar * 3
   end
end

c = C.new
c.bar = 4
c.foo # => 12

So attr_writer, attr_reader and attr_accessor generate "accessor" 
methods for the instance variables in the given class.