Ruby Syntax Highlighting (and a Ruby Parser BUG) 
Author Message
 Ruby Syntax Highlighting (and a Ruby Parser BUG)

I'm just trying to write a syntax highlighting plug in for my editor
and found this task very very hard.

For example look at the following string:

print "String #{ ($a={1=>2, 2=> "mystring" , 4=>6}).length }"

I think an editor should highlight everything after "print" in the
string color. But it is impossible for my colorize algorithm to find
out where the string ends without writing a complete ruby expression
parser.

And i discovered that the ruby interpreter itself does not handle this
situation right. If you replace the string  "mystring" with the string
")}mystring" so that the line becomes

print "String #{ ($a={1=>2, 2=> "})mystring" , 4=>6}).length }"

the parser signals a syntax error. This is a RUBY (1.6.7) bug.

The more time i spend with this terrible ruby syntax the more i think
it should be cleaned up from all this special cases that saves you
(in the best case) not more then 4 keystrokes.

And finally a question:

What do you expect from editor syntax highlighting ?

Until now i believed heuristics are evil, because the different colors
are there to show you errors immediately. But with this ruby example
i'm not sure any more. And i don't think that any of the other ruby
tools are working correctly.



Thu, 11 Nov 2004 09:53:40 GMT  
 Ruby Syntax Highlighting (and a Ruby Parser BUG)

Quote:

> I'm just trying to write a syntax highlighting plug in for my editor
> and found this task very very hard.

> For example look at the following string:

> print "String #{ ($a={1=>2, 2=> "mystring" , 4=>6}).length }"

This is solveable if one uses stack-based parser instead of a regex
since a regex cannot count.

Quote:
> print "String #{ ($a={1=>2, 2=> "})mystring" , 4=>6}).length }"

Full grammar parser.

Quote:
> What do you expect from editor syntax highlighting ?

I don't expect perfectness. I'd expect, however, a way to reset the
state. For example, a complicated regex will most certainly mess up
all but the most sophisticated highlighter. However, at least in
emacs, if you put a line of comment after the regex, everything will
be all right again.

As you said, the perfect parser is too expensive to write. However a
reset button (like a line of comment) is cheap.

YS.



Thu, 11 Nov 2004 10:12:02 GMT  
 Ruby Syntax Highlighting (and a Ruby Parser BUG)

Quote:

> situation right. If you replace the string  "mystring" with the string
> ")}mystring" so that the line becomes

> print "String #{ ($a={1=>2, 2=> "})mystring" , 4=>6}).length }"

> the parser signals a syntax error. This is a RUBY (1.6.7) bug.

1.6.7 parser will handle this:
print "String #{ ($a={1=>2, 2=> "\})mystring" , 4=>6}).length }"

Quote:
> The more time i spend with this terrible ruby syntax the more i think

IMO, syntax is not terrible at all - it's just the current parser
implementation being crippled a little. I'm sure it will be cleaned up
soon. I don't think that you should handle special cases
Quote:
> it should be cleaned up from all this special cases that saves you
> (in the best case) not more then 4 keystrokes.

> And finally a question:

> What do you expect from editor syntax highlighting ?

> Until now i believed heuristics are evil, because the different colors
> are there to show you errors immediately. But with this ruby example
> i'm not sure any more. And i don't think that any of the other ruby
> tools are working correctly.



Tue, 16 Nov 2004 12:28:10 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. ruby syntax file for GNU Source-highlight

2. Small patch for ruby,vim syntax highlighting file

3. Ruby Syntax highlighting for JED?

4. Kate editor in KDE2 -- Ruby syntax highlighting

5. Ruby parsers in Ruby

6. Bug in cvs ruby parser

7. pthread trouble with ruby-opengl on FreeBSD (was: Re: [announcement] Ruby 3D Ruby)

8. Ruby books (Ruby NG FAQ, Ruby FAQ, home page)

9. Why are parser tools rarely used in ruby?

10. Why are parser tools rarely used in Ruby?

11. Ruby, XML schema( RELAX NG) and a Q&D parser

12. ruby-htmltools, a tree-building HTML parser version 1.01

 

 
Powered by phpBB® Forum Software