mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
218 lines
6.4 KiB
JavaScript
218 lines
6.4 KiB
JavaScript
// Generated by CoffeeScript 1.6.0
|
|
(function() {
|
|
var Lexer, baseFileName, 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');
|
|
|
|
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);
|
|
};
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
exports.VERSION = '1.6.0';
|
|
|
|
exports.helpers = require('./helpers');
|
|
|
|
baseFileName = function(fileName) {
|
|
var extension;
|
|
extension = path.extname(fileName);
|
|
return path.basename(fileName, extension);
|
|
};
|
|
|
|
exports.compile = compile = function(code, options) {
|
|
var answer, coffeeFile, currentColumn, currentLine, fragment, fragments, header, js, jsFile, merge, newLines, sourceMap, _j, _len1;
|
|
if (options == null) {
|
|
options = {};
|
|
}
|
|
merge = exports.helpers.merge;
|
|
try {
|
|
if (options.sourceMap) {
|
|
coffeeFile = path.basename(options.filename);
|
|
jsFile = baseFileName(options.filename) + ".js";
|
|
sourceMap = new sourcemap.SourceMap();
|
|
}
|
|
fragments = (parser.parse(lexer.tokenize(code, options))).compileToFragments(options);
|
|
currentLine = 0;
|
|
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
|
|
});
|
|
}
|
|
newLines = helpers.count(fragment.code, "\n");
|
|
currentLine += newLines;
|
|
currentColumn = fragment.code.length - (newLines ? fragment.code.lastIndexOf("\n") : 0);
|
|
}
|
|
js += fragment.code;
|
|
}
|
|
} catch (err) {
|
|
if (options.filename) {
|
|
err.message = "In " + options.filename + ", " + err.message;
|
|
}
|
|
throw err;
|
|
}
|
|
if (options.header) {
|
|
header = "Generated by CoffeeScript " + this.VERSION;
|
|
js = "// " + header + "\n" + js;
|
|
}
|
|
if (options.sourceMap) {
|
|
answer = {
|
|
js: js
|
|
};
|
|
if (sourceMap) {
|
|
answer.sourceMap = sourceMap;
|
|
answer.v3SourceMap = sourcemap.generateV3SourceMap(sourceMap, coffeeFile, jsFile);
|
|
}
|
|
return answer;
|
|
} else {
|
|
return js;
|
|
}
|
|
};
|
|
|
|
exports.tokens = function(code, options) {
|
|
return lexer.tokenize(code, options);
|
|
};
|
|
|
|
exports.nodes = function(source, options) {
|
|
if (typeof source === 'string') {
|
|
return parser.parse(lexer.tokenize(source, options));
|
|
} else {
|
|
return parser.parse(source);
|
|
}
|
|
};
|
|
|
|
exports.run = function(code, options) {
|
|
var mainModule;
|
|
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;
|
|
if (options == null) {
|
|
options = {};
|
|
}
|
|
if (!(code = code.trim())) {
|
|
return;
|
|
}
|
|
Script = vm.Script;
|
|
if (Script) {
|
|
if (options.sandbox != null) {
|
|
if (options.sandbox instanceof Script.createContext().constructor) {
|
|
sandbox = options.sandbox;
|
|
} else {
|
|
sandbox = Script.createContext();
|
|
_ref1 = options.sandbox;
|
|
for (k in _ref1) {
|
|
if (!__hasProp.call(_ref1, k)) continue;
|
|
v = _ref1[k];
|
|
sandbox[k] = v;
|
|
}
|
|
}
|
|
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox;
|
|
} else {
|
|
sandbox = global;
|
|
}
|
|
sandbox.__filename = options.filename || 'eval';
|
|
sandbox.__dirname = path.dirname(sandbox.__filename);
|
|
if (!(sandbox !== global || sandbox.module || sandbox.require)) {
|
|
Module = require('module');
|
|
sandbox.module = _module = new Module(options.modulename || 'eval');
|
|
sandbox.require = _require = function(path) {
|
|
return Module._load(path, _module, true);
|
|
};
|
|
_module.filename = sandbox.__filename;
|
|
_ref2 = Object.getOwnPropertyNames(require);
|
|
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
|
|
r = _ref2[_j];
|
|
if (r !== 'paths') {
|
|
_require[r] = require[r];
|
|
}
|
|
}
|
|
_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);
|
|
if (sandbox === global) {
|
|
return vm.runInThisContext(js);
|
|
} else {
|
|
return vm.runInContext(js, sandbox);
|
|
}
|
|
};
|
|
|
|
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 = '';
|
|
}
|
|
return tag;
|
|
},
|
|
setInput: function(tokens) {
|
|
this.tokens = tokens;
|
|
return this.pos = 0;
|
|
},
|
|
upcomingInput: function() {
|
|
return "";
|
|
}
|
|
};
|
|
|
|
parser.yy = require('./nodes');
|
|
|
|
}).call(this);
|