coffee-script.js 14 KB

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