parse.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. var at, // The index of the current character
  2. ch, // The current character
  3. escapee = {
  4. '"': '"',
  5. '\\': '\\',
  6. '/': '/',
  7. b: '\b',
  8. f: '\f',
  9. n: '\n',
  10. r: '\r',
  11. t: '\t'
  12. },
  13. text,
  14. error = function (m) {
  15. // Call error when something is wrong.
  16. throw {
  17. name: 'SyntaxError',
  18. message: m,
  19. at: at,
  20. text: text
  21. };
  22. },
  23. next = function (c) {
  24. // If a c parameter is provided, verify that it matches the current character.
  25. if (c && c !== ch) {
  26. error("Expected '" + c + "' instead of '" + ch + "'");
  27. }
  28. // Get the next character. When there are no more characters,
  29. // return the empty string.
  30. ch = text.charAt(at);
  31. at += 1;
  32. return ch;
  33. },
  34. number = function () {
  35. // Parse a number value.
  36. var number,
  37. string = '';
  38. if (ch === '-') {
  39. string = '-';
  40. next('-');
  41. }
  42. while (ch >= '0' && ch <= '9') {
  43. string += ch;
  44. next();
  45. }
  46. if (ch === '.') {
  47. string += '.';
  48. while (next() && ch >= '0' && ch <= '9') {
  49. string += ch;
  50. }
  51. }
  52. if (ch === 'e' || ch === 'E') {
  53. string += ch;
  54. next();
  55. if (ch === '-' || ch === '+') {
  56. string += ch;
  57. next();
  58. }
  59. while (ch >= '0' && ch <= '9') {
  60. string += ch;
  61. next();
  62. }
  63. }
  64. number = +string;
  65. if (!isFinite(number)) {
  66. error("Bad number");
  67. } else {
  68. return number;
  69. }
  70. },
  71. string = function () {
  72. // Parse a string value.
  73. var hex,
  74. i,
  75. string = '',
  76. uffff;
  77. // When parsing for string values, we must look for " and \ characters.
  78. if (ch === '"') {
  79. while (next()) {
  80. if (ch === '"') {
  81. next();
  82. return string;
  83. } else if (ch === '\\') {
  84. next();
  85. if (ch === 'u') {
  86. uffff = 0;
  87. for (i = 0; i < 4; i += 1) {
  88. hex = parseInt(next(), 16);
  89. if (!isFinite(hex)) {
  90. break;
  91. }
  92. uffff = uffff * 16 + hex;
  93. }
  94. string += String.fromCharCode(uffff);
  95. } else if (typeof escapee[ch] === 'string') {
  96. string += escapee[ch];
  97. } else {
  98. break;
  99. }
  100. } else {
  101. string += ch;
  102. }
  103. }
  104. }
  105. error("Bad string");
  106. },
  107. white = function () {
  108. // Skip whitespace.
  109. while (ch && ch <= ' ') {
  110. next();
  111. }
  112. },
  113. word = function () {
  114. // true, false, or null.
  115. switch (ch) {
  116. case 't':
  117. next('t');
  118. next('r');
  119. next('u');
  120. next('e');
  121. return true;
  122. case 'f':
  123. next('f');
  124. next('a');
  125. next('l');
  126. next('s');
  127. next('e');
  128. return false;
  129. case 'n':
  130. next('n');
  131. next('u');
  132. next('l');
  133. next('l');
  134. return null;
  135. }
  136. error("Unexpected '" + ch + "'");
  137. },
  138. value, // Place holder for the value function.
  139. array = function () {
  140. // Parse an array value.
  141. var array = [];
  142. if (ch === '[') {
  143. next('[');
  144. white();
  145. if (ch === ']') {
  146. next(']');
  147. return array; // empty array
  148. }
  149. while (ch) {
  150. array.push(value());
  151. white();
  152. if (ch === ']') {
  153. next(']');
  154. return array;
  155. }
  156. next(',');
  157. white();
  158. }
  159. }
  160. error("Bad array");
  161. },
  162. object = function () {
  163. // Parse an object value.
  164. var key,
  165. object = {};
  166. if (ch === '{') {
  167. next('{');
  168. white();
  169. if (ch === '}') {
  170. next('}');
  171. return object; // empty object
  172. }
  173. while (ch) {
  174. key = string();
  175. white();
  176. next(':');
  177. if (Object.hasOwnProperty.call(object, key)) {
  178. error('Duplicate key "' + key + '"');
  179. }
  180. object[key] = value();
  181. white();
  182. if (ch === '}') {
  183. next('}');
  184. return object;
  185. }
  186. next(',');
  187. white();
  188. }
  189. }
  190. error("Bad object");
  191. };
  192. value = function () {
  193. // Parse a JSON value. It could be an object, an array, a string, a number,
  194. // or a word.
  195. white();
  196. switch (ch) {
  197. case '{':
  198. return object();
  199. case '[':
  200. return array();
  201. case '"':
  202. return string();
  203. case '-':
  204. return number();
  205. default:
  206. return ch >= '0' && ch <= '9' ? number() : word();
  207. }
  208. };
  209. // Return the json_parse function. It will have access to all of the above
  210. // functions and variables.
  211. module.exports = function (source, reviver) {
  212. var result;
  213. text = source;
  214. at = 0;
  215. ch = ' ';
  216. result = value();
  217. white();
  218. if (ch) {
  219. error("Syntax error");
  220. }
  221. // If there is a reviver function, we recursively walk the new structure,
  222. // passing each name/value pair to the reviver function for possible
  223. // transformation, starting with a temporary root object that holds the result
  224. // in an empty key. If there is not a reviver function, we simply return the
  225. // result.
  226. return typeof reviver === 'function' ? (function walk(holder, key) {
  227. var k, v, value = holder[key];
  228. if (value && typeof value === 'object') {
  229. for (k in value) {
  230. if (Object.prototype.hasOwnProperty.call(value, k)) {
  231. v = walk(value, k);
  232. if (v !== undefined) {
  233. value[k] = v;
  234. } else {
  235. delete value[k];
  236. }
  237. }
  238. }
  239. }
  240. return reviver.call(holder, key, value);
  241. }({'': result}, '')) : result;
  242. };