Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80677 views
1
// Generated by CoffeeScript 1.9.3
2
(function() {
3
var Lexer, SourceMap, base, compile, ext, formatSourcePosition, fs, getSourceMap, helpers, i, len, lexer, parser, path, ref, sourceMaps, vm, withPrettyErrors,
4
hasProp = {}.hasOwnProperty,
5
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
6
7
fs = require('fs');
8
9
vm = require('vm');
10
11
path = require('path');
12
13
Lexer = require('./lexer').Lexer;
14
15
parser = require('./parser').parser;
16
17
helpers = require('./helpers');
18
19
SourceMap = require('./sourcemap');
20
21
exports.VERSION = '1.9.3';
22
23
exports.FILE_EXTENSIONS = ['.coffee', '.litcoffee', '.coffee.md'];
24
25
exports.helpers = helpers;
26
27
withPrettyErrors = function(fn) {
28
return function(code, options) {
29
var err;
30
if (options == null) {
31
options = {};
32
}
33
try {
34
return fn.call(this, code, options);
35
} catch (_error) {
36
err = _error;
37
if (typeof code !== 'string') {
38
throw err;
39
}
40
throw helpers.updateSyntaxError(err, code, options.filename);
41
}
42
};
43
};
44
45
exports.compile = compile = withPrettyErrors(function(code, options) {
46
var answer, currentColumn, currentLine, extend, fragment, fragments, header, i, js, len, map, merge, newLines, token, tokens;
47
merge = helpers.merge, extend = helpers.extend;
48
options = extend({}, options);
49
if (options.sourceMap) {
50
map = new SourceMap;
51
}
52
tokens = lexer.tokenize(code, options);
53
options.referencedVars = (function() {
54
var i, len, results;
55
results = [];
56
for (i = 0, len = tokens.length; i < len; i++) {
57
token = tokens[i];
58
if (token.variable) {
59
results.push(token[1]);
60
}
61
}
62
return results;
63
})();
64
fragments = parser.parse(tokens).compileToFragments(options);
65
currentLine = 0;
66
if (options.header) {
67
currentLine += 1;
68
}
69
if (options.shiftLine) {
70
currentLine += 1;
71
}
72
currentColumn = 0;
73
js = "";
74
for (i = 0, len = fragments.length; i < len; i++) {
75
fragment = fragments[i];
76
if (options.sourceMap) {
77
if (fragment.locationData && !/^[;\s]*$/.test(fragment.code)) {
78
map.add([fragment.locationData.first_line, fragment.locationData.first_column], [currentLine, currentColumn], {
79
noReplace: true
80
});
81
}
82
newLines = helpers.count(fragment.code, "\n");
83
currentLine += newLines;
84
if (newLines) {
85
currentColumn = fragment.code.length - (fragment.code.lastIndexOf("\n") + 1);
86
} else {
87
currentColumn += fragment.code.length;
88
}
89
}
90
js += fragment.code;
91
}
92
if (options.header) {
93
header = "Generated by CoffeeScript " + this.VERSION;
94
js = "// " + header + "\n" + js;
95
}
96
if (options.sourceMap) {
97
answer = {
98
js: js
99
};
100
answer.sourceMap = map;
101
answer.v3SourceMap = map.generate(options, code);
102
return answer;
103
} else {
104
return js;
105
}
106
});
107
108
exports.tokens = withPrettyErrors(function(code, options) {
109
return lexer.tokenize(code, options);
110
});
111
112
exports.nodes = withPrettyErrors(function(source, options) {
113
if (typeof source === 'string') {
114
return parser.parse(lexer.tokenize(source, options));
115
} else {
116
return parser.parse(source);
117
}
118
});
119
120
exports.run = function(code, options) {
121
var answer, dir, mainModule, ref;
122
if (options == null) {
123
options = {};
124
}
125
mainModule = require.main;
126
mainModule.filename = process.argv[1] = options.filename ? fs.realpathSync(options.filename) : '.';
127
mainModule.moduleCache && (mainModule.moduleCache = {});
128
dir = options.filename ? path.dirname(fs.realpathSync(options.filename)) : fs.realpathSync('.');
129
mainModule.paths = require('module')._nodeModulePaths(dir);
130
if (!helpers.isCoffee(mainModule.filename) || require.extensions) {
131
answer = compile(code, options);
132
code = (ref = answer.js) != null ? ref : answer;
133
}
134
return mainModule._compile(code, mainModule.filename);
135
};
136
137
exports["eval"] = function(code, options) {
138
var Module, _module, _require, createContext, i, isContext, js, k, len, o, r, ref, ref1, ref2, ref3, sandbox, v;
139
if (options == null) {
140
options = {};
141
}
142
if (!(code = code.trim())) {
143
return;
144
}
145
createContext = (ref = vm.Script.createContext) != null ? ref : vm.createContext;
146
isContext = (ref1 = vm.isContext) != null ? ref1 : function(ctx) {
147
return options.sandbox instanceof createContext().constructor;
148
};
149
if (createContext) {
150
if (options.sandbox != null) {
151
if (isContext(options.sandbox)) {
152
sandbox = options.sandbox;
153
} else {
154
sandbox = createContext();
155
ref2 = options.sandbox;
156
for (k in ref2) {
157
if (!hasProp.call(ref2, k)) continue;
158
v = ref2[k];
159
sandbox[k] = v;
160
}
161
}
162
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox;
163
} else {
164
sandbox = global;
165
}
166
sandbox.__filename = options.filename || 'eval';
167
sandbox.__dirname = path.dirname(sandbox.__filename);
168
if (!(sandbox !== global || sandbox.module || sandbox.require)) {
169
Module = require('module');
170
sandbox.module = _module = new Module(options.modulename || 'eval');
171
sandbox.require = _require = function(path) {
172
return Module._load(path, _module, true);
173
};
174
_module.filename = sandbox.__filename;
175
ref3 = Object.getOwnPropertyNames(require);
176
for (i = 0, len = ref3.length; i < len; i++) {
177
r = ref3[i];
178
if (r !== 'paths') {
179
_require[r] = require[r];
180
}
181
}
182
_require.paths = _module.paths = Module._nodeModulePaths(process.cwd());
183
_require.resolve = function(request) {
184
return Module._resolveFilename(request, _module);
185
};
186
}
187
}
188
o = {};
189
for (k in options) {
190
if (!hasProp.call(options, k)) continue;
191
v = options[k];
192
o[k] = v;
193
}
194
o.bare = true;
195
js = compile(code, o);
196
if (sandbox === global) {
197
return vm.runInThisContext(js);
198
} else {
199
return vm.runInContext(js, sandbox);
200
}
201
};
202
203
exports.register = function() {
204
return require('./register');
205
};
206
207
if (require.extensions) {
208
ref = this.FILE_EXTENSIONS;
209
for (i = 0, len = ref.length; i < len; i++) {
210
ext = ref[i];
211
if ((base = require.extensions)[ext] == null) {
212
base[ext] = function() {
213
throw new Error("Use CoffeeScript.register() or require the coffee-script/register module to require " + ext + " files.");
214
};
215
}
216
}
217
}
218
219
exports._compileFile = function(filename, sourceMap) {
220
var answer, err, raw, stripped;
221
if (sourceMap == null) {
222
sourceMap = false;
223
}
224
raw = fs.readFileSync(filename, 'utf8');
225
stripped = raw.charCodeAt(0) === 0xFEFF ? raw.substring(1) : raw;
226
try {
227
answer = compile(stripped, {
228
filename: filename,
229
sourceMap: sourceMap,
230
literate: helpers.isLiterate(filename)
231
});
232
} catch (_error) {
233
err = _error;
234
throw helpers.updateSyntaxError(err, stripped, filename);
235
}
236
return answer;
237
};
238
239
lexer = new Lexer;
240
241
parser.lexer = {
242
lex: function() {
243
var tag, token;
244
token = parser.tokens[this.pos++];
245
if (token) {
246
tag = token[0], this.yytext = token[1], this.yylloc = token[2];
247
parser.errorToken = token.origin || token;
248
this.yylineno = this.yylloc.first_line;
249
} else {
250
tag = '';
251
}
252
return tag;
253
},
254
setInput: function(tokens) {
255
parser.tokens = tokens;
256
return this.pos = 0;
257
},
258
upcomingInput: function() {
259
return "";
260
}
261
};
262
263
parser.yy = require('./nodes');
264
265
parser.yy.parseError = function(message, arg) {
266
var errorLoc, errorTag, errorText, errorToken, token, tokens;
267
token = arg.token;
268
errorToken = parser.errorToken, tokens = parser.tokens;
269
errorTag = errorToken[0], errorText = errorToken[1], errorLoc = errorToken[2];
270
errorText = (function() {
271
switch (false) {
272
case errorToken !== tokens[tokens.length - 1]:
273
return 'end of input';
274
case errorTag !== 'INDENT' && errorTag !== 'OUTDENT':
275
return 'indentation';
276
case errorTag !== 'IDENTIFIER' && errorTag !== 'NUMBER' && errorTag !== 'STRING' && errorTag !== 'STRING_START' && errorTag !== 'REGEX' && errorTag !== 'REGEX_START':
277
return errorTag.replace(/_START$/, '').toLowerCase();
278
default:
279
return helpers.nameWhitespaceCharacter(errorText);
280
}
281
})();
282
return helpers.throwSyntaxError("unexpected " + errorText, errorLoc);
283
};
284
285
formatSourcePosition = function(frame, getSourceMapping) {
286
var as, column, fileLocation, fileName, functionName, isConstructor, isMethodCall, line, methodName, source, tp, typeName;
287
fileName = void 0;
288
fileLocation = '';
289
if (frame.isNative()) {
290
fileLocation = "native";
291
} else {
292
if (frame.isEval()) {
293
fileName = frame.getScriptNameOrSourceURL();
294
if (!fileName) {
295
fileLocation = (frame.getEvalOrigin()) + ", ";
296
}
297
} else {
298
fileName = frame.getFileName();
299
}
300
fileName || (fileName = "<anonymous>");
301
line = frame.getLineNumber();
302
column = frame.getColumnNumber();
303
source = getSourceMapping(fileName, line, column);
304
fileLocation = source ? fileName + ":" + source[0] + ":" + source[1] : fileName + ":" + line + ":" + column;
305
}
306
functionName = frame.getFunctionName();
307
isConstructor = frame.isConstructor();
308
isMethodCall = !(frame.isToplevel() || isConstructor);
309
if (isMethodCall) {
310
methodName = frame.getMethodName();
311
typeName = frame.getTypeName();
312
if (functionName) {
313
tp = as = '';
314
if (typeName && functionName.indexOf(typeName)) {
315
tp = typeName + ".";
316
}
317
if (methodName && functionName.indexOf("." + methodName) !== functionName.length - methodName.length - 1) {
318
as = " [as " + methodName + "]";
319
}
320
return "" + tp + functionName + as + " (" + fileLocation + ")";
321
} else {
322
return typeName + "." + (methodName || '<anonymous>') + " (" + fileLocation + ")";
323
}
324
} else if (isConstructor) {
325
return "new " + (functionName || '<anonymous>') + " (" + fileLocation + ")";
326
} else if (functionName) {
327
return functionName + " (" + fileLocation + ")";
328
} else {
329
return fileLocation;
330
}
331
};
332
333
sourceMaps = {};
334
335
getSourceMap = function(filename) {
336
var answer, ref1;
337
if (sourceMaps[filename]) {
338
return sourceMaps[filename];
339
}
340
if (ref1 = path != null ? path.extname(filename) : void 0, indexOf.call(exports.FILE_EXTENSIONS, ref1) < 0) {
341
return;
342
}
343
answer = exports._compileFile(filename, true);
344
return sourceMaps[filename] = answer.sourceMap;
345
};
346
347
Error.prepareStackTrace = function(err, stack) {
348
var frame, frames, getSourceMapping;
349
getSourceMapping = function(filename, line, column) {
350
var answer, sourceMap;
351
sourceMap = getSourceMap(filename);
352
if (sourceMap) {
353
answer = sourceMap.sourceLocation([line - 1, column - 1]);
354
}
355
if (answer) {
356
return [answer[0] + 1, answer[1] + 1];
357
} else {
358
return null;
359
}
360
};
361
frames = (function() {
362
var j, len1, results;
363
results = [];
364
for (j = 0, len1 = stack.length; j < len1; j++) {
365
frame = stack[j];
366
if (frame.getFunction() === exports.run) {
367
break;
368
}
369
results.push(" at " + (formatSourcePosition(frame, getSourceMapping)));
370
}
371
return results;
372
})();
373
return (err.toString()) + "\n" + (frames.join('\n')) + "\n";
374
};
375
376
}).call(this);
377
378