1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/lib/coffee-script/coffee-script.js

218 lines
6.3 KiB
JavaScript
Raw Normal View History

2013-03-04 17:07:16 -05:00
// Generated by CoffeeScript 1.6.1
(function() {
var CompilerError, Lexer, compile, ext, fs, helpers, lexer, loadFile, parser, path, sourcemap, vm, _i, _len, _ref,
__hasProp = {}.hasOwnProperty;
fs = require('fs');
path = require('path');
Lexer = require('./lexer').Lexer;
parser = require('./parser').parser;
helpers = require('./helpers');
2013-02-28 15:51:29 -05:00
2013-02-25 13:09:42 -05:00
CompilerError = require('./error').CompilerError;
2011-10-23 22:45:32 -04:00
vm = require('vm');
sourcemap = require('./sourcemap');
loadFile = function(module, filename) {
var raw, stripped;
raw = fs.readFileSync(filename, 'utf8');
stripped = raw.charCodeAt(0) === 0xFEFF ? raw.substring(1) : raw;
return module._compile(compile(stripped, {
filename: filename,
literate: helpers.isLiterate(filename)
}), filename);
2012-07-11 00:08:14 -04:00
};
if (require.extensions) {
_ref = ['.coffee', '.litcoffee', '.md', '.coffee.md'];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
ext = _ref[_i];
require.extensions[ext] = loadFile;
}
}
2013-03-04 17:07:16 -05:00
exports.VERSION = '1.6.1';
exports.helpers = helpers;
exports.compile = compile = function(code, options) {
2013-03-04 09:45:25 -05:00
var answer, coffeeFile, currentColumn, currentLine, fragment, fragments, header, js, jsFile, merge, newLines, sourceMap, _j, _len1;
2012-04-10 14:57:45 -04:00
if (options == null) {
options = {};
}
merge = exports.helpers.merge;
if (options.sourceMap) {
coffeeFile = helpers.baseFileName(options.filename);
jsFile = helpers.baseFileName(options.filename, true) + ".js";
sourceMap = new sourcemap.SourceMap();
}
fragments = (parser.parse(lexer.tokenize(code, options))).compileToFragments(options);
currentLine = 0;
if (options.header) {
currentLine += 1;
}
currentColumn = 0;
js = "";
for (_j = 0, _len1 = fragments.length; _j < _len1; _j++) {
fragment = fragments[_j];
if (sourceMap) {
if (fragment.locationData) {
sourceMap.addMapping([fragment.locationData.first_line, fragment.locationData.first_column], [currentLine, currentColumn], {
noReplace: true
});
2013-02-28 15:51:29 -05:00
}
newLines = helpers.count(fragment.code, "\n");
currentLine += newLines;
currentColumn = fragment.code.length - (newLines ? fragment.code.lastIndexOf("\n") : 0);
2013-02-28 15:51:29 -05:00
}
js += fragment.code;
}
if (options.header) {
header = "Generated by CoffeeScript " + this.VERSION;
js = "// " + header + "\n" + js;
}
if (options.sourceMap) {
2013-03-04 09:45:25 -05:00
answer = {
js: js
};
2013-03-04 09:45:25 -05:00
if (sourceMap) {
answer.sourceMap = sourceMap;
answer.v3SourceMap = sourcemap.generateV3SourceMap(sourceMap, coffeeFile, jsFile);
2013-02-28 15:51:29 -05:00
}
2013-03-04 09:45:25 -05:00
return answer;
} else {
return js;
2013-02-28 15:51:29 -05:00
}
};
exports.tokens = function(code, options) {
return lexer.tokenize(code, options);
};
exports.nodes = function(source, options) {
if (typeof source === 'string') {
2010-11-20 16:25:22 -05:00
return parser.parse(lexer.tokenize(source, options));
} else {
return parser.parse(source);
}
};
exports.run = function(code, options) {
var mainModule;
2012-04-10 14:57:45 -04:00
if (options == null) {
options = {};
}
mainModule = require.main;
mainModule.filename = process.argv[1] = options.filename ? fs.realpathSync(options.filename) : '.';
mainModule.moduleCache && (mainModule.moduleCache = {});
mainModule.paths = require('module')._nodeModulePaths(path.dirname(fs.realpathSync(options.filename)));
if (!helpers.isCoffee(mainModule.filename) || require.extensions) {
return mainModule._compile(compile(code, options), mainModule.filename);
} else {
return mainModule._compile(code, mainModule.filename);
}
};
exports["eval"] = function(code, options) {
var Module, Script, js, k, o, r, sandbox, v, _j, _len1, _module, _ref1, _ref2, _require;
2012-04-10 14:57:45 -04:00
if (options == null) {
options = {};
}
if (!(code = code.trim())) {
return;
}
2011-10-23 22:45:32 -04:00
Script = vm.Script;
2011-08-27 13:20:29 -04:00
if (Script) {
2011-08-04 23:17:23 -04:00
if (options.sandbox != null) {
2011-10-23 22:45:32 -04:00
if (options.sandbox instanceof Script.createContext().constructor) {
2011-08-04 23:17:23 -04:00
sandbox = options.sandbox;
} else {
2011-10-23 22:45:32 -04:00
sandbox = Script.createContext();
_ref1 = options.sandbox;
for (k in _ref1) {
if (!__hasProp.call(_ref1, k)) continue;
v = _ref1[k];
2011-08-04 23:17:23 -04:00
sandbox[k] = v;
}
}
2011-10-23 22:45:32 -04:00
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox;
} else {
sandbox = global;
}
2011-08-04 23:17:23 -04:00
sandbox.__filename = options.filename || 'eval';
sandbox.__dirname = path.dirname(sandbox.__filename);
2011-10-23 22:45:32 -04:00
if (!(sandbox !== global || sandbox.module || sandbox.require)) {
2011-08-04 23:17:23 -04:00
Module = require('module');
sandbox.module = _module = new Module(options.modulename || 'eval');
sandbox.require = _require = function(path) {
2011-10-23 22:45:32 -04:00
return Module._load(path, _module, true);
2011-08-04 23:17:23 -04:00
};
_module.filename = sandbox.__filename;
_ref2 = Object.getOwnPropertyNames(require);
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
r = _ref2[_j];
2012-04-10 14:57:45 -04:00
if (r !== 'paths') {
_require[r] = require[r];
}
2011-08-04 23:17:23 -04:00
}
_require.paths = _module.paths = Module._nodeModulePaths(process.cwd());
_require.resolve = function(request) {
return Module._resolveFilename(request, _module);
};
}
}
o = {};
for (k in options) {
if (!__hasProp.call(options, k)) continue;
v = options[k];
o[k] = v;
}
o.bare = true;
js = compile(code, o);
2011-10-23 22:45:32 -04:00
if (sandbox === global) {
return vm.runInThisContext(js);
2011-08-04 23:17:23 -04:00
} else {
2011-10-23 22:45:32 -04:00
return vm.runInContext(js, sandbox);
2011-08-04 23:17:23 -04:00
}
};
lexer = new Lexer;
parser.lexer = {
lex: function() {
var tag, token;
token = this.tokens[this.pos++];
if (token) {
tag = token[0], this.yytext = token[1], this.yylloc = token[2];
this.yylineno = this.yylloc.first_line;
} else {
tag = '';
}
2010-11-02 00:05:06 -04:00
return tag;
},
setInput: function(tokens) {
this.tokens = tokens;
return this.pos = 0;
},
upcomingInput: function() {
return "";
}
};
2010-09-21 03:50:32 -04:00
parser.yy = require('./nodes');
2011-12-14 10:39:20 -05:00
2013-02-25 13:09:42 -05:00
parser.yy.parseError = function(message, _arg) {
2013-02-26 21:03:33 -05:00
var token;
2013-02-26 03:55:09 -05:00
token = _arg.token;
2013-02-25 13:09:42 -05:00
message = "unexpected " + token;
2013-02-26 21:03:33 -05:00
throw CompilerError.fromLocationData(message, parser.lexer.yylloc);
2013-02-25 13:09:42 -05:00
};
}).call(this);