react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / JSONStream / node_modules / jsonparse / jsonparse.js
80738 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.NULL3 = 0x42;26var NULL3 = C.NULL2 = 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;7273this.key = undefined;74this.mode = undefined;75this.stack = [];76this.state = VALUE;77this.bytes_remaining = 0; // number of bytes remaining in multi byte utf8 char to read after split boundary78this.bytes_in_sequence = 0; // bytes in multi byte utf8 char to read79this.temp_buffs = { "2": new Buffer(2), "3": new Buffer(3), "4": new Buffer(4) }; // for rebuilding chars split before boundary is reached80}81var proto = Parser.prototype;82proto.charError = function (buffer, i) {83this.onError(new Error("Unexpected " + JSON.stringify(String.fromCharCode(buffer[i])) + " at position " + i + " in state " + toknam(this.tState)));84};85proto.onError = function (err) { throw err; };86proto.write = function (buffer) {87if (typeof buffer === "string") buffer = new Buffer(buffer);88//process.stdout.write("Input: ");89//console.dir(buffer.toString());90var n;91for (var i = 0, l = buffer.length; i < l; i++) {92if (this.tState === START){93n = buffer[i];94if(n === 0x7b){ this.onToken(LEFT_BRACE, "{"); // {95}else if(n === 0x7d){ this.onToken(RIGHT_BRACE, "}"); // }96}else if(n === 0x5b){ this.onToken(LEFT_BRACKET, "["); // [97}else if(n === 0x5d){ this.onToken(RIGHT_BRACKET, "]"); // ]98}else if(n === 0x3a){ this.onToken(COLON, ":"); // :99}else if(n === 0x2c){ this.onToken(COMMA, ","); // ,100}else if(n === 0x74){ this.tState = TRUE1; // t101}else if(n === 0x66){ this.tState = FALSE1; // f102}else if(n === 0x6e){ this.tState = NULL1; // n103}else if(n === 0x22){ this.string = ""; this.tState = STRING1; // "104}else if(n === 0x2d){ this.negative = true; this.tState = NUMBER1; // -105}else if(n === 0x30){ this.magnatude = 0; this.tState = NUMBER2; // 0106}else{107if (n > 0x30 && n < 0x40) { // 1-9108this.magnatude = n - 0x30; this.tState = NUMBER3;109} else if (n === 0x20 || n === 0x09 || n === 0x0a || n === 0x0d) {110// whitespace111} else { this.charError(buffer, i); }112}113}else if (this.tState === STRING1){ // After open quote114n = buffer[i]; // get current byte from buffer115// check for carry over of a multi byte char split between data chunks116// & fill temp buffer it with start of this data chunk up to the boundary limit set in the last iteration117if (this.bytes_remaining > 0) {118for (var j = 0; j < this.bytes_remaining; j++) {119this.temp_buffs[this.bytes_in_sequence][this.bytes_in_sequence - this.bytes_remaining + j] = buffer[j];120}121this.string += this.temp_buffs[this.bytes_in_sequence].toString();122this.bytes_in_sequence = this.bytes_remaining = 0;123i = i + j - 1;124} else if (this.bytes_remaining === 0 && n >= 128) { // else if no remainder bytes carried over, parse multi byte (>=128) chars one at a time125if ((n >= 194) && (n <= 223)) this.bytes_in_sequence = 2;126if ((n >= 224) && (n <= 239)) this.bytes_in_sequence = 3;127if ((n >= 240) && (n <= 244)) this.bytes_in_sequence = 4;128if ((this.bytes_in_sequence + i) > buffer.length) { // if bytes needed to complete char fall outside buffer length, we have a boundary split129for (var k = 0; k <= (buffer.length - 1 - i); k++) {130this.temp_buffs[this.bytes_in_sequence][k] = buffer[i + k]; // fill temp buffer of correct size with bytes available in this chunk131}132this.bytes_remaining = (i + this.bytes_in_sequence) - buffer.length;133i = buffer.length - 1;134} else {135this.string += buffer.slice(i, (i + this.bytes_in_sequence)).toString();136i = i + this.bytes_in_sequence - 1;137}138} else if (n === 0x22) { this.tState = START; this.onToken(STRING, this.string); this.string = undefined; }139else if (n === 0x5c) { this.tState = STRING2; }140else if (n >= 0x20) { this.string += String.fromCharCode(n); }141else { this.charError(buffer, i); }142}else if (this.tState === STRING2){ // After backslash143n = buffer[i];144if(n === 0x22){ this.string += "\""; this.tState = STRING1;145}else if(n === 0x5c){ this.string += "\\"; this.tState = STRING1;146}else if(n === 0x2f){ this.string += "\/"; this.tState = STRING1;147}else if(n === 0x62){ this.string += "\b"; this.tState = STRING1;148}else if(n === 0x66){ this.string += "\f"; this.tState = STRING1;149}else if(n === 0x6e){ this.string += "\n"; this.tState = STRING1;150}else if(n === 0x72){ this.string += "\r"; this.tState = STRING1;151}else if(n === 0x74){ this.string += "\t"; this.tState = STRING1;152}else if(n === 0x75){ this.unicode = ""; this.tState = STRING3;153}else{154this.charError(buffer, i);155}156}else if (this.tState === STRING3 || this.tState === STRING4 || this.tState === STRING5 || this.tState === STRING6){ // unicode hex codes157n = buffer[i];158// 0-9 A-F a-f159if ((n >= 0x30 && n < 0x40) || (n > 0x40 && n <= 0x46) || (n > 0x60 && n <= 0x66)) {160this.unicode += String.fromCharCode(n);161if (this.tState++ === STRING6) {162this.string += String.fromCharCode(parseInt(this.unicode, 16));163this.unicode = undefined;164this.tState = STRING1;165}166} else {167this.charError(buffer, i);168}169}else if (this.tState === NUMBER1){ // after minus170n = buffer[i];171if (n === 0x30) { this.magnatude = 0; this.tState = NUMBER2; }172else if (n > 0x30 && n < 0x40) { this.magnatude = n - 0x30; this.tState = NUMBER3; }173else { this.charError(buffer, i); }174}else if (this.tState === NUMBER2){ // * After initial zero175n = buffer[i];176if(n === 0x2e){ // .177this.position = 0.1; this.tState = NUMBER4;178}else if(n === 0x65 || n === 0x45){ // e/E179this.exponent = 0; this.tState = NUMBER6;180}else{181this.tState = START;182this.onToken(NUMBER, 0);183this.magnatude = undefined;184this.negative = undefined;185i--;186}187}else if (this.tState === NUMBER3){ // * After digit (before period)188n = buffer[i];189if(n === 0x2e){ // .190this.position = 0.1; this.tState = NUMBER4;191}else if(n === 0x65 || n === 0x45){ // e/E192this.exponent = 0; this.tState = NUMBER6;193}else{194if (n >= 0x30 && n < 0x40) { this.magnatude = this.magnatude * 10 + n - 0x30; }195else {196this.tState = START;197if (this.negative) {198this.magnatude = -this.magnatude;199this.negative = undefined;200}201this.onToken(NUMBER, this.magnatude);202this.magnatude = undefined;203i--;204}205}206}else if (this.tState === NUMBER4){ // After period207n = buffer[i];208if (n >= 0x30 && n < 0x40) { // 0-9209this.magnatude += this.position * (n - 0x30);210this.position /= 10;211this.tState = NUMBER5;212} else { this.charError(buffer, i); }213}else if (this.tState === NUMBER5){ // * After digit (after period)214n = buffer[i];215if (n >= 0x30 && n < 0x40) { // 0-9216this.magnatude += this.position * (n - 0x30);217this.position /= 10;218}219else if (n === 0x65 || n === 0x45) { this.exponent = 0; this.tState = NUMBER6; } // E/e220else {221this.tState = START;222if (this.negative) {223this.magnatude = -this.magnatude;224this.negative = undefined;225}226this.onToken(NUMBER, this.negative ? -this.magnatude : this.magnatude);227this.magnatude = undefined;228this.position = undefined;229i--;230}231}else if (this.tState === NUMBER6){ // After E232n = buffer[i];233if (n === 0x2b || n === 0x2d) { // +/-234if (n === 0x2d) { this.negativeExponent = true; }235this.tState = NUMBER7;236}237else if (n >= 0x30 && n < 0x40) {238this.exponent = this.exponent * 10 + (n - 0x30);239this.tState = NUMBER8;240}241else { this.charError(buffer, i); }242}else if (this.tState === NUMBER7){ // After +/-243n = buffer[i];244if (n >= 0x30 && n < 0x40) { // 0-9245this.exponent = this.exponent * 10 + (n - 0x30);246this.tState = NUMBER8;247}248else { this.charError(buffer, i); }249}else if (this.tState === NUMBER8){ // * After digit (after +/-)250n = buffer[i];251if (n >= 0x30 && n < 0x40) { // 0-9252this.exponent = this.exponent * 10 + (n - 0x30);253}254else {255if (this.negativeExponent) {256this.exponent = -this.exponent;257this.negativeExponent = undefined;258}259this.magnatude *= Math.pow(10, this.exponent);260this.exponent = undefined;261if (this.negative) {262this.magnatude = -this.magnatude;263this.negative = undefined;264}265this.tState = START;266this.onToken(NUMBER, this.magnatude);267this.magnatude = undefined;268i--;269}270}else if (this.tState === TRUE1){ // r271if (buffer[i] === 0x72) { this.tState = TRUE2; }272else { this.charError(buffer, i); }273}else if (this.tState === TRUE2){ // u274if (buffer[i] === 0x75) { this.tState = TRUE3; }275else { this.charError(buffer, i); }276}else if (this.tState === TRUE3){ // e277if (buffer[i] === 0x65) { this.tState = START; this.onToken(TRUE, true); }278else { this.charError(buffer, i); }279}else if (this.tState === FALSE1){ // a280if (buffer[i] === 0x61) { this.tState = FALSE2; }281else { this.charError(buffer, i); }282}else if (this.tState === FALSE2){ // l283if (buffer[i] === 0x6c) { this.tState = FALSE3; }284else { this.charError(buffer, i); }285}else if (this.tState === FALSE3){ // s286if (buffer[i] === 0x73) { this.tState = FALSE4; }287else { this.charError(buffer, i); }288}else if (this.tState === FALSE4){ // e289if (buffer[i] === 0x65) { this.tState = START; this.onToken(FALSE, false); }290else { this.charError(buffer, i); }291}else if (this.tState === NULL1){ // u292if (buffer[i] === 0x75) { this.tState = NULL2; }293else { this.charError(buffer, i); }294}else if (this.tState === NULL2){ // l295if (buffer[i] === 0x6c) { this.tState = NULL3; }296else { this.charError(buffer, i); }297}else if (this.tState === NULL3){ // l298if (buffer[i] === 0x6c) { this.tState = START; this.onToken(NULL, null); }299else { this.charError(buffer, i); }300}301}302};303proto.onToken = function (token, value) {304// Override this to get events305};306307proto.parseError = function (token, value) {308this.onError(new Error("Unexpected " + toknam(token) + (value ? ("(" + JSON.stringify(value) + ")") : "") + " in state " + toknam(this.state)));309};310proto.onError = function (err) { throw err; };311proto.push = function () {312this.stack.push({value: this.value, key: this.key, mode: this.mode});313};314proto.pop = function () {315var value = this.value;316var parent = this.stack.pop();317this.value = parent.value;318this.key = parent.key;319this.mode = parent.mode;320this.emit(value);321if (!this.mode) { this.state = VALUE; }322};323proto.emit = function (value) {324if (this.mode) { this.state = COMMA; }325this.onValue(value);326};327proto.onValue = function (value) {328// Override me329};330proto.onToken = function (token, value) {331//console.log("OnToken: state=%s token=%s %s", toknam(this.state), toknam(token), value?JSON.stringify(value):"");332if(this.state === VALUE){333if(token === STRING || token === NUMBER || token === TRUE || token === FALSE || token === NULL){334if (this.value) {335this.value[this.key] = value;336}337this.emit(value);338}else if(token === LEFT_BRACE){339this.push();340if (this.value) {341this.value = this.value[this.key] = {};342} else {343this.value = {};344}345this.key = undefined;346this.state = KEY;347this.mode = OBJECT;348}else if(token === LEFT_BRACKET){349this.push();350if (this.value) {351this.value = this.value[this.key] = [];352} else {353this.value = [];354}355this.key = 0;356this.mode = ARRAY;357this.state = VALUE;358}else if(token === RIGHT_BRACE){359if (this.mode === OBJECT) {360this.pop();361} else {362this.parseError(token, value);363}364}else if(token === RIGHT_BRACKET){365if (this.mode === ARRAY) {366this.pop();367} else {368this.parseError(token, value);369}370}else{371this.parseError(token, value);372}373}else if(this.state === KEY){374if (token === STRING) {375this.key = value;376this.state = COLON;377} else if (token === RIGHT_BRACE) {378this.pop();379} else {380this.parseError(token, value);381}382}else if(this.state === COLON){383if (token === COLON) { this.state = VALUE; }384else { this.parseError(token, value); }385}else if(this.state === COMMA){386if (token === COMMA) {387if (this.mode === ARRAY) { this.key++; this.state = VALUE; }388else if (this.mode === OBJECT) { this.state = KEY; }389390} else if (token === RIGHT_BRACKET && this.mode === ARRAY || token === RIGHT_BRACE && this.mode === OBJECT) {391this.pop();392} else {393this.parseError(token, value);394}395}else{396this.parseError(token, value);397}398};399400module.exports = Parser;401402403