diff --git a/parse.js b/parse.js index 38934aa..25dbf27 100644 --- a/parse.js +++ b/parse.js @@ -12,6 +12,7 @@ { // break the input down into "parts": either words, digit strings, single character operators, or whitespace let parts = []; + let index = 0; while (input.length > 0) { // match our possible sequences of more than one character (words, digits, or whitespace) @@ -20,14 +21,16 @@ (match = input.match(/^\s+/))) { const matched = match[0]; - parts.push(matched); + parts.push({index:index, part:matched}); input = input.substr(matched.length); + index += matched.length; } else { // everything else is a single character (punctuation) - parts.push(input[0]); + parts.push({index:index, part:input[0]}); input = input.substr(1); + index++; } } @@ -36,7 +39,7 @@ for (let i = 0; i < parts.length; i++) { const partsLeft = parts.length - i - 1; - let token = parts[i]; + let { token, index } = parts[i]; if (token.trim().length == 0) { @@ -51,7 +54,7 @@ { token += "." + parts[i + 2]; } - tokens.push({type:TokenType.NumberLit, token:token}); + tokens.push({type:TokenType.NumberLit, token:token, index:index}); } else if (token == '"') { @@ -99,17 +102,17 @@ throw "Unclosed string"; } - tokens.push({type:TokenType.StringLit, token:token}); + tokens.push({type:TokenType.StringLit, token:token, index:index}); } else if (token.match(/\w+/)) { // any keyword or identifier - tokens.push({type:TokenType.Word, token:token}); + tokens.push({type:TokenType.Word, token:token, index:index}); } else { // anything left is an operator - tokens.push({type:TokenType.Op, token:token}); + tokens.push({type:TokenType.Op, token:token, index:index}); } }