react / wstein / node_modules / browserify / node_modules / shasum / node_modules / json-stable-stringify / node_modules / jsonify / lib / parse.js
80556 viewsvar at, // The index of the current character1ch, // The current character2escapee = {3'"': '"',4'\\': '\\',5'/': '/',6b: '\b',7f: '\f',8n: '\n',9r: '\r',10t: '\t'11},12text,1314error = function (m) {15// Call error when something is wrong.16throw {17name: 'SyntaxError',18message: m,19at: at,20text: text21};22},2324next = function (c) {25// If a c parameter is provided, verify that it matches the current character.26if (c && c !== ch) {27error("Expected '" + c + "' instead of '" + ch + "'");28}2930// Get the next character. When there are no more characters,31// return the empty string.3233ch = text.charAt(at);34at += 1;35return ch;36},3738number = function () {39// Parse a number value.40var number,41string = '';4243if (ch === '-') {44string = '-';45next('-');46}47while (ch >= '0' && ch <= '9') {48string += ch;49next();50}51if (ch === '.') {52string += '.';53while (next() && ch >= '0' && ch <= '9') {54string += ch;55}56}57if (ch === 'e' || ch === 'E') {58string += ch;59next();60if (ch === '-' || ch === '+') {61string += ch;62next();63}64while (ch >= '0' && ch <= '9') {65string += ch;66next();67}68}69number = +string;70if (!isFinite(number)) {71error("Bad number");72} else {73return number;74}75},7677string = function () {78// Parse a string value.79var hex,80i,81string = '',82uffff;8384// When parsing for string values, we must look for " and \ characters.85if (ch === '"') {86while (next()) {87if (ch === '"') {88next();89return string;90} else if (ch === '\\') {91next();92if (ch === 'u') {93uffff = 0;94for (i = 0; i < 4; i += 1) {95hex = parseInt(next(), 16);96if (!isFinite(hex)) {97break;98}99uffff = uffff * 16 + hex;100}101string += String.fromCharCode(uffff);102} else if (typeof escapee[ch] === 'string') {103string += escapee[ch];104} else {105break;106}107} else {108string += ch;109}110}111}112error("Bad string");113},114115white = function () {116117// Skip whitespace.118119while (ch && ch <= ' ') {120next();121}122},123124word = function () {125126// true, false, or null.127128switch (ch) {129case 't':130next('t');131next('r');132next('u');133next('e');134return true;135case 'f':136next('f');137next('a');138next('l');139next('s');140next('e');141return false;142case 'n':143next('n');144next('u');145next('l');146next('l');147return null;148}149error("Unexpected '" + ch + "'");150},151152value, // Place holder for the value function.153154array = function () {155156// Parse an array value.157158var array = [];159160if (ch === '[') {161next('[');162white();163if (ch === ']') {164next(']');165return array; // empty array166}167while (ch) {168array.push(value());169white();170if (ch === ']') {171next(']');172return array;173}174next(',');175white();176}177}178error("Bad array");179},180181object = function () {182183// Parse an object value.184185var key,186object = {};187188if (ch === '{') {189next('{');190white();191if (ch === '}') {192next('}');193return object; // empty object194}195while (ch) {196key = string();197white();198next(':');199if (Object.hasOwnProperty.call(object, key)) {200error('Duplicate key "' + key + '"');201}202object[key] = value();203white();204if (ch === '}') {205next('}');206return object;207}208next(',');209white();210}211}212error("Bad object");213};214215value = function () {216217// Parse a JSON value. It could be an object, an array, a string, a number,218// or a word.219220white();221switch (ch) {222case '{':223return object();224case '[':225return array();226case '"':227return string();228case '-':229return number();230default:231return ch >= '0' && ch <= '9' ? number() : word();232}233};234235// Return the json_parse function. It will have access to all of the above236// functions and variables.237238module.exports = function (source, reviver) {239var result;240241text = source;242at = 0;243ch = ' ';244result = value();245white();246if (ch) {247error("Syntax error");248}249250// If there is a reviver function, we recursively walk the new structure,251// passing each name/value pair to the reviver function for possible252// transformation, starting with a temporary root object that holds the result253// in an empty key. If there is not a reviver function, we simply return the254// result.255256return typeof reviver === 'function' ? (function walk(holder, key) {257var k, v, value = holder[key];258if (value && typeof value === 'object') {259for (k in value) {260if (Object.prototype.hasOwnProperty.call(value, k)) {261v = walk(value, k);262if (v !== undefined) {263value[k] = v;264} else {265delete value[k];266}267}268}269}270return reviver.call(holder, key, value);271}({'': result}, '')) : result;272};273274275