Wednesday, February 3, 2010

parsing indifferent to strings

I think I have implemented a solution to #226. The problem was seemingly complex, first thing I thought is I would have to keep track of all the parsing, so that it didn't parse a string, but then I thought if I could remove all strings, then replace them later, after the parsing is completed, and that's what I've done. It seems to be working! This is what I did.

At the beginning of the parsing, I run this block of code

var strings = aCode.match(/".*"/g);

for ( var i = 0; /".*"/.test(aCode); i++)
{
aCode = aCode.replace(/".*"/, "<STRING " + i + ">");
}


This, first, goes through all the code, and grabs all strings, and saves them in an array. Then, I loop through the code, applying in place of the "string". I do this until there is no more "string"'s left.

Then, at the end of the code, after all other parsing has been done, before I return aCode, I reapply all the strings like this

for( var i = 0; l = i < strings.length; i++ ){
var ex = new RegExp("\<STRING " + i + "\>");
aCode = aCode.replace(ex, strings[i]);
}

This goes through the array of strings, one at a time, until they have all been gone through, each time, applying that string to the proper with n being the index of that string in relation the the array of strings.

I believe this is pretty solid and can be demonstrated here. You should see three alert boxes, displaying messages that would normally be parsed out, then the usual black box of success. Also notice how I put a string inside the comment, to make sure it was able to handle that, as I parse the string before I parse a comment, as to avoid removing some string that happens to contain // in it. I also tested a multi line string, as I was curious, it handles it fine just so long as it's not written like this.

alert("first line
second line");

which to my knowledge, should not work anyway, plus, it's horrible code!

I think I'm done this bug, and I think the next step is to figure out how to submit my code...

3 comments:

  1. Scott, could you please explain how this piece of code:

    var ex = new RegExp("\");
    aCode = aCode.replace(ex, strings[i]);

    works.
    If you substituted all the strings with "" (which is 2 quotes or an empty string?), then how are you looking for the position, where you want to put the initial string back?
    Thanks!

    ReplyDelete
  2. Ah, this is embarrassing. I first thought I copied and pasted it wrong, but it seems to be blogspot simply parsing it, in it's own little way. I'll edit the main post now.

    ReplyDelete