| Summary: | Javascript parser bug in parenthesed comma expressions : see jQuery 1.4.2 Regexp.exec() | ||
|---|---|---|---|
| Product: | [WebTools] JSDT | Reporter: | Philippe Verdy <verdy_p> |
| Component: | General | Assignee: | Project Inbox <jsdt.javascript-inbox> |
| Status: | RESOLVED FIXED | QA Contact: | Nitin Dahyabhai <thatnitind> |
| Severity: | enhancement | ||
| Priority: | P3 | CC: | cmjaun, paul.beusterien |
| Version: | unspecified | ||
| Target Milestone: | Future | ||
| Hardware: | PC | ||
| OS: | Windows 7 | ||
| Whiteboard: | |||
See also Ticket #7032 in jQuery. http://dev.jquery.com/ticket/7032#preview Note: jQuery 1.4.3 has fixed the bug with a work-around (Ticket #7032), but the bug remains in Eclipse's Javascript parser itself (independantly of jQuery with which this bug was detected). |
Trying to load a project that uses jQuery 1.4.2, Eclipse fails to parse correctly the javascript because of this line: // Reset the position of the chunker regexp (start from head) while ((chunker.exec(''), (m = chunker.exec(soFar)) !== null)) { ... } Where chunker is a regexp. In fact it rejects as well this simple line as well in an empty javascript: while ((/x/.exec(''), (m = /y/.exec('xyz')) !== null) { /* do something here */ break; } The cure is to remove the extra parentheses surrounding the tested comma expression, so that the nullity test will be done in the second subexpression of the comma expression, and which should be equivalent: while (/x/.exec(''), (m = /y/.exec('xyz') !== null) { /* do something here */ break; } But then, there exists browsers whose javascript parser don't like "multiple" parameters for while(), so the extra parentheses are needed. The bug is not caused by the while() statement because it also occurs in simple statements using Regexp.exec()... (/x/.exec(''), 0); But it does not occur in: /x/.exec(''), 0; Apparently Eclipse does not like that we put too many parentheses to surround comma expressions, and instead of warning that the extra parentheses are not needed, it fails with a completely unrelated syntax error. This is most probably an artefact of the grammatical analusis where it attempts to detect unneeded parentheses, but the code still does not handle the warning correctly, and then it returns back to the parser as if it was a fatal error, causing the parser to recover backward, and try to parse the code differently, assuming that the only operator allowed there would then be a dot, assuming that a Regexp only returns a String object or null, but could be wring as well because the string could be tested or appended. trying to append a string to the first exec() call still fails anyway: (/x/.exec('')+'yz', 0); Another way to write the loop above would be of course (without the extra parentheses): while (/x/.exec(''), (m = /y/.exec('xyz') !== null) { /* do something here */ break; } Eclipse MAY warn OK if extra parentheses are present, but should NOT report such syntax errors. Eclipse correctly accepts strings in comma expressions like: ('foo', 0); It just fails with the String expressions returned by Regexp.exec(). In fact, searching a bit more, it fails each time there's ANY comma expression (of any type) between parentheses like: (test(), 0); or even just: (test, 0) Here again there's the same bogous diagnostic : « Syntax error on token ",", delete this token. » Extra parentheses are very common in many scripts, to clarify things, or because the code to evaluate is generated by a script, or because the expression has been used as well as a function call parameter, where comma expressions are needed. Javascript is more relaxed than Java (which does not accept any comma expresson, except in very few constructs such as the initializer part of a for() loop, and only to declare or assign multiple variables). Trying to assign the first expression does not work either and fails with the same syntax error: (m=test, 0); -- Configuration Details -- Product: Eclipse Platform 3.6.0.v201006080911 (org.eclipse.platform.ide) Installed Features: org.eclipse.platform 3.6.0.v20100602-9gF78GpqFt6trOGhL60z0oEx3fz-JKNwxPY