1
0
mirror of synced 2025-12-15 10:52:19 +08:00

command line interface and updates for npm

This commit is contained in:
Zach Carter
2011-05-01 17:36:27 -04:00
parent ac7b47930b
commit 9c1023cfb1
12 changed files with 486 additions and 357 deletions

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
node_modules/jison
node_modules/test
node_modules/uglify-js
node_modules/.bin

View File

@@ -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']);
});

11
Makefile Normal file
View File

@@ -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

View File

@@ -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). 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`.

33
lib/cli.js Executable file
View File

@@ -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));

View File

@@ -1,325 +1,412 @@
/* Jison generated parser */ /* Jison generated parser */
var grammar = (function(){ var jsonlint = (function(){
var parser = {trace: function trace() { var parser = {trace: function trace() { },
},
yy: {}, 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}, 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_: {"3":"STRING","5":"NUMBER","7":"NULL","9":"TRUE","10":"FALSE","15":"{","16":"}","19":":","20":",","21":"[","22":"]"}, terminals_: {2:"error",4:"STRING",6:"NUMBER",8:"NULL",10:"TRUE",11:"FALSE",16:"{",17:"}",20:":",21:",",22:"[",23:"]"},
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]], 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) { performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) {
var $$ = arguments[5], $0 = arguments[5].length;
switch (arguments[4]) { var $0 = $$.length - 1;
case 1: switch (yystate) {
this.$ = yytext; case 1:this.$ = yytext;
break; break;
case 2: case 2:this.$ = Number(yytext);
this.$ = Number(yytext); break;
break; case 3:this.$ = null;
case 3: break;
this.$ = null; case 4:this.$ = true;
break; break;
case 4: case 5:this.$ = false;
this.$ = true; break;
break; case 6:return this.$ = $$[$0];
case 5: break;
this.$ = false; case 7:return this.$ = $$[$0];
break; break;
case 6: case 8:this.$ = $$[$0];
return this.$ = $$[$0 - 1 + 1 - 1]; break;
break; case 9:this.$ = $$[$0];
case 7: break;
return this.$ = $$[$0 - 1 + 1 - 1]; case 10:this.$ = $$[$0];
break; break;
case 8: case 11:this.$ = $$[$0];
this.$ = $$[$0 - 1 + 1 - 1]; break;
break; case 12:this.$ = $$[$0];
case 9: break;
this.$ = $$[$0 - 1 + 1 - 1]; case 13:this.$ = $$[$0];
break; break;
case 10: case 14:this.$ = {};
this.$ = $$[$0 - 1 + 1 - 1]; break;
break; case 15:this.$ = $$[$0-1];
case 11: break;
this.$ = $$[$0 - 1 + 1 - 1]; case 16:this.$ = [$$[$0-2], $$[$0]];
break; break;
case 12: case 17:this.$ = {}; this.$[$$[$0][0]] = $$[$0][1];
this.$ = $$[$0 - 1 + 1 - 1]; break;
break; case 18:this.$ = $$[$0-2]; $$[$0-2][$$[$0][0]] = $$[$0][1];
case 13: break;
this.$ = $$[$0 - 1 + 1 - 1]; case 19:this.$ = [];
break; break;
case 14: case 20:this.$ = $$[$0-1];
this.$ = {}; break;
break; case 21:this.$ = [$$[$0]];
case 15: break;
this.$ = $$[$0 - 3 + 2 - 1]; case 22:this.$ = $$[$0-2]; $$[$0-2].push($$[$0]);
break; 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]}], 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) { parseError: function parseError(str, hash) {
throw new Error(str); throw new Error(str);
}, },
parse: function parse(input) { 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.setInput(input);
this.lexer.yy = this.yy; 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() { function lex() {
var token; var token;
token = self.lexer.lex() || 1; token = self.lexer.lex() || 1; // $end = 1
if (typeof token !== "number") { // if token isn't its numeric value, convert
token = self.symbols_[token]; if (typeof token !== 'number') {
token = self.symbols_[token] || token;
} }
return 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; return true;
}};/* Jison generated lexer */ }};/* Jison generated lexer */
var lexer = (function(){var lexer = ({EOF:"", var lexer = (function(){var lexer = ({EOF:1,
parseError:function parseError(str, hash) { parseError:function parseError(str, hash) {
if (this.yy.parseError) { if (this.yy.parseError) {
this.yy.parseError(str, hash); this.yy.parseError(str, hash);
} else { } else {
throw new Error(str); throw new Error(str);
} }
}, },
setInput:function (input) { setInput:function (input) {
this._input = input; this._input = input;
this._more = this._less = this.done = false; this._more = this._less = this.done = false;
this.yylineno = this.yyleng = 0; this.yylineno = this.yyleng = 0;
this.yytext = this.matched = this.match = ""; this.yytext = this.matched = this.match = '';
return this; this.conditionStack = ['INITIAL'];
}, this.yylloc = {first_line:1,first_column:0,last_line:1,last_column:0};
return this;
},
input:function () { input:function () {
var ch = this._input[0]; var ch = this._input[0];
this.yytext += ch; this.yytext+=ch;
this.yyleng++; this.yyleng++;
this.match += ch; this.match+=ch;
this.matched += ch; this.matched+=ch;
var lines = ch.match(/\n/); var lines = ch.match(/\n/);
if (lines) { if (lines) this.yylineno++;
this.yylineno++; this._input = this._input.slice(1);
} return ch;
this._input = this._input.slice(1); },
return ch;
},
unput:function (ch) { unput:function (ch) {
this._input = ch + this._input; this._input = ch + this._input;
return this; return this;
}, },
more:function () { more:function () {
this._more = true; this._more = true;
return this; return this;
}, },
pastInput:function () { pastInput:function () {
var past = this.matched.substr(0, this.matched.length - this.match.length); var past = this.matched.substr(0, this.matched.length - this.match.length);
return (past.length > 20 ? "..." : "") + past.substr(-20).replace(/\n/g, ""); return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, "");
}, },
upcomingInput:function () { upcomingInput:function () {
var next = this.match; var next = this.match;
if (next.length < 20) { if (next.length < 20) {
next += this._input.substr(0, 20 - next.length); next += this._input.substr(0, 20-next.length);
} }
return (next.substr(0, 20) + (next.length > 20 ? "..." : "")).replace(/\n/g, ""); return (next.substr(0,20)+(next.length > 20 ? '...':'')).replace(/\n/g, "");
}, },
showPosition:function () { showPosition:function () {
var pre = this.pastInput(); var pre = this.pastInput();
var c = (new Array(pre.length + 1)).join("-"); var c = new Array(pre.length + 1).join("-");
return pre + this.upcomingInput() + "\n" + c + "^"; return pre + this.upcomingInput() + "\n" + c+"^";
}, },
next:function () { next:function () {
if (this.done) { if (this.done) {
return this.EOF; return this.EOF;
} }
if (!this._input) { if (!this._input) this.done = true;
this.done = true;
} var token,
var token, match, lines; match,
if (!this._more) { col,
this.yytext = ""; lines;
this.match = ""; if (!this._more) {
} this.yytext = '';
for (var i = 0; i < this.rules.length; i++) { this.match = '';
match = this._input.match(this.rules[i]); }
if (match) { var rules = this._currentRules();
lines = match[0].match(/\n/g); for (var i=0;i < rules.length; i++) {
if (lines) { match = this._input.match(this.rules[rules[i]]);
this.yylineno += lines.length; if (match) {
} lines = match[0].match(/\n.*/g);
this.yytext += match[0]; if (lines) this.yylineno += lines.length;
this.match += match[0]; this.yylloc = {first_line: this.yylloc.last_line,
this.matches = match; last_line: this.yylineno+1,
this.yyleng = this.yytext.length; first_column: this.yylloc.last_column,
this._more = false; last_column: lines ? lines[lines.length-1].length-1 : this.yylloc.last_column + match[0].length}
this._input = this._input.slice(match[0].length); this.yytext += match[0];
this.matched += match[0]; this.match += match[0];
token = this.performAction.call(this, this.yy, this, i); this.matches = match;
if (token) { this.yyleng = this.yytext.length;
return token; this._more = false;
} else { this._input = this._input.slice(match[0].length);
return; 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 === "") {
if (this._input == this.EOF) { return this.EOF;
return this.EOF; } else {
} else { this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(),
this.parseError("Lexical error on line " + (this.yylineno + 1) + ". Unrecognized text.\n" + this.showPosition(), {text: "", token: null, line: this.yylineno}); {text: "", token: null, line: this.yylineno});
} }
}, },
lex:function () { lex:function lex() {
var r = this.next(); var r = this.next();
if (typeof r !== "undefined") { if (typeof r !== 'undefined') {
return r; return r;
} else { } else {
return this.lex(); return this.lex();
} }
}}); },
lexer.performAction = function anonymous(yy, yy_) { begin:function begin(condition) {
switch (arguments[2]) { this.conditionStack.push(condition);
case 0: },
break; popState:function popState() {
case 1: return this.conditionStack.pop();
return 5; },
break; _currentRules:function _currentRules() {
case 2: return this.conditions[this.conditionStack[this.conditionStack.length-1]].rules;
yy_.yytext = yy_.yytext.substr(1, yy_.yyleng - 2); }});
return 3; lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) {
break;
case 3: var YYSTATE=YY_START
return 15; switch($avoiding_name_collisions) {
break; case 0:/* skip whitespace */
case 4: break;
return 16; case 1:return 6;
break; break;
case 5: case 2:yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2); return 4;
return 21; break;
break; case 3: return 16
case 6: break;
return 22; case 4: return 17
break; break;
case 7: case 5:return 22
return 20; break;
break; case 6:return 23
case 8: break;
return 19; case 7:return 21
break; break;
case 9: case 8:return 20
return 9; break;
break; case 9:return 10
case 10: break;
return 10; case 10:return 11
break; break;
case 11: case 11:return 8
return 7; break;
break; case 12:return 'INVALID'
case 12: break;
return "INVALID"; }
break;
default:;
}
}; };
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; parser.lexer = lexer;
return parser; return parser;
})(); })();
if (typeof require !== 'undefined') { if (typeof require !== 'undefined' && typeof exports !== 'undefined') {
exports.parser = grammar; exports.parser = jsonlint;
exports.parse = function () { return grammar.parse.apply(grammar, arguments); } exports.parse = function () { return jsonlint.parse.apply(jsonlint, arguments); }
exports.main = function commonjsMain(args) { exports.main = function commonjsMain(args) {
var cwd = require("file").path(require("file").cwd()); if (!args[1])
if (!args[1]) { throw new Error('Usage: '+args[0]+' FILE');
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"}); return exports.parser.parse(source);
this.parse(source);
}
if (require.main === module) {
exports.main(require("system").args);
} }
if (typeof module !== 'undefined' && require.main === module) {
exports.main(typeof process !== 'undefined' ? process.argv.slice(1) : require("system").args);
} }
}

View File

@@ -1,14 +1,30 @@
{ {
"author": "Zach Carter <zach@carter.name> (http://zaa.ch)",
"name": "jsonlint", "name": "jsonlint",
"author": "Zach Carter", "description": "Validate JSON",
"email": "zach@carter.name", "keywords": ["json", "validation", "lint", "jsonlint"],
"keywords": [ "version": "1.0.0",
"jsonlint", "preferGlobal": true,
"json", "repository": {
"parser", "type": "git",
"lint" "url": "git://github.com/zaach/jsonlint.git"
], },
"githubName": "jsonlint", "bugs": {
"type": "zip", "web": "http://github.com/zaach/jsonlint/issues"
"location": "http://github.com/zaach/jsonlint/zipball/master" },
"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/"
} }

9
scripts/bundle.js Normal file
View File

@@ -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);

6
tests/all-tests.js → test/all-tests.js Executable file → Normal file
View File

@@ -1,6 +1,4 @@
#!/usr/bin/env narwhal var fs = require("fs"),
var fs = require("file"),
assert = require("assert"), assert = require("assert"),
parser = require("../lib/jsonlint").parser; parser = require("../lib/jsonlint").parser;
@@ -20,4 +18,4 @@ exports["test string with line break"] = function () {
}; };
if (require.main === module) if (require.main === module)
require("os").exit(require("test").run(exports)); require("test").run(exports);

File diff suppressed because one or more lines are too long