Page MenuHomePhabricator

Differential and Paste incorrectly highlight java "enhanced for" loop if there is no space before the colon
Closed, InvalidPublic

Description

In https://secure.phabricator.com/P1088 , "aString:" appears to be highlighted as a label, but it's not. It's a variable name followed by the ":" in the java for loop syntax. Interestingly, remarkup highlights this differently and doesn't exhibit the problem (though it doesn't highlight the real label either):

List<String> strings = getStrings();
fail:
for (String aString: strings) {
  if (aString.equals("fail") break fail;
  System.out.println(aString);
}

Event Timeline

dreiss raised the priority of this task from to Low.
dreiss updated the task description. (Show Details)
dreiss added a subscriber: dreiss.

We use Pygments to highlight languages which we don't have a native lexer for, so the solutions are probably:

  1. Update Pygments.
  2. If the problem persists, file an issue against Pygments.
  3. Or, port the lexer to PHP (see T3130). This has advantages (it's 100x faster, and can interpret symbols correctly) but also has disadvantages (you have to sink an afternoon into writing a lexer).

Does that seem reasonable? I'll see if I can easyupgrade pygments or something on this box to differentiate (1) and (2).

My efforts to easy upgrade seem to be stuck at 1.4, while the current release is 1.6. I am not sure I'm qualified to use pip, the difficult Python package manager for advanced experts.

Oh, and in P1088, we've correctly guessed the language is "Java", based on the file extension.

In remarkup, we don't know that it's Java, and our efforts to guess what it is fail (our heuristics aren't very sophisticated; possibly we could do better than we do). Since we can't guess what it is, we fall back to the default language (PHP on this install). Using lang=java explicitly will reproduce the Paste result:

loop:
for (Array a: item) {
  // ...
}

Oh, duh. I don't know how I missed that.

Also, I imagine it's not relevant, but this is probably missing a ). It's vaguely possible that's tripping something up on the Bitbucket side -- at least in Phabricator, we have separate lexers for syntactically valid code (which we fully parse, and can further contextualize) and "code" containing syntax errors (which we make a best effort on). I'd guess this is unusual, though. Also it's possible this is valid Java now and I'm just behind the times.

if (aString.equals("fail") break fail;

Fixed the syntax. Same result. My original paste actually didn't even have that statement.