jsonlint.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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, 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. }};
  235. /* Jison generated lexer */
  236. var lexer = (function(){
  237. var lexer = ({EOF:1,
  238. parseError:function parseError(str, hash) {
  239. if (this.yy.parseError) {
  240. this.yy.parseError(str, hash);
  241. } else {
  242. throw new Error(str);
  243. }
  244. },
  245. setInput:function (input) {
  246. this._input = input;
  247. this._more = this._less = this.done = false;
  248. this.yylineno = this.yyleng = 0;
  249. this.yytext = this.matched = this.match = '';
  250. this.conditionStack = ['INITIAL'];
  251. this.yylloc = {first_line:1,first_column:0,last_line:1,last_column:0};
  252. return this;
  253. },
  254. input:function () {
  255. var ch = this._input[0];
  256. this.yytext+=ch;
  257. this.yyleng++;
  258. this.match+=ch;
  259. this.matched+=ch;
  260. var lines = ch.match(/\n/);
  261. if (lines) this.yylineno++;
  262. this._input = this._input.slice(1);
  263. return ch;
  264. },
  265. unput:function (ch) {
  266. this._input = ch + this._input;
  267. return this;
  268. },
  269. more:function () {
  270. this._more = true;
  271. return this;
  272. },
  273. less:function (n) {
  274. this._input = this.match.slice(n) + this._input;
  275. },
  276. pastInput:function () {
  277. var past = this.matched.substr(0, this.matched.length - this.match.length);
  278. return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, "");
  279. },
  280. upcomingInput:function () {
  281. var next = this.match;
  282. if (next.length < 20) {
  283. next += this._input.substr(0, 20-next.length);
  284. }
  285. return (next.substr(0,20)+(next.length > 20 ? '...':'')).replace(/\n/g, "");
  286. },
  287. showPosition:function () {
  288. var pre = this.pastInput();
  289. var c = new Array(pre.length + 1).join("-");
  290. return pre + this.upcomingInput() + "\n" + c+"^";
  291. },
  292. next:function () {
  293. if (this.done) {
  294. return this.EOF;
  295. }
  296. if (!this._input) this.done = true;
  297. var token,
  298. match,
  299. tempMatch,
  300. index,
  301. lines;
  302. if (!this._more) {
  303. this.yytext = '';
  304. this.match = '';
  305. }
  306. var rules = this._currentRules();
  307. for (var i=0;i < rules.length; i++) {
  308. tempMatch = this._input.match(this.rules[rules[i]]);
  309. if (tempMatch && (!match || tempMatch[0].length > match[0].length)) {
  310. match = tempMatch;
  311. index = i;
  312. if (!this.options.flex) break;
  313. }
  314. }
  315. if (match) {
  316. lines = match[0].match(/\n.*/g);
  317. if (lines) this.yylineno += lines.length;
  318. this.yylloc = {first_line: this.yylloc.last_line,
  319. last_line: this.yylineno+1,
  320. first_column: this.yylloc.last_column,
  321. last_column: lines ? lines[lines.length-1].length-1 : this.yylloc.last_column + match[0].length};
  322. this.yytext += match[0];
  323. this.match += match[0];
  324. this.yyleng = this.yytext.length;
  325. this._more = false;
  326. this._input = this._input.slice(match[0].length);
  327. this.matched += match[0];
  328. token = this.performAction.call(this, this.yy, this, rules[index],this.conditionStack[this.conditionStack.length-1]);
  329. if (this.done && this._input) this.done = false;
  330. if (token) return token;
  331. else return;
  332. }
  333. if (this._input === "") {
  334. return this.EOF;
  335. } else {
  336. this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(),
  337. {text: "", token: null, line: this.yylineno});
  338. }
  339. },
  340. lex:function lex() {
  341. var r = this.next();
  342. if (typeof r !== 'undefined') {
  343. return r;
  344. } else {
  345. return this.lex();
  346. }
  347. },
  348. begin:function begin(condition) {
  349. this.conditionStack.push(condition);
  350. },
  351. popState:function popState() {
  352. return this.conditionStack.pop();
  353. },
  354. _currentRules:function _currentRules() {
  355. return this.conditions[this.conditionStack[this.conditionStack.length-1]].rules;
  356. },
  357. topState:function () {
  358. return this.conditionStack[this.conditionStack.length-2];
  359. },
  360. pushState:function begin(condition) {
  361. this.begin(condition);
  362. }});
  363. lexer.options = {};
  364. lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions) {
  365. switch($avoiding_name_collisions) {
  366. case 0:/* skip whitespace */
  367. break;
  368. case 1:return 6
  369. break;
  370. case 2:yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2); return 4
  371. break;
  372. case 3:return 17
  373. break;
  374. case 4:return 18
  375. break;
  376. case 5:return 23
  377. break;
  378. case 6:return 24
  379. break;
  380. case 7:return 22
  381. break;
  382. case 8:return 21
  383. break;
  384. case 9:return 10
  385. break;
  386. case 10:return 11
  387. break;
  388. case 11:return 8
  389. break;
  390. case 12:return 14
  391. break;
  392. case 13:return 'INVALID'
  393. break;
  394. }
  395. };
  396. 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)/,/^(?:$)/,/^(?:.)/];
  397. lexer.conditions = {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13],"inclusive":true}};
  398. ;
  399. return lexer;})()
  400. parser.lexer = lexer;
  401. return parser;
  402. })();
  403. if (typeof require !== 'undefined' && typeof exports !== 'undefined') {
  404. exports.parser = jsonlint;
  405. exports.parse = function () { return jsonlint.parse.apply(jsonlint, arguments); }
  406. exports.main = function commonjsMain(args) {
  407. if (!args[1])
  408. throw new Error('Usage: '+args[0]+' FILE');
  409. if (typeof process !== 'undefined') {
  410. var source = require('fs').readFileSync(require('path').join(process.cwd(), args[1]), "utf8");
  411. } else {
  412. var cwd = require("file").path(require("file").cwd());
  413. var source = cwd.join(args[1]).read({charset: "utf-8"});
  414. }
  415. return exports.parser.parse(source);
  416. }
  417. if (typeof module !== 'undefined' && require.main === module) {
  418. exports.main(typeof process !== 'undefined' ? process.argv.slice(1) : require("system").args);
  419. }
  420. }