1 // Generated by CoffeeScript 1.10.0
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; };
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.10.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 if (typeof code !== 'string') {
40 throw helpers.updateSyntaxError(err, code, options.filename);
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) {
52 tokens = lexer.tokenize(code, options);
53 options.referencedVars = (function() {
56 for (i = 0, len = tokens.length; i < len; i++) {
59 results.push(token[1]);
64 fragments = parser.parse(tokens).compileToFragments(options);
69 if (options.shiftLine) {
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], {
82 newLines = helpers.count(fragment.code, "\n");
83 currentLine += newLines;
85 currentColumn = fragment.code.length - (fragment.code.lastIndexOf("\n") + 1);
87 currentColumn += fragment.code.length;
93 header = "Generated by CoffeeScript " + this.VERSION;
94 js = "// " + header + "\n" + js;
96 if (options.sourceMap) {
100 answer.sourceMap = map;
101 answer.v3SourceMap = map.generate(options, code);
108 exports.tokens = withPrettyErrors(function(code, options) {
109 return lexer.tokenize(code, options);
112 exports.nodes = withPrettyErrors(function(source, options) {
113 if (typeof source === 'string') {
114 return parser.parse(lexer.tokenize(source, options));
116 return parser.parse(source);
120 exports.run = function(code, options) {
121 var answer, dir, mainModule, ref;
122 if (options == null) {
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;
134 return mainModule._compile(code, mainModule.filename);
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) {
142 if (!(code = code.trim())) {
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;
150 if (options.sandbox != null) {
151 if (isContext(options.sandbox)) {
152 sandbox = options.sandbox;
154 sandbox = createContext();
155 ref2 = options.sandbox;
157 if (!hasProp.call(ref2, k)) continue;
162 sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox;
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);
174 _module.filename = sandbox.__filename;
175 ref3 = Object.getOwnPropertyNames(require);
176 for (i = 0, len = ref3.length; i < len; i++) {
178 if (r !== 'paths' && r !== 'arguments' && r !== 'caller') {
179 _require[r] = require[r];
182 _require.paths = _module.paths = Module._nodeModulePaths(process.cwd());
183 _require.resolve = function(request) {
184 return Module._resolveFilename(request, _module);
190 if (!hasProp.call(options, k)) continue;
195 js = compile(code, o);
196 if (sandbox === global) {
197 return vm.runInThisContext(js);
199 return vm.runInContext(js, sandbox);
203 exports.register = function() {
204 return require('./register');
207 if (require.extensions) {
208 ref = this.FILE_EXTENSIONS;
209 for (i = 0, len = ref.length; i < len; 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.");
219 exports._compileFile = function(filename, sourceMap) {
220 var answer, err, error, raw, stripped;
221 if (sourceMap == null) {
224 raw = fs.readFileSync(filename, 'utf8');
225 stripped = raw.charCodeAt(0) === 0xFEFF ? raw.substring(1) : raw;
227 answer = compile(stripped, {
229 sourceMap: sourceMap,
230 literate: helpers.isLiterate(filename)
234 throw helpers.updateSyntaxError(err, stripped, filename);
244 token = parser.tokens[this.pos++];
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;
254 setInput: function(tokens) {
255 parser.tokens = tokens;
258 upcomingInput: function() {
263 parser.yy = require('./nodes');
265 parser.yy.parseError = function(message, arg) {
266 var errorLoc, errorTag, errorText, errorToken, token, tokens;
268 errorToken = parser.errorToken, tokens = parser.tokens;
269 errorTag = errorToken[0], errorText = errorToken[1], errorLoc = errorToken[2];
270 errorText = (function() {
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();
279 return helpers.nameWhitespaceCharacter(errorText);
282 return helpers.throwSyntaxError("unexpected " + errorText, errorLoc);
285 formatSourcePosition = function(frame, getSourceMapping) {
286 var as, column, fileLocation, fileName, functionName, isConstructor, isMethodCall, line, methodName, source, tp, typeName;
289 if (frame.isNative()) {
290 fileLocation = "native";
292 if (frame.isEval()) {
293 fileName = frame.getScriptNameOrSourceURL();
295 fileLocation = (frame.getEvalOrigin()) + ", ";
298 fileName = frame.getFileName();
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;
306 functionName = frame.getFunctionName();
307 isConstructor = frame.isConstructor();
308 isMethodCall = !(frame.isToplevel() || isConstructor);
310 methodName = frame.getMethodName();
311 typeName = frame.getTypeName();
314 if (typeName && functionName.indexOf(typeName)) {
317 if (methodName && functionName.indexOf("." + methodName) !== functionName.length - methodName.length - 1) {
318 as = " [as " + methodName + "]";
320 return "" + tp + functionName + as + " (" + fileLocation + ")";
322 return typeName + "." + (methodName || '<anonymous>') + " (" + fileLocation + ")";
324 } else if (isConstructor) {
325 return "new " + (functionName || '<anonymous>') + " (" + fileLocation + ")";
326 } else if (functionName) {
327 return functionName + " (" + fileLocation + ")";
335 getSourceMap = function(filename) {
337 if (sourceMaps[filename]) {
338 return sourceMaps[filename];
340 if (ref1 = path != null ? path.extname(filename) : void 0, indexOf.call(exports.FILE_EXTENSIONS, ref1) < 0) {
343 answer = exports._compileFile(filename, true);
344 return sourceMaps[filename] = answer.sourceMap;
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);
353 answer = sourceMap.sourceLocation([line - 1, column - 1]);
356 return [answer[0] + 1, answer[1] + 1];
361 frames = (function() {
362 var j, len1, results;
364 for (j = 0, len1 = stack.length; j < len1; j++) {
366 if (frame.getFunction() === exports.run) {
369 results.push(" at " + (formatSourcePosition(frame, getSourceMapping)));
373 return (err.toString()) + "\n" + (frames.join('\n')) + "\n";