Sublime Forum

Ruby semantic highlight error with array insertion

#1

When I try to insert a negative value in an array, everything after becomes yellow.

variable<<-1

It doesn’t really bother me, because if you space it, it works. But I thought you’d like to know.

0 Likes

#2

Line 4 isn’t an array insertion, it’s the beginning of a heredoc. The syntax highlighting looks correct. Try putting a space between the << and -.

EDIT: Never mind, I’m wrong and the syntax is wrong. After the variable b, the << should be parsed as an infix operator even with the - character following it. A lot of syntaxes have trouble with this sort of thing. I know of a general solution, but it isn’t trivial.

1 Like

#3

I’m wrong again. Consider the following:

foo <<bar

What is this? It depends. If foo is a variable, then << is an operator, but if foo is a method, then << is the beginning of a heredoc. It is impossible for Sublime or any context-free parser to tell the difference.

All the syntax can do is make a reasonable guess. However, in your example I do feel that the syntax has made the wrong guess. In principle, I think that the syntax should work the way I originally thought the language worked because that seems like the best guess. However, implementing that would be a great deal of work, and while it might be worth it to make the syntax correct, it might not be merely to provide a better guess.

In addition, there’s the greater problem that a context-free grammar can’t possibly tell when the heredoc ends.

2 Likes

#4

Well, sublime’s parser has some minimal state where it can remember the capture groups of a context at a certain level in the stack (I’m certain you’re aware of this). What this means in practice is that one can do heredocs, but only if nothing funny happens in-between the start and end tokens.

In Ruby, you can chain heredocs, though:

myfunc(<<"THIS", 23, <<'THAT')
Here's a line
or two.
THIS
and here's another.
THAT

It’s impossible to handle this with the current parser.

Bash also has this problem:

#!/bin/bash
cat <<ONE ; cat <<TWO
Hello,
ONE
world!
TWO

The only solution will be to add more state to sublime’s parser, in one way or another.

1 Like

#5

This might be a clue:

In this case, with space before the heredoc, it becomes italic…

1 Like

#6

Are you using the default Sublime Text python syntax? You can open issue for it on: https://github.com/sublimehq/Packages/issues

0 Likes