coffee-script.js 12 KB

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