Skip to content

Commit

Permalink
Generate node compatible parsers.
Browse files Browse the repository at this point in the history
  • Loading branch information
zaach committed Jun 8, 2010
1 parent ab19e54 commit 81400fa
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/jison.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,13 +855,14 @@ lrGeneratorMixin.generate = function parser_generate (opt) {
lrGeneratorMixin.generateCommonJSModule = function generateCommonJSModule (opt) {
opt = typal.mix.call({}, this.options, opt);
var moduleName = opt.moduleName || "parser";
var out = this.generateModule(opt);
out += "\nif (typeof require !== 'undefined') {";
out += "\nexports.parser = "+moduleName+";";
out += "\nexports.parse = function () { return "+moduleName+".parse.apply("+moduleName+", arguments); }";
out += "\nexports.main = "+ String(opt.moduleMain || commonjsMain);
out += "\nif (require.main === module) {\n\texports.main(require(\"system\").args);\n}";
out += "\n}";
var out = this.generateModule(opt)
+ "\nif (typeof require !== 'undefined') {"
+ "\nexports.parser = "+moduleName+";"
+ "\nexports.parse = function () { return "+moduleName+".parse.apply("+moduleName+", arguments); }"
+ "\nexports.main = "+ String(opt.moduleMain || commonjsMain)
+ "\nif (require.main === module) {\n"
+ " exports.main(typeof process !== 'undefined' ? process.argv.slice(1) : require(\"system\").args);\n}"
+ "\n}"

return out;
};
Expand Down Expand Up @@ -902,11 +903,15 @@ lrGeneratorMixin.generateModule_ = function generateModule_ () {

// default main method for generated commonjs modules
function commonjsMain (args) {
var cwd = require("file").path(require("file").cwd());
if (!args[1])
throw new Error('Usage: '+args[0]+' FILE');
var source = cwd.join(args[1]).read({charset: "utf-8"});
exports.parser.parse(source);
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"});
}
return exports.parser.parse(source);
}

// debug mixin for LR parser generators
Expand Down

0 comments on commit 81400fa

Please sign in to comment.