codemirror / dev

Development repository for the CodeMirror editor project
https://codemirror.net/
Other
5.92k stars 376 forks source link

false positive for regex in javascript syntax highlighting #679

Closed SArpnt closed 2 years ago

SArpnt commented 2 years ago

in this code snipper /a,a/ is detected as a regex, removing commas stops the issue, and adding them or adding in other list items usually doesn't fix the issue.

[a,,,,,,,,,,,,,,,,,,,,,,,,,,a/a,a/a]

i'm using the extensions javascript() and classHighlightStyle, and i'm using a rollup bundle of codemirror and addons.

marijnh commented 2 years ago

What seems to be happening is that an ambiguity stays around for too long (pattern versus array expression), and the parser decides to cut one option (the one that should be taken, in this case) off. The result is that parsing goes into error-correction mode at the slash (which can't appear in an array pattern) and makes a regexp out of it.

Did this occur with real code or were you fuzzing the highlighting?

SArpnt commented 2 years ago

i had this issue happen with a bytebeat expression (to avoid a long explanation it's just supposed to be a js code snippet that evaluates to a number)

r=1.05946309,b=t/.2575,as=b/r,a=as/r,c=b*r,cs=c*r,d=cs*r,ds=d*r,e=ds*r,f=e*r,fs=f*r,g=fs*r,gs=g*r,([fs,e,cs,b,as,,,,,,,,,,ds,,b,cs,d,ds,fs,,,,,,,,,,cs,,ds,cs,as,gs/2,ds/2,,,,,,,,,,as,,cs,b,gs/2,g/2,fs/2][(t>>10)%64]&127)+([fs/8,0,0,fs/4,fs/8,0,g/4,0,fs/8,0,fs/4,0,fs/8,0,fs/4][(t>>10)%16]&127)

this appears as image

marijnh commented 2 years ago

Attached patch makes it so that you need 2.5× more commas before running into this issue. I don't want to remove pruning of long-running split parses entirely, since in some highly-ambiguous grammars you might end up doing the entire parsing work multiple times.

marijnh commented 2 years ago

I'll close this until the next time it causes an issue, but do feel free to add further feedback.