2016-10-22 18:48:54 +00:00
|
|
|
// Generated by CoffeeScript 2.0.0-alpha
|
2010-07-24 15:31:43 +00:00
|
|
|
(function() {
|
2013-04-27 22:56:44 +00:00
|
|
|
var LONG_FLAG, MULTI_FLAG, OPTIONAL, OptionParser, SHORT_FLAG, buildRule, buildRules, normalizeArguments, repeat;
|
|
|
|
|
|
|
|
repeat = require('./helpers').repeat;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-12-23 18:50:52 +00:00
|
|
|
exports.OptionParser = OptionParser = (function() {
|
2015-01-30 19:33:03 +00:00
|
|
|
function OptionParser(rules, banner) {
|
|
|
|
this.banner = banner;
|
2010-11-12 02:48:08 +00:00
|
|
|
this.rules = buildRules(rules);
|
|
|
|
}
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-05-15 03:40:04 +00:00
|
|
|
OptionParser.prototype.parse = function(args) {
|
2015-01-30 19:33:03 +00:00
|
|
|
var arg, i, isOption, j, k, len, len1, matchedRule, options, originalArgs, pos, ref, rule, seenNonOptionArg, skippingArgument, value;
|
2010-02-28 00:46:45 +00:00
|
|
|
options = {
|
2012-01-26 00:47:03 +00:00
|
|
|
"arguments": []
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
2011-12-21 19:06:34 +00:00
|
|
|
skippingArgument = false;
|
2011-10-06 08:11:41 +00:00
|
|
|
originalArgs = args;
|
2010-06-12 23:05:13 +00:00
|
|
|
args = normalizeArguments(args);
|
2015-01-30 19:33:03 +00:00
|
|
|
for (i = j = 0, len = args.length; j < len; i = ++j) {
|
2010-10-01 22:26:37 +00:00
|
|
|
arg = args[i];
|
2011-12-21 19:06:34 +00:00
|
|
|
if (skippingArgument) {
|
|
|
|
skippingArgument = false;
|
|
|
|
continue;
|
|
|
|
}
|
2010-12-18 14:29:04 +00:00
|
|
|
if (arg === '--') {
|
2011-10-06 18:51:27 +00:00
|
|
|
pos = originalArgs.indexOf('--');
|
2012-01-26 00:47:03 +00:00
|
|
|
options["arguments"] = options["arguments"].concat(originalArgs.slice(pos + 1));
|
2010-12-18 14:29:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-06-12 23:05:13 +00:00
|
|
|
isOption = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
|
2012-01-26 00:47:03 +00:00
|
|
|
seenNonOptionArg = options["arguments"].length > 0;
|
|
|
|
if (!seenNonOptionArg) {
|
|
|
|
matchedRule = false;
|
2015-01-30 19:33:03 +00:00
|
|
|
ref = this.rules;
|
|
|
|
for (k = 0, len1 = ref.length; k < len1; k++) {
|
|
|
|
rule = ref[k];
|
2012-01-26 00:47:03 +00:00
|
|
|
if (rule.shortFlag === arg || rule.longFlag === arg) {
|
|
|
|
value = true;
|
|
|
|
if (rule.hasArgument) {
|
|
|
|
skippingArgument = true;
|
|
|
|
value = args[i + 1];
|
|
|
|
}
|
|
|
|
options[rule.name] = rule.isList ? (options[rule.name] || []).concat(value) : value;
|
|
|
|
matchedRule = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isOption && !matchedRule) {
|
2016-11-28 14:05:51 +00:00
|
|
|
throw new Error(`unrecognized option: ${arg}`);
|
2010-02-28 00:46:45 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-10 18:57:45 +00:00
|
|
|
if (seenNonOptionArg || !isOption) {
|
|
|
|
options["arguments"].push(arg);
|
|
|
|
}
|
2010-02-14 20:16:33 +00:00
|
|
|
}
|
2010-02-28 00:46:45 +00:00
|
|
|
return options;
|
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-05-15 03:40:04 +00:00
|
|
|
OptionParser.prototype.help = function() {
|
2015-01-30 19:33:03 +00:00
|
|
|
var j, len, letPart, lines, ref, rule, spaces;
|
2011-01-15 16:04:50 +00:00
|
|
|
lines = [];
|
2012-04-10 18:57:45 +00:00
|
|
|
if (this.banner) {
|
2016-11-28 14:05:51 +00:00
|
|
|
lines.unshift(`${this.banner}\n`);
|
2012-04-10 18:57:45 +00:00
|
|
|
}
|
2015-01-30 19:33:03 +00:00
|
|
|
ref = this.rules;
|
|
|
|
for (j = 0, len = ref.length; j < len; j++) {
|
|
|
|
rule = ref[j];
|
2010-06-12 23:05:13 +00:00
|
|
|
spaces = 15 - rule.longFlag.length;
|
2013-04-27 22:56:44 +00:00
|
|
|
spaces = spaces > 0 ? repeat(' ', spaces) : '';
|
2010-06-12 23:05:13 +00:00
|
|
|
letPart = rule.shortFlag ? rule.shortFlag + ', ' : ' ';
|
2010-08-07 12:02:16 +00:00
|
|
|
lines.push(' ' + letPart + rule.longFlag + spaces + rule.description);
|
2010-02-14 20:16:33 +00:00
|
|
|
}
|
2016-11-28 14:05:51 +00:00
|
|
|
return `\n${lines.join('\n')}\n`;
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-02-28 00:46:45 +00:00
|
|
|
return OptionParser;
|
2011-12-14 15:39:20 +00:00
|
|
|
|
2010-12-23 18:50:52 +00:00
|
|
|
})();
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2012-01-26 00:47:03 +00:00
|
|
|
LONG_FLAG = /^(--\w[\w\-]*)/;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2012-01-26 00:47:03 +00:00
|
|
|
SHORT_FLAG = /^(-\w)$/;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-02-26 00:06:08 +00:00
|
|
|
MULTI_FLAG = /^-(\w{2,})/;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-08-08 03:33:35 +00:00
|
|
|
OPTIONAL = /\[(\w+(\*?))\]/;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-06-12 23:05:13 +00:00
|
|
|
buildRules = function(rules) {
|
2015-01-30 19:33:03 +00:00
|
|
|
var j, len, results, tuple;
|
|
|
|
results = [];
|
|
|
|
for (j = 0, len = rules.length; j < len; j++) {
|
|
|
|
tuple = rules[j];
|
2012-04-10 18:57:45 +00:00
|
|
|
if (tuple.length < 3) {
|
|
|
|
tuple.unshift(null);
|
|
|
|
}
|
Compile splats in arrays and function calls to ES2015 splats (#4353)
Rather than compiling splats to arrays built using `Array#concat`, splats
are now compiled directly to ES2015 splats, e.g.
f foo, arguments..., bar
[ foo, arguments..., bar ]
Which used to be compiled to:
f.apply(null, [foo].concat(slice.call(arguments), [bar]));
[foo].concat(slice.call(arguments), [bar]);
Is now compiled to:
f(foo, ...arguments, bar);
[ foo, ...arguments, bar ];
2016-11-06 16:30:04 +00:00
|
|
|
results.push(buildRule(...tuple));
|
2010-02-14 20:16:33 +00:00
|
|
|
}
|
2015-01-30 19:33:03 +00:00
|
|
|
return results;
|
2010-02-14 20:16:33 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
[CS2] Output ES2015 arrow functions, default parameters, rest parameters (#4311)
* Eliminate wrapper around “bound” (arrow) functions; output `=>` for such functions
* Remove irrelevant (and breaking) tests
* Minor cleanup
* When a function parameter is a splat (i.e., it uses the ES2015 rest parameter syntax) output that parameter as ES2015
* Rearrange function parameters when one of the parameters is a splat and isn’t the last parameter (very WIP)
* Handle params like `@param`, adding assignment expressions for them when they appear; ensure splat parameter is last
* Add parameter names (not a text like `'\nValue IdentifierLiteral: a'`) to the scope, so that parameters can’t be deleted; move body-related lines together; more explanation of what’s going on
* For parameters with a default value, correctly add the parameter name to the function scope
* Handle expansions in function parameters: when an expansion is found, set the parameters to only be the original parameters left of the expansion, then an `...args` parameter; and in the function body define variables for the parameters to the right of the expansion, including setting default values
* Handle splat parameters the same way we handle expansions: if a splat parameter is found, it becomes the last parameter in the function definition, and all following parameters get declared in the function body. Fix the splat/rest parameter values after the post-splat parameters have been extracted from it. Clean up `Code.compileNode` so that we loop through the parameters only once, and we create all expressions using calls like `new IdentifierLiteral` rather than `@makeCode`.
* Fix parameter name when a parameter is a splat attached to `this` (e.g. `@param...`)
* Rather than assigning post-splat parameters based on index, use slice; passes test “Functions with splats being called with too few arguments”
* Dial back our w00t indentation
* Better parsing of parameter names (WIP)
* Refactor processing of splat/expansion parameters
* Fix assignment of default parameters for parameters that come after a splat
* Better check for whether a param is attached to `this`
* More understandable variable names
* For parameters after a splat or expansion, assign them similar to the 1.x destructuring method of using `arguments`, except only concern ourselves with the post-splat parameters instead of all parameters; and use the splat/expansion parameter name, since `arguments` in ES fat arrow functions refers to the parent function’s `arguments` rather than the fat arrow function’s arguments/parameters
* Don’t add unnamed parameters (like `[]` as a parameter) to the function scope
* Disallow multiple splat/expansion parameters in function definitions; disallow lone expansion parameters
* Fix `this` params not getting assigned if the parameter is after a splat parameter
* Allow names of function parameters attached to `this` to be reserved words
* Always add a statement to the function body defining a variable with its default value, if it has one, if the variable `== null`; this covers the case when ES doesn’t apply the default value when `null` is passed in as a value, but CoffeeScript expects `null` and `undefined` to act interchangeably
* Aftermath of having both `undefined` and `null` trigger the use of default values for parameters with default values
* More careful parsing of destructured parameters
* Fall back to processing destructured parameters in the function body, to account for `this` or default values within destructured objects
* Clean up comments
* Restore new bare function test, minus the arrow function part of it
* Test that bound/arrow functions aren’t overwriting the `arguments` object, which should refer to the parent scope’s `arguments` (like `this`)
* Follow ES2015 spec for parameter default values: `null` gets assigned as as `null`, not the default value
* Mimic ES default parameters behavior for parameters after a splat or expansion parameter
* Bound functions cannot be generators: remove no-longer-relevant test, add check to throw error if `yield` appears inside a bound (arrow) function
* Error for bound generator functions should underline the `yield`
2016-10-26 05:26:13 +00:00
|
|
|
buildRule = function(shortFlag, longFlag, description, options = {}) {
|
2010-02-14 20:16:33 +00:00
|
|
|
var match;
|
2010-06-12 23:05:13 +00:00
|
|
|
match = longFlag.match(OPTIONAL);
|
|
|
|
longFlag = longFlag.match(LONG_FLAG)[1];
|
2010-02-14 20:16:33 +00:00
|
|
|
return {
|
2010-06-12 23:05:13 +00:00
|
|
|
name: longFlag.substr(2),
|
|
|
|
shortFlag: shortFlag,
|
|
|
|
longFlag: longFlag,
|
2010-02-14 20:16:33 +00:00
|
|
|
description: description,
|
Add command-line compiler hooks. To invoke, pass a file after -r and listen for any of these events: 'compile', 'success' and 'exception'. Example:
coffee -e -r ./snarl 'Hello!'
Contents of 'snarl.coffee' in the working directory:
http = require 'http'
CoffeeScript.on 'exception', (err) ->
client = http.createClient 9889, 'localhost'
request = client.request 'GET', '/?d={"action":1,"applicationName":"CoffeeScript","title":' + JSON.stringify(err.message) + ',"description":' + JSON.stringify(err.stack) + ',"priority":3}'
request.end()
err.handled = yes
To examine arguments available for each event (for debugging and getting started), use `puts JSON.stringify arguments`.
See http://nodejs.org/api.html#modules-309 and NODE_PATH for more details on how -r looks for files.
2010-08-07 17:24:37 +00:00
|
|
|
hasArgument: !!(match && match[1]),
|
2010-08-08 03:33:35 +00:00
|
|
|
isList: !!(match && match[2])
|
2010-02-14 20:16:33 +00:00
|
|
|
};
|
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-06-12 23:05:13 +00:00
|
|
|
normalizeArguments = function(args) {
|
2015-01-30 19:33:03 +00:00
|
|
|
var arg, j, k, l, len, len1, match, ref, result;
|
2010-02-26 00:06:08 +00:00
|
|
|
args = args.slice(0);
|
|
|
|
result = [];
|
2015-01-30 19:33:03 +00:00
|
|
|
for (j = 0, len = args.length; j < len; j++) {
|
|
|
|
arg = args[j];
|
2010-08-14 21:52:37 +00:00
|
|
|
if (match = arg.match(MULTI_FLAG)) {
|
2015-01-30 19:33:03 +00:00
|
|
|
ref = match[1].split('');
|
|
|
|
for (k = 0, len1 = ref.length; k < len1; k++) {
|
|
|
|
l = ref[k];
|
2010-02-26 00:06:08 +00:00
|
|
|
result.push('-' + l);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.push(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
2011-12-14 15:39:20 +00:00
|
|
|
|
2010-09-21 07:53:58 +00:00
|
|
|
}).call(this);
|