mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
fe45f1bf35
Conflicts: lib/coffee-script/grammar.js lib/coffee-script/rewriter.js src/grammar.coffee src/rewriter.coffee
177 lines
5.1 KiB
JavaScript
177 lines
5.1 KiB
JavaScript
(function() {
|
|
var Lexer, compile, ext, extensions, fs, lexer, loadFile, parser, path, vm, _i, _len,
|
|
__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; },
|
|
__hasProp = {}.hasOwnProperty;
|
|
|
|
fs = require('fs');
|
|
|
|
path = require('path');
|
|
|
|
Lexer = require('./lexer').Lexer;
|
|
|
|
parser = require('./parser').parser;
|
|
|
|
vm = require('vm');
|
|
|
|
extensions = ['.coffee', '.litcoffee'];
|
|
|
|
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
|
|
}), filename);
|
|
};
|
|
|
|
if (require.extensions) {
|
|
for (_i = 0, _len = extensions.length; _i < _len; _i++) {
|
|
ext = extensions[_i];
|
|
require.extensions[ext] = loadFile;
|
|
}
|
|
}
|
|
|
|
exports.VERSION = '1.5.0-pre';
|
|
|
|
exports.helpers = require('./helpers');
|
|
|
|
exports.compile = compile = function(code, options) {
|
|
var footer, js, merge;
|
|
if (options == null) {
|
|
options = {};
|
|
}
|
|
merge = exports.helpers.merge;
|
|
try {
|
|
js = (parser.parse(lexer.tokenize(code, options))).compile(options);
|
|
if (!options.header) {
|
|
return js;
|
|
}
|
|
} catch (err) {
|
|
if (options.filename) {
|
|
err.message = "In " + options.filename + ", " + err.message;
|
|
}
|
|
throw err;
|
|
}
|
|
footer = "Generated by CoffeeScript " + this.VERSION;
|
|
return "" + js + "\n// " + footer + "\n";
|
|
};
|
|
|
|
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, _ref;
|
|
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 ((_ref = path.extname(mainModule.filename), __indexOf.call(extensions, _ref) < 0) || 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, _ref, _ref1, _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();
|
|
_ref = options.sandbox;
|
|
for (k in _ref) {
|
|
if (!__hasProp.call(_ref, k)) continue;
|
|
v = _ref[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;
|
|
_ref1 = Object.getOwnPropertyNames(require);
|
|
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
|
|
r = _ref1[_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);
|
|
|
|
// Generated by CoffeeScript 1.5.0-pre
|