2016-10-22 18:48:54 +00:00
|
|
|
// Generated by CoffeeScript 2.0.0-alpha
|
2010-07-25 07:15:12 +00:00
|
|
|
(function() {
|
2016-10-23 15:37:51 +00:00
|
|
|
var buildLocationData, extend, flatten, marked, ref, repeat, syntaxErrorToString;
|
|
|
|
|
|
|
|
marked = require('marked');
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-09-25 00:29:44 +00:00
|
|
|
exports.starts = function(string, literal, start) {
|
2010-09-25 00:18:47 +00:00
|
|
|
return literal === string.substr(start, literal.length);
|
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-09-25 00:29:44 +00:00
|
|
|
exports.ends = function(string, literal, back) {
|
2010-09-26 14:28:48 +00:00
|
|
|
var len;
|
|
|
|
len = literal.length;
|
|
|
|
return literal === string.substr(string.length - len - (back || 0), len);
|
2010-09-25 00:18:47 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2013-03-05 04:13:46 +00:00
|
|
|
exports.repeat = repeat = function(str, n) {
|
2013-02-26 17:41:01 +00:00
|
|
|
var res;
|
|
|
|
res = '';
|
|
|
|
while (n > 0) {
|
|
|
|
if (n & 1) {
|
|
|
|
res += str;
|
|
|
|
}
|
|
|
|
n >>>= 1;
|
|
|
|
str += str;
|
|
|
|
}
|
|
|
|
return res;
|
2013-02-25 17:41:34 +00:00
|
|
|
};
|
|
|
|
|
2010-09-25 00:29:44 +00:00
|
|
|
exports.compact = function(array) {
|
2015-01-30 19:33:03 +00:00
|
|
|
var i, item, len1, results;
|
|
|
|
results = [];
|
|
|
|
for (i = 0, len1 = array.length; i < len1; i++) {
|
|
|
|
item = array[i];
|
2012-04-10 18:57:45 +00:00
|
|
|
if (item) {
|
2015-01-30 19:33:03 +00:00
|
|
|
results.push(item);
|
2012-04-10 18:57:45 +00:00
|
|
|
}
|
2010-03-10 02:24:30 +00:00
|
|
|
}
|
2015-01-30 19:33:03 +00:00
|
|
|
return results;
|
2010-09-25 00:18:47 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-12-13 10:51:57 +00:00
|
|
|
exports.count = function(string, substr) {
|
2010-03-10 02:24:30 +00:00
|
|
|
var num, pos;
|
2010-10-20 17:29:06 +00:00
|
|
|
num = pos = 0;
|
2012-04-10 18:57:45 +00:00
|
|
|
if (!substr.length) {
|
|
|
|
return 1 / 0;
|
|
|
|
}
|
2010-12-13 10:51:57 +00:00
|
|
|
while (pos = 1 + string.indexOf(substr, pos)) {
|
2010-09-25 00:18:47 +00:00
|
|
|
num++;
|
2010-03-10 02:24:30 +00:00
|
|
|
}
|
|
|
|
return num;
|
2010-09-25 00:18:47 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-09-25 00:29:44 +00:00
|
|
|
exports.merge = function(options, overrides) {
|
2010-09-25 00:18:47 +00:00
|
|
|
return extend(extend({}, options), overrides);
|
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-10-20 17:29:06 +00:00
|
|
|
extend = exports.extend = function(object, properties) {
|
2010-10-19 15:07:10 +00:00
|
|
|
var key, val;
|
2010-10-01 22:26:37 +00:00
|
|
|
for (key in properties) {
|
|
|
|
val = properties[key];
|
2010-09-26 14:28:48 +00:00
|
|
|
object[key] = val;
|
2010-03-10 02:24:30 +00:00
|
|
|
}
|
2010-09-25 00:18:47 +00:00
|
|
|
return object;
|
2010-10-20 17:29:06 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-10-20 17:29:06 +00:00
|
|
|
exports.flatten = flatten = function(array) {
|
2015-01-30 19:33:03 +00:00
|
|
|
var element, flattened, i, len1;
|
2010-09-28 18:28:05 +00:00
|
|
|
flattened = [];
|
2015-01-30 19:33:03 +00:00
|
|
|
for (i = 0, len1 = array.length; i < len1; i++) {
|
|
|
|
element = array[i];
|
2015-05-21 01:46:01 +00:00
|
|
|
if ('[object Array]' === Object.prototype.toString.call(element)) {
|
2010-09-28 18:28:05 +00:00
|
|
|
flattened = flattened.concat(flatten(element));
|
|
|
|
} else {
|
|
|
|
flattened.push(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flattened;
|
2010-10-20 17:29:06 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-09-25 00:29:44 +00:00
|
|
|
exports.del = function(obj, key) {
|
2010-03-10 02:24:30 +00:00
|
|
|
var val;
|
|
|
|
val = obj[key];
|
|
|
|
delete obj[key];
|
|
|
|
return val;
|
2010-09-25 00:18:47 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2015-01-30 19:33:03 +00:00
|
|
|
exports.some = (ref = Array.prototype.some) != null ? ref : function(fn) {
|
Refactor `Literal` into several subtypes
Previously, the parser created `Literal` nodes for many things. This resulted in
information loss. Instead of being able to check the node type, we had to use
regexes to tell the different types of `Literal`s apart. That was a bit like
parsing literals twice: Once in the lexer, and once (or more) in the compiler.
It also caused problems, such as `` `this` `` and `this` being indistinguishable
(fixes #2009).
Instead returning `new Literal` in the grammar, subtypes of it are now returned
instead, such as `NumberLiteral`, `StringLiteral` and `IdentifierLiteral`. `new
Literal` by itself is only used to represent code chunks that fit no category.
(While mentioning `NumberLiteral`, there's also `InfinityLiteral` now, which is
a subtype of `NumberLiteral`.)
`StringWithInterpolations` has been added as a subtype of `Parens`, and
`RegexWithInterpolations` as a subtype of `Call`. This makes it easier for other
programs to make use of CoffeeScript's "AST" (nodes). For example, it is now
possible to distinguish between `"a #{b} c"` and `"a " + b + " c"`. Fixes #4192.
`SuperCall` has been added as a subtype of `Call`.
Note, though, that some information is still lost, especially in the lexer. For
example, there is no way to distinguish a heredoc from a regular string, or a
heregex without interpolations from a regular regex. Binary and octal number
literals are indistinguishable from hexadecimal literals.
After the new subtypes were added, they were taken advantage of, removing most
regexes in nodes.coffee. `SIMPLENUM` (which matches non-hex integers) had to be
kept, though, because such numbers need special handling in JavaScript (for
example in `1..toString()`).
An especially nice hack to get rid of was using `new String()` for the token
value for reserved identifiers (to be able to set a property on them which could
survive through the parser). Now it's a good old regular string.
In range literals, slices, splices and for loop steps when number literals
are involved, CoffeeScript can do some optimizations, such as precomputing the
value of, say, `5 - 3` (outputting `2` instead of `5 - 3` literally). As a side
bonus, this now also works with hexadecimal number literals, such as `0x02`.
Finally, this also improves the output of `coffee --nodes`:
# Before:
$ bin/coffee -ne 'while true
"#{a}"
break'
Block
While
Value
Bool
Block
Value
Parens
Block
Op +
Value """"
Value
Parens
Block
Value "a" "break"
# After:
$ bin/coffee -ne 'while true
"#{a}"
break'
Block
While
Value BooleanLiteral: true
Block
Value
StringWithInterpolations
Block
Op +
Value StringLiteral: ""
Value
Parens
Block
Value IdentifierLiteral: a
StatementLiteral: break
2016-01-31 19:24:31 +00:00
|
|
|
var e, i, len1, ref1;
|
|
|
|
ref1 = this;
|
|
|
|
for (i = 0, len1 = ref1.length; i < len1; i++) {
|
|
|
|
e = ref1[i];
|
2012-05-16 19:50:34 +00:00
|
|
|
if (fn(e)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2013-03-05 02:45:57 +00:00
|
|
|
exports.invertLiterate = function(code) {
|
2016-10-23 15:37:51 +00:00
|
|
|
var generateRandomToken, i, item, len1, out, ref1, token;
|
|
|
|
generateRandomToken = function() {
|
|
|
|
return "" + (Math.random() * Date.now());
|
|
|
|
};
|
|
|
|
while (token === void 0 || code.indexOf(token) !== -1) {
|
|
|
|
token = generateRandomToken();
|
|
|
|
}
|
|
|
|
code = code.replace("\t", token);
|
|
|
|
out = "";
|
|
|
|
ref1 = marked.lexer(code, {});
|
|
|
|
for (i = 0, len1 = ref1.length; i < len1; i++) {
|
|
|
|
item = ref1[i];
|
|
|
|
if (item.type === 'code') {
|
|
|
|
out += item.text + "\n";
|
2013-03-05 02:45:57 +00:00
|
|
|
}
|
2016-10-23 15:37:51 +00:00
|
|
|
}
|
|
|
|
out.replace(token, "\t");
|
|
|
|
return out;
|
2013-03-05 02:45:57 +00:00
|
|
|
};
|
|
|
|
|
2012-11-15 20:35:01 +00:00
|
|
|
buildLocationData = function(first, last) {
|
|
|
|
if (!last) {
|
|
|
|
return first;
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
first_line: first.first_line,
|
|
|
|
first_column: first.first_column,
|
|
|
|
last_line: last.last_line,
|
|
|
|
last_column: last.last_column
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.addLocationDataFn = function(first, last) {
|
|
|
|
return function(obj) {
|
|
|
|
if (((typeof obj) === 'object') && (!!obj['updateLocationDataIfMissing'])) {
|
|
|
|
obj.updateLocationDataIfMissing(buildLocationData(first, last));
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-11-17 00:09:56 +00:00
|
|
|
exports.locationDataToString = function(obj) {
|
|
|
|
var locationData;
|
2013-01-14 20:20:35 +00:00
|
|
|
if (("2" in obj) && ("first_line" in obj[2])) {
|
|
|
|
locationData = obj[2];
|
2012-11-17 00:09:56 +00:00
|
|
|
} else if ("first_line" in obj) {
|
|
|
|
locationData = obj;
|
|
|
|
}
|
|
|
|
if (locationData) {
|
Refactor interpolation (and string and regex) handling in lexer
- Fix #3394: Unclosed single-quoted strings (both regular ones and heredocs)
used to pass through the lexer, causing a parsing error later, while
double-quoted strings caused an error already in the lexing phase. Now both
single and double-quoted unclosed strings error out in the lexer (which is the
more logical option) with consistent error messages. This also fixes the last
comment by @satyr in #3301.
- Similar to the above, unclosed heregexes also used to pass through the lexer
and not error until in the parsing phase, which resulted in confusing error
messages. This has been fixed, too.
- Fix #3348, by adding passing tests.
- Fix #3529: If a string starts with an interpolation, an empty string is no
longer emitted before the interpolation (unless it is needed to coerce the
interpolation into a string).
- Block comments cannot contain `*/`. Now the error message also shows exactly
where the offending `*/`. This improvement might seem unrelated, but I had to
touch that code anyway to refactor string and regex related code, and the
change was very trivial. Moreover, it's consistent with the next two points.
- Regexes cannot start with `*`. Now the error message also shows exactly where
the offending `*` is. (It might actually not be exatly at the start in
heregexes.) It is a very minor improvement, but it was trivial to add.
- Octal escapes in strings are forbidden in CoffeeScript (just like in
JavaScript strict mode). However, this used to be the case only for regular
strings. Now they are also forbidden in heredocs. Moreover, the errors now
point at the offending octal escape.
- Invalid regex flags are no longer allowed. This includes repeated modifiers
and unknown ones. Moreover, invalid modifiers do not stop a heregex from
being matched, which results in better error messages.
- Fix #3621: `///a#{1}///` compiles to `RegExp("a" + 1)`. So does
`RegExp("a#{1}")`. Still, those two code snippets used to generate different
tokens, which is a bit weird, but more importantly causes problems for
coffeelint (see clutchski/coffeelint#340). This required lots of tests in
test/location.coffee to be updated. Note that some updates to those tests are
unrelated to this point; some have been updated to be more consistent (I
discovered this because the refactored code happened to be seemingly more
correct).
- Regular regex literals used to erraneously allow newlines to be escaped,
causing invalid JavaScript output. This has been fixed.
- Heregexes may now be completely empty (`//////`), instead of erroring out with
a confusing message.
- Fix #2388: Heredocs and heregexes used to be lexed simply, which meant that
you couldn't nest a heredoc within a heredoc (double-quoted, that is) or a
heregex inside a heregex.
- Fix #2321: If you used division inside interpolation and then a slash later in
the string containing that interpolation, the division slash and the latter
slash was erraneously matched as a regex. This has been fixed.
- Indentation inside interpolations in heredocs no longer affect how much
indentation is removed from each line of the heredoc (which is more
intuitive).
- Whitespace is now correctly trimmed from the start and end of strings in a few
edge cases.
- Last but not least, the lexing of interpolated strings now seems to be more
efficient. For a regular double-quoted string, we used to use a custom
function to find the end of it (taking interpolations and interpolations
within interpolations etc. into account). Then we used to re-find the
interpolations and recursively lex their contents. In effect, the same string
was processed twice, or even more in the case of deeper nesting of
interpolations. Now the same string is processed just once.
- Code duplication between regular strings, heredocs, regular regexes and
heregexes has been reduced.
- The above two points should result in more easily read code, too.
2015-01-03 22:40:43 +00:00
|
|
|
return ((locationData.first_line + 1) + ":" + (locationData.first_column + 1) + "-") + ((locationData.last_line + 1) + ":" + (locationData.last_column + 1));
|
2012-11-17 00:09:56 +00:00
|
|
|
} else {
|
|
|
|
return "No location data";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-20 16:11:42 +00:00
|
|
|
exports.baseFileName = function(file, stripExt, useWinPathSep) {
|
|
|
|
var parts, pathSep;
|
2013-03-04 22:07:16 +00:00
|
|
|
if (stripExt == null) {
|
|
|
|
stripExt = false;
|
|
|
|
}
|
2013-03-20 16:11:42 +00:00
|
|
|
if (useWinPathSep == null) {
|
|
|
|
useWinPathSep = false;
|
2013-03-15 22:13:04 +00:00
|
|
|
}
|
2013-03-20 16:11:42 +00:00
|
|
|
pathSep = useWinPathSep ? /\\|\// : /\//;
|
2013-03-15 22:13:04 +00:00
|
|
|
parts = file.split(pathSep);
|
2013-03-04 21:40:39 +00:00
|
|
|
file = parts[parts.length - 1];
|
2013-10-20 22:09:55 +00:00
|
|
|
if (!(stripExt && file.indexOf('.') >= 0)) {
|
2013-03-04 22:07:16 +00:00
|
|
|
return file;
|
|
|
|
}
|
2013-03-04 21:40:39 +00:00
|
|
|
parts = file.split('.');
|
|
|
|
parts.pop();
|
2013-03-06 21:41:34 +00:00
|
|
|
if (parts[parts.length - 1] === 'coffee' && parts.length > 1) {
|
2013-03-04 21:40:39 +00:00
|
|
|
parts.pop();
|
|
|
|
}
|
|
|
|
return parts.join('.');
|
|
|
|
};
|
|
|
|
|
2013-02-28 02:54:17 +00:00
|
|
|
exports.isCoffee = function(file) {
|
|
|
|
return /\.((lit)?coffee|coffee\.md)$/.test(file);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.isLiterate = function(file) {
|
|
|
|
return /\.(litcoffee|coffee\.md)$/.test(file);
|
|
|
|
};
|
|
|
|
|
2013-03-05 04:13:46 +00:00
|
|
|
exports.throwSyntaxError = function(message, location) {
|
2013-04-22 18:38:17 +00:00
|
|
|
var error;
|
2013-03-05 04:13:46 +00:00
|
|
|
error = new SyntaxError(message);
|
|
|
|
error.location = location;
|
2013-07-31 11:27:49 +00:00
|
|
|
error.toString = syntaxErrorToString;
|
2013-08-02 04:52:36 +00:00
|
|
|
error.stack = error.toString();
|
2013-03-05 04:13:46 +00:00
|
|
|
throw error;
|
|
|
|
};
|
|
|
|
|
2013-08-02 04:52:36 +00:00
|
|
|
exports.updateSyntaxError = function(error, code, filename) {
|
|
|
|
if (error.toString === syntaxErrorToString) {
|
|
|
|
error.code || (error.code = code);
|
|
|
|
error.filename || (error.filename = filename);
|
|
|
|
error.stack = error.toString();
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
};
|
|
|
|
|
2013-07-31 11:27:49 +00:00
|
|
|
syntaxErrorToString = function() {
|
2015-03-08 13:40:27 +00:00
|
|
|
var codeLine, colorize, colorsEnabled, end, filename, first_column, first_line, last_column, last_line, marker, ref1, ref2, ref3, ref4, start;
|
2013-07-31 11:27:49 +00:00
|
|
|
if (!(this.code && this.location)) {
|
|
|
|
return Error.prototype.toString.call(this);
|
|
|
|
}
|
2015-01-30 19:33:03 +00:00
|
|
|
ref1 = this.location, first_line = ref1.first_line, first_column = ref1.first_column, last_line = ref1.last_line, last_column = ref1.last_column;
|
2013-07-31 11:27:49 +00:00
|
|
|
if (last_line == null) {
|
|
|
|
last_line = first_line;
|
2013-03-05 04:13:46 +00:00
|
|
|
}
|
2013-07-31 11:27:49 +00:00
|
|
|
if (last_column == null) {
|
|
|
|
last_column = first_column;
|
|
|
|
}
|
|
|
|
filename = this.filename || '[stdin]';
|
|
|
|
codeLine = this.code.split('\n')[first_line];
|
2013-03-05 04:13:46 +00:00
|
|
|
start = first_column;
|
|
|
|
end = first_line === last_line ? last_column + 1 : codeLine.length;
|
2014-07-16 09:56:07 +00:00
|
|
|
marker = codeLine.slice(0, start).replace(/[^\s]/g, ' ') + repeat('^', end - start);
|
2013-07-31 11:27:49 +00:00
|
|
|
if (typeof process !== "undefined" && process !== null) {
|
2015-03-08 13:40:27 +00:00
|
|
|
colorsEnabled = ((ref2 = process.stdout) != null ? ref2.isTTY : void 0) && !((ref3 = process.env) != null ? ref3.NODE_DISABLE_COLORS : void 0);
|
2013-07-31 11:27:49 +00:00
|
|
|
}
|
2015-03-08 13:40:27 +00:00
|
|
|
if ((ref4 = this.colorful) != null ? ref4 : colorsEnabled) {
|
2013-03-05 04:13:46 +00:00
|
|
|
colorize = function(str) {
|
|
|
|
return "\x1B[1;31m" + str + "\x1B[0m";
|
|
|
|
};
|
|
|
|
codeLine = codeLine.slice(0, start) + colorize(codeLine.slice(start, end)) + codeLine.slice(end);
|
|
|
|
marker = colorize(marker);
|
|
|
|
}
|
Refactor interpolation (and string and regex) handling in lexer
- Fix #3394: Unclosed single-quoted strings (both regular ones and heredocs)
used to pass through the lexer, causing a parsing error later, while
double-quoted strings caused an error already in the lexing phase. Now both
single and double-quoted unclosed strings error out in the lexer (which is the
more logical option) with consistent error messages. This also fixes the last
comment by @satyr in #3301.
- Similar to the above, unclosed heregexes also used to pass through the lexer
and not error until in the parsing phase, which resulted in confusing error
messages. This has been fixed, too.
- Fix #3348, by adding passing tests.
- Fix #3529: If a string starts with an interpolation, an empty string is no
longer emitted before the interpolation (unless it is needed to coerce the
interpolation into a string).
- Block comments cannot contain `*/`. Now the error message also shows exactly
where the offending `*/`. This improvement might seem unrelated, but I had to
touch that code anyway to refactor string and regex related code, and the
change was very trivial. Moreover, it's consistent with the next two points.
- Regexes cannot start with `*`. Now the error message also shows exactly where
the offending `*` is. (It might actually not be exatly at the start in
heregexes.) It is a very minor improvement, but it was trivial to add.
- Octal escapes in strings are forbidden in CoffeeScript (just like in
JavaScript strict mode). However, this used to be the case only for regular
strings. Now they are also forbidden in heredocs. Moreover, the errors now
point at the offending octal escape.
- Invalid regex flags are no longer allowed. This includes repeated modifiers
and unknown ones. Moreover, invalid modifiers do not stop a heregex from
being matched, which results in better error messages.
- Fix #3621: `///a#{1}///` compiles to `RegExp("a" + 1)`. So does
`RegExp("a#{1}")`. Still, those two code snippets used to generate different
tokens, which is a bit weird, but more importantly causes problems for
coffeelint (see clutchski/coffeelint#340). This required lots of tests in
test/location.coffee to be updated. Note that some updates to those tests are
unrelated to this point; some have been updated to be more consistent (I
discovered this because the refactored code happened to be seemingly more
correct).
- Regular regex literals used to erraneously allow newlines to be escaped,
causing invalid JavaScript output. This has been fixed.
- Heregexes may now be completely empty (`//////`), instead of erroring out with
a confusing message.
- Fix #2388: Heredocs and heregexes used to be lexed simply, which meant that
you couldn't nest a heredoc within a heredoc (double-quoted, that is) or a
heregex inside a heregex.
- Fix #2321: If you used division inside interpolation and then a slash later in
the string containing that interpolation, the division slash and the latter
slash was erraneously matched as a regex. This has been fixed.
- Indentation inside interpolations in heredocs no longer affect how much
indentation is removed from each line of the heredoc (which is more
intuitive).
- Whitespace is now correctly trimmed from the start and end of strings in a few
edge cases.
- Last but not least, the lexing of interpolated strings now seems to be more
efficient. For a regular double-quoted string, we used to use a custom
function to find the end of it (taking interpolations and interpolations
within interpolations etc. into account). Then we used to re-find the
interpolations and recursively lex their contents. In effect, the same string
was processed twice, or even more in the case of deeper nesting of
interpolations. Now the same string is processed just once.
- Code duplication between regular strings, heredocs, regular regexes and
heregexes has been reduced.
- The above two points should result in more easily read code, too.
2015-01-03 22:40:43 +00:00
|
|
|
return filename + ":" + (first_line + 1) + ":" + (first_column + 1) + ": error: " + this.message + "\n" + codeLine + "\n" + marker;
|
2013-03-05 04:13:46 +00:00
|
|
|
};
|
|
|
|
|
2014-01-22 02:44:50 +00:00
|
|
|
exports.nameWhitespaceCharacter = function(string) {
|
|
|
|
switch (string) {
|
|
|
|
case ' ':
|
|
|
|
return 'space';
|
|
|
|
case '\n':
|
|
|
|
return 'newline';
|
|
|
|
case '\r':
|
|
|
|
return 'carriage return';
|
|
|
|
case '\t':
|
|
|
|
return 'tab';
|
|
|
|
default:
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-09-21 07:53:58 +00:00
|
|
|
}).call(this);
|