jsonlint.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. // Full source:
  2. //
  3. // https://github.com/zaach/jsonlint
  4. //
  5. // Copyright (C) 2012 Zachary Carter
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a
  8. // copy of this software and associated documentation files (the "Software"),
  9. // to deal in the Software without restriction, including without limitation
  10. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. // and/or sell copies of the Software, and to permit persons to whom the
  12. // Software is furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEAL-
  23. // INGS IN THE SOFTWARE.
  24. /* Jison generated parser */
  25. var jsonlint = (function(){
  26. var parser = {trace: function trace() { },
  27. yy: {},
  28. symbols_: {"error":2,"JSONString":3,"STRING":4,"JSONNumber":5,"NUMBER":6,"JSONNullLiteral":7,"NULL":8,"JSONBooleanLiteral":9,"TRUE":10,"FALSE":11,"JSONText":12,"JSONValue":13,"EOF":14,"JSONObject":15,"JSONArray":16,"{":17,"}":18,"JSONMemberList":19,"JSONMember":20,":":21,",":22,"[":23,"]":24,"JSONElementList":25,"$accept":0,"$end":1},
  29. terminals_: {2:"error",4:"STRING",6:"NUMBER",8:"NULL",10:"TRUE",11:"FALSE",14:"EOF",17:"{",18:"}",21:":",22:",",23:"[",24:"]"},
  30. productions_: [0,[3,1],[5,1],[7,1],[9,1],[9,1],[12,2],[13,1],[13,1],[13,1],[13,1],[13,1],[13,1],[15,2],[15,3],[20,3],[19,1],[19,3],[16,2],[16,3],[25,1],[25,3]],
  31. performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) {
  32. var $0 = $$.length - 1;
  33. switch (yystate) {
  34. case 1: // replace escaped characters with actual character
  35. this.$ = yytext.replace(/\\(\\|")/g, "$"+"1")
  36. .replace(/\\n/g,'\n')
  37. .replace(/\\r/g,'\r')
  38. .replace(/\\t/g,'\t')
  39. .replace(/\\v/g,'\v')
  40. .replace(/\\f/g,'\f')
  41. .replace(/\\b/g,'\b');
  42. break;
  43. case 2:this.$ = Number(yytext);
  44. break;
  45. case 3:this.$ = null;
  46. break;
  47. case 4:this.$ = true;
  48. break;
  49. case 5:this.$ = false;
  50. break;
  51. case 6:return this.$ = $$[$0-1];
  52. break;
  53. case 13:this.$ = {};
  54. break;
  55. case 14:this.$ = $$[$0-1];
  56. break;
  57. case 15:this.$ = [$$[$0-2], $$[$0]];
  58. break;
  59. case 16:this.$ = {}; this.$[$$[$0][0]] = $$[$0][1];
  60. break;
  61. case 17:this.$ = $$[$0-2]; $$[$0-2][$$[$0][0]] = $$[$0][1];
  62. break;
  63. case 18:this.$ = [];
  64. break;
  65. case 19:this.$ = $$[$0-1];
  66. break;
  67. case 20:this.$ = [$$[$0]];
  68. break;
  69. case 21:this.$ = $$[$0-2]; $$[$0-2].push($$[$0]);
  70. break;
  71. }
  72. },
  73. table: [{3:5,4:[1,12],5:6,6:[1,13],7:3,8:[1,9],9:4,10:[1,10],11:[1,11],12:1,13:2,15:7,16:8,17:[1,14],23:[1,15]},{1:[3]},{14:[1,16]},{14:[2,7],18:[2,7],22:[2,7],24:[2,7]},{14:[2,8],18:[2,8],22:[2,8],24:[2,8]},{14:[2,9],18:[2,9],22:[2,9],24:[2,9]},{14:[2,10],18:[2,10],22:[2,10],24:[2,10]},{14:[2,11],18:[2,11],22:[2,11],24:[2,11]},{14:[2,12],18:[2,12],22:[2,12],24:[2,12]},{14:[2,3],18:[2,3],22:[2,3],24:[2,3]},{14:[2,4],18:[2,4],22:[2,4],24:[2,4]},{14:[2,5],18:[2,5],22:[2,5],24:[2,5]},{14:[2,1],18:[2,1],21:[2,1],22:[2,1],24:[2,1]},{14:[2,2],18:[2,2],22:[2,2],24:[2,2]},{3:20,4:[1,12],18:[1,17],19:18,20:19},{3:5,4:[1,12],5:6,6:[1,13],7:3,8:[1,9],9:4,10:[1,10],11:[1,11],13:23,15:7,16:8,17:[1,14],23:[1,15],24:[1,21],25:22},{1:[2,6]},{14:[2,13],18:[2,13],22:[2,13],24:[2,13]},{18:[1,24],22:[1,25]},{18:[2,16],22:[2,16]},{21:[1,26]},{14:[2,18],18:[2,18],22:[2,18],24:[2,18]},{22:[1,28],24:[1,27]},{22:[2,20],24:[2,20]},{14:[2,14],18:[2,14],22:[2,14],24:[2,14]},{3:20,4:[1,12],20:29},{3:5,4:[1,12],5:6,6:[1,13],7:3,8:[1,9],9:4,10:[1,10],11:[1,11],13:30,15:7,16:8,17:[1,14],23:[1,15]},{14:[2,19],18:[2,19],22:[2,19],24:[2,19]},{3:5,4:[1,12],5:6,6:[1,13],7:3,8:[1,9],9:4,10:[1,10],11:[1,11],13:31,15:7,16:8,17:[1,14],23:[1,15]},{18:[2,17],22:[2,17]},{18:[2,15],22:[2,15]},{22:[2,21],24:[2,21]}],
  74. defaultActions: {16:[2,6]},
  75. parseError: function parseError(str, hash) {
  76. throw new Error(str);
  77. },
  78. parse: function parse(input) {
  79. var self = this,
  80. stack = [0],
  81. vstack = [null], // semantic value stack
  82. lstack = [], // location stack
  83. table = this.table,
  84. yytext = '',
  85. yylineno = 0,
  86. yyleng = 0,
  87. recovering = 0,
  88. TERROR = 2,
  89. EOF = 1;
  90. //this.reductionCount = this.shiftCount = 0;
  91. this.lexer.setInput(input);
  92. this.lexer.yy = this.yy;
  93. this.yy.lexer = this.lexer;
  94. if (typeof this.lexer.yylloc == 'undefined')
  95. this.lexer.yylloc = {};
  96. var yyloc = this.lexer.yylloc;
  97. lstack.push(yyloc);
  98. if (typeof this.yy.parseError === 'function')
  99. this.parseError = this.yy.parseError;
  100. function popStack (n) {
  101. stack.length = stack.length - 2*n;
  102. vstack.length = vstack.length - n;
  103. lstack.length = lstack.length - n;
  104. }
  105. function lex() {
  106. var token;
  107. token = self.lexer.lex() || 1; // $end = 1
  108. // if token isn't its numeric value, convert
  109. if (typeof token !== 'number') {
  110. token = self.symbols_[token] || token;
  111. }
  112. return token;
  113. }
  114. var symbol, preErrorSymbol, state, action, a, r, yyval={},p,len,newState, expected;
  115. while (true) {
  116. // retreive state number from top of stack
  117. state = stack[stack.length-1];
  118. // use default actions if available
  119. if (this.defaultActions[state]) {
  120. action = this.defaultActions[state];
  121. } else {
  122. if (symbol == null)
  123. symbol = lex();
  124. // read action for current state and first input
  125. action = table[state] && table[state][symbol];
  126. }
  127. // handle parse error
  128. _handle_error:
  129. if (typeof action === 'undefined' || !action.length || !action[0]) {
  130. if (!recovering) {
  131. // Report error
  132. expected = [];
  133. for (p in table[state]) if (this.terminals_[p] && p > 2) {
  134. expected.push("'"+this.terminals_[p]+"'");
  135. }
  136. var errStr = '';
  137. if (this.lexer.showPosition) {
  138. errStr = 'Parse error on line '+(yylineno+1)+":\n"+this.lexer.showPosition()+"\nExpecting "+expected.join(', ') + ", got '" + this.terminals_[symbol]+ "'";
  139. } else {
  140. errStr = 'Parse error on line '+(yylineno+1)+": Unexpected " +
  141. (symbol == 1 /*EOF*/ ? "end of input" :
  142. ("'"+(this.terminals_[symbol] || symbol)+"'"));
  143. }
  144. this.parseError(errStr,
  145. {text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, loc: yyloc, expected: expected});
  146. }
  147. // just recovered from another error
  148. if (recovering == 3) {
  149. if (symbol == EOF) {
  150. throw new Error(errStr || 'Parsing halted.');
  151. }
  152. // discard current lookahead and grab another
  153. yyleng = this.lexer.yyleng;
  154. yytext = this.lexer.yytext;
  155. yylineno = this.lexer.yylineno;
  156. yyloc = this.lexer.yylloc;
  157. symbol = lex();
  158. }
  159. // try to recover from error
  160. while (1) {
  161. // check for error recovery rule in this state
  162. if ((TERROR.toString()) in table[state]) {
  163. break;
  164. }
  165. if (state == 0) {
  166. throw new Error(errStr || 'Parsing halted.');
  167. }
  168. popStack(1);
  169. state = stack[stack.length-1];
  170. }
  171. preErrorSymbol = symbol; // save the lookahead token
  172. symbol = TERROR; // insert generic error symbol as new lookahead
  173. state = stack[stack.length-1];
  174. action = table[state] && table[state][TERROR];
  175. recovering = 3; // allow 3 real symbols to be shifted before reporting a new error
  176. }
  177. // this shouldn't happen, unless resolve defaults are off
  178. if (action[0] instanceof Array && action.length > 1) {
  179. throw new Error('Parse Error: multiple actions possible at state: '+state+', token: '+symbol);
  180. }
  181. switch (action[0]) {
  182. case 1: // shift
  183. //this.shiftCount++;
  184. stack.push(symbol);
  185. vstack.push(this.lexer.yytext);
  186. lstack.push(this.lexer.yylloc);
  187. stack.push(action[1]); // push state
  188. symbol = null;
  189. if (!preErrorSymbol) { // normal execution/no error
  190. yyleng = this.lexer.yyleng;
  191. yytext = this.lexer.yytext;
  192. yylineno = this.lexer.yylineno;
  193. yyloc = this.lexer.yylloc;
  194. if (recovering > 0)
  195. recovering--;
  196. } else { // error just occurred, resume old lookahead f/ before error
  197. symbol = preErrorSymbol;
  198. preErrorSymbol = null;
  199. }
  200. break;
  201. case 2: // reduce
  202. //this.reductionCount++;
  203. len = this.productions_[action[1]][1];
  204. // perform semantic action
  205. yyval.$ = vstack[vstack.length-len]; // default to $$ = $1
  206. // default location, uses first token for firsts, last for lasts
  207. yyval._$ = {
  208. first_line: lstack[lstack.length-(len||1)].first_line,
  209. last_line: lstack[lstack.length-1].last_line,
  210. first_column: lstack[lstack.length-(len||1)].first_column,
  211. last_column: lstack[lstack.length-1].last_column
  212. };
  213. r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, action[1], vstack, lstack);
  214. if (typeof r !== 'undefined') {
  215. return r;
  216. }
  217. // pop off stack
  218. if (len) {
  219. stack = stack.slice(0,-1*len*2);
  220. vstack = vstack.slice(0, -1*len);
  221. lstack = lstack.slice(0, -1*len);
  222. }
  223. stack.push(this.productions_[action[1]][0]); // push nonterminal (reduce)
  224. vstack.push(yyval.$);
  225. lstack.push(yyval._$);
  226. // goto new state = table[STATE][NONTERMINAL]
  227. newState = table[stack[stack.length-2]][stack[stack.length-1]];
  228. stack.push(newState);
  229. break;
  230. case 3: // accept
  231. return true;
  232. }
  233. }
  234. return true;
  235. }};
  236. /* Jison generated lexer */
  237. var lexer = (function(){
  238. var lexer = ({EOF:1,
  239. parseError:function parseError(str, hash) {
  240. if (this.yy.parseError) {
  241. this.yy.parseError(str, hash);
  242. } else {
  243. throw new Error(str);
  244. }
  245. },
  246. setInput:function (input) {
  247. this._input = input;
  248. this._more = this._less = this.done = false;
  249. this.yylineno = this.yyleng = 0;
  250. this.yytext = this.matched = this.match = '';
  251. this.conditionStack = ['INITIAL'];
  252. this.yylloc = {first_line:1,first_column:0,last_line:1,last_column:0};
  253. return this;
  254. },
  255. input:function () {
  256. var ch = this._input[0];
  257. this.yytext+=ch;
  258. this.yyleng++;
  259. this.match+=ch;
  260. this.matched+=ch;
  261. var lines = ch.match(/\n/);
  262. if (lines) this.yylineno++;
  263. this._input = this._input.slice(1);
  264. return ch;
  265. },
  266. unput:function (ch) {
  267. this._input = ch + this._input;
  268. return this;
  269. },
  270. more:function () {
  271. this._more = true;
  272. return this;
  273. },
  274. less:function (n) {
  275. this._input = this.match.slice(n) + this._input;
  276. },
  277. pastInput:function () {
  278. var past = this.matched.substr(0, this.matched.length - this.match.length);
  279. return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, "");
  280. },
  281. upcomingInput:function () {
  282. var next = this.match;
  283. if (next.length < 20) {
  284. next += this._input.substr(0, 20-next.length);
  285. }
  286. return (next.substr(0,20)+(next.length > 20 ? '...':'')).replace(/\n/g, "");
  287. },
  288. showPosition:function () {
  289. var pre = this.pastInput();
  290. var c = new Array(pre.length + 1).join("-");
  291. return pre + this.upcomingInput() + "\n" + c+"^";
  292. },
  293. next:function () {
  294. if (this.done) {
  295. return this.EOF;
  296. }
  297. if (!this._input) this.done = true;
  298. var token,
  299. match,
  300. tempMatch,
  301. index,
  302. col,
  303. lines;
  304. if (!this._more) {
  305. this.yytext = '';
  306. this.match = '';
  307. }
  308. var rules = this._currentRules();
  309. for (var i=0;i < rules.length; i++) {
  310. tempMatch = this._input.match(this.rules[rules[i]]);
  311. if (tempMatch && (!match || tempMatch[0].length > match[0].length)) {
  312. match = tempMatch;
  313. index = i;
  314. if (!this.options.flex) break;
  315. }
  316. }
  317. if (match) {
  318. lines = match[0].match(/\n.*/g);
  319. if (lines) this.yylineno += lines.length;
  320. this.yylloc = {first_line: this.yylloc.last_line,
  321. last_line: this.yylineno+1,
  322. first_column: this.yylloc.last_column,
  323. last_column: lines ? lines[lines.length-1].length-1 : this.yylloc.last_column + match[0].length}
  324. this.yytext += match[0];
  325. this.match += match[0];
  326. this.yyleng = this.yytext.length;
  327. this._more = false;
  328. this._input = this._input.slice(match[0].length);
  329. this.matched += match[0];
  330. token = this.performAction.call(this, this.yy, this, rules[index],this.conditionStack[this.conditionStack.length-1]);
  331. if (this.done && this._input) this.done = false;
  332. if (token) return token;
  333. else return;
  334. }
  335. if (this._input === "") {
  336. return this.EOF;
  337. } else {
  338. this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(),
  339. {text: "", token: null, line: this.yylineno});
  340. }
  341. },
  342. lex:function lex() {
  343. var r = this.next();
  344. if (typeof r !== 'undefined') {
  345. return r;
  346. } else {
  347. return this.lex();
  348. }
  349. },
  350. begin:function begin(condition) {
  351. this.conditionStack.push(condition);
  352. },
  353. popState:function popState() {
  354. return this.conditionStack.pop();
  355. },
  356. _currentRules:function _currentRules() {
  357. return this.conditions[this.conditionStack[this.conditionStack.length-1]].rules;
  358. },
  359. topState:function () {
  360. return this.conditionStack[this.conditionStack.length-2];
  361. },
  362. pushState:function begin(condition) {
  363. this.begin(condition);
  364. }});
  365. lexer.options = {};
  366. lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) {
  367. var YYSTATE=YY_START
  368. switch($avoiding_name_collisions) {
  369. case 0:/* skip whitespace */
  370. break;
  371. case 1:return 6
  372. break;
  373. case 2:yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2); return 4
  374. break;
  375. case 3:return 17
  376. break;
  377. case 4:return 18
  378. break;
  379. case 5:return 23
  380. break;
  381. case 6:return 24
  382. break;
  383. case 7:return 22
  384. break;
  385. case 8:return 21
  386. break;
  387. case 9:return 10
  388. break;
  389. case 10:return 11
  390. break;
  391. case 11:return 8
  392. break;
  393. case 12:return 14
  394. break;
  395. case 13:return 'INVALID'
  396. break;
  397. }
  398. };
  399. lexer.rules = [/^(?:\s+)/,/^(?:(-?([0-9]|[1-9][0-9]+))(\.[0-9]+)?([eE][-+]?[0-9]+)?\b)/,/^(?:"(?:\\[\\"bfnrt/]|\\u[a-fA-F0-9]{4}|[^\\\0-\x09\x0a-\x1f"])*")/,/^(?:\{)/,/^(?:\})/,/^(?:\[)/,/^(?:\])/,/^(?:,)/,/^(?::)/,/^(?:true\b)/,/^(?:false\b)/,/^(?:null\b)/,/^(?:$)/,/^(?:.)/];
  400. lexer.conditions = {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13],"inclusive":true}};
  401. ;
  402. return lexer;})()
  403. parser.lexer = lexer;
  404. return parser;
  405. })();
  406. if (typeof require !== 'undefined' && typeof exports !== 'undefined') {
  407. exports.parser = jsonlint;
  408. exports.parse = function () { return jsonlint.parse.apply(jsonlint, arguments); }
  409. exports.main = function commonjsMain(args) {
  410. if (!args[1])
  411. throw new Error('Usage: '+args[0]+' FILE');
  412. if (typeof process !== 'undefined') {
  413. var source = require('fs').readFileSync(require('path').join(process.cwd(), args[1]), "utf8");
  414. } else {
  415. var cwd = require("file").path(require("file").cwd());
  416. var source = cwd.join(args[1]).read({charset: "utf-8"});
  417. }
  418. return exports.parser.parse(source);
  419. }
  420. if (typeof module !== 'undefined' && require.main === module) {
  421. exports.main(typeof process !== 'undefined' ? process.argv.slice(1) : require("system").args);
  422. }
  423. }