jsonlint.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. 'use strict';
  2. // From: https://github.com/zaach/jsonlint
  3. // Vendored in Jest to avoid jsonlint's transitive dependencies.
  4. /* eslint-disable */
  5. var jsonlint = (function() {
  6. var parser = {
  7. trace: function trace() {},
  8. yy: {},
  9. symbols_: {
  10. error: 2,
  11. JSONString: 3,
  12. STRING: 4,
  13. JSONNumber: 5,
  14. NUMBER: 6,
  15. JSONNullLiteral: 7,
  16. NULL: 8,
  17. JSONBooleanLiteral: 9,
  18. TRUE: 10,
  19. FALSE: 11,
  20. JSONText: 12,
  21. JSONValue: 13,
  22. EOF: 14,
  23. JSONObject: 15,
  24. JSONArray: 16,
  25. '{': 17,
  26. '}': 18,
  27. JSONMemberList: 19,
  28. JSONMember: 20,
  29. ':': 21,
  30. ',': 22,
  31. '[': 23,
  32. ']': 24,
  33. JSONElementList: 25,
  34. $accept: 0,
  35. $end: 1
  36. },
  37. terminals_: {
  38. 2: 'error',
  39. 4: 'STRING',
  40. 6: 'NUMBER',
  41. 8: 'NULL',
  42. 10: 'TRUE',
  43. 11: 'FALSE',
  44. 14: 'EOF',
  45. 17: '{',
  46. 18: '}',
  47. 21: ':',
  48. 22: ',',
  49. 23: '[',
  50. 24: ']'
  51. },
  52. productions_: [
  53. 0,
  54. [3, 1],
  55. [5, 1],
  56. [7, 1],
  57. [9, 1],
  58. [9, 1],
  59. [12, 2],
  60. [13, 1],
  61. [13, 1],
  62. [13, 1],
  63. [13, 1],
  64. [13, 1],
  65. [13, 1],
  66. [15, 2],
  67. [15, 3],
  68. [20, 3],
  69. [19, 1],
  70. [19, 3],
  71. [16, 2],
  72. [16, 3],
  73. [25, 1],
  74. [25, 3]
  75. ],
  76. performAction: function anonymous(
  77. yytext,
  78. yyleng,
  79. yylineno,
  80. yy,
  81. yystate,
  82. $$,
  83. _$
  84. ) {
  85. var $0 = $$.length - 1;
  86. switch (yystate) {
  87. case 1:
  88. // replace escaped characters with actual character
  89. this.$ = yytext
  90. .replace(/\\(\\|")/g, '$' + '1')
  91. .replace(/\\n/g, '\n')
  92. .replace(/\\r/g, '\r')
  93. .replace(/\\t/g, '\t')
  94. .replace(/\\v/g, '\v')
  95. .replace(/\\f/g, '\f')
  96. .replace(/\\b/g, '\b');
  97. break;
  98. case 2:
  99. this.$ = Number(yytext);
  100. break;
  101. case 3:
  102. this.$ = null;
  103. break;
  104. case 4:
  105. this.$ = true;
  106. break;
  107. case 5:
  108. this.$ = false;
  109. break;
  110. case 6:
  111. return (this.$ = $$[$0 - 1]);
  112. break;
  113. case 13:
  114. this.$ = {};
  115. break;
  116. case 14:
  117. this.$ = $$[$0 - 1];
  118. break;
  119. case 15:
  120. this.$ = [$$[$0 - 2], $$[$0]];
  121. break;
  122. case 16:
  123. this.$ = {};
  124. this.$[$$[$0][0]] = $$[$0][1];
  125. break;
  126. case 17:
  127. this.$ = $$[$0 - 2];
  128. $$[$0 - 2][$$[$0][0]] = $$[$0][1];
  129. break;
  130. case 18:
  131. this.$ = [];
  132. break;
  133. case 19:
  134. this.$ = $$[$0 - 1];
  135. break;
  136. case 20:
  137. this.$ = [$$[$0]];
  138. break;
  139. case 21:
  140. this.$ = $$[$0 - 2];
  141. $$[$0 - 2].push($$[$0]);
  142. break;
  143. }
  144. },
  145. table: [
  146. {
  147. 3: 5,
  148. 4: [1, 12],
  149. 5: 6,
  150. 6: [1, 13],
  151. 7: 3,
  152. 8: [1, 9],
  153. 9: 4,
  154. 10: [1, 10],
  155. 11: [1, 11],
  156. 12: 1,
  157. 13: 2,
  158. 15: 7,
  159. 16: 8,
  160. 17: [1, 14],
  161. 23: [1, 15]
  162. },
  163. {1: [3]},
  164. {14: [1, 16]},
  165. {14: [2, 7], 18: [2, 7], 22: [2, 7], 24: [2, 7]},
  166. {14: [2, 8], 18: [2, 8], 22: [2, 8], 24: [2, 8]},
  167. {14: [2, 9], 18: [2, 9], 22: [2, 9], 24: [2, 9]},
  168. {14: [2, 10], 18: [2, 10], 22: [2, 10], 24: [2, 10]},
  169. {14: [2, 11], 18: [2, 11], 22: [2, 11], 24: [2, 11]},
  170. {14: [2, 12], 18: [2, 12], 22: [2, 12], 24: [2, 12]},
  171. {14: [2, 3], 18: [2, 3], 22: [2, 3], 24: [2, 3]},
  172. {14: [2, 4], 18: [2, 4], 22: [2, 4], 24: [2, 4]},
  173. {14: [2, 5], 18: [2, 5], 22: [2, 5], 24: [2, 5]},
  174. {14: [2, 1], 18: [2, 1], 21: [2, 1], 22: [2, 1], 24: [2, 1]},
  175. {14: [2, 2], 18: [2, 2], 22: [2, 2], 24: [2, 2]},
  176. {3: 20, 4: [1, 12], 18: [1, 17], 19: 18, 20: 19},
  177. {
  178. 3: 5,
  179. 4: [1, 12],
  180. 5: 6,
  181. 6: [1, 13],
  182. 7: 3,
  183. 8: [1, 9],
  184. 9: 4,
  185. 10: [1, 10],
  186. 11: [1, 11],
  187. 13: 23,
  188. 15: 7,
  189. 16: 8,
  190. 17: [1, 14],
  191. 23: [1, 15],
  192. 24: [1, 21],
  193. 25: 22
  194. },
  195. {1: [2, 6]},
  196. {14: [2, 13], 18: [2, 13], 22: [2, 13], 24: [2, 13]},
  197. {18: [1, 24], 22: [1, 25]},
  198. {18: [2, 16], 22: [2, 16]},
  199. {21: [1, 26]},
  200. {14: [2, 18], 18: [2, 18], 22: [2, 18], 24: [2, 18]},
  201. {22: [1, 28], 24: [1, 27]},
  202. {22: [2, 20], 24: [2, 20]},
  203. {14: [2, 14], 18: [2, 14], 22: [2, 14], 24: [2, 14]},
  204. {3: 20, 4: [1, 12], 20: 29},
  205. {
  206. 3: 5,
  207. 4: [1, 12],
  208. 5: 6,
  209. 6: [1, 13],
  210. 7: 3,
  211. 8: [1, 9],
  212. 9: 4,
  213. 10: [1, 10],
  214. 11: [1, 11],
  215. 13: 30,
  216. 15: 7,
  217. 16: 8,
  218. 17: [1, 14],
  219. 23: [1, 15]
  220. },
  221. {14: [2, 19], 18: [2, 19], 22: [2, 19], 24: [2, 19]},
  222. {
  223. 3: 5,
  224. 4: [1, 12],
  225. 5: 6,
  226. 6: [1, 13],
  227. 7: 3,
  228. 8: [1, 9],
  229. 9: 4,
  230. 10: [1, 10],
  231. 11: [1, 11],
  232. 13: 31,
  233. 15: 7,
  234. 16: 8,
  235. 17: [1, 14],
  236. 23: [1, 15]
  237. },
  238. {18: [2, 17], 22: [2, 17]},
  239. {18: [2, 15], 22: [2, 15]},
  240. {22: [2, 21], 24: [2, 21]}
  241. ],
  242. defaultActions: {16: [2, 6]},
  243. parseError: function parseError(str, hash) {
  244. throw new Error(str);
  245. },
  246. parse: function parse(input) {
  247. var self = this,
  248. stack = [0],
  249. vstack = [null],
  250. // semantic value stack
  251. lstack = [],
  252. // location stack
  253. table = this.table,
  254. yytext = '',
  255. yylineno = 0,
  256. yyleng = 0,
  257. recovering = 0,
  258. TERROR = 2,
  259. EOF = 1;
  260. //this.reductionCount = this.shiftCount = 0;
  261. this.lexer.setInput(input);
  262. this.lexer.yy = this.yy;
  263. this.yy.lexer = this.lexer;
  264. if (typeof this.lexer.yylloc == 'undefined') this.lexer.yylloc = {};
  265. var yyloc = this.lexer.yylloc;
  266. lstack.push(yyloc);
  267. if (typeof this.yy.parseError === 'function')
  268. this.parseError = this.yy.parseError;
  269. function popStack(n) {
  270. stack.length = stack.length - 2 * n;
  271. vstack.length = vstack.length - n;
  272. lstack.length = lstack.length - n;
  273. }
  274. function lex() {
  275. var token;
  276. token = self.lexer.lex() || 1; // $end = 1
  277. // if token isn't its numeric value, convert
  278. if (typeof token !== 'number') {
  279. token = self.symbols_[token] || token;
  280. }
  281. return token;
  282. }
  283. var symbol,
  284. preErrorSymbol,
  285. state,
  286. action,
  287. a,
  288. r,
  289. yyval = {},
  290. p,
  291. len,
  292. newState,
  293. expected;
  294. while (true) {
  295. // retrieve state number from top of stack
  296. state = stack[stack.length - 1];
  297. // use default actions if available
  298. if (this.defaultActions[state]) {
  299. action = this.defaultActions[state];
  300. } else {
  301. if (symbol == null) symbol = lex();
  302. // read action for current state and first input
  303. action = table[state] && table[state][symbol];
  304. }
  305. // handle parse error
  306. _handle_error: if (
  307. typeof action === 'undefined' ||
  308. !action.length ||
  309. !action[0]
  310. ) {
  311. if (!recovering) {
  312. // Report error
  313. expected = [];
  314. for (p in table[state])
  315. if (this.terminals_[p] && p > 2) {
  316. expected.push("'" + this.terminals_[p] + "'");
  317. }
  318. var errStr = '';
  319. if (this.lexer.showPosition) {
  320. errStr =
  321. 'Parse error on line ' +
  322. (yylineno + 1) +
  323. ':\n' +
  324. this.lexer.showPosition() +
  325. '\nExpecting ' +
  326. expected.join(', ') +
  327. ", got '" +
  328. this.terminals_[symbol] +
  329. "'";
  330. } else {
  331. errStr =
  332. 'Parse error on line ' +
  333. (yylineno + 1) +
  334. ': Unexpected ' +
  335. (symbol == 1 /*EOF*/
  336. ? 'end of input'
  337. : "'" + (this.terminals_[symbol] || symbol) + "'");
  338. }
  339. this.parseError(errStr, {
  340. text: this.lexer.match,
  341. token: this.terminals_[symbol] || symbol,
  342. line: this.lexer.yylineno,
  343. loc: yyloc,
  344. expected: expected
  345. });
  346. }
  347. // just recovered from another error
  348. if (recovering == 3) {
  349. if (symbol == EOF) {
  350. throw new Error(errStr || 'Parsing halted.');
  351. }
  352. // discard current lookahead and grab another
  353. yyleng = this.lexer.yyleng;
  354. yytext = this.lexer.yytext;
  355. yylineno = this.lexer.yylineno;
  356. yyloc = this.lexer.yylloc;
  357. symbol = lex();
  358. }
  359. // try to recover from error
  360. while (1) {
  361. // check for error recovery rule in this state
  362. if (TERROR.toString() in table[state]) {
  363. break;
  364. }
  365. if (state == 0) {
  366. throw new Error(errStr || 'Parsing halted.');
  367. }
  368. popStack(1);
  369. state = stack[stack.length - 1];
  370. }
  371. preErrorSymbol = symbol; // save the lookahead token
  372. symbol = TERROR; // insert generic error symbol as new lookahead
  373. state = stack[stack.length - 1];
  374. action = table[state] && table[state][TERROR];
  375. recovering = 3; // allow 3 real symbols to be shifted before reporting a new error
  376. }
  377. // this shouldn't happen, unless resolve defaults are off
  378. if (action[0] instanceof Array && action.length > 1) {
  379. throw new Error(
  380. 'Parse Error: multiple actions possible at state: ' +
  381. state +
  382. ', token: ' +
  383. symbol
  384. );
  385. }
  386. switch (action[0]) {
  387. case 1:
  388. // shift
  389. //this.shiftCount++;
  390. stack.push(symbol);
  391. vstack.push(this.lexer.yytext);
  392. lstack.push(this.lexer.yylloc);
  393. stack.push(action[1]); // push state
  394. symbol = null;
  395. if (!preErrorSymbol) {
  396. // normal execution/no error
  397. yyleng = this.lexer.yyleng;
  398. yytext = this.lexer.yytext;
  399. yylineno = this.lexer.yylineno;
  400. yyloc = this.lexer.yylloc;
  401. if (recovering > 0) recovering--;
  402. } else {
  403. // error just occurred, resume old lookahead f/ before error
  404. symbol = preErrorSymbol;
  405. preErrorSymbol = null;
  406. }
  407. break;
  408. case 2:
  409. // reduce
  410. //this.reductionCount++;
  411. len = this.productions_[action[1]][1];
  412. // perform semantic action
  413. yyval.$ = vstack[vstack.length - len]; // default to $$ = $1
  414. // default location, uses first token for firsts, last for lasts
  415. yyval._$ = {
  416. first_line: lstack[lstack.length - (len || 1)].first_line,
  417. last_line: lstack[lstack.length - 1].last_line,
  418. first_column: lstack[lstack.length - (len || 1)].first_column,
  419. last_column: lstack[lstack.length - 1].last_column
  420. };
  421. r = this.performAction.call(
  422. yyval,
  423. yytext,
  424. yyleng,
  425. yylineno,
  426. this.yy,
  427. action[1],
  428. vstack,
  429. lstack
  430. );
  431. if (typeof r !== 'undefined') {
  432. return r;
  433. }
  434. // pop off stack
  435. if (len) {
  436. stack = stack.slice(0, -1 * len * 2);
  437. vstack = vstack.slice(0, -1 * len);
  438. lstack = lstack.slice(0, -1 * len);
  439. }
  440. stack.push(this.productions_[action[1]][0]); // push nonterminal (reduce)
  441. vstack.push(yyval.$);
  442. lstack.push(yyval._$);
  443. // goto new state = table[STATE][NONTERMINAL]
  444. newState = table[stack[stack.length - 2]][stack[stack.length - 1]];
  445. stack.push(newState);
  446. break;
  447. case 3:
  448. // accept
  449. return true;
  450. }
  451. }
  452. return true;
  453. }
  454. };
  455. /* Jison generated lexer */
  456. var lexer = (function() {
  457. var lexer = {
  458. EOF: 1,
  459. parseError: function parseError(str, hash) {
  460. if (this.yy.parseError) {
  461. this.yy.parseError(str, hash);
  462. } else {
  463. throw new Error(str);
  464. }
  465. },
  466. setInput: function(input) {
  467. this._input = input;
  468. this._more = this._less = this.done = false;
  469. this.yylineno = this.yyleng = 0;
  470. this.yytext = this.matched = this.match = '';
  471. this.conditionStack = ['INITIAL'];
  472. this.yylloc = {
  473. first_line: 1,
  474. first_column: 0,
  475. last_line: 1,
  476. last_column: 0
  477. };
  478. return this;
  479. },
  480. input: function() {
  481. var ch = this._input[0];
  482. this.yytext += ch;
  483. this.yyleng++;
  484. this.match += ch;
  485. this.matched += ch;
  486. var lines = ch.match(/\n/);
  487. if (lines) this.yylineno++;
  488. this._input = this._input.slice(1);
  489. return ch;
  490. },
  491. unput: function(ch) {
  492. this._input = ch + this._input;
  493. return this;
  494. },
  495. more: function() {
  496. this._more = true;
  497. return this;
  498. },
  499. less: function(n) {
  500. this._input = this.match.slice(n) + this._input;
  501. },
  502. pastInput: function() {
  503. var past = this.matched.substr(
  504. 0,
  505. this.matched.length - this.match.length
  506. );
  507. return (
  508. (past.length > 20 ? '...' : '') + past.substr(-20).replace(/\n/g, '')
  509. );
  510. },
  511. upcomingInput: function() {
  512. var next = this.match;
  513. if (next.length < 20) {
  514. next += this._input.substr(0, 20 - next.length);
  515. }
  516. return (next.substr(0, 20) + (next.length > 20 ? '...' : '')).replace(
  517. /\n/g,
  518. ''
  519. );
  520. },
  521. showPosition: function() {
  522. var pre = this.pastInput();
  523. var c = new Array(pre.length + 1).join('-');
  524. return pre + this.upcomingInput() + '\n' + c + '^';
  525. },
  526. next: function() {
  527. if (this.done) {
  528. return this.EOF;
  529. }
  530. if (!this._input) this.done = true;
  531. var token, match, tempMatch, index, col, lines;
  532. if (!this._more) {
  533. this.yytext = '';
  534. this.match = '';
  535. }
  536. var rules = this._currentRules();
  537. for (var i = 0; i < rules.length; i++) {
  538. tempMatch = this._input.match(this.rules[rules[i]]);
  539. if (tempMatch && (!match || tempMatch[0].length > match[0].length)) {
  540. match = tempMatch;
  541. index = i;
  542. if (!this.options.flex) break;
  543. }
  544. }
  545. if (match) {
  546. lines = match[0].match(/\n.*/g);
  547. if (lines) this.yylineno += lines.length;
  548. this.yylloc = {
  549. first_line: this.yylloc.last_line,
  550. last_line: this.yylineno + 1,
  551. first_column: this.yylloc.last_column,
  552. last_column: lines
  553. ? lines[lines.length - 1].length - 1
  554. : this.yylloc.last_column + match[0].length
  555. };
  556. this.yytext += match[0];
  557. this.match += match[0];
  558. this.yyleng = this.yytext.length;
  559. this._more = false;
  560. this._input = this._input.slice(match[0].length);
  561. this.matched += match[0];
  562. token = this.performAction.call(
  563. this,
  564. this.yy,
  565. this,
  566. rules[index],
  567. this.conditionStack[this.conditionStack.length - 1]
  568. );
  569. if (this.done && this._input) this.done = false;
  570. if (token) return token;
  571. else return;
  572. }
  573. if (this._input === '') {
  574. return this.EOF;
  575. } else {
  576. this.parseError(
  577. 'Lexical error on line ' +
  578. (this.yylineno + 1) +
  579. '. Unrecognized text.\n' +
  580. this.showPosition(),
  581. {text: '', token: null, line: this.yylineno}
  582. );
  583. }
  584. },
  585. lex: function lex() {
  586. var r = this.next();
  587. if (typeof r !== 'undefined') {
  588. return r;
  589. } else {
  590. return this.lex();
  591. }
  592. },
  593. begin: function begin(condition) {
  594. this.conditionStack.push(condition);
  595. },
  596. popState: function popState() {
  597. return this.conditionStack.pop();
  598. },
  599. _currentRules: function _currentRules() {
  600. return this.conditions[
  601. this.conditionStack[this.conditionStack.length - 1]
  602. ].rules;
  603. },
  604. topState: function() {
  605. return this.conditionStack[this.conditionStack.length - 2];
  606. },
  607. pushState: function begin(condition) {
  608. this.begin(condition);
  609. }
  610. };
  611. lexer.options = {};
  612. lexer.performAction = function anonymous(
  613. yy,
  614. yy_,
  615. $avoiding_name_collisions,
  616. YY_START
  617. ) {
  618. var YYSTATE = YY_START;
  619. switch ($avoiding_name_collisions) {
  620. case 0 /* skip whitespace */:
  621. break;
  622. case 1:
  623. return 6;
  624. break;
  625. case 2:
  626. yy_.yytext = yy_.yytext.substr(1, yy_.yyleng - 2);
  627. return 4;
  628. break;
  629. case 3:
  630. return 17;
  631. break;
  632. case 4:
  633. return 18;
  634. break;
  635. case 5:
  636. return 23;
  637. break;
  638. case 6:
  639. return 24;
  640. break;
  641. case 7:
  642. return 22;
  643. break;
  644. case 8:
  645. return 21;
  646. break;
  647. case 9:
  648. return 10;
  649. break;
  650. case 10:
  651. return 11;
  652. break;
  653. case 11:
  654. return 8;
  655. break;
  656. case 12:
  657. return 14;
  658. break;
  659. case 13:
  660. return 'INVALID';
  661. break;
  662. }
  663. };
  664. lexer.rules = [
  665. /^(?:\s+)/,
  666. /^(?:(-?([0-9]|[1-9][0-9]+))(\.[0-9]+)?([eE][-+]?[0-9]+)?\b)/,
  667. /^(?:"(?:\\[\\"bfnrt/]|\\u[a-fA-F0-9]{4}|[^\\\0-\x09\x0a-\x1f"])*")/,
  668. /^(?:\{)/,
  669. /^(?:\})/,
  670. /^(?:\[)/,
  671. /^(?:\])/,
  672. /^(?:,)/,
  673. /^(?::)/,
  674. /^(?:true\b)/,
  675. /^(?:false\b)/,
  676. /^(?:null\b)/,
  677. /^(?:$)/,
  678. /^(?:.)/
  679. ];
  680. lexer.conditions = {
  681. INITIAL: {
  682. rules: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
  683. inclusive: true
  684. }
  685. };
  686. return lexer;
  687. })();
  688. parser.lexer = lexer;
  689. return parser;
  690. })();
  691. exports.parser = jsonlint;
  692. exports.errors = function(input) {
  693. try {
  694. this.parse(input);
  695. } catch (e) {
  696. return e.stack;
  697. }
  698. };
  699. exports.parse = function() {
  700. return jsonlint.parse.apply(jsonlint, arguments);
  701. };
  702. exports.main = function commonjsMain(args) {
  703. if (!args[1]) throw new Error('Usage: ' + args[0] + ' FILE');
  704. if (typeof process !== 'undefined') {
  705. var source = require('fs').readFileSync(
  706. require('path').join(process.cwd(), args[1]),
  707. 'utf8'
  708. );
  709. } else {
  710. var cwd = require('file').path(require('file').cwd());
  711. var source = cwd.join(args[1]).read({charset: 'utf-8'});
  712. }
  713. return exports.parser.parse(source);
  714. };