1 var at, // The index of the current character
2 ch, // The current character
15 error = function (m) {
16 // Call error when something is wrong.
26 // If a c parameter is provided, verify that it matches the current character.
28 error("Expected '" + c + "' instead of '" + ch + "'");
31 // Get the next character. When there are no more characters,
32 // return the empty string.
39 number = function () {
40 // Parse a number value.
48 while (ch >= '0' && ch <= '9') {
54 while (next() && ch >= '0' && ch <= '9') {
58 if (ch === 'e' || ch === 'E') {
61 if (ch === '-' || ch === '+') {
65 while (ch >= '0' && ch <= '9') {
71 if (!isFinite(number)) {
78 string = function () {
79 // Parse a string value.
85 // When parsing for string values, we must look for " and \ characters.
91 } else if (ch === '\\') {
95 for (i = 0; i < 4; i += 1) {
96 hex = parseInt(next(), 16);
100 uffff = uffff * 16 + hex;
102 string += String.fromCharCode(uffff);
103 } else if (typeof escapee[ch] === 'string') {
104 string += escapee[ch];
116 white = function () {
120 while (ch && ch <= ' ') {
127 // true, false, or null.
150 error("Unexpected '" + ch + "'");
153 value, // Place holder for the value function.
155 array = function () {
157 // Parse an array value.
166 return array; // empty array
182 object = function () {
184 // Parse an object value.
194 return object; // empty object
200 if (Object.hasOwnProperty.call(object, key)) {
201 error('Duplicate key "' + key + '"');
203 object[key] = value();
216 value = function () {
218 // Parse a JSON value. It could be an object, an array, a string, a number,
232 return ch >= '0' && ch <= '9' ? number() : word();
236 // Return the json_parse function. It will have access to all of the above
237 // functions and variables.
239 module.exports = function (source, reviver) {
248 error("Syntax error");
251 // If there is a reviver function, we recursively walk the new structure,
252 // passing each name/value pair to the reviver function for possible
253 // transformation, starting with a temporary root object that holds the result
254 // in an empty key. If there is not a reviver function, we simply return the
257 return typeof reviver === 'function' ? (function walk(holder, key) {
258 var k, v, value = holder[key];
259 if (value && typeof value === 'object') {
261 if (Object.prototype.hasOwnProperty.call(value, k)) {
263 if (v !== undefined) {
271 return reviver.call(holder, key, value);
272 }({'': result}, '')) : result;