1 // Generated by CoffeeScript 1.8.0
3 var Lexer, SourceMap, compile, ext, formatSourcePosition, fs, getSourceMap, helpers, lexer, parser, path, sourceMaps, vm, withPrettyErrors, _base, _i, _len, _ref,
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; };
11 path = require('path');
13 Lexer = require('./lexer').Lexer;
15 parser = require('./parser').parser;
17 helpers = require('./helpers');
19 SourceMap = require('./sourcemap');
21 exports.VERSION = '1.8.0';
23 exports.FILE_EXTENSIONS = ['.coffee', '.litcoffee', '.coffee.md'];
25 exports.helpers = helpers;
27 withPrettyErrors = function(fn) {
28 return function(code, options) {
30 if (options == null) {
34 return fn.call(this, code, options);
37 throw helpers.updateSyntaxError(err, code, options.filename);
42 exports.compile = compile = withPrettyErrors(function(code, options) {
43 var answer, currentColumn, currentLine, extend, fragment, fragments, header, js, map, merge, newLines, _i, _len;
44 merge = helpers.merge, extend = helpers.extend;
45 options = extend({}, options);
46 if (options.sourceMap) {
49 fragments = parser.parse(lexer.tokenize(code, options)).compileToFragments(options);
54 if (options.shiftLine) {
59 for (_i = 0, _len = fragments.length; _i < _len; _i++) {
60 fragment = fragments[_i];
61 if (options.sourceMap) {
62 if (fragment.locationData) {
63 map.add([fragment.locationData.first_line, fragment.locationData.first_column], [currentLine, currentColumn], {
67 newLines = helpers.count(fragment.code, "\n");
68 currentLine += newLines;
70 currentColumn = fragment.code.length - (fragment.code.lastIndexOf("\n") + 1);
72 currentColumn += fragment.code.length;
78 header = "Generated by CoffeeScript " + this.VERSION;
79 js = "// " + header + "\n" + js;
81 if (options.sourceMap) {
85 answer.sourceMap = map;
86 answer.v3SourceMap = map.generate(options, code);
93 exports.tokens = withPrettyErrors(function(code, options) {
94 return lexer.tokenize(code, options);
97 exports.nodes = withPrettyErrors(function(source, options) {
98 if (typeof source === 'string') {
99 return parser.parse(lexer.tokenize(source, options));
101 return parser.parse(source);
105 exports.run = function(code, options) {
106 var answer, dir, mainModule, _ref;
107 if (options == null) {
110 mainModule = require.main;
111 mainModule.filename = process.argv[1] = options.filename ? fs.realpathSync(options.filename) : '.';
112 mainModule.moduleCache && (mainModule.moduleCache = {});
113 dir = options.filename ? path.dirname(fs.realpathSync(options.filename)) : fs.realpathSync('.');
114 mainModule.paths = require('module')._nodeModulePaths(dir);
115 if (!helpers.isCoffee(mainModule.filename) || require.extensions) {
116 answer = compile(code, options);
117 code = (_ref = answer.js) != null ? _ref : answer;
119 return mainModule._compile(code, mainModule.filename);
122 exports["eval"] = function(code, options) {
123 var Module, Script, js, k, o, r, sandbox, v, _i, _len, _module, _ref, _ref1, _require;
124 if (options == null) {
127 if (!(code = code.trim())) {
132 if (options.sandbox != null) {
133 if (options.sandbox instanceof Script.createContext().constructor) {
134 sandbox = options.sandbox;
136 sandbox = Script.createContext();
137 _ref = options.sandbox;
139 if (!__hasProp.call(_ref, k)) continue;
144 sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox;
148 sandbox.__filename = options.filename || 'eval';
149 sandbox.__dirname = path.dirname(sandbox.__filename);
150 if (!(sandbox !== global || sandbox.module || sandbox.require)) {
151 Module = require('module');
152 sandbox.module = _module = new Module(options.modulename || 'eval');
153 sandbox.require = _require = function(path) {
154 return Module._load(path, _module, true);
156 _module.filename = sandbox.__filename;
157 _ref1 = Object.getOwnPropertyNames(require);
158 for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
161 _require[r] = require[r];
164 _require.paths = _module.paths = Module._nodeModulePaths(process.cwd());
165 _require.resolve = function(request) {
166 return Module._resolveFilename(request, _module);
172 if (!__hasProp.call(options, k)) continue;
177 js = compile(code, o);
178 if (sandbox === global) {
179 return vm.runInThisContext(js);
181 return vm.runInContext(js, sandbox);
185 exports.register = function() {
186 return require('./register');
189 if (require.extensions) {
190 _ref = this.FILE_EXTENSIONS;
191 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
193 if ((_base = require.extensions)[ext] == null) {
194 _base[ext] = function() {
195 throw new Error("Use CoffeeScript.register() or require the coffee-script/register module to require " + ext + " files.");
201 exports._compileFile = function(filename, sourceMap) {
202 var answer, err, raw, stripped;
203 if (sourceMap == null) {
206 raw = fs.readFileSync(filename, 'utf8');
207 stripped = raw.charCodeAt(0) === 0xFEFF ? raw.substring(1) : raw;
209 answer = compile(stripped, {
211 sourceMap: sourceMap,
212 literate: helpers.isLiterate(filename)
216 throw helpers.updateSyntaxError(err, stripped, filename);
226 token = this.tokens[this.pos++];
228 tag = token[0], this.yytext = token[1], this.yylloc = token[2];
229 this.errorToken = token.origin || token;
230 this.yylineno = this.yylloc.first_line;
236 setInput: function(tokens) {
237 this.tokens = tokens;
240 upcomingInput: function() {
245 parser.yy = require('./nodes');
247 parser.yy.parseError = function(message, _arg) {
248 var errorLoc, errorTag, errorText, errorToken, token, tokens, _ref1;
250 _ref1 = parser.lexer, errorToken = _ref1.errorToken, tokens = _ref1.tokens;
251 errorTag = errorToken[0], errorText = errorToken[1], errorLoc = errorToken[2];
252 errorText = errorToken === tokens[tokens.length - 1] ? 'end of input' : errorTag === 'INDENT' || errorTag === 'OUTDENT' ? 'indentation' : helpers.nameWhitespaceCharacter(errorText);
253 return helpers.throwSyntaxError("unexpected " + errorText, errorLoc);
256 formatSourcePosition = function(frame, getSourceMapping) {
257 var as, column, fileLocation, fileName, functionName, isConstructor, isMethodCall, line, methodName, source, tp, typeName;
260 if (frame.isNative()) {
261 fileLocation = "native";
263 if (frame.isEval()) {
264 fileName = frame.getScriptNameOrSourceURL();
266 fileLocation = "" + (frame.getEvalOrigin()) + ", ";
269 fileName = frame.getFileName();
271 fileName || (fileName = "<anonymous>");
272 line = frame.getLineNumber();
273 column = frame.getColumnNumber();
274 source = getSourceMapping(fileName, line, column);
275 fileLocation = source ? "" + fileName + ":" + source[0] + ":" + source[1] : "" + fileName + ":" + line + ":" + column;
277 functionName = frame.getFunctionName();
278 isConstructor = frame.isConstructor();
279 isMethodCall = !(frame.isToplevel() || isConstructor);
281 methodName = frame.getMethodName();
282 typeName = frame.getTypeName();
285 if (typeName && functionName.indexOf(typeName)) {
286 tp = "" + typeName + ".";
288 if (methodName && functionName.indexOf("." + methodName) !== functionName.length - methodName.length - 1) {
289 as = " [as " + methodName + "]";
291 return "" + tp + functionName + as + " (" + fileLocation + ")";
293 return "" + typeName + "." + (methodName || '<anonymous>') + " (" + fileLocation + ")";
295 } else if (isConstructor) {
296 return "new " + (functionName || '<anonymous>') + " (" + fileLocation + ")";
297 } else if (functionName) {
298 return "" + functionName + " (" + fileLocation + ")";
306 getSourceMap = function(filename) {
308 if (sourceMaps[filename]) {
309 return sourceMaps[filename];
311 if (_ref1 = path != null ? path.extname(filename) : void 0, __indexOf.call(exports.FILE_EXTENSIONS, _ref1) < 0) {
314 answer = exports._compileFile(filename, true);
315 return sourceMaps[filename] = answer.sourceMap;
318 Error.prepareStackTrace = function(err, stack) {
319 var frame, frames, getSourceMapping;
320 getSourceMapping = function(filename, line, column) {
321 var answer, sourceMap;
322 sourceMap = getSourceMap(filename);
324 answer = sourceMap.sourceLocation([line - 1, column - 1]);
327 return [answer[0] + 1, answer[1] + 1];
332 frames = (function() {
333 var _j, _len1, _results;
335 for (_j = 0, _len1 = stack.length; _j < _len1; _j++) {
337 if (frame.getFunction() === exports.run) {
340 _results.push(" at " + (formatSourcePosition(frame, getSourceMapping)));
344 return "" + (err.toString()) + "\n" + (frames.join('\n')) + "\n";