PageRenderTime 68ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/coffee-script/coffee-script.js

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