2010-07-24 15:31:43 +00:00
|
|
|
(function() {
|
2010-08-18 00:35:52 +00:00
|
|
|
var Lexer, compile, grind, grindRemote, helpers, lexer, parser, path, processScripts;
|
2010-08-14 21:52:37 +00:00
|
|
|
if (typeof process !== "undefined" && process !== null) {
|
2010-02-13 20:25:04 +00:00
|
|
|
path = require('path');
|
2010-03-16 03:39:46 +00:00
|
|
|
Lexer = require('./lexer').Lexer;
|
|
|
|
parser = require('./parser').parser;
|
2010-03-16 05:53:25 +00:00
|
|
|
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);
|
|
|
|
});
|
2010-08-14 15:42:19 +00:00
|
|
|
}
|
2010-02-13 20:25:04 +00:00
|
|
|
} else {
|
2010-02-18 04:25:17 +00:00
|
|
|
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-02-13 20:25:04 +00:00
|
|
|
}
|
2010-08-24 02:08:33 +00:00
|
|
|
exports.VERSION = '0.9.2';
|
2010-03-10 04:04:16 +00:00
|
|
|
lexer = new Lexer();
|
2010-05-15 03:40:04 +00:00
|
|
|
exports.compile = (compile = function(code, options) {
|
2010-08-14 22:02:07 +00:00
|
|
|
options || (options = {});
|
2010-03-08 03:08:24 +00:00
|
|
|
try {
|
|
|
|
return (parser.parse(lexer.tokenize(code))).compile(options);
|
|
|
|
} catch (err) {
|
2010-07-31 00:37:12 +00:00
|
|
|
if (options.fileName) {
|
2010-08-07 12:02:16 +00:00
|
|
|
err.message = ("In " + (options.fileName) + ", " + (err.message));
|
2010-03-08 03:08:24 +00:00
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
2010-03-16 06:03:30 +00:00
|
|
|
});
|
2010-05-15 03:40:04 +00:00
|
|
|
exports.tokens = function(code) {
|
2010-03-07 01:30:40 +00:00
|
|
|
return lexer.tokenize(code);
|
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
exports.nodes = function(code) {
|
2010-03-07 01:30:40 +00:00
|
|
|
return parser.parse(lexer.tokenize(code));
|
|
|
|
};
|
2010-03-20 03:15:42 +00:00
|
|
|
exports.run = (function(code, options) {
|
2010-03-08 02:49:08 +00:00
|
|
|
var __dirname, __filename;
|
2010-07-31 00:37:12 +00:00
|
|
|
module.filename = (__filename = options.fileName);
|
2010-04-01 01:30:14 +00:00
|
|
|
__dirname = path.dirname(__filename);
|
|
|
|
return eval(exports.compile(code, options));
|
2010-03-20 03:15:42 +00:00
|
|
|
});
|
2010-02-11 06:57:33 +00:00
|
|
|
parser.lexer = {
|
2010-05-15 03:40:04 +00:00
|
|
|
lex: function() {
|
2010-02-11 06:57:33 +00:00
|
|
|
var token;
|
|
|
|
token = this.tokens[this.pos] || [""];
|
|
|
|
this.pos += 1;
|
|
|
|
this.yylineno = token[2];
|
|
|
|
this.yytext = token[1];
|
|
|
|
return token[0];
|
|
|
|
},
|
2010-05-15 03:40:04 +00:00
|
|
|
setInput: function(tokens) {
|
2010-02-11 06:57:33 +00:00
|
|
|
this.tokens = tokens;
|
2010-07-24 19:27:11 +00:00
|
|
|
return (this.pos = 0);
|
2010-02-11 06:57:33 +00:00
|
|
|
},
|
2010-05-15 03:40:04 +00:00
|
|
|
upcomingInput: function() {
|
2010-02-11 06:57:33 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
};
|
2010-08-18 00:35:52 +00:00
|
|
|
if ((typeof document === "undefined" || document === null) ? undefined : document.getElementsByTagName) {
|
|
|
|
grind = function(coffee) {
|
2010-08-18 01:29:13 +00:00
|
|
|
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);
|
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
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-14 15:42:19 +00:00
|
|
|
}
|
2010-02-25 11:26:27 +00:00
|
|
|
}
|
2010-08-18 00:35:52 +00:00
|
|
|
return null;
|
2010-02-25 11:26:27 +00:00
|
|
|
};
|
|
|
|
if (window.addEventListener) {
|
2010-08-18 00:35:52 +00:00
|
|
|
addEventListener('DOMContentLoaded', processScripts, false);
|
|
|
|
} else {
|
|
|
|
attachEvent('onload', processScripts);
|
2010-02-25 00:57:29 +00:00
|
|
|
}
|
2010-02-25 04:57:39 +00:00
|
|
|
}
|
2010-02-24 23:56:32 +00:00
|
|
|
})();
|