Chevrotain / chevrotain

Parser Building Toolkit for JavaScript
https://chevrotain.io
Apache License 2.0
2.44k stars 200 forks source link

JSON example: lexer gets hung up for several seconds #1788

Closed mindplay-dk closed 2 years ago

mindplay-dk commented 2 years ago

In the playground, open the default "JSON Grammar and CST output" example.

Then paste the following input:

{
    "name" : "Bobby",
    "children ages : [1, 2, 3, 4],
    "partial success": "only one array element lost"
}

⚠ Notice the deliberate omission of the closing quote after children ages.

Lexer appears to get stuck for more than 6 seconds:

image

I don't know how that's even possible with an input this small - what could it possibly be doing for this long? 🙂

NaridaL commented 2 years ago

What browser are you using?

mindplay-dk commented 2 years ago

Just upgraded from Chrome 99 to Chrome 100, so:

Version 99.0.4844.84 (Official Build) (64-bit)
Version 100.0.4896.75 (Official Build) (64-bit)

Upgrading to Chrome 100 seems to reveal a bit more detail in the Performance report:

image

It looks like it's hung up on a RegExp?

mindplay-dk commented 2 years ago

Tested in recent versions of Firefox with similar results.

97.0.1 (64-bit)
98.0.2 (64-bit)

image

mindplay-dk commented 2 years ago

Also note, in Firefox, the "Output" panel comes up blank after trying to parse this. 🤔

mindplay-dk commented 2 years ago

For the record (as you might expect) same issue in Edge.

NaridaL commented 2 years ago

It's odd, I can't reproduce it in firefox or chrome:

chrome Version 99.0.4844.84 (Official Build) (64-bit):

EDIT: NEVERMIND I was pasting in the wrong field..... I indeed get the same thing

NaridaL commented 2 years ago

Yeah the issue is the regex for the StringLiteral. As far as i can tell, the + nested inside the * leads to an explosion of combinations if it isn't found.

Hold on...

mindplay-dk commented 2 years ago

Sorry, I don't know enough regex to say if the PR is right. 😅

NaridaL commented 2 years ago

@mindplay-dk Should be fixed now.

mindplay-dk commented 2 years ago

Confirmed, the issue is gone. Nice work! Thanks. 🙂