react / wstein / node_modules / browserify / node_modules / JSONStream / node_modules / jsonparse / jsonparse.js
80536 views/*global Buffer*/1// Named constants with unique integer values2var C = {};3// Tokens4var LEFT_BRACE = C.LEFT_BRACE = 0x1;5var RIGHT_BRACE = C.RIGHT_BRACE = 0x2;6var LEFT_BRACKET = C.LEFT_BRACKET = 0x3;7var RIGHT_BRACKET = C.RIGHT_BRACKET = 0x4;8var COLON = C.COLON = 0x5;9var COMMA = C.COMMA = 0x6;10var TRUE = C.TRUE = 0x7;11var FALSE = C.FALSE = 0x8;12var NULL = C.NULL = 0x9;13var STRING = C.STRING = 0xa;14var NUMBER = C.NUMBER = 0xb;15// Tokenizer States16var START = C.START = 0x11;17var TRUE1 = C.TRUE1 = 0x21;18var TRUE2 = C.TRUE2 = 0x22;19var TRUE3 = C.TRUE3 = 0x23;20var FALSE1 = C.FALSE1 = 0x31;21var FALSE2 = C.FALSE2 = 0x32;22var FALSE3 = C.FALSE3 = 0x33;23var FALSE4 = C.FALSE4 = 0x34;24var NULL1 = C.NULL1 = 0x41;25var NULL2 = C.NULL2 = 0x42;26var NULL3 = C.NULL3 = 0x43;27var NUMBER1 = C.NUMBER1 = 0x51;28var NUMBER2 = C.NUMBER2 = 0x52;29var NUMBER3 = C.NUMBER3 = 0x53;30var NUMBER4 = C.NUMBER4 = 0x54;31var NUMBER5 = C.NUMBER5 = 0x55;32var NUMBER6 = C.NUMBER6 = 0x56;33var NUMBER7 = C.NUMBER7 = 0x57;34var NUMBER8 = C.NUMBER8 = 0x58;35var STRING1 = C.STRING1 = 0x61;36var STRING2 = C.STRING2 = 0x62;37var STRING3 = C.STRING3 = 0x63;38var STRING4 = C.STRING4 = 0x64;39var STRING5 = C.STRING5 = 0x65;40var STRING6 = C.STRING6 = 0x66;41// Parser States42var VALUE = C.VALUE = 0x71;43var KEY = C.KEY = 0x72;44// Parser Modes45var OBJECT = C.OBJECT = 0x81;46var ARRAY = C.ARRAY = 0x82;4748// Slow code to string converter (only used when throwing syntax errors)49function toknam(code) {50var keys = Object.keys(C);51for (var i = 0, l = keys.length; i < l; i++) {52var key = keys[i];53if (C[key] === code) { return key; }54}55return code && ("0x" + code.toString(16));56}575859function Parser() {60this.tState = START;61this.value = undefined;6263this.string = undefined; // string data64this.unicode = undefined; // unicode escapes6566// For number parsing67this.negative = undefined;68this.magnatude = undefined;69this.position = undefined;70this.exponent = undefined;71this.negativeExponent = undefined;72this.numberLength = 0;7374this.key = undefined;75this.mode = undefined;76this.stack = [];77this.state = VALUE;78this.bytes_remaining = 0; // number of bytes remaining in multi byte utf8 char to read after split boundary79this.bytes_in_sequence = 0; // bytes in multi byte utf8 char to read80this.temp_buffs = { "2": new Buffer(2), "3": new Buffer(3), "4": new Buffer(4) }; // for rebuilding chars split before boundary is reached8182// Stream offset83this.offset = -1;84}85var proto = Parser.prototype;86proto.charError = function (buffer, i) {87this.onError(new Error("Unexpected " + JSON.stringify(String.fromCharCode(buffer[i])) + " at position " + i + " in state " + toknam(this.tState)));88};89proto.onError = function (err) { throw err; };90proto.write = function (buffer) {91if (typeof buffer === "string") buffer = new Buffer(buffer);92//process.stdout.write("Input: ");93//console.dir(buffer.toString());94var n;95for (var i = 0, l = buffer.length; i < l; i++) {96if (this.tState === START){97n = buffer[i];98this.offset++;99if(n === 0x7b){ this.onToken(LEFT_BRACE, "{"); // {100}else if(n === 0x7d){ this.onToken(RIGHT_BRACE, "}"); // }101}else if(n === 0x5b){ this.onToken(LEFT_BRACKET, "["); // [102}else if(n === 0x5d){ this.onToken(RIGHT_BRACKET, "]"); // ]103}else if(n === 0x3a){ this.onToken(COLON, ":"); // :104}else if(n === 0x2c){ this.onToken(COMMA, ","); // ,105}else if(n === 0x74){ this.tState = TRUE1; // t106}else if(n === 0x66){ this.tState = FALSE1; // f107}else if(n === 0x6e){ this.tState = NULL1; // n108}else if(n === 0x22){ this.string = ""; this.tState = STRING1; // "109}else if(n === 0x2d){ this.negative = true; this.tState = NUMBER1; // -110}else if(n === 0x30){ this.magnatude = 0; this.tState = NUMBER2; // 0111}else{112if (n > 0x30 && n < 0x40) { // 1-9113this.magnatude = n - 0x30; this.tState = NUMBER3;114} else if (n === 0x20 || n === 0x09 || n === 0x0a || n === 0x0d) {115// whitespace116} else { this.charError(buffer, i); }117}118}else if (this.tState === STRING1){ // After open quote119n = buffer[i]; // get current byte from buffer120// check for carry over of a multi byte char split between data chunks121// & fill temp buffer it with start of this data chunk up to the boundary limit set in the last iteration122if (this.bytes_remaining > 0) {123for (var j = 0; j < this.bytes_remaining; j++) {124this.temp_buffs[this.bytes_in_sequence][this.bytes_in_sequence - this.bytes_remaining + j] = buffer[j];125}126this.string += this.temp_buffs[this.bytes_in_sequence].toString();127this.bytes_in_sequence = this.bytes_remaining = 0;128i = i + j - 1;129} else if (this.bytes_remaining === 0 && n >= 128) { // else if no remainder bytes carried over, parse multi byte (>=128) chars one at a time130if (n <= 193) {131this.onError(new Error("Invalid UTF-8 character at position " + i + " in state " + toknam(this.tState)));132return133}134if ((n >= 194) && (n <= 223)) this.bytes_in_sequence = 2;135if ((n >= 224) && (n <= 239)) this.bytes_in_sequence = 3;136if ((n >= 240) && (n <= 244)) this.bytes_in_sequence = 4;137if ((this.bytes_in_sequence + i) > buffer.length) { // if bytes needed to complete char fall outside buffer length, we have a boundary split138for (var k = 0; k <= (buffer.length - 1 - i); k++) {139this.temp_buffs[this.bytes_in_sequence][k] = buffer[i + k]; // fill temp buffer of correct size with bytes available in this chunk140}141this.bytes_remaining = (i + this.bytes_in_sequence) - buffer.length;142i = buffer.length - 1;143} else {144this.string += buffer.slice(i, (i + this.bytes_in_sequence)).toString();145i = i + this.bytes_in_sequence - 1;146}147} else if (n === 0x22) { this.tState = START; this.onToken(STRING, this.string); this.offset += Buffer.byteLength(this.string, 'utf8') + 1; this.string = undefined; }148else if (n === 0x5c) { this.tState = STRING2; }149else if (n >= 0x20) { this.string += String.fromCharCode(n); }150else { this.charError(buffer, i); }151}else if (this.tState === STRING2){ // After backslash152n = buffer[i];153if(n === 0x22){ this.string += "\""; this.tState = STRING1;154}else if(n === 0x5c){ this.string += "\\"; this.tState = STRING1;155}else if(n === 0x2f){ this.string += "\/"; this.tState = STRING1;156}else if(n === 0x62){ this.string += "\b"; this.tState = STRING1;157}else if(n === 0x66){ this.string += "\f"; this.tState = STRING1;158}else if(n === 0x6e){ this.string += "\n"; this.tState = STRING1;159}else if(n === 0x72){ this.string += "\r"; this.tState = STRING1;160}else if(n === 0x74){ this.string += "\t"; this.tState = STRING1;161}else if(n === 0x75){ this.unicode = ""; this.tState = STRING3;162}else{163this.charError(buffer, i);164}165}else if (this.tState === STRING3 || this.tState === STRING4 || this.tState === STRING5 || this.tState === STRING6){ // unicode hex codes166n = buffer[i];167// 0-9 A-F a-f168if ((n >= 0x30 && n < 0x40) || (n > 0x40 && n <= 0x46) || (n > 0x60 && n <= 0x66)) {169this.unicode += String.fromCharCode(n);170if (this.tState++ === STRING6) {171this.string += String.fromCharCode(parseInt(this.unicode, 16));172this.unicode = undefined;173this.tState = STRING1;174}175} else {176this.charError(buffer, i);177}178}else if (this.tState === NUMBER1){ // after minus179n = buffer[i];180this.numberLength++;181if (n === 0x30) { this.magnatude = 0; this.tState = NUMBER2; }182else if (n > 0x30 && n < 0x40) { this.magnatude = n - 0x30; this.tState = NUMBER3; }183else { this.charError(buffer, i); }184}else if (this.tState === NUMBER2){ // * After initial zero185n = buffer[i];186this.numberLength++;187if(n === 0x2e){ // .188this.position = 0.1; this.tState = NUMBER4;189}else if(n === 0x65 || n === 0x45){ // e/E190this.exponent = 0; this.tState = NUMBER6;191}else{192this.tState = START;193this.onToken(NUMBER, 0);194this.offset += this.numberLength - 1;195this.numberLength = 0;196this.magnatude = undefined;197this.negative = undefined;198i--;199}200}else if (this.tState === NUMBER3){ // * After digit (before period)201n = buffer[i];202this.numberLength++;203if(n === 0x2e){ // .204this.position = 0.1; this.tState = NUMBER4;205}else if(n === 0x65 || n === 0x45){ // e/E206this.exponent = 0; this.tState = NUMBER6;207}else{208if (n >= 0x30 && n < 0x40) { this.magnatude = this.magnatude * 10 + n - 0x30; }209else {210this.tState = START;211if (this.negative) {212this.magnatude = -this.magnatude;213this.negative = undefined;214}215this.onToken(NUMBER, this.magnatude);216this.offset += this.numberLength - 1;217this.numberLength = 0;218this.magnatude = undefined;219i--;220}221}222}else if (this.tState === NUMBER4){ // After period223n = buffer[i];224this.numberLength++;225if (n >= 0x30 && n < 0x40) { // 0-9226this.magnatude += this.position * (n - 0x30);227this.position /= 10;228this.tState = NUMBER5;229} else { this.charError(buffer, i); }230}else if (this.tState === NUMBER5){ // * After digit (after period)231n = buffer[i];232this.numberLength++;233if (n >= 0x30 && n < 0x40) { // 0-9234this.magnatude += this.position * (n - 0x30);235this.position /= 10;236}237else if (n === 0x65 || n === 0x45) { this.exponent = 0; this.tState = NUMBER6; } // E/e238else {239this.tState = START;240if (this.negative) {241this.magnatude = -this.magnatude;242this.negative = undefined;243}244this.onToken(NUMBER, this.negative ? -this.magnatude : this.magnatude);245this.offset += this.numberLength - 1;246this.numberLength = 0;247this.magnatude = undefined;248this.position = undefined;249i--;250}251}else if (this.tState === NUMBER6){ // After E252n = buffer[i];253this.numberLength++;254if (n === 0x2b || n === 0x2d) { // +/-255if (n === 0x2d) { this.negativeExponent = true; }256this.tState = NUMBER7;257}258else if (n >= 0x30 && n < 0x40) {259this.exponent = this.exponent * 10 + (n - 0x30);260this.tState = NUMBER8;261}262else { this.charError(buffer, i); }263}else if (this.tState === NUMBER7){ // After +/-264n = buffer[i];265this.numberLength++;266if (n >= 0x30 && n < 0x40) { // 0-9267this.exponent = this.exponent * 10 + (n - 0x30);268this.tState = NUMBER8;269}270else { this.charError(buffer, i); }271}else if (this.tState === NUMBER8){ // * After digit (after +/-)272n = buffer[i];273this.numberLength++;274if (n >= 0x30 && n < 0x40) { // 0-9275this.exponent = this.exponent * 10 + (n - 0x30);276}277else {278if (this.negativeExponent) {279this.exponent = -this.exponent;280this.negativeExponent = undefined;281}282this.magnatude *= Math.pow(10, this.exponent);283this.exponent = undefined;284if (this.negative) {285this.magnatude = -this.magnatude;286this.negative = undefined;287}288this.tState = START;289this.onToken(NUMBER, this.magnatude);290this.offset += this.numberLength - 1;291this.numberLength = 0;292this.magnatude = undefined;293i--;294}295}else if (this.tState === TRUE1){ // r296if (buffer[i] === 0x72) { this.tState = TRUE2; }297else { this.charError(buffer, i); }298}else if (this.tState === TRUE2){ // u299if (buffer[i] === 0x75) { this.tState = TRUE3; }300else { this.charError(buffer, i); }301}else if (this.tState === TRUE3){ // e302if (buffer[i] === 0x65) { this.tState = START; this.onToken(TRUE, true); this.offset+= 3; }303else { this.charError(buffer, i); }304}else if (this.tState === FALSE1){ // a305if (buffer[i] === 0x61) { this.tState = FALSE2; }306else { this.charError(buffer, i); }307}else if (this.tState === FALSE2){ // l308if (buffer[i] === 0x6c) { this.tState = FALSE3; }309else { this.charError(buffer, i); }310}else if (this.tState === FALSE3){ // s311if (buffer[i] === 0x73) { this.tState = FALSE4; }312else { this.charError(buffer, i); }313}else if (this.tState === FALSE4){ // e314if (buffer[i] === 0x65) { this.tState = START; this.onToken(FALSE, false); this.offset+= 4; }315else { this.charError(buffer, i); }316}else if (this.tState === NULL1){ // u317if (buffer[i] === 0x75) { this.tState = NULL2; }318else { this.charError(buffer, i); }319}else if (this.tState === NULL2){ // l320if (buffer[i] === 0x6c) { this.tState = NULL3; }321else { this.charError(buffer, i); }322}else if (this.tState === NULL3){ // l323if (buffer[i] === 0x6c) { this.tState = START; this.onToken(NULL, null); this.offset += 3; }324else { this.charError(buffer, i); }325}326}327};328proto.onToken = function (token, value) {329// Override this to get events330};331332proto.parseError = function (token, value) {333this.onError(new Error("Unexpected " + toknam(token) + (value ? ("(" + JSON.stringify(value) + ")") : "") + " in state " + toknam(this.state)));334};335proto.push = function () {336this.stack.push({value: this.value, key: this.key, mode: this.mode});337};338proto.pop = function () {339var value = this.value;340var parent = this.stack.pop();341this.value = parent.value;342this.key = parent.key;343this.mode = parent.mode;344this.emit(value);345if (!this.mode) { this.state = VALUE; }346};347proto.emit = function (value) {348if (this.mode) { this.state = COMMA; }349this.onValue(value);350};351proto.onValue = function (value) {352// Override me353};354proto.onToken = function (token, value) {355//console.log("OnToken: state=%s token=%s %s", toknam(this.state), toknam(token), value?JSON.stringify(value):"");356if(this.state === VALUE){357if(token === STRING || token === NUMBER || token === TRUE || token === FALSE || token === NULL){358if (this.value) {359this.value[this.key] = value;360}361this.emit(value);362}else if(token === LEFT_BRACE){363this.push();364if (this.value) {365this.value = this.value[this.key] = {};366} else {367this.value = {};368}369this.key = undefined;370this.state = KEY;371this.mode = OBJECT;372}else if(token === LEFT_BRACKET){373this.push();374if (this.value) {375this.value = this.value[this.key] = [];376} else {377this.value = [];378}379this.key = 0;380this.mode = ARRAY;381this.state = VALUE;382}else if(token === RIGHT_BRACE){383if (this.mode === OBJECT) {384this.pop();385} else {386this.parseError(token, value);387}388}else if(token === RIGHT_BRACKET){389if (this.mode === ARRAY) {390this.pop();391} else {392this.parseError(token, value);393}394}else{395this.parseError(token, value);396}397}else if(this.state === KEY){398if (token === STRING) {399this.key = value;400this.state = COLON;401} else if (token === RIGHT_BRACE) {402this.pop();403} else {404this.parseError(token, value);405}406}else if(this.state === COLON){407if (token === COLON) { this.state = VALUE; }408else { this.parseError(token, value); }409}else if(this.state === COMMA){410if (token === COMMA) {411if (this.mode === ARRAY) { this.key++; this.state = VALUE; }412else if (this.mode === OBJECT) { this.state = KEY; }413414} else if (token === RIGHT_BRACKET && this.mode === ARRAY || token === RIGHT_BRACE && this.mode === OBJECT) {415this.pop();416} else {417this.parseError(token, value);418}419}else{420this.parseError(token, value);421}422};423424Parser.C = C;425426module.exports = Parser;427428429