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