var Parser = function(v) { /* var s = v; var i = 0; var END = -1; var QUOTATION = -2; this.nextChar = function() { if (i >= s.length) { return END; } var c = s.charAt(i); i++; if (c == '"') { return QUOTATION; } else if (c == '\\') { if (i >= s.length) { return END; } c = s.charAt(i); i++; return c; } return c; }; this.nextToken = function() { if (i >= s.length) { return null; } var b = []; var quotation = false; var c; while ((c = this.nextChar()) != END) { if (quotation) { if (c == QUOTATION) { quotation = false; } else { b.push(c); } } else { if (c == QUOTATION) { quotation = true; } else if (c == ' ') { break; } else { b.push(c); } } } return b.join(''); }; this.getTokens = function() { var list = []; var a; while ((a = this.nextToken()) != null) { list.push(a); } return list; }; */ }; Parser.prototype.setLine = function(line) { this.line = line; this.index = 0; this.originalIndex = this.index; }; Parser.prototype.isSpace = function(c) { return c == ' ' || c == '\t'; }; Parser.prototype.walkToNonSpace = function() { while (this.index < this.line.length && this.isSpace(this.line.charAt(this.index))) { this.index++; } }; Parser.prototype.nextToken = function() { this.walkToNonSpace(); if (this.index >= this.line.length) { return null; } var buffer = []; var quoted = false; while (this.index < this.line.length) { var c = this.line.charAt(this.index); this.index++; if (!quoted) { if (c == '"') { quoted = true; continue; } if (this.isSpace(c)) { break; } buffer.push(c); } else { if (c == '"') { quoted = false; continue; } buffer.push(c); } } return buffer.join(''); }; Parser.prototype.nextOriginalToken = function() { if (this.originalIndex >= this.line.length) { return null; } var buffer = []; var nonSpaceFound = false; var quoted = false; while (this.originalIndex < this.line.length) { var c = this.line.charAt(this.originalIndex); this.originalIndex++; if (!quoted) { if (c == '"') { quoted = true; nonSpaceFound = true; continue; } if (this.isSpace(c)) { if (nonSpaceFound) { this.originalIndex--; break; } } else { nonSpaceFound = true; } buffer.push(c); } else { if (c == '"') { quoted = false; continue; } buffer.push(c); } } return buffer.join(''); }; Parser.prototype.parse = function() { this.tokens = []; while (this.index < this.line.length) { var token = this.nextToken(); if (token != null) { this.tokens.push(token); } } this.originalTokens = []; while (this.originalIndex < this.line.length) { var originalToken = this.nextOriginalToken(); if (originalToken != null) { this.originalTokens.push(originalToken); } } };