jashkenas--coffeescript/lib/coffee-script.js

102 lines
2.9 KiB
JavaScript
Raw Normal View History

(function() {
2010-08-18 00:35:52 +00:00
var Lexer, compile, grind, grindRemote, helpers, lexer, parser, path, processScripts;
if (typeof process !== "undefined" && process !== null) {
path = require('path');
Lexer = require('./lexer').Lexer;
parser = require('./parser').parser;
helpers = require('./helpers').helpers;
helpers.extend(global, require('./nodes'));
2010-08-11 04:40:15 +00:00
if (require.registerExtension) {
require.registerExtension('.coffee', function(content) {
return compile(content);
});
}
} else {
this.exports = (this.CoffeeScript = {});
2010-03-10 14:47:02 +00:00
Lexer = this.Lexer;
parser = this.parser;
2010-03-17 03:18:54 +00:00
helpers = this.helpers;
}
2010-08-24 02:08:33 +00:00
exports.VERSION = '0.9.2';
lexer = new Lexer();
exports.compile = (compile = function(code, options) {
options || (options = {});
try {
return (parser.parse(lexer.tokenize(code))).compile(options);
} catch (err) {
if (options.fileName) {
err.message = ("In " + (options.fileName) + ", " + (err.message));
}
throw err;
}
});
exports.tokens = function(code) {
return lexer.tokenize(code);
};
exports.nodes = function(code) {
return parser.parse(lexer.tokenize(code));
};
exports.run = (function(code, options) {
var __dirname, __filename;
module.filename = (__filename = options.fileName);
__dirname = path.dirname(__filename);
return eval(exports.compile(code, options));
});
parser.lexer = {
lex: function() {
var token;
token = this.tokens[this.pos] || [""];
this.pos += 1;
this.yylineno = token[2];
this.yytext = token[1];
return token[0];
},
setInput: function(tokens) {
this.tokens = tokens;
return (this.pos = 0);
},
upcomingInput: function() {
return "";
}
};
2010-08-18 00:35:52 +00:00
if ((typeof document === "undefined" || document === null) ? undefined : document.getElementsByTagName) {
grind = function(coffee) {
return setTimeout(exports.compile(coffee));
2010-08-18 00:35:52 +00:00
};
grindRemote = function(url) {
var xhr;
xhr = new (window.ActiveXObject || XMLHttpRequest)('Microsoft.XMLHTTP');
xhr.open('GET', url, true);
if ('overrideMimeType' in xhr) {
xhr.overrideMimeType('text/plain');
}
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
return grind(xhr.responseText);
}
};
return xhr.send(null);
};
processScripts = function() {
2010-08-18 00:35:52 +00:00
var _a, _b, _c, script;
_b = document.getElementsByTagName('script');
for (_a = 0, _c = _b.length; _a < _c; _a++) {
script = _b[_a];
if (script.type === 'text/coffeescript') {
if (script.src) {
grindRemote(script.src);
} else {
grind(script.innerHTML);
}
}
}
2010-08-18 00:35:52 +00:00
return null;
};
if (window.addEventListener) {
2010-08-18 00:35:52 +00:00
addEventListener('DOMContentLoaded', processScripts, false);
} else {
attachEvent('onload', processScripts);
}
}
})();