Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts

    Divisions and regex

    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.
    Code:
    var a=8
    /2
    Is naturally handled as if there was no new-line there. But, the following is a possibility
    Code:
    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:
    Code:
    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?
    Last edited by liorean; 10-23-2005 at 01:45 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #3
    Regular Coder
    Join Date
    Dec 2002
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I wonder if you could iteratively test the typeof of eval statements for each of your various subexpressions ... ?!??


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •