[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby << is ambiguos

Neville Franks

1/28/2007 8:50:00 PM

Hi, I working on syntax highlighting for ED4W and have problem with <<
because it has three uses.

1. << operator
2. << HERE Doc
3. class << Name - Singleton Class

The problem is determining if << starts a Here Doc. It is easy to handle
class<<Name is this regard, but I can't see how you can work out << in
the following:

Ared = "mary"
angy = "bill"
angy <<Ared # does this start a Here Doc or is it operator <<
print <<Ared # as above.

Thanks.

---
Neville Franks, http://www.g... http://www.surf...

--
Posted via http://www.ruby-....

7 Answers

Stefano Crocco

1/28/2007 8:58:00 PM

0

Alle 21:50, domenica 28 gennaio 2007, Neville Franks ha scritto:
> Hi, I working on syntax highlighting for ED4W and have problem with <<
> because it has three uses.
>
> 1. << operator
> 2. << HERE Doc
> 3. class << Name - Singleton Class
>
> The problem is determining if << starts a Here Doc. It is easy to handle
> class<<Name is this regard, but I can't see how you can work out << in
> the following:
>
> Ared = "mary"
> angy = "bill"
> angy <<Ared # does this start a Here Doc or is it operator <<
> print <<Ared # as above.
>
> Thanks.
>
> ---
> Neville Franks, http://www.g... http://www.surf...

Some editors (kate and vim, for example) assume the presence of a whitespace
after the << in the operator case:

a=[]
s="abc"
#this gets highlighted as a HERE Doc
a<<s

#this gets highlighted as an operator
a<< s

Stefano

WoNáDo

1/28/2007 9:05:00 PM

0

Neville Franks schrieb:
> Hi, I working on syntax highlighting for ED4W and have problem with <<
> because it has three uses.
>
> 1. << operator
> 2. << HERE Doc
> 3. class << Name - Singleton Class
>
> The problem is determining if << starts a Here Doc. It is easy to handle
> class<<Name is this regard, but I can't see how you can work out << in
> the following:
>
> Ared = "mary"
> angy = "bill"
> angy <<Ared # does this start a Here Doc or is it operator <<
> print <<Ared # as above.
>
> Thanks.
>
> ---
> Neville Franks, http://www.g... http://www.surf...
>
Ooops! - I Think Ruby itself has some problems with it:

irb(main):001:0> Ared = "mary"
=> "mary"
irb(main):002:0> angy = "bill"
=> "bill"
irb(main):003:0> angy <<Ared
irb(main):004:0" Ared
=> "mary"
irb(main):005:0> angy
=> "billmary"
irb(main):006:0> angy << Ared
=> "billmarymary"
irb(main):007:0> angy <<EOT
irb(main):008:0" xxxxx
irb(main):009:0" EOT
NameError: uninitialized constant EOT
from (irb):7
irb(main):010:0> angy
=> "billmarymary"
irb(main):011:0> angy << EOT
NameError: uninitialized constant EOT
from (irb):11

Wolfgang Nádasi-Donner

Neville Franks

1/28/2007 9:07:00 PM

0

Stefano Crocco wrote:
> Alle 21:50, domenica 28 gennaio 2007, Neville Franks ha scritto:
>>
>> Ared = "mary"
>> angy = "bill"
>> angy <<Ared # does this start a Here Doc or is it operator <<
>> print <<Ared # as above.
>>
>> Thanks.
>>
>> ---
>> Neville Franks, http://www.g... http://www.surf...
>
> Some editors (kate and vim, for example) assume the presence of a
> whitespace
> after the << in the operator case:
>
> a=[]
> s="abc"
> #this gets highlighted as a HERE Doc
> a<<s
>
> #this gets highlighted as an operator
> a<< s
>
> Stefano

Stefano,
Thanks, but that isn't how the Ruby interpreter works, so this is really
a hack that may or may not work with real code.

I can just do the same thing, but if possible I'd like a 'correct'
solution.

I've been looking at all of the Ruby Editors on Windows and am surprised
at how poorly they handle Ruby syntax. I would never release a product
like these.

--
Posted via http://www.ruby-....

James Gray

1/28/2007 9:14:00 PM

0

On Jan 28, 2007, at 3:06 PM, Neville Franks wrote:

> Thanks, but that isn't how the Ruby interpreter works, so this is
> really
> a hack that may or may not work with real code.

You're definitely going to need heuristics like this in syntax
highlighting Ruby source code. Only ruby can read Ruby, to borrow
the Perl expression. ;)

James Edward Gray II

Neville Franks

1/29/2007 11:30:00 PM

0

Steven Lumos wrote:
> Neville Franks <subs@surfulater.com> writes:
>
>>
>> Ared = "mary"
>> angy = "bill"
>> angy <<Ared # does this start a Here Doc or is it operator <<
>> print <<Ared # as above.
>>
>> Thanks.
>
> Your example is incomplete.
>
> angy <<Ared # is an operator
> print <<Ared # is a HERE doc...
> some string
> Ared # ...because it has an end marker
>
> That's how the Emacs ruby-mode seems to do it.
>
> Steve

Steve, in that case Emacs ruby-mode is incorrect.

angy <<Ared # is an operator
some string
Ared

gives:

C:\ruby\bin\ruby.exe D:\Ed32\BrowserTest\RubyHereDoc3.rb
D:/Ed32/BrowserTest/RubyHereDoc3.rb:3: undefined method `angy' for
main:Object (NoMethodError)

To get this to compile, a method angry must be defined:

def angy var
end

So the only way to determine if << is a Here Doc is to know if the
preceeding token is a method. And an editor can't know that.

--
Posted via http://www.ruby-....

Neville Franks

1/29/2007 11:32:00 PM

0

Neville Franks wrote:
> ...
> Steve, in that case Emacs ruby-mode is incorrect.
>
> angy <<Ared # is an operator
> some string
> Ared
>
> gives:
>
> C:\ruby\bin\ruby.exe D:\Ed32\BrowserTest\RubyHereDoc3.rb
> D:/Ed32/BrowserTest/RubyHereDoc3.rb:3: undefined method `angy' for
> main:Object (NoMethodError)
>
> To get this to compile, a method angry must be defined:
>
> def angy var
> end
>
> So the only way to determine if << is a Here Doc is to know if the
> preceeding token is a method. And an editor can't know that.

PS. Also see my comment here:

http://seclib.blogspot.com/2005/11/more-on-leftshift-and-he...

---
Neville Franks, http://www.g... http://www.surf...

--
Posted via http://www.ruby-....

WoNáDo

2/1/2007 12:23:00 PM

0

There is a similar problem with "?" and "?:".

irb(main):001:0> ?2
=> 50
irb(main):002:0> sep = "x" * (true?2:1)
SyntaxError: compile error
(irb):2: parse error, unexpected ':', expecting ')'
sep = "x" * (true?2:1)
^
from (irb):2
irb(main):003:0> sep = "x" * (true ?2:1)
=> "xx"

Wolfgang Nádasi-Donner