mirror of
https://github.com/circlecell/jsonlint-mod.git
synced 2026-01-21 05:07:49 +08:00
Compare commits
32 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c07fb3db3b | ||
|
|
f88c4842ff | ||
|
|
eca60bb22b | ||
|
|
4f24c338fa | ||
|
|
f4d68f8fd1 | ||
|
|
9eb4bd8260 | ||
|
|
11e7192a72 | ||
|
|
c42d2abffc | ||
|
|
ee5c2d557c | ||
|
|
2e869ca7ec | ||
|
|
833a75648f | ||
|
|
efd1ae6071 | ||
|
|
0653b731db | ||
|
|
5cca03512e | ||
|
|
4c65154163 | ||
|
|
c21e8eb8de | ||
|
|
6d09e77ef9 | ||
|
|
1956caa5a4 | ||
|
|
5a52ba4662 | ||
|
|
1ff64514b6 | ||
|
|
19fd9a63d2 | ||
|
|
36370390e0 | ||
|
|
cdd45ca8ad | ||
|
|
348eeb4c11 | ||
|
|
acf3cd47bc | ||
|
|
cfda950c3c | ||
|
|
ae5025e5ac | ||
|
|
77766eeae9 | ||
|
|
280ccc5121 | ||
|
|
f08f1b245f | ||
|
|
7d7e4b499f | ||
|
|
26d697c45f |
8
Makefile
8
Makefile
@@ -1,11 +1,17 @@
|
||||
|
||||
all: build test
|
||||
all: build test site
|
||||
|
||||
build:
|
||||
jison src/jsonlint.y src/jsonlint.l
|
||||
mv jsonlint.js lib/jsonlint.js
|
||||
node scripts/bundle.js | uglifyjs > web/jsonlint.js
|
||||
|
||||
site:
|
||||
cp web/jsonlint.js ../jsonlint-pages/jsonlint.js
|
||||
|
||||
deploy: site
|
||||
cd ../jsonlint-pages && git commit -a -m 'deploy site updates' && git push origin gh-pages
|
||||
|
||||
test: lib/jsonlint.js test/all-tests.js
|
||||
node test/all-tests.js
|
||||
|
||||
|
||||
30
README.md
30
README.md
@@ -1,7 +1,7 @@
|
||||
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 [jsonlint.com](http://jsonlint.com).
|
||||
|
||||
## Command line interface
|
||||
Install jsonlint with npm to use the command line interface:
|
||||
@@ -18,6 +18,19 @@ or pipe input into stdin:
|
||||
|
||||
jsonlint will either report a syntax error with details or pretty print the source if it is valid.
|
||||
|
||||
### Options
|
||||
|
||||
$ jsonlint -h
|
||||
Usage: jsonlint <file> [options]
|
||||
|
||||
<file> file to parse; otherwise uses stdin
|
||||
|
||||
options:
|
||||
-v, --version print version and exit
|
||||
-s, --sort-keys sort object keys
|
||||
-i, --in-place overwrite the file
|
||||
-t CHAR, --indent CHAR character(s) to use for indentation
|
||||
|
||||
## 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:
|
||||
@@ -27,3 +40,18 @@ I'm not sure why you wouldn't use the built in `JSON.parse` but you can use json
|
||||
jsonlint.parse('{"creative?": false}');
|
||||
|
||||
It returns the parsed object or throws an `Error`.
|
||||
|
||||
## Vim Plugins
|
||||
|
||||
* [Syntastic](http://www.vim.org/scripts/script.php?script_id=2736)
|
||||
* [sourcebeautify](http://www.vim.org/scripts/script.php?script_id=4079)
|
||||
|
||||
## MIT License
|
||||
|
||||
Copyright (C) 2012 Zachary Carter
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
92
lib/cli.js
92
lib/cli.js
@@ -1,22 +1,77 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
var sys = require("sys");
|
||||
var fs = require("fs");
|
||||
var parser = require("./jsonlint").parser;
|
||||
var options = require("nomnom")
|
||||
.scriptName("jsonlint")
|
||||
.opts({
|
||||
file: {
|
||||
position: 0,
|
||||
help: "file to parse; otherwise uses stdin"
|
||||
},
|
||||
version: {
|
||||
flag : true,
|
||||
string: '-v, --version',
|
||||
help: 'print version and exit',
|
||||
callback: function() {
|
||||
return JSON.parse(fs.readFileSync(__dirname + "/../package.json", "utf8")).version;
|
||||
}
|
||||
},
|
||||
sort : {
|
||||
flag : true,
|
||||
string: '-s, --sort-keys',
|
||||
help: 'sort object keys'
|
||||
},
|
||||
inplace : {
|
||||
flag : true,
|
||||
string: '-i, --in-place',
|
||||
help: 'overwrite the file'
|
||||
},
|
||||
indent : {
|
||||
string: '-t CHAR, --indent CHAR',
|
||||
"default": " ",
|
||||
help: 'character(s) to use for indentation'
|
||||
},
|
||||
compact : {
|
||||
flag : true,
|
||||
string: '-c, --compact',
|
||||
help : 'compact error display'
|
||||
}
|
||||
})
|
||||
.parseArgs();
|
||||
|
||||
if (options.compact) {
|
||||
var fileName = options.file? options.file + ': ' : '';
|
||||
parser.parseError = parser.lexer.parseError = function(str, hash) {
|
||||
console.error(fileName + 'line '+ hash.loc.first_line +', col '+ hash.loc.last_column +', found: \''+ hash.token +'\' - expected: '+ hash.expected.join(', ') +'.');
|
||||
throw new Error(str);
|
||||
};
|
||||
}
|
||||
|
||||
function parse (source) {
|
||||
try {
|
||||
sys.puts(JSON.stringify(parser.parse(source),null," "));
|
||||
var parsed = options.sort ?
|
||||
sortObject(parser.parse(source)) :
|
||||
parser.parse(source);
|
||||
return JSON.stringify(parsed,null,options.indent);
|
||||
} catch (e) {
|
||||
sys.puts(e);
|
||||
if (! options.compact) {
|
||||
console.log(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);
|
||||
if (options.file) {
|
||||
var path = require('path').normalize(options.file);
|
||||
source = parse(fs.readFileSync(path, "utf8"));
|
||||
if (options.inplace) {
|
||||
fs.writeSync(fs.openSync(path,'w+'), source, 0, "utf8");
|
||||
} else {
|
||||
console.log(source);
|
||||
}
|
||||
} else {
|
||||
var stdin = process.openStdin();
|
||||
stdin.setEncoding('utf8');
|
||||
@@ -25,9 +80,32 @@ function main (args) {
|
||||
source += chunk.toString('utf8');
|
||||
});
|
||||
stdin.on('end', function () {
|
||||
parse(source);
|
||||
console.log(parse(source));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// from http://stackoverflow.com/questions/1359761/sorting-a-json-object-in-javascript
|
||||
function sortObject(o) {
|
||||
if (Object.prototype.toString.call(o) !== '[object Object]') {
|
||||
return o;
|
||||
}
|
||||
|
||||
var sorted = {},
|
||||
key, a = [];
|
||||
|
||||
for (key in o) {
|
||||
if (o.hasOwnProperty(key)) {
|
||||
a.push(key);
|
||||
}
|
||||
}
|
||||
|
||||
a.sort();
|
||||
|
||||
for (key = 0; key < a.length; key++) {
|
||||
sorted[a[key]] = sortObject(o[a[key]]);
|
||||
}
|
||||
return sorted;
|
||||
}
|
||||
|
||||
main(process.argv.slice(1));
|
||||
|
||||
114
lib/jsonlint.js
114
lib/jsonlint.js
@@ -2,14 +2,14 @@
|
||||
var jsonlint = (function(){
|
||||
var parser = {trace: function trace() { },
|
||||
yy: {},
|
||||
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,"EOF":14,"JSONArray":15,"JSONValue":16,"{":17,"}":18,"JSONMemberList":19,"JSONMember":20,":":21,",":22,"[":23,"]":24,"JSONElementList":25,"$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,"JSONValue":13,"EOF":14,"JSONObject":15,"JSONArray":16,"{":17,"}":18,"JSONMemberList":19,"JSONMember":20,":":21,",":22,"[":23,"]":24,"JSONElementList":25,"$accept":0,"$end":1},
|
||||
terminals_: {2:"error",4:"STRING",6:"NUMBER",8:"NULL",10:"TRUE",11:"FALSE",14:"EOF",17:"{",18:"}",21:":",22:",",23:"[",24:"]"},
|
||||
productions_: [0,[3,1],[5,1],[7,1],[9,1],[9,1],[12,2],[12,2],[16,1],[16,1],[16,1],[16,1],[16,1],[16,1],[13,2],[13,3],[20,3],[19,1],[19,3],[15,2],[15,3],[25,1],[25,3]],
|
||||
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]],
|
||||
performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) {
|
||||
|
||||
var $0 = $$.length - 1;
|
||||
switch (yystate) {
|
||||
case 1:this.$ = yytext;
|
||||
case 1:this.$ = yytext.replace(/\\\\/g, "\\");
|
||||
break;
|
||||
case 2:this.$ = Number(yytext);
|
||||
break;
|
||||
@@ -21,42 +21,28 @@ case 5:this.$ = false;
|
||||
break;
|
||||
case 6:return this.$ = $$[$0-1];
|
||||
break;
|
||||
case 7:return this.$ = $$[$0-1];
|
||||
case 13:this.$ = {};
|
||||
break;
|
||||
case 8:this.$ = $$[$0];
|
||||
case 14:this.$ = $$[$0-1];
|
||||
break;
|
||||
case 9:this.$ = $$[$0];
|
||||
case 15:this.$ = [$$[$0-2], $$[$0]];
|
||||
break;
|
||||
case 10:this.$ = $$[$0];
|
||||
case 16:this.$ = {}; this.$[$$[$0][0]] = $$[$0][1];
|
||||
break;
|
||||
case 11:this.$ = $$[$0];
|
||||
case 17:this.$ = $$[$0-2]; $$[$0-2][$$[$0][0]] = $$[$0][1];
|
||||
break;
|
||||
case 12:this.$ = $$[$0];
|
||||
case 18:this.$ = [];
|
||||
break;
|
||||
case 13:this.$ = $$[$0];
|
||||
case 19:this.$ = $$[$0-1];
|
||||
break;
|
||||
case 14:this.$ = {};
|
||||
case 20:this.$ = [$$[$0]];
|
||||
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]);
|
||||
case 21:this.$ = $$[$0-2]; $$[$0-2].push($$[$0]);
|
||||
break;
|
||||
}
|
||||
},
|
||||
table: [{12:1,13:2,15:3,17:[1,4],23:[1,5]},{1:[3]},{14:[1,6]},{14:[1,7]},{3:11,4:[1,12],18:[1,8],19:9,20:10},{3:18,4:[1,12],5:19,6:[1,25],7:16,8:[1,22],9:17,10:[1,23],11:[1,24],13:20,15:21,16:15,17:[1,4],23:[1,5],24:[1,13],25:14},{1:[2,6]},{1:[2,7]},{14:[2,14],18:[2,14],22:[2,14],24:[2,14]},{18:[1,26],22:[1,27]},{18:[2,17],22:[2,17]},{21:[1,28]},{18:[2,1],21:[2,1],22:[2,1],24:[2,1]},{14:[2,19],18:[2,19],22:[2,19],24:[2,19]},{22:[1,30],24:[1,29]},{22:[2,21],24:[2,21]},{18:[2,8],22:[2,8],24:[2,8]},{18:[2,9],22:[2,9],24:[2,9]},{18:[2,10],22:[2,10],24:[2,10]},{18:[2,11],22:[2,11],24:[2,11]},{18:[2,12],22:[2,12],24:[2,12]},{18:[2,13],22:[2,13],24:[2,13]},{18:[2,3],22:[2,3],24:[2,3]},{18:[2,4],22:[2,4],24:[2,4]},{18:[2,5],22:[2,5],24:[2,5]},{18:[2,2],22:[2,2],24:[2,2]},{14:[2,15],18:[2,15],22:[2,15],24:[2,15]},{3:11,4:[1,12],20:31},{3:18,4:[1,12],5:19,6:[1,25],7:16,8:[1,22],9:17,10:[1,23],11:[1,24],13:20,15:21,16:32,17:[1,4],23:[1,5]},{14:[2,20],18:[2,20],22:[2,20],24:[2,20]},{3:18,4:[1,12],5:19,6:[1,25],7:16,8:[1,22],9:17,10:[1,23],11:[1,24],13:20,15:21,16:33,17:[1,4],23:[1,5]},{18:[2,18],22:[2,18]},{18:[2,16],22:[2,16]},{22:[2,22],24:[2,22]}],
|
||||
defaultActions: {6:[2,6],7:[2,7]},
|
||||
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]}],
|
||||
defaultActions: {16:[2,6]},
|
||||
parseError: function parseError(str, hash) {
|
||||
throw new Error(str);
|
||||
},
|
||||
@@ -100,7 +86,7 @@ parse: function parse(input) {
|
||||
token = self.symbols_[token] || token;
|
||||
}
|
||||
return token;
|
||||
};
|
||||
}
|
||||
|
||||
var symbol, preErrorSymbol, state, action, a, r, yyval={},p,len,newState, expected;
|
||||
while (true) {
|
||||
@@ -118,6 +104,7 @@ parse: function parse(input) {
|
||||
}
|
||||
|
||||
// handle parse error
|
||||
_handle_error:
|
||||
if (typeof action === 'undefined' || !action.length || !action[0]) {
|
||||
|
||||
if (!recovering) {
|
||||
@@ -128,7 +115,7 @@ parse: function parse(input) {
|
||||
}
|
||||
var errStr = '';
|
||||
if (this.lexer.showPosition) {
|
||||
errStr = 'Parse error on line '+(yylineno+1)+":\n"+this.lexer.showPosition()+'\nExpecting '+expected.join(', ');
|
||||
errStr = 'Parse error on line '+(yylineno+1)+":\n"+this.lexer.showPosition()+"\nExpecting "+expected.join(', ') + ", got '" + this.terminals_[symbol]+ "'";
|
||||
} else {
|
||||
errStr = 'Parse error on line '+(yylineno+1)+": Unexpected " +
|
||||
(symbol == 1 /*EOF*/ ? "end of input" :
|
||||
@@ -242,8 +229,10 @@ parse: function parse(input) {
|
||||
}
|
||||
|
||||
return true;
|
||||
}};/* Jison generated lexer */
|
||||
var lexer = (function(){var lexer = ({EOF:1,
|
||||
}};
|
||||
/* Jison generated lexer */
|
||||
var lexer = (function(){
|
||||
var lexer = ({EOF:1,
|
||||
parseError:function parseError(str, hash) {
|
||||
if (this.yy.parseError) {
|
||||
this.yy.parseError(str, hash);
|
||||
@@ -279,6 +268,9 @@ more:function () {
|
||||
this._more = true;
|
||||
return this;
|
||||
},
|
||||
less:function (n) {
|
||||
this._input = this.match.slice(n) + this._input;
|
||||
},
|
||||
pastInput:function () {
|
||||
var past = this.matched.substr(0, this.matched.length - this.match.length);
|
||||
return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, "");
|
||||
@@ -303,6 +295,8 @@ next:function () {
|
||||
|
||||
var token,
|
||||
match,
|
||||
tempMatch,
|
||||
index,
|
||||
col,
|
||||
lines;
|
||||
if (!this._more) {
|
||||
@@ -311,26 +305,31 @@ next:function () {
|
||||
}
|
||||
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;
|
||||
tempMatch = this._input.match(this.rules[rules[i]]);
|
||||
if (tempMatch && (!match || tempMatch[0].length > match[0].length)) {
|
||||
match = tempMatch;
|
||||
index = i;
|
||||
if (!this.options.flex) break;
|
||||
}
|
||||
}
|
||||
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.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[index],this.conditionStack[this.conditionStack.length-1]);
|
||||
if (this.done && this._input) this.done = false;
|
||||
if (token) return token;
|
||||
else return;
|
||||
}
|
||||
if (this._input === "") {
|
||||
return this.EOF;
|
||||
} else {
|
||||
@@ -354,7 +353,14 @@ popState:function popState() {
|
||||
},
|
||||
_currentRules:function _currentRules() {
|
||||
return this.conditions[this.conditionStack[this.conditionStack.length-1]].rules;
|
||||
},
|
||||
topState:function () {
|
||||
return this.conditionStack[this.conditionStack.length-2];
|
||||
},
|
||||
pushState:function begin(condition) {
|
||||
this.begin(condition);
|
||||
}});
|
||||
lexer.options = {};
|
||||
lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) {
|
||||
|
||||
var YYSTATE=YY_START
|
||||
@@ -389,8 +395,12 @@ case 13:return 'INVALID'
|
||||
break;
|
||||
}
|
||||
};
|
||||
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/,/^$/,/^./];
|
||||
lexer.conditions = {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13],"inclusive":true}};return lexer;})()
|
||||
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)/,/^(?:$)/,/^(?:.)/];
|
||||
lexer.conditions = {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13],"inclusive":true}};
|
||||
|
||||
|
||||
;
|
||||
return lexer;})()
|
||||
parser.lexer = lexer;
|
||||
return parser;
|
||||
})();
|
||||
|
||||
13
package.json
13
package.json
@@ -8,7 +8,7 @@
|
||||
"lint",
|
||||
"jsonlint"
|
||||
],
|
||||
"version": "1.0.1",
|
||||
"version": "1.4.0",
|
||||
"preferGlobal": true,
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -22,9 +22,11 @@
|
||||
"jsonlint": "lib/cli.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": "0.4 || 0.5"
|
||||
"node": ">= 0.6"
|
||||
},
|
||||
"dependencies": {
|
||||
"nomnom": ">= 1.x.x"
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"test": "*",
|
||||
"jison": "*",
|
||||
@@ -33,5 +35,6 @@
|
||||
"scripts": {
|
||||
"test": "node test/all-tests.js"
|
||||
},
|
||||
"homepage": "http://zaach.github.com/jsonlint/"
|
||||
}
|
||||
"homepage": "http://zaach.github.com/jsonlint/",
|
||||
"optionalDependencies": {}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,24 @@
|
||||
esc "\\"
|
||||
int "-"?([0-9]|[1-9][0-9]+)
|
||||
exp ([eE][-+]?[0-9]+)
|
||||
frac ("."[0-9]+)
|
||||
exp [eE][-+]?[0-9]+
|
||||
frac "."[0-9]+
|
||||
|
||||
%%
|
||||
\s+ {/* skip whitespace */}
|
||||
{int}{frac}?{exp}?\b {return 'NUMBER';}
|
||||
'"'("\\"["bfnrt/{esc}]|"\\u"[a-fA-F0-9]{4}|[^\0-\x09\x0a-\x1f"{esc}])*'"' {yytext = yytext.substr(1,yyleng-2); return 'STRING';}
|
||||
"{" %{ return '{' %}
|
||||
"}" %{ return '}' %}
|
||||
"[" {return '['}
|
||||
"]" {return ']'}
|
||||
"," {return ','}
|
||||
":" {return ':'}
|
||||
"true" {return 'TRUE'}
|
||||
"false" {return 'FALSE'}
|
||||
"null" {return 'NULL'}
|
||||
<<EOF>> {return 'EOF'}
|
||||
. {return 'INVALID'}
|
||||
\s+ /* skip whitespace */
|
||||
|
||||
{int}{frac}?{exp}?\b return 'NUMBER'
|
||||
\"(?:'\\'[\\"bfnrt/]|'\\u'[a-fA-F0-9]{4}|[^\\\0-\x09\x0a-\x1f"])*\" yytext = yytext.substr(1,yyleng-2); return 'STRING'
|
||||
|
||||
"{" return '{'
|
||||
"}" return ''
|
||||
"[" return '['
|
||||
"]" return ']'
|
||||
"," return ','
|
||||
":" return ':'
|
||||
"true" return 'TRUE'
|
||||
"false" return 'FALSE'
|
||||
"null" return 'NULL'
|
||||
<<EOF>> return 'EOF'
|
||||
. return 'INVALID'
|
||||
|
||||
%%
|
||||
|
||||
|
||||
@@ -1,20 +1,15 @@
|
||||
%start JSONText
|
||||
|
||||
/*
|
||||
/*
|
||||
ECMA-262 5th Edition, 15.12.1 The JSON Grammar.
|
||||
Modified to forbid top level primitives.
|
||||
*/
|
||||
|
||||
|
||||
/* author: Zach Carter */
|
||||
|
||||
|
||||
%start JSONText
|
||||
|
||||
%%
|
||||
|
||||
JSONString
|
||||
: STRING
|
||||
{$$ = yytext;}
|
||||
{$$ = yytext.replace(/\\\\/g, "\\");}
|
||||
;
|
||||
|
||||
JSONNumber
|
||||
@@ -35,25 +30,17 @@ JSONBooleanLiteral
|
||||
;
|
||||
|
||||
JSONText
|
||||
: JSONObject EOF
|
||||
{return $$ = $1;}
|
||||
| JSONArray EOF
|
||||
: JSONValue EOF
|
||||
{return $$ = $1;}
|
||||
;
|
||||
|
||||
JSONValue
|
||||
: JSONNullLiteral
|
||||
{$$ = $1;}
|
||||
| JSONBooleanLiteral
|
||||
{$$ = $1;}
|
||||
| JSONString
|
||||
{$$ = $1;}
|
||||
| JSONNumber
|
||||
{$$ = $1;}
|
||||
| JSONObject
|
||||
{$$ = $1;}
|
||||
| JSONArray
|
||||
{$$ = $1;}
|
||||
;
|
||||
|
||||
JSONObject
|
||||
|
||||
@@ -7,6 +7,11 @@ exports["test object"] = function () {
|
||||
assert.deepEqual(parser.parse(json), {"foo": "bar"});
|
||||
};
|
||||
|
||||
exports["test escaped backslash"] = function () {
|
||||
var json = '{"foo": "\\\\"}';
|
||||
assert.deepEqual(parser.parse(json), {"foo": "\\"});
|
||||
};
|
||||
|
||||
exports["test string with escaped line break"] = function () {
|
||||
var json = '{"foo": "bar\\nbar"}';
|
||||
assert.deepEqual(parser.parse(json), {"foo": "bar\\nbar"});
|
||||
@@ -17,9 +22,24 @@ exports["test string with line break"] = function () {
|
||||
assert["throws"](function () {parser.parse(json)}, "should throw error");
|
||||
};
|
||||
|
||||
exports["test a json payload should be an object or array not a string"] = function () {
|
||||
var json = fs.readFileSync(__dirname + "/fails/1.json").toString();
|
||||
assert["throws"](function () {parser.parse(json)}, "should throw error");
|
||||
exports["test string literal"] = function () {
|
||||
var json = '"foo"';
|
||||
assert.equal(parser.parse(json), "foo");
|
||||
};
|
||||
|
||||
exports["test number literal"] = function () {
|
||||
var json = '1234';
|
||||
assert.equal(parser.parse(json), 1234);
|
||||
};
|
||||
|
||||
exports["test null literal"] = function () {
|
||||
var json = '1234';
|
||||
assert.equal(parser.parse(json), 1234);
|
||||
};
|
||||
|
||||
exports["test boolean literal"] = function () {
|
||||
var json = 'true';
|
||||
assert.equal(parser.parse(json), true);
|
||||
};
|
||||
|
||||
exports["test unclosed array"] = function () {
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
"A JSON payload should be an object or array, not a string."
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user