diff --git a/parse.js b/parse.js index 25dbf27..893afd1 100644 --- a/parse.js +++ b/parse.js @@ -12,6 +12,8 @@ { // break the input down into "parts": either words, digit strings, single character operators, or whitespace let parts = []; + + // track where we are in the original string let index = 0; while (input.length > 0) { @@ -21,14 +23,17 @@ (match = input.match(/^\s+/))) { const matched = match[0]; - parts.push({index:index, part:matched}); + parts.push({token:matched, index:index}); + + // advance the string input = input.substr(matched.length); index += matched.length; } else { // everything else is a single character (punctuation) - parts.push({index:index, part:input[0]}); + parts.push({token:input[0], index:index}); + input = input.substr(1); index++; } @@ -39,6 +44,7 @@ for (let i = 0; i < parts.length; i++) { const partsLeft = parts.length - i - 1; + let { token, index } = parts[i]; if (token.trim().length == 0) @@ -50,9 +56,9 @@ if (token.match(/\d+/)) { // try and get the decimal, if any - if (partsLeft > 2 && parts[i + 1] == "." && parts[i + 2].match(/\d+/)) + if (partsLeft > 2 && parts[i + 1].token == "." && parts[i + 2].token.match(/\d+/)) { - token += "." + parts[i + 2]; + token += "." + parts[i + 2].token; } tokens.push({type:TokenType.NumberLit, token:token, index:index}); } @@ -67,23 +73,23 @@ for (let j = i + 1; j < parts.length; j++) { // find an escape character - if (parts[j] == '\\') + if (parts[j].token == '\\') { if (j + 1 < parts.length) { // the escape character is the next character (all tokens are guaranteed length > 0) - const escaped = parts[j + 1][0]; + const escaped = parts[j + 1].token[0]; // remove the escaped character from the next token, being sure to remove the token // if it was only that 1 character - parts[j + 1] = parts[j + 1].substr(1); - if (parts[j + 1].length == 0) parts.splice(j + 1, 1); + parts[j + 1].token = parts[j + 1].token.substr(1); + if (parts[j + 1].token.length == 0) parts.splice(j + 1, 1); // for now, just put the escaped character in literally, we'll deal with escape sequences later token += escaped; } } - else if (parts[j] == '"') + else if (parts[j].token == '"') { // close the string i = j; @@ -93,7 +99,7 @@ else { // build up the string out of all tokens before the closing " - token += parts[j]; + token += parts[j].token; } }