2012-02-24 06:16:46 +08:00
|
|
|
function ArrayParser(source, converter) {
|
|
|
|
this.source = source;
|
|
|
|
this.converter = converter;
|
|
|
|
this.pos = 0;
|
|
|
|
this.entries = [];
|
|
|
|
this.recorded = [];
|
|
|
|
this.dimension = 0;
|
|
|
|
if (!this.converter) {
|
|
|
|
this.converter = function(entry) {
|
|
|
|
return entry;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2013-03-07 00:26:40 +08:00
|
|
|
|
2012-02-24 06:16:46 +08:00
|
|
|
ArrayParser.prototype.eof = function() {
|
|
|
|
return this.pos >= this.source.length;
|
|
|
|
};
|
2013-03-07 00:26:40 +08:00
|
|
|
|
2012-02-24 06:16:46 +08:00
|
|
|
ArrayParser.prototype.nextChar = function() {
|
|
|
|
var c;
|
|
|
|
if ((c = this.source[this.pos++]) === "\\") {
|
|
|
|
return {
|
|
|
|
char: this.source[this.pos++],
|
|
|
|
escaped: true
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
char: c,
|
|
|
|
escaped: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2013-03-07 00:26:40 +08:00
|
|
|
|
2013-01-24 08:24:03 +08:00
|
|
|
ArrayParser.prototype.record = function(c) {
|
|
|
|
return this.recorded.push(c);
|
2012-02-24 06:16:46 +08:00
|
|
|
};
|
2013-03-07 00:26:40 +08:00
|
|
|
|
2012-02-24 16:05:08 +08:00
|
|
|
ArrayParser.prototype.newEntry = function(includeEmpty) {
|
2012-02-24 06:16:46 +08:00
|
|
|
var entry;
|
2012-02-24 16:05:08 +08:00
|
|
|
if (this.recorded.length > 0 || includeEmpty) {
|
2012-02-24 06:16:46 +08:00
|
|
|
entry = this.recorded.join("");
|
2012-03-06 07:52:00 +08:00
|
|
|
if (entry === "NULL" && !includeEmpty) {
|
2012-02-24 06:16:46 +08:00
|
|
|
entry = null;
|
|
|
|
}
|
|
|
|
if (entry !== null) {
|
|
|
|
entry = this.converter(entry);
|
|
|
|
}
|
|
|
|
this.entries.push(entry);
|
|
|
|
this.recorded = [];
|
|
|
|
}
|
|
|
|
};
|
2013-03-07 00:26:40 +08:00
|
|
|
|
2012-02-24 06:16:46 +08:00
|
|
|
ArrayParser.prototype.parse = function(nested) {
|
|
|
|
var c, p, quote;
|
2013-01-21 21:11:07 +08:00
|
|
|
if (nested === null) {
|
2012-02-24 06:16:46 +08:00
|
|
|
nested = false;
|
|
|
|
}
|
|
|
|
quote = false;
|
|
|
|
while (!this.eof()) {
|
|
|
|
c = this.nextChar();
|
|
|
|
if (c.char === "{" && !quote) {
|
|
|
|
this.dimension++;
|
|
|
|
if (this.dimension > 1) {
|
|
|
|
p = new ArrayParser(this.source.substr(this.pos - 1), this.converter);
|
|
|
|
this.entries.push(p.parse(true));
|
|
|
|
this.pos += p.pos - 2;
|
|
|
|
}
|
|
|
|
} else if (c.char === "}" && !quote) {
|
|
|
|
this.dimension--;
|
|
|
|
if (this.dimension === 0) {
|
|
|
|
this.newEntry();
|
|
|
|
if (nested) {
|
|
|
|
return this.entries;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (c.char === '"' && !c.escaped) {
|
|
|
|
if (quote) {
|
2012-02-24 16:05:08 +08:00
|
|
|
this.newEntry(true);
|
2012-02-24 06:16:46 +08:00
|
|
|
}
|
|
|
|
quote = !quote;
|
|
|
|
} else if (c.char === ',' && !quote) {
|
|
|
|
this.newEntry();
|
|
|
|
} else {
|
|
|
|
this.record(c.char);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.dimension !== 0) {
|
|
|
|
throw "array dimension not balanced";
|
|
|
|
}
|
|
|
|
return this.entries;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
create: function(source, converter){
|
|
|
|
return new ArrayParser(source, converter);
|
|
|
|
}
|
2013-01-21 21:11:07 +08:00
|
|
|
};
|