grammars-v4 icon indicating copy to clipboard operation
grammars-v4 copied to clipboard

Javascript Closing braces are not correctly parsed

Open wterry opened this issue 4 years ago • 4 comments

I've been trying to run some tests on the JavaScript grammar and I've stumbled across this problem.

I haven't been able to figure out whether it's something I'm doing wrong or if it's an issue because, strictly speaking, I don't get an error, but I can't parse anything that includes a closing brace correctly (that is to say, } ). It's even breaking on the examples I grab from the examples folder in the JavaScript section. Here's what's happening:

If I try to parse anything (or anything I've thrown at it so far, at least) it goes fine. Take a simple if, for example: image

As can be seen on the parse tree generated on the right, everything is going fine and the closing brace is the only thing missing to complete the block. However, once the closing brace is added, here's what happens:

image

Parsing is aborted (no error thrown or anything) and as can be seen in the parsed hierarchy above, the closing brace is still marked as missing despite it having been included. I'm thinking that highlighted EOF might be part of the problem (because I can't see why it's there; the captured text doesn't end there) but I'm not getting an ambiguity report or anything of the sort. In fact, I ran a test with a parse tree walker feeding it the same input as above from a file (to make sure there's no hidden characters or anything that could be causing trouble) and the parsing still breaks on the closing brace. I know the closing brace token is read (and correctly, at that) because a debug run does stop at the processCloseBrace() method in JavaScriptLexerBase.js that is called in the lexer for the CloseBrace token here:

CloseBrace: '}' {this.ProcessCloseBrace();};

and everything is correct (the token read IS a closing brace) according to what shows up on the debug information below (some personal details redacted for privacy):

image

Here, expanding the input stream we can see everything has been read correctly, but there's an odd runtime error right at the top that I haven't been able to track down (this._inputTokens["3"] throws a type error cannot read from undefined). Regardless of that error, the ProcessCloseBrace method itself resolves correctly without a problem (returns false).

End result is, I don't know why it's breaking there, but nothing I've tried that includes a closing brace can be correctly parsed.

Any help, advice or correction would be greatly appreciated. Thanks in advance!

wterry avatar Jul 29 '21 21:07 wterry

Thanks for the prompt response and the suggestions.

In your opinion, if we tried to use this same grammar but with the Java interpreter that's in this project, would everything be stable? That is to say, changing the parsing of JS in JS to parsing JS in Java instead.

wterry avatar Jul 30 '21 16:07 wterry

Update: I'm going through the grammars in the repo with a snapshot of Antlr 4.9.3, which should be released soon. It parses all examples in javascript/javascript/examples/ for the javascript/javascript grammar. Version 4.9.3 has several critical bug fixes and enhancements in memory usage in the runtime for JavaScript and other targets.

kaby76 avatar Oct 28 '21 11:10 kaby76