Browse files

remove remaining narwhal code bits

  • Loading branch information...
1 parent fd89108 commit 800e1d4c35a5972435b024eac60b694da4f7d107 @zaach committed Jan 27, 2013
Showing with 91 additions and 118 deletions.
  1. +81 −71 lib/cli.js
  2. +9 −9 lib/jison.js
  3. +0 −37 lib/util/io.js
  4. +1 −1 package.json
View
152 lib/cli.js
@@ -1,96 +1,106 @@
#!/usr/bin/env node
-var JISON = require('../jison'),
- IO = require('./util/io'),
- nomnom = require('nomnom');
+var jison = require('./jison.js');
+var nomnom = require('nomnom');
+var fs = require('fs');
+var path = require('path');
+var ebnfParser = require('ebnf-parser');
+var lexParser = require('lex-parser');
-var opts = [
- { name: 'file',
+var version = require('../package.json').version;
+
+var opts = require("nomnom")
+ .script('jison')
+ .option('file', {
+ flag: true,
position: 0,
- help: '\t\t\tGrammar file'
- },
- { name: 'lexfile',
+ help: 'file containing a grammar'
+ })
+ .option('lexfile', {
+ flag: true,
position: 1,
- help: '\t\tLexical grammar file (optional)'
- },
- { name: 'outfile',
- string: '-o FILE, --output-file=FILE',
+ help: 'file containing a lexical grammar'
+ })
+ .option('outfile', {
+ abbr: 'o',
+ metavar: 'FILE',
help: 'Filename and base module name of the generated parser'
- },
- { name: 'debug',
- string: '-t, --debug',
- "default": false,
- help: '\t\tUse debug mode'
- },
- { name: 'moduleType',
- string: '-m, --module-type',
- "default": "commonjs",
- help: '\tThe type of module to generate (commonjs, amd, js)'
- },
- { name: 'version',
- string: '-V, --version',
- help: '\t\tVersion number'
- }
-];
+ })
+ .option('debug', {
+ abbr: 't',
+ default: false,
+ help: 'Debug mode'
+ })
+ .option('module-type', {
+ abbr: 't',
+ default: 'commonjs',
+ metavar: 'TYPE',
+ help: 'The type of module to generate (commonjs, amd, js)'
+ })
+ .option('version', {
+ abbr: 'V',
+ flag: true,
+ help: 'print version and exit',
+ callback: function() {
+ return version;
+ }
+ })
+ .parse();
-var args;
-exports.main = function (argv) {
- argv.shift();
- args = nomnom.parseArgs(opts, {script: 'jison', printFunc: function (s) {IO.p(s); IO.exit(0);}}, argv);
- if (args.version) {
- IO.p(readVersion());
- IO.exit(0);
- }
+exports.main = function () {
+ if (opts.file) {
+ var raw = fs.readFileSync(path.normalize(opts.file), 'utf8');
+ var name = path.basename((opts.outfile||opts.file)).replace(/\..*$/g,'');
+ var lex;
- if (args.file) {
- var raw = IO.read(IO.join(IO.cwd(),args.file)),
- name = IO.basename((args.outfile||args.file)).replace(/\..*$/g,''),
- lex;
- raw = raw.replace(/\r\n/g, '\n');
- if (args.lexfile) {
- lex = IO.read(IO.join(IO.cwd(),args.lexfile));
+ if (opts.lexfile) {
+ lex = fs.readFileSync(path.normalize(opts.lexfile), 'utf8');
}
- IO.write(args.outfile||(name+'.js'), processGrammar(raw, lex, name));
+
+ fs.writeFileSync(opts.outfile||(name + '.js'), processGrammar(raw, lex, name));
} else {
- IO.stdin(function (raw) {
- IO.stdout(processGrammar(raw));
+ readin(function (raw) {
+ console.log(processGrammar(raw));
});
}
-}
-
-function readVersion () {
- var pack;
- try {
- pack = IO.read(IO.join(__dirname,'..','..','package.json'));
- } catch(e) {
- var fs = require("file");
- pack = fs.path(fs.dirname(module.id)).canonical().join('..','package.json')
- .read({charset: "utf-8"});
- }
- return JSON.parse(pack).version;
-}
+};
-function processGrammar (rawGrammar, lex, name) {
+function processGrammar (file, lexFile, name) {
var grammar;
try {
- grammar = require("./bnf").parse(rawGrammar);
+ grammar = ebnfParser.parse(file);
} catch (e) {
try {
- grammar = JSON.parse(rawGrammar);
+ grammar = JSON.parse(file);
} catch (e2) {
throw e;
}
}
- var opt = grammar.options || {};
- if (lex) grammar.lex = require("./jisonlex").parse(lex);
- opt.debug = args.debug;
- if (!opt.moduleType) opt.moduleType = args.moduleType;
- if (!opt.moduleName && name) opt.moduleName = name.replace(/-\w/g, function (match){ return match.charAt(1).toUpperCase(); });
- var generator = new JISON.Generator(grammar, opt);
- return generator.generate(opt);
+ var settings = grammar.options || {};
+ if (lexFile) grammar.lex = lexParser.parse(lexFile);
+ settings.debug = opts.debug;
+ if (!settings.moduleType) settings.moduleType = opts.moduleType;
+ if (!settings.moduleName && name) settings.moduleName = name.replace(/-\w/g, function (match){ return match.charAt(1).toUpperCase(); });
+
+ var generator = new jison.Generator(grammar, settings);
+ return generator.generate(settings);
}
-if (typeof process !== 'undefined' || require.main === module)
- exports.main(IO.args);
+function readin (cb) {
+ var stdin = process.openStdin(),
+ data = '';
+
+ stdin.setEncoding('utf8');
+ stdin.addListener('data', function (chunk) {
+ data += chunk;
+ });
+ stdin.addListener('end', function () {
+ cb(data);
+ });
+}
+
+if (require.main === module)
+ exports.main();
+
View
18 lib/jison.js
@@ -9,7 +9,10 @@ var ebnfParser = require('ebnf-parser');
var JSONSelect = require('JSONSelect');
var Reflect = require('reflect');
+var version = require('../package.json').version;
+
var Jison = exports.Jison = exports;
+Jison.version = version;
// detect print
if (typeof console !== 'undefined' && console.log) {
@@ -927,7 +930,7 @@ lrGeneratorMixin.generateCommonJSModule = function generateCommonJSModule (opt)
+ "\nexports.parse = function () { return "+moduleName+".parse.apply("+moduleName+", arguments); };"
+ "\nexports.main = "+ String(opt.moduleMain || commonjsMain) + ";"
+ "\nif (typeof module !== 'undefined' && require.main === module) {\n"
- + " exports.main(typeof process !== 'undefined' ? process.argv.slice(1) : require(\"system\").args);\n}"
+ + " exports.main(process.argv.slice(1));\n}"
+ "\n}"
return out;
@@ -936,7 +939,7 @@ lrGeneratorMixin.generateCommonJSModule = function generateCommonJSModule (opt)
lrGeneratorMixin.generateModule = function generateModule (opt) {
opt = typal.mix.call({}, this.options, opt);
var moduleName = opt.moduleName || "parser";
- var out = "/* Jison generated parser */\n";
+ var out = "/* parser generated by jison " + version + " */\n";
out += (moduleName.match(/\./) ? moduleName : "var "+moduleName)+" = (function(){";
out += "\nvar parser = "+this.generateModule_();
out += "\n"+this.moduleInclude;
@@ -990,14 +993,11 @@ lrGeneratorMixin.generateModule_ = function generateModule_ () {
// default main method for generated commonjs modules
function commonjsMain (args) {
- if (!args[1])
- throw new Error('Usage: '+args[0]+' FILE');
- var source, cwd;
- if (typeof process !== 'undefined') {
- source = require('fs').readFileSync(require('path').resolve(args[1]), "utf8");
- } else {
- source = require("file").path(require("file").cwd()).join(args[1]).read({charset: "utf-8"});
+ if (!args[1]) {
+ console.log('Usage: '+args[0]+' FILE');
+ process.exit(1);
}
+ var source = require('fs').readFileSync(require('path').normalize(args[1]), "utf8");
return exports.parser.parse(source);
}
View
37 lib/util/io.js
@@ -1,37 +0,0 @@
-
-var fs = require('fs');
-var util = require('util');
-
-exports.p = util.puts;
-exports.cwd = process.cwd;
-exports.join = require('path').join;
-exports.basename = require('path').basename;
-exports.args = process.argv.slice(1);
-exports.exit = process.exit;
-exports.resolve = require('url').resolve;
-
-exports.read = function (fname) {
- return fs.readFileSync(fname, "utf8");
-};
-
-exports.write = function (fname, data) {
- fs.writeFileSync(fname, data);
-};
-
-exports.stdin = function (cb) {
- var stdin = process.openStdin(),
- data = '';
-
- stdin.setEncoding('utf8');
- stdin.addListener('data', function (chunk) {
- data += chunk;
- });
- stdin.addListener('end', function () {
- cb(data);
- });
-};
-
-exports.stdout = function (out) {
- process.stdout.write(out);
-};
-
View
2 package.json
@@ -34,7 +34,7 @@
"jison-lex": "0.0.1",
"ebnf-parser": "0.0.1",
"lex-parser": "0.0.1",
- "nomnom": "0.4.3"
+ "nomnom": "1.5.2"
},
"devDependencies": {
"test": "0.4.4",

0 comments on commit 800e1d4

Please sign in to comment.