jashkenas--coffeescript/vendor/jison/examples/json.js

81 lines
2.3 KiB
JavaScript

var Generator = require("jison").Generator;
var system = require("system");
var fs = require("file");
exports.grammar = {
"comment": "ECMA-262 5th Edition, 15.12.1 The JSON Grammar.",
"author": "Zach Carter",
"lex": {
"macros": {
"digit": "[0-9]",
"esc": "\\\\",
"int": "-?(?:[0-9]|[1-9][0-9]+)",
"exp": "(?:[eE][-+]?[0-9]+)",
"frac": "(?:\\.[0-9]+)"
},
"rules": [
["\\s+", "/* skip whitespace */"],
["{int}{frac}?{exp}?\\b", "return 'NUMBER';"],
["\"(?:{esc}[\"bfnrt/{esc}]|{esc}u[a-fA-F0-9]{4}|[^\"{esc}])*\"", "yytext = yytext.substr(1,yyleng-2); return 'STRING';"],
["\\{", "return '{'"],
["\\}", "return '}'"],
["\\[", "return '['"],
["\\]", "return ']'"],
[",", "return ','"],
[":", "return ':'"],
["true\\b", "return 'TRUE'"],
["false\\b", "return 'FALSE'"],
["null\\b", "return 'NULL'"]
]
},
"tokens": "STRING NUMBER { } [ ] , : TRUE FALSE NULL",
"start": "JSONText",
"bnf": {
"JSONString": [ "STRING" ],
"JSONNumber": [ "NUMBER" ],
"JSONBooleanLiteral": [ "TRUE", "FALSE" ],
"JSONText": [ "JSONValue" ],
"JSONValue": [ "JSONNullLiteral",
"JSONBooleanLiteral",
"JSONString",
"JSONNumber",
"JSONObject",
"JSONArray" ],
"JSONObject": [ "{ }",
"{ JSONMemberList }" ],
"JSONMember": [ "JSONString : JSONValue" ],
"JSONMemberList": [ "JSONMember",
"JSONMemberList , JSONMember" ],
"JSONArray": [ "[ ]",
"[ JSONElementList ]" ],
"JSONElementList": [ "JSONValue",
"JSONElementList , JSONValue" ]
}
};
var options = {type: "slr", moduleType: "commonjs", moduleName: "jsoncheck"};
exports.main = function main (args) {
var cwd = fs.path(fs.cwd()),
code = new Generator(exports.grammar, options).generate(),
stream = cwd.join(options.moduleName+".js").open("w");
stream.print(code).close();
};
if (require.main === module)
exports.main(system.args);