diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d58f884 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules/jison +node_modules/test +node_modules/uglify-js +node_modules/.bin diff --git a/Jakefile b/Jakefile deleted file mode 100644 index a380241..0000000 --- a/Jakefile +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env narwhal - -var FILE = require("file"), - ENV = require("system").env, - OS = require("os"), - jake = require("jake"); - -var cwd = FILE.path(FILE.cwd()); - -jake.task("build", ["build:commonjs", "build:web"]); - -jake.task("build:commonjs", function () { - OS.system(['jison', 'src/grammar.jison', 'src/grammar.jisonlex']); - OS.system(['mv', 'grammar.js', 'lib/jsonlint.js']); -}); - -jake.task("build:web", function () { - var lint = cwd.join('lib', 'jsonlint.js'); - - var sourceArray = ["var jsonlint = (function(){var require=true,module=false;var exports={};"]; - sourceArray.push(lint.read({charset: "utf-8"}), - "return exports;})()"); - - var source = require("jsmin").encode(sourceArray.join("\n")); - - var stream = cwd.join('web', 'jsonlint.js').open("w"); - stream.print(source).close(); -}); - -jake.task("test", function () { - OS.system(['narwhal', 'tests/all-tests.js']); -}); diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9b498a6 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ + +all: build test + +build: + jison src/jsonlint.y src/jsonlint.l + mv jsonlint.js lib/jsonlint.js + node scripts/bundle.js | uglifyjs > web/jsonlint.js + +test: lib/jsonlint.js test/all-tests.js + node test/all-tests.js + diff --git a/README.md b/README.md index 8f3b26c..128e064 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,27 @@ JSON Lint A pure [JavaScript version](http://zaach.github.com/jsonlint/) of the service provided at [jsonlin.com](http://jsonlint.com). +## Command line interface +Install jsonlint with npm to use the command line interface: + + npm install jsonlint -g + +Validate a file like so: + + jsonlint myfile.json + +or pipe input into stdin: + + cat myfile.json | jsonlint + +jsonlint will either report a syntax error with details or pretty print the source if it is valid. + +## Module interface + +I'm not sure why you wouldn't use the built in `JSON.parse` but you can use jsonlint from a CommonJS module: + + var jsonlint = require("jsonlint"); + + jsonlint.parse('{"creative?": false}'); + +It returns the parsed object or throws an `Error`. diff --git a/lib/cli.js b/lib/cli.js new file mode 100755 index 0000000..74f2f5f --- /dev/null +++ b/lib/cli.js @@ -0,0 +1,33 @@ +#!/usr/bin/env node + +var sys = require("sys"); +var parser = require("./jsonlint").parser; + +function parse (source) { + try { + sys.puts(JSON.stringify(parser.parse(source),null," ")); + } catch (e) { + sys.puts(e); + process.exit(1); + } +} + +function main (args) { + var source = ''; + if (args[1]) { + source = require('fs').readFileSync(require('path').join(process.cwd(), args[1]), "utf8"); + parse(source); + } else { + var stdin = process.openStdin(); + stdin.setEncoding('utf8'); + + stdin.on('data', function (chunk) { + source += chunk.toString('utf8'); + }); + stdin.on('end', function () { + parse(source); + }); + } +} + +main(process.argv.slice(1)); diff --git a/lib/jsonlint.js b/lib/jsonlint.js index 808199a..64089cb 100644 --- a/lib/jsonlint.js +++ b/lib/jsonlint.js @@ -1,325 +1,412 @@ /* Jison generated parser */ -var grammar = (function(){ -var parser = {trace: function trace() { -}, +var jsonlint = (function(){ +var parser = {trace: function trace() { }, yy: {}, -symbols_: {"JSONString":2,"STRING":3,"JSONNumber":4,"NUMBER":5,"JSONNullLiteral":6,"NULL":7,"JSONBooleanLiteral":8,"TRUE":9,"FALSE":10,"JSONText":11,"JSONObject":12,"JSONArray":13,"JSONValue":14,"{":15,"}":16,"JSONMemberList":17,"JSONMember":18,":":19,",":20,"[":21,"]":22,"JSONElementList":23,"$accept":0,"$end":1}, -terminals_: {"3":"STRING","5":"NUMBER","7":"NULL","9":"TRUE","10":"FALSE","15":"{","16":"}","19":":","20":",","21":"[","22":"]"}, -productions_: [0,[2,1],[4,1],[6,1],[8,1],[8,1],[11,1],[11,1],[14,1],[14,1],[14,1],[14,1],[14,1],[14,1],[12,2],[12,3],[18,3],[17,1],[17,3],[13,2],[13,3],[23,1],[23,3]], -performAction: function anonymous(yytext, yyleng, yylineno, yy) { - var $$ = arguments[5], $0 = arguments[5].length; - switch (arguments[4]) { - case 1: - this.$ = yytext; - break; - case 2: - this.$ = Number(yytext); - break; - case 3: - this.$ = null; - break; - case 4: - this.$ = true; - break; - case 5: - this.$ = false; - break; - case 6: - return this.$ = $$[$0 - 1 + 1 - 1]; - break; - case 7: - return this.$ = $$[$0 - 1 + 1 - 1]; - break; - case 8: - this.$ = $$[$0 - 1 + 1 - 1]; - break; - case 9: - this.$ = $$[$0 - 1 + 1 - 1]; - break; - case 10: - this.$ = $$[$0 - 1 + 1 - 1]; - break; - case 11: - this.$ = $$[$0 - 1 + 1 - 1]; - break; - case 12: - this.$ = $$[$0 - 1 + 1 - 1]; - break; - case 13: - this.$ = $$[$0 - 1 + 1 - 1]; - break; - case 14: - this.$ = {}; - break; - case 15: - this.$ = $$[$0 - 3 + 2 - 1]; - break; - case 16: - this.$ = [$$[$0 - 3 + 1 - 1], $$[$0 - 3 + 3 - 1]]; - break; - case 17: - this.$ = {}; - this.$[$$[$0 - 1 + 1 - 1][0]] = $$[$0 - 1 + 1 - 1][1]; - break; - case 18: - this.$ = $$[$0 - 3 + 1 - 1]; - $$[$0 - 3 + 1 - 1][$$[$0 - 3 + 3 - 1][0]] = $$[$0 - 3 + 3 - 1][1]; - break; - case 19: - this.$ = []; - break; - case 20: - this.$ = $$[$0 - 3 + 2 - 1]; - break; - case 21: - this.$ = [$$[$0 - 1 + 1 - 1]]; - break; - case 22: - this.$ = $$[$0 - 3 + 1 - 1]; - $$[$0 - 3 + 1 - 1].push($$[$0 - 3 + 3 - 1]); - break; - default:; - } +symbols_: {"error":2,"JSONString":3,"STRING":4,"JSONNumber":5,"NUMBER":6,"JSONNullLiteral":7,"NULL":8,"JSONBooleanLiteral":9,"TRUE":10,"FALSE":11,"JSONText":12,"JSONObject":13,"JSONArray":14,"JSONValue":15,"{":16,"}":17,"JSONMemberList":18,"JSONMember":19,":":20,",":21,"[":22,"]":23,"JSONElementList":24,"$accept":0,"$end":1}, +terminals_: {2:"error",4:"STRING",6:"NUMBER",8:"NULL",10:"TRUE",11:"FALSE",16:"{",17:"}",20:":",21:",",22:"[",23:"]"}, +productions_: [0,[3,1],[5,1],[7,1],[9,1],[9,1],[12,1],[12,1],[15,1],[15,1],[15,1],[15,1],[15,1],[15,1],[13,2],[13,3],[19,3],[18,1],[18,3],[14,2],[14,3],[24,1],[24,3]], +performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) { + +var $0 = $$.length - 1; +switch (yystate) { +case 1:this.$ = yytext; +break; +case 2:this.$ = Number(yytext); +break; +case 3:this.$ = null; +break; +case 4:this.$ = true; +break; +case 5:this.$ = false; +break; +case 6:return this.$ = $$[$0]; +break; +case 7:return this.$ = $$[$0]; +break; +case 8:this.$ = $$[$0]; +break; +case 9:this.$ = $$[$0]; +break; +case 10:this.$ = $$[$0]; +break; +case 11:this.$ = $$[$0]; +break; +case 12:this.$ = $$[$0]; +break; +case 13:this.$ = $$[$0]; +break; +case 14:this.$ = {}; +break; +case 15:this.$ = $$[$0-1]; +break; +case 16:this.$ = [$$[$0-2], $$[$0]]; +break; +case 17:this.$ = {}; this.$[$$[$0][0]] = $$[$0][1]; +break; +case 18:this.$ = $$[$0-2]; $$[$0-2][$$[$0][0]] = $$[$0][1]; +break; +case 19:this.$ = []; +break; +case 20:this.$ = $$[$0-1]; +break; +case 21:this.$ = [$$[$0]]; +break; +case 22:this.$ = $$[$0-2]; $$[$0-2].push($$[$0]); +break; +} }, -table: [{"11":1,"12":2,"13":3,"15":[1,4],"21":[1,5]},{"1":[3]},{"1":[2,6]},{"1":[2,7]},{"16":[1,6],"17":7,"18":8,"2":9,"3":[1,10]},{"22":[1,11],"23":12,"14":13,"6":14,"8":15,"2":16,"4":17,"12":18,"13":19,"7":[1,20],"9":[1,21],"10":[1,22],"3":[1,10],"5":[1,23],"15":[1,4],"21":[1,5]},{"1":[2,14],"22":[2,14],"20":[2,14],"16":[2,14]},{"16":[1,24],"20":[1,25]},{"16":[2,17],"20":[2,17]},{"19":[1,26]},{"19":[2,1],"22":[2,1],"20":[2,1],"16":[2,1]},{"1":[2,19],"22":[2,19],"20":[2,19],"16":[2,19]},{"22":[1,27],"20":[1,28]},{"22":[2,21],"20":[2,21]},{"22":[2,8],"20":[2,8],"16":[2,8]},{"22":[2,9],"20":[2,9],"16":[2,9]},{"22":[2,10],"20":[2,10],"16":[2,10]},{"22":[2,11],"20":[2,11],"16":[2,11]},{"22":[2,12],"20":[2,12],"16":[2,12]},{"22":[2,13],"20":[2,13],"16":[2,13]},{"22":[2,3],"20":[2,3],"16":[2,3]},{"22":[2,4],"20":[2,4],"16":[2,4]},{"22":[2,5],"20":[2,5],"16":[2,5]},{"22":[2,2],"20":[2,2],"16":[2,2]},{"1":[2,15],"22":[2,15],"20":[2,15],"16":[2,15]},{"18":29,"2":9,"3":[1,10]},{"14":30,"6":14,"8":15,"2":16,"4":17,"12":18,"13":19,"7":[1,20],"9":[1,21],"10":[1,22],"3":[1,10],"5":[1,23],"15":[1,4],"21":[1,5]},{"1":[2,20],"22":[2,20],"20":[2,20],"16":[2,20]},{"14":31,"6":14,"8":15,"2":16,"4":17,"12":18,"13":19,"7":[1,20],"9":[1,21],"10":[1,22],"3":[1,10],"5":[1,23],"15":[1,4],"21":[1,5]},{"16":[2,18],"20":[2,18]},{"16":[2,16],"20":[2,16]},{"22":[2,22],"20":[2,22]}], +table: [{12:1,13:2,14:3,16:[1,4],22:[1,5]},{1:[3]},{1:[2,6]},{1:[2,7]},{3:9,4:[1,10],17:[1,6],18:7,19:8},{3:16,4:[1,10],5:17,6:[1,23],7:14,8:[1,20],9:15,10:[1,21],11:[1,22],13:18,14:19,15:13,16:[1,4],22:[1,5],23:[1,11],24:12},{1:[2,14],17:[2,14],21:[2,14],23:[2,14]},{17:[1,24],21:[1,25]},{17:[2,17],21:[2,17]},{20:[1,26]},{17:[2,1],20:[2,1],21:[2,1],23:[2,1]},{1:[2,19],17:[2,19],21:[2,19],23:[2,19]},{21:[1,28],23:[1,27]},{21:[2,21],23:[2,21]},{17:[2,8],21:[2,8],23:[2,8]},{17:[2,9],21:[2,9],23:[2,9]},{17:[2,10],21:[2,10],23:[2,10]},{17:[2,11],21:[2,11],23:[2,11]},{17:[2,12],21:[2,12],23:[2,12]},{17:[2,13],21:[2,13],23:[2,13]},{17:[2,3],21:[2,3],23:[2,3]},{17:[2,4],21:[2,4],23:[2,4]},{17:[2,5],21:[2,5],23:[2,5]},{17:[2,2],21:[2,2],23:[2,2]},{1:[2,15],17:[2,15],21:[2,15],23:[2,15]},{3:9,4:[1,10],19:29},{3:16,4:[1,10],5:17,6:[1,23],7:14,8:[1,20],9:15,10:[1,21],11:[1,22],13:18,14:19,15:30,16:[1,4],22:[1,5]},{1:[2,20],17:[2,20],21:[2,20],23:[2,20]},{3:16,4:[1,10],5:17,6:[1,23],7:14,8:[1,20],9:15,10:[1,21],11:[1,22],13:18,14:19,15:31,16:[1,4],22:[1,5]},{17:[2,18],21:[2,18]},{17:[2,16],21:[2,16]},{21:[2,22],23:[2,22]}], +defaultActions: {2:[2,6],3:[2,7]}, parseError: function parseError(str, hash) { throw new Error(str); }, parse: function parse(input) { - var self = this, stack = [0], vstack = [null], table = this.table, yytext = "", yylineno = 0, yyleng = 0, shifts = 0, reductions = 0; + var self = this, + stack = [0], + vstack = [null], // semantic value stack + lstack = [], // location stack + table = this.table, + yytext = '', + yylineno = 0, + yyleng = 0, + recovering = 0, + TERROR = 2, + EOF = 1; + + //this.reductionCount = this.shiftCount = 0; + this.lexer.setInput(input); this.lexer.yy = this.yy; - var parseError = this.yy.parseError = this.yy.parseError || this.parseError; + this.yy.lexer = this.lexer; + if (typeof this.lexer.yylloc == 'undefined') + this.lexer.yylloc = {}; + var yyloc = this.lexer.yylloc; + lstack.push(yyloc); + + if (typeof this.yy.parseError === 'function') + this.parseError = this.yy.parseError; + + function popStack (n) { + stack.length = stack.length - 2*n; + vstack.length = vstack.length - n; + lstack.length = lstack.length - n; + } function lex() { var token; - token = self.lexer.lex() || 1; - if (typeof token !== "number") { - token = self.symbols_[token]; + token = self.lexer.lex() || 1; // $end = 1 + // if token isn't its numeric value, convert + if (typeof token !== 'number') { + token = self.symbols_[token] || token; } return token; + }; + + var symbol, preErrorSymbol, state, action, a, r, yyval={},p,len,newState, expected; + while (true) { + // retreive state number from top of stack + state = stack[stack.length-1]; + + // use default actions if available + if (this.defaultActions[state]) { + action = this.defaultActions[state]; + } else { + if (symbol == null) + symbol = lex(); + // read action for current state and first input + action = table[state] && table[state][symbol]; + } + + // handle parse error + if (typeof action === 'undefined' || !action.length || !action[0]) { + + if (!recovering) { + // Report error + expected = []; + for (p in table[state]) if (this.terminals_[p] && p > 2) { + expected.push("'"+this.terminals_[p]+"'"); + } + var errStr = ''; + if (this.lexer.showPosition) { + errStr = 'Parse error on line '+(yylineno+1)+":\n"+this.lexer.showPosition()+'\nExpecting '+expected.join(', '); + } else { + errStr = 'Parse error on line '+(yylineno+1)+": Unexpected " + + (symbol == 1 /*EOF*/ ? "end of input" : + ("'"+(this.terminals_[symbol] || symbol)+"'")); + } + this.parseError(errStr, + {text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, loc: yyloc, expected: expected}); + } + + // just recovered from another error + if (recovering == 3) { + if (symbol == EOF) { + throw new Error(errStr || 'Parsing halted.'); + } + + // discard current lookahead and grab another + yyleng = this.lexer.yyleng; + yytext = this.lexer.yytext; + yylineno = this.lexer.yylineno; + yyloc = this.lexer.yylloc; + symbol = lex(); + } + + // try to recover from error + while (1) { + // check for error recovery rule in this state + if ((TERROR.toString()) in table[state]) { + break; + } + if (state == 0) { + throw new Error(errStr || 'Parsing halted.'); + } + popStack(1); + state = stack[stack.length-1]; + } + + preErrorSymbol = symbol; // save the lookahead token + symbol = TERROR; // insert generic error symbol as new lookahead + state = stack[stack.length-1]; + action = table[state] && table[state][TERROR]; + recovering = 3; // allow 3 real symbols to be shifted before reporting a new error + } + + // this shouldn't happen, unless resolve defaults are off + if (action[0] instanceof Array && action.length > 1) { + throw new Error('Parse Error: multiple actions possible at state: '+state+', token: '+symbol); + } + + switch (action[0]) { + + case 1: // shift + //this.shiftCount++; + + stack.push(symbol); + vstack.push(this.lexer.yytext); + lstack.push(this.lexer.yylloc); + stack.push(action[1]); // push state + symbol = null; + if (!preErrorSymbol) { // normal execution/no error + yyleng = this.lexer.yyleng; + yytext = this.lexer.yytext; + yylineno = this.lexer.yylineno; + yyloc = this.lexer.yylloc; + if (recovering > 0) + recovering--; + } else { // error just occurred, resume old lookahead f/ before error + symbol = preErrorSymbol; + preErrorSymbol = null; + } + break; + + case 2: // reduce + //this.reductionCount++; + + len = this.productions_[action[1]][1]; + + // perform semantic action + yyval.$ = vstack[vstack.length-len]; // default to $$ = $1 + // default location, uses first token for firsts, last for lasts + yyval._$ = { + first_line: lstack[lstack.length-(len||1)].first_line, + last_line: lstack[lstack.length-1].last_line, + first_column: lstack[lstack.length-(len||1)].first_column, + last_column: lstack[lstack.length-1].last_column + }; + r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, action[1], vstack, lstack); + + if (typeof r !== 'undefined') { + return r; + } + + // pop off stack + if (len) { + stack = stack.slice(0,-1*len*2); + vstack = vstack.slice(0, -1*len); + lstack = lstack.slice(0, -1*len); + } + + stack.push(this.productions_[action[1]][0]); // push nonterminal (reduce) + vstack.push(yyval.$); + lstack.push(yyval._$); + // goto new state = table[STATE][NONTERMINAL] + newState = table[stack[stack.length-2]][stack[stack.length-1]]; + stack.push(newState); + break; + + case 3: // accept + return true; + } + } - var symbol, state, action, a, r, yyval = {}, p, len, ip = 0, newState, expected; - symbol = lex(); - while (true) { - state = stack[stack.length - 1]; - action = table[state] && table[state][symbol]; - if (typeof action === "undefined" || !action.length || !action[0]) { - expected = []; - for (p in table[state]) { - if (this.terminals_[p] && p != 1) { - expected.push("'" + this.terminals_[p] + "'"); - } - } - if (this.lexer.showPosition) { - parseError("Parse error on line " + (yylineno + 1) + ":\n" + this.lexer.showPosition() + "\nExpecting " + expected.join(", "), {text: this.lexer.match, token: this.terminals_[symbol], line: this.lexer.yylineno, expected: expected}); - } else { - parseError("Parse error on line " + (yylineno + 1) + ": Unexpected '" + this.terminals_[symbol] + "'", {text: this.lexer.match, token: this.terminals_[symbol], line: this.lexer.yylineno, expected: expected}); - } - } - if (action[0] instanceof Array && action.length > 1) { - throw new Error("Parse Error: multiple actions possible at state: " + state + ", token: " + symbol); - } - a = action; - switch (a[0]) { - case 1: - shifts++; - stack.push(symbol); - ++ip; - yyleng = this.lexer.yyleng; - yytext = this.lexer.yytext; - yylineno = this.lexer.yylineno; - symbol = lex(); - vstack.push(null); - stack.push(a[1]); - break; - case 2: - reductions++; - len = this.productions_[a[1]][1]; - yyval.$ = vstack[vstack.length - len]; - r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, a[1], vstack); - if (typeof r !== "undefined") { - return r; - } - if (len) { - stack = stack.slice(0, -1 * len * 2); - vstack = vstack.slice(0, -1 * len); - } - stack.push(this.productions_[a[1]][0]); - vstack.push(yyval.$); - newState = table[stack[stack.length - 2]][stack[stack.length - 1]]; - stack.push(newState); - break; - case 3: - this.reductionCount = reductions; - this.shiftCount = shifts; - return true; - default:; - } - } return true; }};/* Jison generated lexer */ -var lexer = (function(){var lexer = ({EOF:"", +var lexer = (function(){var lexer = ({EOF:1, parseError:function parseError(str, hash) { - if (this.yy.parseError) { - this.yy.parseError(str, hash); - } else { - throw new Error(str); - } -}, + if (this.yy.parseError) { + this.yy.parseError(str, hash); + } else { + throw new Error(str); + } + }, setInput:function (input) { - this._input = input; - this._more = this._less = this.done = false; - this.yylineno = this.yyleng = 0; - this.yytext = this.matched = this.match = ""; - return this; -}, + this._input = input; + this._more = this._less = this.done = false; + this.yylineno = this.yyleng = 0; + this.yytext = this.matched = this.match = ''; + this.conditionStack = ['INITIAL']; + this.yylloc = {first_line:1,first_column:0,last_line:1,last_column:0}; + return this; + }, input:function () { - var ch = this._input[0]; - this.yytext += ch; - this.yyleng++; - this.match += ch; - this.matched += ch; - var lines = ch.match(/\n/); - if (lines) { - this.yylineno++; - } - this._input = this._input.slice(1); - return ch; -}, + var ch = this._input[0]; + this.yytext+=ch; + this.yyleng++; + this.match+=ch; + this.matched+=ch; + var lines = ch.match(/\n/); + if (lines) this.yylineno++; + this._input = this._input.slice(1); + return ch; + }, unput:function (ch) { - this._input = ch + this._input; - return this; -}, + this._input = ch + this._input; + return this; + }, more:function () { - this._more = true; - return this; -}, + this._more = true; + return this; + }, pastInput:function () { - var past = this.matched.substr(0, this.matched.length - this.match.length); - return (past.length > 20 ? "..." : "") + past.substr(-20).replace(/\n/g, ""); -}, + var past = this.matched.substr(0, this.matched.length - this.match.length); + return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, ""); + }, upcomingInput:function () { - var next = this.match; - if (next.length < 20) { - next += this._input.substr(0, 20 - next.length); - } - return (next.substr(0, 20) + (next.length > 20 ? "..." : "")).replace(/\n/g, ""); -}, + var next = this.match; + if (next.length < 20) { + next += this._input.substr(0, 20-next.length); + } + return (next.substr(0,20)+(next.length > 20 ? '...':'')).replace(/\n/g, ""); + }, showPosition:function () { - var pre = this.pastInput(); - var c = (new Array(pre.length + 1)).join("-"); - return pre + this.upcomingInput() + "\n" + c + "^"; -}, + var pre = this.pastInput(); + var c = new Array(pre.length + 1).join("-"); + return pre + this.upcomingInput() + "\n" + c+"^"; + }, next:function () { - if (this.done) { - return this.EOF; - } - if (!this._input) { - this.done = true; - } - var token, match, lines; - if (!this._more) { - this.yytext = ""; - this.match = ""; - } - for (var i = 0; i < this.rules.length; i++) { - match = this._input.match(this.rules[i]); - if (match) { - lines = match[0].match(/\n/g); - if (lines) { - this.yylineno += lines.length; - } - this.yytext += match[0]; - this.match += match[0]; - this.matches = match; - this.yyleng = this.yytext.length; - this._more = false; - this._input = this._input.slice(match[0].length); - this.matched += match[0]; - token = this.performAction.call(this, this.yy, this, i); - if (token) { - return token; - } else { - return; + if (this.done) { + return this.EOF; + } + if (!this._input) this.done = true; + + var token, + match, + col, + lines; + if (!this._more) { + this.yytext = ''; + this.match = ''; + } + var rules = this._currentRules(); + for (var i=0;i < rules.length; i++) { + match = this._input.match(this.rules[rules[i]]); + if (match) { + lines = match[0].match(/\n.*/g); + if (lines) this.yylineno += lines.length; + this.yylloc = {first_line: this.yylloc.last_line, + last_line: this.yylineno+1, + first_column: this.yylloc.last_column, + last_column: lines ? lines[lines.length-1].length-1 : this.yylloc.last_column + match[0].length} + this.yytext += match[0]; + this.match += match[0]; + this.matches = match; + this.yyleng = this.yytext.length; + this._more = false; + this._input = this._input.slice(match[0].length); + this.matched += match[0]; + token = this.performAction.call(this, this.yy, this, rules[i],this.conditionStack[this.conditionStack.length-1]); + if (token) return token; + else return; } } - } - if (this._input == this.EOF) { - return this.EOF; - } else { - this.parseError("Lexical error on line " + (this.yylineno + 1) + ". Unrecognized text.\n" + this.showPosition(), {text: "", token: null, line: this.yylineno}); - } -}, -lex:function () { - var r = this.next(); - if (typeof r !== "undefined") { - return r; - } else { - return this.lex(); - } -}}); -lexer.performAction = function anonymous(yy, yy_) { - switch (arguments[2]) { - case 0: - break; - case 1: - return 5; - break; - case 2: - yy_.yytext = yy_.yytext.substr(1, yy_.yyleng - 2); - return 3; - break; - case 3: - return 15; - break; - case 4: - return 16; - break; - case 5: - return 21; - break; - case 6: - return 22; - break; - case 7: - return 20; - break; - case 8: - return 19; - break; - case 9: - return 9; - break; - case 10: - return 10; - break; - case 11: - return 7; - break; - case 12: - return "INVALID"; - break; - default:; - } + if (this._input === "") { + return this.EOF; + } else { + this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(), + {text: "", token: null, line: this.yylineno}); + } + }, +lex:function lex() { + var r = this.next(); + if (typeof r !== 'undefined') { + return r; + } else { + return this.lex(); + } + }, +begin:function begin(condition) { + this.conditionStack.push(condition); + }, +popState:function popState() { + return this.conditionStack.pop(); + }, +_currentRules:function _currentRules() { + return this.conditions[this.conditionStack[this.conditionStack.length-1]].rules; + }}); +lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) { + +var YYSTATE=YY_START +switch($avoiding_name_collisions) { +case 0:/* skip whitespace */ +break; +case 1:return 6; +break; +case 2:yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2); return 4; +break; +case 3: return 16 +break; +case 4: return 17 +break; +case 5:return 22 +break; +case 6:return 23 +break; +case 7:return 21 +break; +case 8:return 20 +break; +case 9:return 10 +break; +case 10:return 11 +break; +case 11:return 8 +break; +case 12:return 'INVALID' +break; +} }; -lexer.rules = [/^\s+/,/^-?([0-9]|[1-9][0-9]+)(\.[0-9]+)?([eE][-+]?[0-9]+)?\b\b/,/^"(\\["bfnrt\/\\]|\\u[a-fA-F0-9]{4}|[^\0-\x08\x0a-\x1f"\\])*"/,/^\{/,/^\}/,/^\[/,/^\]/,/^,/,/^:/,/^true\b/,/^false\b/,/^null\b/,/^./];return lexer;})() +lexer.rules = [/^\s+/,/^-?([0-9]|[1-9][0-9]+)(\.[0-9]+)?([eE][-+]?[0-9]+)?\b\b/,/^"(\\["bfnrt/\\]|\\u[a-fA-F0-9]{4}|[^\0-\x08\x0a-\x1f"\\])*"/,/^\{/,/^\}/,/^\[/,/^\]/,/^,/,/^:/,/^true\b/,/^false\b/,/^null\b/,/^./]; +lexer.conditions = {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12],"inclusive":true}};return lexer;})() parser.lexer = lexer; return parser; })(); -if (typeof require !== 'undefined') { -exports.parser = grammar; -exports.parse = function () { return grammar.parse.apply(grammar, arguments); } +if (typeof require !== 'undefined' && typeof exports !== 'undefined') { +exports.parser = jsonlint; +exports.parse = function () { return jsonlint.parse.apply(jsonlint, arguments); } exports.main = function commonjsMain(args) { - var cwd = require("file").path(require("file").cwd()); - if (!args[1]) { - throw new Error("Usage: " + args[0] + " FILE"); + if (!args[1]) + throw new Error('Usage: '+args[0]+' FILE'); + if (typeof process !== 'undefined') { + var source = require('fs').readFileSync(require('path').join(process.cwd(), args[1]), "utf8"); + } else { + var cwd = require("file").path(require("file").cwd()); + var source = cwd.join(args[1]).read({charset: "utf-8"}); } - var source = cwd.join(args[1]).read({charset: "utf-8"}); - this.parse(source); -} -if (require.main === module) { - exports.main(require("system").args); + return exports.parser.parse(source); } +if (typeof module !== 'undefined' && require.main === module) { + exports.main(typeof process !== 'undefined' ? process.argv.slice(1) : require("system").args); } +} \ No newline at end of file diff --git a/package.json b/package.json index 959445c..062422f 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,30 @@ { + "author": "Zach Carter (http://zaa.ch)", "name": "jsonlint", - "author": "Zach Carter", - "email": "zach@carter.name", - "keywords": [ - "jsonlint", - "json", - "parser", - "lint" - ], - "githubName": "jsonlint", - "type": "zip", - "location": "http://github.com/zaach/jsonlint/zipball/master" + "description": "Validate JSON", + "keywords": ["json", "validation", "lint", "jsonlint"], + "version": "1.0.0", + "preferGlobal": true, + "repository": { + "type": "git", + "url": "git://github.com/zaach/jsonlint.git" + }, + "bugs": { + "web": "http://github.com/zaach/jsonlint/issues" + }, + "main": "lib/jsonlint.js", + "bin": "lib/cli.js", + "engines": { + "node": "0.4 || 0.5" + }, + "dependencies": {}, + "devDependencies": { + "test": "*", + "jison": "*", + "uglify-js": "*" + }, + "scripts": { + "test": "node test/all-tests.js" + }, + "homepage": "http://zaach.github.com/jsonlint/" } diff --git a/scripts/bundle.js b/scripts/bundle.js new file mode 100644 index 0000000..c05b93e --- /dev/null +++ b/scripts/bundle.js @@ -0,0 +1,9 @@ +var fs = require('fs'); +var sys = require('sys'); + +var source = "var jsonlint = (function(){var require=true,module=false;var exports={};" + + fs.readFileSync(__dirname+'/../lib/jsonlint.js', 'utf8') + + "return exports;})()"; + +sys.puts(source); + diff --git a/src/grammar.jisonlex b/src/jsonlint.l similarity index 100% rename from src/grammar.jisonlex rename to src/jsonlint.l diff --git a/src/grammar.jison b/src/jsonlint.y similarity index 100% rename from src/grammar.jison rename to src/jsonlint.y diff --git a/tests/all-tests.js b/test/all-tests.js old mode 100755 new mode 100644 similarity index 84% rename from tests/all-tests.js rename to test/all-tests.js index 59a9ba5..60eb50a --- a/tests/all-tests.js +++ b/test/all-tests.js @@ -1,6 +1,4 @@ -#!/usr/bin/env narwhal - -var fs = require("file"), +var fs = require("fs"), assert = require("assert"), parser = require("../lib/jsonlint").parser; @@ -20,4 +18,4 @@ exports["test string with line break"] = function () { }; if (require.main === module) - require("os").exit(require("test").run(exports)); + require("test").run(exports); diff --git a/web/jsonlint.js b/web/jsonlint.js index 30964ba..7349023 100644 --- a/web/jsonlint.js +++ b/web/jsonlint.js @@ -1,22 +1 @@ - -var jsonlint=(function(){var require=true,module=false;var exports={};var grammar=(function(){var parser={trace:function trace(){},yy:{},symbols_:{"JSONString":2,"STRING":3,"JSONNumber":4,"NUMBER":5,"JSONNullLiteral":6,"NULL":7,"JSONBooleanLiteral":8,"TRUE":9,"FALSE":10,"JSONText":11,"JSONObject":12,"JSONArray":13,"JSONValue":14,"{":15,"}":16,"JSONMemberList":17,"JSONMember":18,":":19,",":20,"[":21,"]":22,"JSONElementList":23,"$accept":0,"$end":1},terminals_:{"3":"STRING","5":"NUMBER","7":"NULL","9":"TRUE","10":"FALSE","15":"{","16":"}","19":":","20":",","21":"[","22":"]"},productions_:[0,[2,1],[4,1],[6,1],[8,1],[8,1],[11,1],[11,1],[14,1],[14,1],[14,1],[14,1],[14,1],[14,1],[12,2],[12,3],[18,3],[17,1],[17,3],[13,2],[13,3],[23,1],[23,3]],performAction:function anonymous(yytext,yyleng,yylineno,yy){var $$=arguments[5],$0=arguments[5].length;switch(arguments[4]){case 1:this.$=yytext;break;case 2:this.$=Number(yytext);break;case 3:this.$=null;break;case 4:this.$=true;break;case 5:this.$=false;break;case 6:return this.$=$$[$0-1+1-1];break;case 7:return this.$=$$[$0-1+1-1];break;case 8:this.$=$$[$0-1+1-1];break;case 9:this.$=$$[$0-1+1-1];break;case 10:this.$=$$[$0-1+1-1];break;case 11:this.$=$$[$0-1+1-1];break;case 12:this.$=$$[$0-1+1-1];break;case 13:this.$=$$[$0-1+1-1];break;case 14:this.$={};break;case 15:this.$=$$[$0-3+2-1];break;case 16:this.$=[$$[$0-3+1-1],$$[$0-3+3-1]];break;case 17:this.$={};this.$[$$[$0-1+1-1][0]]=$$[$0-1+1-1][1];break;case 18:this.$=$$[$0-3+1-1];$$[$0-3+1-1][$$[$0-3+3-1][0]]=$$[$0-3+3-1][1];break;case 19:this.$=[];break;case 20:this.$=$$[$0-3+2-1];break;case 21:this.$=[$$[$0-1+1-1]];break;case 22:this.$=$$[$0-3+1-1];$$[$0-3+1-1].push($$[$0-3+3-1]);break;default:;}},table:[{"11":1,"12":2,"13":3,"15":[1,4],"21":[1,5]},{"1":[3]},{"1":[2,6]},{"1":[2,7]},{"16":[1,6],"17":7,"18":8,"2":9,"3":[1,10]},{"22":[1,11],"23":12,"14":13,"6":14,"8":15,"2":16,"4":17,"12":18,"13":19,"7":[1,20],"9":[1,21],"10":[1,22],"3":[1,10],"5":[1,23],"15":[1,4],"21":[1,5]},{"1":[2,14],"22":[2,14],"20":[2,14],"16":[2,14]},{"16":[1,24],"20":[1,25]},{"16":[2,17],"20":[2,17]},{"19":[1,26]},{"19":[2,1],"22":[2,1],"20":[2,1],"16":[2,1]},{"1":[2,19],"22":[2,19],"20":[2,19],"16":[2,19]},{"22":[1,27],"20":[1,28]},{"22":[2,21],"20":[2,21]},{"22":[2,8],"20":[2,8],"16":[2,8]},{"22":[2,9],"20":[2,9],"16":[2,9]},{"22":[2,10],"20":[2,10],"16":[2,10]},{"22":[2,11],"20":[2,11],"16":[2,11]},{"22":[2,12],"20":[2,12],"16":[2,12]},{"22":[2,13],"20":[2,13],"16":[2,13]},{"22":[2,3],"20":[2,3],"16":[2,3]},{"22":[2,4],"20":[2,4],"16":[2,4]},{"22":[2,5],"20":[2,5],"16":[2,5]},{"22":[2,2],"20":[2,2],"16":[2,2]},{"1":[2,15],"22":[2,15],"20":[2,15],"16":[2,15]},{"18":29,"2":9,"3":[1,10]},{"14":30,"6":14,"8":15,"2":16,"4":17,"12":18,"13":19,"7":[1,20],"9":[1,21],"10":[1,22],"3":[1,10],"5":[1,23],"15":[1,4],"21":[1,5]},{"1":[2,20],"22":[2,20],"20":[2,20],"16":[2,20]},{"14":31,"6":14,"8":15,"2":16,"4":17,"12":18,"13":19,"7":[1,20],"9":[1,21],"10":[1,22],"3":[1,10],"5":[1,23],"15":[1,4],"21":[1,5]},{"16":[2,18],"20":[2,18]},{"16":[2,16],"20":[2,16]},{"22":[2,22],"20":[2,22]}],parseError:function parseError(str,hash){throw new Error(str);},parse:function parse(input){var self=this,stack=[0],vstack=[null],table=this.table,yytext="",yylineno=0,yyleng=0,shifts=0,reductions=0;this.lexer.setInput(input);this.lexer.yy=this.yy;var parseError=this.yy.parseError=this.yy.parseError||this.parseError;function lex(){var token;token=self.lexer.lex()||1;if(typeof token!=="number"){token=self.symbols_[token];} -return token;} -var symbol,state,action,a,r,yyval={},p,len,ip=0,newState,expected;symbol=lex();while(true){state=stack[stack.length-1];action=table[state]&&table[state][symbol];if(typeof action==="undefined"||!action.length||!action[0]){expected=[];for(p in table[state]){if(this.terminals_[p]&&p!=1){expected.push("'"+this.terminals_[p]+"'");}} -if(this.lexer.showPosition){parseError("Parse error on line "+(yylineno+1)+":\n"+this.lexer.showPosition()+"\nExpecting "+expected.join(", "),{text:this.lexer.match,token:this.terminals_[symbol],line:this.lexer.yylineno,expected:expected});}else{parseError("Parse error on line "+(yylineno+1)+": Unexpected '"+this.terminals_[symbol]+"'",{text:this.lexer.match,token:this.terminals_[symbol],line:this.lexer.yylineno,expected:expected});}} -if(action[0]instanceof Array&&action.length>1){throw new Error("Parse Error: multiple actions possible at state: "+state+", token: "+symbol);} -a=action;switch(a[0]){case 1:shifts++;stack.push(symbol);++ip;yyleng=this.lexer.yyleng;yytext=this.lexer.yytext;yylineno=this.lexer.yylineno;symbol=lex();vstack.push(null);stack.push(a[1]);break;case 2:reductions++;len=this.productions_[a[1]][1];yyval.$=vstack[vstack.length-len];r=this.performAction.call(yyval,yytext,yyleng,yylineno,this.yy,a[1],vstack);if(typeof r!=="undefined"){return r;} -if(len){stack=stack.slice(0,-1*len*2);vstack=vstack.slice(0,-1*len);} -stack.push(this.productions_[a[1]][0]);vstack.push(yyval.$);newState=table[stack[stack.length-2]][stack[stack.length-1]];stack.push(newState);break;case 3:this.reductionCount=reductions;this.shiftCount=shifts;return true;default:;}} -return true;}};var lexer=(function(){var lexer=({EOF:"",parseError:function parseError(str,hash){if(this.yy.parseError){this.yy.parseError(str,hash);}else{throw new Error(str);}},setInput:function(input){this._input=input;this._more=this._less=this.done=false;this.yylineno=this.yyleng=0;this.yytext=this.matched=this.match="";return this;},input:function(){var ch=this._input[0];this.yytext+=ch;this.yyleng++;this.match+=ch;this.matched+=ch;var lines=ch.match(/\n/);if(lines){this.yylineno++;} -this._input=this._input.slice(1);return ch;},unput:function(ch){this._input=ch+this._input;return this;},more:function(){this._more=true;return this;},pastInput:function(){var past=this.matched.substr(0,this.matched.length-this.match.length);return(past.length>20?"...":"")+past.substr(-20).replace(/\n/g,"");},upcomingInput:function(){var next=this.match;if(next.length<20){next+=this._input.substr(0,20-next.length);} -return(next.substr(0,20)+(next.length>20?"...":"")).replace(/\n/g,"");},showPosition:function(){var pre=this.pastInput();var c=(new Array(pre.length+1)).join("-");return pre+this.upcomingInput()+"\n"+c+"^";},next:function(){if(this.done){return this.EOF;} -if(!this._input){this.done=true;} -var token,match,lines;if(!this._more){this.yytext="";this.match="";} -for(var i=0;i2&&z.push("'"+this.terminals_[w]+"'");var A="";this.lexer.showPosition?A="Parse error on line "+(h+1)+":\n"+this.lexer.showPosition()+"\nExpecting "+z.join(", "):A="Parse error on line "+(h+1)+": Unexpected "+(p==1?"end of input":"'"+(this.terminals_[p]||p)+"'"),this.parseError(A,{text:this.lexer.match,token:this.terminals_[p]||p,line:this.lexer.yylineno,loc:m,expected:z})}if(j==3){if(p==l)throw new Error(A||"Parsing halted.");i=this.lexer.yyleng,g=this.lexer.yytext,h=this.lexer.yylineno,m=this.lexer.yylloc,p=o()}for(;;){if(k.toString()in f[r])break;if(r==0)throw new Error(A||"Parsing halted.");n(1),r=c[c.length-1]}q=p,p=k,r=c[c.length-1],s=f[r]&&f[r][k],j=3}if(s[0]instanceof Array&&s.length>1)throw new Error("Parse Error: multiple actions possible at state: "+r+", token: "+p);switch(s[0]){case 1:c.push(p),d.push(this.lexer.yytext),e.push(this.lexer.yylloc),c.push(s[1]),p=null,q?(p=q,q=null):(i=this.lexer.yyleng,g=this.lexer.yytext,h=this.lexer.yylineno,m=this.lexer.yylloc,j>0&&j--);break;case 2:x=this.productions_[s[1]][1],v.$=d[d.length-x],v._$={first_line:e[e.length-(x||1)].first_line,last_line:e[e.length-1].last_line,first_column:e[e.length-(x||1)].first_column,last_column:e[e.length-1].last_column},u=this.performAction.call(v,g,i,h,this.yy,s[1],d,e);if(typeof u!="undefined")return u;x&&(c=c.slice(0,-1*x*2),d=d.slice(0,-1*x),e=e.slice(0,-1*x)),c.push(this.productions_[s[1]][0]),d.push(v.$),e.push(v._$),y=f[c[c.length-2]][c[c.length-1]],c.push(y);break;case 3:return!0}}return!0}},f=function(){var a={EOF:1,parseError:function(a,b){if(this.yy.parseError)this.yy.parseError(a,b);else throw new Error(a)},setInput:function(a){this._input=a,this._more=this._less=this.done=!1,this.yylineno=this.yyleng=0,this.yytext=this.matched=this.match="",this.conditionStack=["INITIAL"],this.yylloc={first_line:1,first_column:0,last_line:1,last_column:0};return this},input:function(){var a=this._input[0];this.yytext+=a,this.yyleng++,this.match+=a,this.matched+=a;var b=a.match(/\n/);b&&this.yylineno++,this._input=this._input.slice(1);return a},unput:function(a){this._input=a+this._input;return this},more:function(){this._more=!0;return this},pastInput:function(){var a=this.matched.substr(0,this.matched.length-this.match.length);return(a.length>20?"...":"")+a.substr(-20).replace(/\n/g,"")},upcomingInput:function(){var a=this.match;a.length<20&&(a+=this._input.substr(0,20-a.length));return(a.substr(0,20)+(a.length>20?"...":"")).replace(/\n/g,"")},showPosition:function(){var a=this.pastInput(),b=Array(a.length+1).join("-");return a+this.upcomingInput()+"\n"+b+"^"},next:function(){if(this.done)return this.EOF;this._input||(this.done=!0);var a,b,c,d;this._more||(this.yytext="",this.match="");var e=this._currentRules();for(var f=0;f