antlr4: Parsing Java with JavaScript parser fails while CSharp/Java parser succeeds

I have been parsing some Java code using grammars-v4/java/java/*.g4, and I noticed that for the following input, the JavaScript parser fails but the CSharp and Java parsers succeed.

public class HelloWorld { 
   public static void main(String[] args) { 
      System.out.println("Hello, World" + "hi There");
   }
}

Note, in all parsers I generate for the grammar, I used the default listeners.

Debugging the JavaScript parser side-by-side with the CSharp parser, I’ve determined that the JS parser fails to parse the expression “Hello, World” + “hi There” at the ‘+’ operator.

Looking over the generated parsers JavaParser.cs and JavaParser.js, I have noticed that the likely culprit is the generated code:

CSharp:

if ( ParseListeners!=null )
    TriggerExitRuleEvent();

JavaScript:

if(this._parseListeners!==null) {
    this.triggerExitRuleEvent();
}

These codes are not the same, and they execute differently.

In CSharp, ParseListeners in the snippet is a get property call in the Parser class that returns an empty list if the backing field is null. In this case, it returns an empty list, which is for sure not equal to null, and this.triggerExitRuleEvent() is called.

In JavaScript, this._parseListeners is a reference to the backing field itself, not the getter getParseListeners(). In JavaScript, triggerExitRuleEvent() is never called.

Also, just scanning through the generated parsers, I’ve also noticed constants ~0x3f used in CSharp vs ~0x1f used in JavaScript. While it’s likely not important in this example, it is rather irritating to see magic number differences between the runtimes.

–Ken

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 24 (24 by maintainers)

Commits related to this issue

Most upvoted comments

@kaby76 Thanks for the tough work of narrowing down the scenario.