Parser.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. 'use strict';
  2. const Utf8Stream = require('./utils/Utf8Stream');
  3. const patterns = {
  4. value1: /^(?:[\"\{\[\]\-\d]|true\b|false\b|null\b|\s{1,256})/,
  5. string: /^(?:[^\x00-\x1f\"\\]{1,256}|\\[bfnrt\"\\\/]|\\u[\da-fA-F]{4}|\")/,
  6. key1: /^(?:[\"\}]|\s{1,256})/,
  7. colon: /^(?:\:|\s{1,256})/,
  8. comma: /^(?:[\,\]\}]|\s{1,256})/,
  9. ws: /^\s{1,256}/,
  10. numberStart: /^\d/,
  11. numberDigit: /^\d{0,256}/,
  12. numberFraction: /^[\.eE]/,
  13. numberExponent: /^[eE]/,
  14. numberExpSign: /^[-+]/
  15. };
  16. const MAX_PATTERN_SIZE = 16;
  17. let noSticky = true;
  18. try {
  19. new RegExp('.', 'y');
  20. noSticky = false;
  21. } catch (e) {
  22. // suppress
  23. }
  24. !noSticky &&
  25. Object.keys(patterns).forEach(key => {
  26. let src = patterns[key].source.slice(1); // lop off ^
  27. if (src.slice(0, 3) === '(?:' && src.slice(-1) === ')') {
  28. src = src.slice(3, -1);
  29. }
  30. patterns[key] = new RegExp(src, 'y');
  31. });
  32. patterns.numberFracStart = patterns.numberExpStart = patterns.numberStart;
  33. patterns.numberFracDigit = patterns.numberExpDigit = patterns.numberDigit;
  34. const values = {true: true, false: false, null: null},
  35. expected = {object: 'objectStop', array: 'arrayStop', '': 'done'};
  36. // long hexadecimal codes: \uXXXX
  37. const fromHex = s => String.fromCharCode(parseInt(s.slice(2), 16));
  38. // short codes: \b \f \n \r \t \" \\ \/
  39. const codes = {b: '\b', f: '\f', n: '\n', r: '\r', t: '\t', '"': '"', '\\': '\\', '/': '/'};
  40. class Parser extends Utf8Stream {
  41. static make(options) {
  42. return new Parser(options);
  43. }
  44. constructor(options) {
  45. super(Object.assign({}, options, {readableObjectMode: true}));
  46. this._packKeys = this._packStrings = this._packNumbers = this._streamKeys = this._streamStrings = this._streamNumbers = true;
  47. if (options) {
  48. 'packValues' in options && (this._packKeys = this._packStrings = this._packNumbers = options.packValues);
  49. 'packKeys' in options && (this._packKeys = options.packKeys);
  50. 'packStrings' in options && (this._packStrings = options.packStrings);
  51. 'packNumbers' in options && (this._packNumbers = options.packNumbers);
  52. 'streamValues' in options && (this._streamKeys = this._streamStrings = this._streamNumbers = options.streamValues);
  53. 'streamKeys' in options && (this._streamKeys = options.streamKeys);
  54. 'streamStrings' in options && (this._streamStrings = options.streamStrings);
  55. 'streamNumbers' in options && (this._streamNumbers = options.streamNumbers);
  56. this._jsonStreaming = options.jsonStreaming;
  57. }
  58. !this._packKeys && (this._streamKeys = true);
  59. !this._packStrings && (this._streamStrings = true);
  60. !this._packNumbers && (this._streamNumbers = true);
  61. this._done = false;
  62. this._expect = this._jsonStreaming ? 'done' : 'value';
  63. this._stack = [];
  64. this._parent = '';
  65. this._open_number = false;
  66. this._accumulator = '';
  67. }
  68. _flush(callback) {
  69. this._done = true;
  70. super._flush(error => {
  71. if (error) return callback(error);
  72. if (this._open_number) {
  73. if (this._streamNumbers) {
  74. this.push({name: 'endNumber'});
  75. }
  76. this._open_number = false;
  77. if (this._packNumbers) {
  78. this.push({name: 'numberValue', value: this._accumulator});
  79. this._accumulator = '';
  80. }
  81. }
  82. callback(null);
  83. });
  84. }
  85. _processBuffer(callback) {
  86. let match,
  87. value,
  88. index = 0;
  89. main: for (;;) {
  90. switch (this._expect) {
  91. case 'value1':
  92. case 'value':
  93. patterns.value1.lastIndex = index;
  94. match = patterns.value1.exec(this._buffer);
  95. if (!match) {
  96. if (this._done || index + MAX_PATTERN_SIZE < this._buffer.length) {
  97. if (index < this._buffer.length) return callback(new Error('Parser cannot parse input: expected a value'));
  98. return callback(new Error('Parser has expected a value'));
  99. }
  100. break main; // wait for more input
  101. }
  102. value = match[0];
  103. switch (value) {
  104. case '"':
  105. this._streamStrings && this.push({name: 'startString'});
  106. this._expect = 'string';
  107. break;
  108. case '{':
  109. this.push({name: 'startObject'});
  110. this._stack.push(this._parent);
  111. this._parent = 'object';
  112. this._expect = 'key1';
  113. break;
  114. case '[':
  115. this.push({name: 'startArray'});
  116. this._stack.push(this._parent);
  117. this._parent = 'array';
  118. this._expect = 'value1';
  119. break;
  120. case ']':
  121. if (this._expect !== 'value1') return callback(new Error("Parser cannot parse input: unexpected token ']'"));
  122. if (this._open_number) {
  123. this._streamNumbers && this.push({name: 'endNumber'});
  124. this._open_number = false;
  125. if (this._packNumbers) {
  126. this.push({name: 'numberValue', value: this._accumulator});
  127. this._accumulator = '';
  128. }
  129. }
  130. this.push({name: 'endArray'});
  131. this._parent = this._stack.pop();
  132. this._expect = expected[this._parent];
  133. break;
  134. case '-':
  135. this._open_number = true;
  136. if (this._streamNumbers) {
  137. this.push({name: 'startNumber'});
  138. this.push({name: 'numberChunk', value: '-'});
  139. }
  140. this._packNumbers && (this._accumulator = '-');
  141. this._expect = 'numberStart';
  142. break;
  143. case '0':
  144. this._open_number = true;
  145. if (this._streamNumbers) {
  146. this.push({name: 'startNumber'});
  147. this.push({name: 'numberChunk', value: '0'});
  148. }
  149. this._packNumbers && (this._accumulator = '0');
  150. this._expect = 'numberFraction';
  151. break;
  152. case '1':
  153. case '2':
  154. case '3':
  155. case '4':
  156. case '5':
  157. case '6':
  158. case '7':
  159. case '8':
  160. case '9':
  161. this._open_number = true;
  162. if (this._streamNumbers) {
  163. this.push({name: 'startNumber'});
  164. this.push({name: 'numberChunk', value: value});
  165. }
  166. this._packNumbers && (this._accumulator = value);
  167. this._expect = 'numberDigit';
  168. break;
  169. case 'true':
  170. case 'false':
  171. case 'null':
  172. if (this._buffer.length - index === value.length && !this._done) break main; // wait for more input
  173. this.push({name: value + 'Value', value: values[value]});
  174. this._expect = expected[this._parent];
  175. break;
  176. // default: // ws
  177. }
  178. if (noSticky) {
  179. this._buffer = this._buffer.slice(value.length);
  180. } else {
  181. index += value.length;
  182. }
  183. break;
  184. case 'keyVal':
  185. case 'string':
  186. patterns.string.lastIndex = index;
  187. match = patterns.string.exec(this._buffer);
  188. if (!match) {
  189. if (index < this._buffer.length && (this._done || this._buffer.length - index >= 6))
  190. return callback(new Error('Parser cannot parse input: escaped characters'));
  191. if (this._done) return callback(new Error('Parser has expected a string value'));
  192. break main; // wait for more input
  193. }
  194. value = match[0];
  195. if (value === '"') {
  196. if (this._expect === 'keyVal') {
  197. this._streamKeys && this.push({name: 'endKey'});
  198. if (this._packKeys) {
  199. this.push({name: 'keyValue', value: this._accumulator});
  200. this._accumulator = '';
  201. }
  202. this._expect = 'colon';
  203. } else {
  204. this._streamStrings && this.push({name: 'endString'});
  205. if (this._packStrings) {
  206. this.push({name: 'stringValue', value: this._accumulator});
  207. this._accumulator = '';
  208. }
  209. this._expect = expected[this._parent];
  210. }
  211. } else if (value.length > 1 && value.charAt(0) === '\\') {
  212. const t = value.length == 2 ? codes[value.charAt(1)] : fromHex(value);
  213. if (this._expect === 'keyVal' ? this._streamKeys : this._streamStrings) {
  214. this.push({name: 'stringChunk', value: t});
  215. }
  216. if (this._expect === 'keyVal' ? this._packKeys : this._packStrings) {
  217. this._accumulator += t;
  218. }
  219. } else {
  220. if (this._expect === 'keyVal' ? this._streamKeys : this._streamStrings) {
  221. this.push({name: 'stringChunk', value: value});
  222. }
  223. if (this._expect === 'keyVal' ? this._packKeys : this._packStrings) {
  224. this._accumulator += value;
  225. }
  226. }
  227. if (noSticky) {
  228. this._buffer = this._buffer.slice(value.length);
  229. } else {
  230. index += value.length;
  231. }
  232. break;
  233. case 'key1':
  234. case 'key':
  235. patterns.key1.lastIndex = index;
  236. match = patterns.key1.exec(this._buffer);
  237. if (!match) {
  238. if (index < this._buffer.length || this._done) return callback(new Error('Parser cannot parse input: expected an object key'));
  239. break main; // wait for more input
  240. }
  241. value = match[0];
  242. if (value === '"') {
  243. this._streamKeys && this.push({name: 'startKey'});
  244. this._expect = 'keyVal';
  245. } else if (value === '}') {
  246. if (this._expect !== 'key1') return callback(new Error("Parser cannot parse input: unexpected token '}'"));
  247. this.push({name: 'endObject'});
  248. this._parent = this._stack.pop();
  249. this._expect = expected[this._parent];
  250. }
  251. if (noSticky) {
  252. this._buffer = this._buffer.slice(value.length);
  253. } else {
  254. index += value.length;
  255. }
  256. break;
  257. case 'colon':
  258. patterns.colon.lastIndex = index;
  259. match = patterns.colon.exec(this._buffer);
  260. if (!match) {
  261. if (index < this._buffer.length || this._done) return callback(new Error("Parser cannot parse input: expected ':'"));
  262. break main; // wait for more input
  263. }
  264. value = match[0];
  265. value === ':' && (this._expect = 'value');
  266. if (noSticky) {
  267. this._buffer = this._buffer.slice(value.length);
  268. } else {
  269. index += value.length;
  270. }
  271. break;
  272. case 'arrayStop':
  273. case 'objectStop':
  274. patterns.comma.lastIndex = index;
  275. match = patterns.comma.exec(this._buffer);
  276. if (!match) {
  277. if (index < this._buffer.length || this._done) return callback(new Error("Parser cannot parse input: expected ','"));
  278. break main; // wait for more input
  279. }
  280. if (this._open_number) {
  281. this._streamNumbers && this.push({name: 'endNumber'});
  282. this._open_number = false;
  283. if (this._packNumbers) {
  284. this.push({name: 'numberValue', value: this._accumulator});
  285. this._accumulator = '';
  286. }
  287. }
  288. value = match[0];
  289. if (value === ',') {
  290. this._expect = this._expect === 'arrayStop' ? 'value' : 'key';
  291. } else if (value === '}' || value === ']') {
  292. if (value === '}' ? this._expect === 'arrayStop' : this._expect !== 'arrayStop') {
  293. return callback(new Error("Parser cannot parse input: expected '" + (this._expect === 'arrayStop' ? ']' : '}') + "'"));
  294. }
  295. this.push({name: value === '}' ? 'endObject' : 'endArray'});
  296. this._parent = this._stack.pop();
  297. this._expect = expected[this._parent];
  298. }
  299. if (noSticky) {
  300. this._buffer = this._buffer.slice(value.length);
  301. } else {
  302. index += value.length;
  303. }
  304. break;
  305. // number chunks
  306. case 'numberStart': // [0-9]
  307. patterns.numberStart.lastIndex = index;
  308. match = patterns.numberStart.exec(this._buffer);
  309. if (!match) {
  310. if (index < this._buffer.length || this._done) return callback(new Error('Parser cannot parse input: expected a starting digit'));
  311. break main; // wait for more input
  312. }
  313. value = match[0];
  314. this._streamNumbers && this.push({name: 'numberChunk', value: value});
  315. this._packNumbers && (this._accumulator += value);
  316. this._expect = value === '0' ? 'numberFraction' : 'numberDigit';
  317. if (noSticky) {
  318. this._buffer = this._buffer.slice(value.length);
  319. } else {
  320. index += value.length;
  321. }
  322. break;
  323. case 'numberDigit': // [0-9]*
  324. patterns.numberDigit.lastIndex = index;
  325. match = patterns.numberDigit.exec(this._buffer);
  326. if (!match) {
  327. if (index < this._buffer.length || this._done) return callback(new Error('Parser cannot parse input: expected a digit'));
  328. break main; // wait for more input
  329. }
  330. value = match[0];
  331. if (value) {
  332. this._streamNumbers && this.push({name: 'numberChunk', value: value});
  333. this._packNumbers && (this._accumulator += value);
  334. if (noSticky) {
  335. this._buffer = this._buffer.slice(value.length);
  336. } else {
  337. index += value.length;
  338. }
  339. } else {
  340. if (index < this._buffer.length) {
  341. this._expect = 'numberFraction';
  342. break;
  343. }
  344. if (this._done) {
  345. this._expect = expected[this._parent];
  346. break;
  347. }
  348. break main; // wait for more input
  349. }
  350. break;
  351. case 'numberFraction': // [\.eE]?
  352. patterns.numberFraction.lastIndex = index;
  353. match = patterns.numberFraction.exec(this._buffer);
  354. if (!match) {
  355. if (index < this._buffer.length || this._done) {
  356. this._expect = expected[this._parent];
  357. break;
  358. }
  359. break main; // wait for more input
  360. }
  361. value = match[0];
  362. this._streamNumbers && this.push({name: 'numberChunk', value: value});
  363. this._packNumbers && (this._accumulator += value);
  364. this._expect = value === '.' ? 'numberFracStart' : 'numberExpSign';
  365. if (noSticky) {
  366. this._buffer = this._buffer.slice(value.length);
  367. } else {
  368. index += value.length;
  369. }
  370. break;
  371. case 'numberFracStart': // [0-9]
  372. patterns.numberFracStart.lastIndex = index;
  373. match = patterns.numberFracStart.exec(this._buffer);
  374. if (!match) {
  375. if (index < this._buffer.length || this._done) return callback(new Error('Parser cannot parse input: expected a fractional part of a number'));
  376. break main; // wait for more input
  377. }
  378. value = match[0];
  379. this._streamNumbers && this.push({name: 'numberChunk', value: value});
  380. this._packNumbers && (this._accumulator += value);
  381. this._expect = 'numberFracDigit';
  382. if (noSticky) {
  383. this._buffer = this._buffer.slice(value.length);
  384. } else {
  385. index += value.length;
  386. }
  387. break;
  388. case 'numberFracDigit': // [0-9]*
  389. patterns.numberFracDigit.lastIndex = index;
  390. match = patterns.numberFracDigit.exec(this._buffer);
  391. value = match[0];
  392. if (value) {
  393. this._streamNumbers && this.push({name: 'numberChunk', value: value});
  394. this._packNumbers && (this._accumulator += value);
  395. if (noSticky) {
  396. this._buffer = this._buffer.slice(value.length);
  397. } else {
  398. index += value.length;
  399. }
  400. } else {
  401. if (index < this._buffer.length) {
  402. this._expect = 'numberExponent';
  403. break;
  404. }
  405. if (this._done) {
  406. this._expect = expected[this._parent];
  407. break;
  408. }
  409. break main; // wait for more input
  410. }
  411. break;
  412. case 'numberExponent': // [eE]?
  413. patterns.numberExponent.lastIndex = index;
  414. match = patterns.numberExponent.exec(this._buffer);
  415. if (!match) {
  416. if (index < this._buffer.length) {
  417. this._expect = expected[this._parent];
  418. break;
  419. }
  420. if (this._done) {
  421. this._expect = 'done';
  422. break;
  423. }
  424. break main; // wait for more input
  425. }
  426. value = match[0];
  427. this._streamNumbers && this.push({name: 'numberChunk', value: value});
  428. this._packNumbers && (this._accumulator += value);
  429. this._expect = 'numberExpSign';
  430. if (noSticky) {
  431. this._buffer = this._buffer.slice(value.length);
  432. } else {
  433. index += value.length;
  434. }
  435. break;
  436. case 'numberExpSign': // [-+]?
  437. patterns.numberExpSign.lastIndex = index;
  438. match = patterns.numberExpSign.exec(this._buffer);
  439. if (!match) {
  440. if (index < this._buffer.length) {
  441. this._expect = 'numberExpStart';
  442. break;
  443. }
  444. if (this._done) return callback(new Error('Parser has expected an exponent value of a number'));
  445. break main; // wait for more input
  446. }
  447. value = match[0];
  448. this._streamNumbers && this.push({name: 'numberChunk', value: value});
  449. this._packNumbers && (this._accumulator += value);
  450. this._expect = 'numberExpStart';
  451. if (noSticky) {
  452. this._buffer = this._buffer.slice(value.length);
  453. } else {
  454. index += value.length;
  455. }
  456. break;
  457. case 'numberExpStart': // [0-9]
  458. patterns.numberExpStart.lastIndex = index;
  459. match = patterns.numberExpStart.exec(this._buffer);
  460. if (!match) {
  461. if (index < this._buffer.length || this._done) return callback(new Error('Parser cannot parse input: expected an exponent part of a number'));
  462. break main; // wait for more input
  463. }
  464. value = match[0];
  465. this._streamNumbers && this.push({name: 'numberChunk', value: value});
  466. this._packNumbers && (this._accumulator += value);
  467. this._expect = 'numberExpDigit';
  468. if (noSticky) {
  469. this._buffer = this._buffer.slice(value.length);
  470. } else {
  471. index += value.length;
  472. }
  473. break;
  474. case 'numberExpDigit': // [0-9]*
  475. patterns.numberExpDigit.lastIndex = index;
  476. match = patterns.numberExpDigit.exec(this._buffer);
  477. value = match[0];
  478. if (value) {
  479. this._streamNumbers && this.push({name: 'numberChunk', value: value});
  480. this._packNumbers && (this._accumulator += value);
  481. if (noSticky) {
  482. this._buffer = this._buffer.slice(value.length);
  483. } else {
  484. index += value.length;
  485. }
  486. } else {
  487. if (index < this._buffer.length || this._done) {
  488. this._expect = expected[this._parent];
  489. break;
  490. }
  491. break main; // wait for more input
  492. }
  493. break;
  494. case 'done':
  495. patterns.ws.lastIndex = index;
  496. match = patterns.ws.exec(this._buffer);
  497. if (!match) {
  498. if (index < this._buffer.length) {
  499. if (this._jsonStreaming) {
  500. this._expect = 'value';
  501. break;
  502. }
  503. return callback(new Error('Parser cannot parse input: unexpected characters'));
  504. }
  505. break main; // wait for more input
  506. }
  507. value = match[0];
  508. if (this._open_number) {
  509. this._streamNumbers && this.push({name: 'endNumber'});
  510. this._open_number = false;
  511. if (this._packNumbers) {
  512. this.push({name: 'numberValue', value: this._accumulator});
  513. this._accumulator = '';
  514. }
  515. }
  516. if (noSticky) {
  517. this._buffer = this._buffer.slice(value.length);
  518. } else {
  519. index += value.length;
  520. }
  521. break;
  522. }
  523. }
  524. !noSticky && (this._buffer = this._buffer.slice(index));
  525. callback(null);
  526. }
  527. }
  528. Parser.parser = Parser.make;
  529. Parser.make.Constructor = Parser;
  530. module.exports = Parser;