Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
acodercc committed Oct 28, 2014
1 parent 8a9959d commit 96cec0d
Show file tree
Hide file tree
Showing 10 changed files with 631 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules
refs
19 changes: 19 additions & 0 deletions fastjson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(function(global){

var parser = fastparser;
var JSONFast = {
parse: function(str){
if(parser.parse(str)){
return parser.$$;
}else{
return {};
}
},
stringify: function(){
}
};

global.JSONFast = JSONFast;


})(this);
22 changes: 22 additions & 0 deletions generate-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

var fs = require('fs');

var Generator = require('jsbison');

Generator.lexParser.parse(fs.readFileSync('./json.l').toString());


Generator.bnfParser.parse(fs.readFileSync('./json.y').toString());

var bnfcfg = Generator.bnfParser.$$;
bnfcfg.lex = Generator.lexParser.$$;
bnfcfg.type = 'LR(1)';


debugger
var generator = new Generator(bnfcfg);

var code = generator.generate();


fs.writeFileSync('./parser.js', code);
21 changes: 21 additions & 0 deletions json.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

%%

\/\/[^\n]* /* skip singleline comment */
\/\*(.|\n|\r)*?\*\/ /* skip multiline comment */
"(?:\\[bfnrt\/"]|\\u[a-fA-F0-9]{4}|[^"\\])*" this.yytext=this.yytext.slice(1, this.yyleng-1);return "STRING";
\s+
\r\n
((0|[1-9][0-9]*)(\.[0-9]*)?|\.[0-9]+)([eE][+-]?[0-9]+)?|[0][xX][0-9a-fA-F]+ return "NUMBER"
null return "NULL";
true return "TRUE";
false return "FALSE";
: return ":";
, return ",";
\[ return "[";
\] return "]";
\{ return "{";
\} return "}";
\s+ /* skip whitespace */
\n /* skip lineterminal */
. return "INVALID";
105 changes: 105 additions & 0 deletions json.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
%start Json
%%

Json :
JsonValue $end
{
this.$$ = $1;
}
;

JsonValue :
NULL
{
this.$$ = null;
}
| BooleanLiteral
{
this.$$ = $1;
}
| STRING
{
this.$$ = $1;
}
| NUMBER
{
this.$$ = parseFloat($1);
}
| JSONObject
{
this.$$ = $1;
}
| JSONArray
{
this.$$ = $1;
}
;

BooleanLiteral :
TRUE
{
this.$$ = true;
}
| FALSE
{
this.$$ = false;
}
;

JSONObject :
{ }
{
this.$$ = {};
}
| { JSONMemberList }
{
this.$$ = $2;
}
;

JSONMemberList :
JSONMemberList , JSONMember
{
this.$$ = $1;
this.$$ = _.merge(this.$$, $3);
}
| JSONMember
{
this.$$ = $1;
}
;
JSONMember :
STRING : JsonValue
{
this.$$ = {};
this.$$[$1] = $3;
}
;

JSONArray :
[ ]
{
this.$$ = [];
}
| [ JSONElementList ]
{
this.$$ = $2;
}
;
JSONElementList:
JSONElementList , JsonValue
{
this.$$ = $1;
this.$$.push($3);
}
| JsonValue
{
this.$$ = [$1];
}
;



%%

global.fastparser = parser;
Empty file added nodes.js
Empty file.
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "JSONFast",
"description": "基于jsbison生成的JSON的快速解析器。",
"version": "0.1.0",
"homepage": "https://github.com/takumi4ichi/JSONFast",
"author": {
"name": "takumi4ichi",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "[email protected]:takumi4ichi/jsonfast.git"
},
"licenses": [
{
"type": "MIT",
"url": "/blob/master/LICENSE"
}
],
"engines": {
"node": ">= 0.8.0"
},
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-clean": "~0.5.0",
"grunt-contrib-copy": "~0.4.1",
"grunt-contrib-uglify": "~0.2.0",
"grunt-contrib-watch": "~0.4.0",
"grunt-contrib-jshint": "~0.10.0",
"jsbison": "~0.1.8"
},
"dependencies": {
"lodash": "^2.4.1"
},
"keywords": [
"lib",
"parser",
"javascript",
"json"
]
}
Loading

0 comments on commit 96cec0d

Please sign in to comment.