Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#282,#283 - support specifying moduleName on CLI, support generate names... #284

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ function getCommandlineOptions () {
metavar : 'TYPE',
help : 'The type of module to generate (commonjs, amd, js)'
})
.option('module-name', {
abbr : 'n',
metavar : 'NAME',
help : 'The name of the generated parser object, namespace supported'
})
.option('parser-type', {
abbr : 'p',
default:
Expand Down Expand Up @@ -93,15 +98,10 @@ cli.main = function cliMain(opts) {
// if they aren't specified.
var name = path.basename((opts.outfile || opts.file));

name = name.replace(/\..*$/g, '');
// get the base name (i.e. the file name without extension)
name = path.basename(name, path.extname(name));

opts.outfile = opts.outfile || (name + '.js');
if (!opts.moduleName && name) {
opts.moduleName = name.replace(/-\w/g,
function (match) {
return match.charAt(1).toUpperCase();
});
}

var parser = processGrammar(raw, lex, opts);
fs.writeFileSync(opts.outfile, parser);
Expand Down Expand Up @@ -148,6 +148,9 @@ cli.generateParserString = function generateParserString(opts, grammar) {
if (!settings.moduleType) {
settings.moduleType = opts['module-type'];
}
if (!settings.moduleName) {
settings.moduleName = opts['module-name'];
}

var generator = new jison.Generator(grammar, settings);
return generator.generate(settings);
Expand Down
28 changes: 22 additions & 6 deletions lib/jison.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,10 +928,7 @@ lrGeneratorMixin.generate = function parser_generate (opt) {
opt = typal.mix.call({}, this.options, opt);
var code = "";

// check for illegal identifier
if (!opt.moduleName || !opt.moduleName.match(/^[A-Za-z_$][A-Za-z0-9_$]*$/)) {
opt.moduleName = "parser";
}
opt.moduleName = opt.moduleName || "parser";
switch (opt.moduleType) {
case "js":
code = this.generateModule(opt);
Expand Down Expand Up @@ -1054,8 +1051,27 @@ lrGeneratorMixin.generateModule = function generateModule (opt) {
+ " recoverable: (boolean: TRUE when the parser has a error recovery rule available for this particular error)\n"
+ " }\n"
+ "*/\n";
out += (moduleName.match(/\./) ? moduleName : "var "+moduleName) +
" = " + this.generateModuleExpr();

var self = this;
var _generateNamespace = function (namespaces, previousNamespace, callback) {
var subModuleName = namespaces.shift();
if (subModuleName != null) {
var moduleName = previousNamespace == null ? subModuleName : previousNamespace + "." + subModuleName;
if (namespaces.length > 0) {
return "var " + subModuleName + ";\n"
+ "(function (" + subModuleName + ") {\n"
+ _generateNamespace(namespaces, subModuleName, callback)
+ "})(" + subModuleName + (previousNamespace == null ? "" : " = " + moduleName) + " || (" + moduleName + " = {}));\n";
}
return callback(moduleName);
}
return "";
};

out += _generateNamespace(moduleName.split('.'), null, function (moduleName) {
return (moduleName.match(/\./) ? moduleName : "var "+moduleName) +
" = " + self.generateModuleExpr() + "\n";
});

return out;
};
Expand Down