View Full Version : Divisions and regex

10-23-2005, 02:42 PM
I'm writing an ECMAScript tokeniser and parser and trying to find out if I can eliminate the switching from tokenising "/" as start of regex or the division operator depending on the parser feedback - essentially, if I can make the tokeniser independent of the parser. (I have a gut feeling this needs too much special casing to be worth it).

Division operator, as well as division-assignment, requires one operand on either side. This is in most cases easy to check for - division can ONLY appear if there is an expression before it. But if it was as easy as just checking one simple thing, there would be no need for ECMAScript to specify two scanner modes, would there? No, there exist cases where "/" potentially could be the start of a regular expression, but where division is the natural interpretation of it. One example is semicolon insertion.
var a=8
/2Is naturally handled as if there was no new-line there. But, the following is a possibility
var a=8
"123".match(/2/)Here, the engine inserts a semicolon and treats the second line as a second statement. Since a statement can open with a regex, this isn't entirely a far fetched situation:
var a=8
/2/.test("123")However, that fails. For what reason? Well, because the "/" is then treated like division in the expression started in the line above instead of start of regex in a new statement.

I'm looking for other cases where division and regex start would both be possible things the author meant to do, but where the engine prioritises division. Anybody know any?

10-25-2005, 03:28 PM
Interesting idea / discussion... I don't have anything yet (something this deep may take a little time to churn over in my mind), but I thought I'd respond so's you wouldn't think you were being ignored...

10-26-2005, 01:00 AM
I wonder if you could iteratively test the typeof of eval statements for each of your various subexpressions ... ?!??