2010-01-29 22:51:51 -05:00
|
|
|
(function(){
|
2010-02-25 06:26:27 -05:00
|
|
|
var lexer, parser, path, process_scripts;
|
2010-02-13 16:23:03 -05:00
|
|
|
// Set up for both the browser and the server.
|
2010-02-17 23:22:05 -05:00
|
|
|
if ((typeof process !== "undefined" && process !== null)) {
|
2010-02-17 00:50:08 -05:00
|
|
|
process.mixin(require('nodes'));
|
2010-02-13 15:25:04 -05:00
|
|
|
path = require('path');
|
2010-02-17 00:50:08 -05:00
|
|
|
lexer = new (require('lexer').Lexer)();
|
|
|
|
parser = require('parser').parser;
|
2010-02-13 15:25:04 -05:00
|
|
|
} else {
|
|
|
|
lexer = new Lexer();
|
2010-02-17 23:22:05 -05:00
|
|
|
parser = exports.parser;
|
2010-02-17 23:25:17 -05:00
|
|
|
this.exports = (this.CoffeeScript = {});
|
2010-02-13 15:25:04 -05:00
|
|
|
}
|
2010-02-11 01:57:33 -05:00
|
|
|
// Thin wrapper for Jison compatibility around the real lexer.
|
|
|
|
parser.lexer = {
|
|
|
|
lex: function lex() {
|
|
|
|
var token;
|
|
|
|
token = this.tokens[this.pos] || [""];
|
|
|
|
this.pos += 1;
|
|
|
|
this.yylineno = token[2];
|
|
|
|
this.yytext = token[1];
|
|
|
|
return token[0];
|
|
|
|
},
|
|
|
|
setInput: function setInput(tokens) {
|
|
|
|
this.tokens = tokens;
|
|
|
|
return this.pos = 0;
|
|
|
|
},
|
|
|
|
upcomingInput: function upcomingInput() {
|
|
|
|
return "";
|
|
|
|
},
|
|
|
|
showPosition: function showPosition() {
|
|
|
|
return this.pos;
|
|
|
|
}
|
|
|
|
};
|
2010-02-25 00:26:59 -05:00
|
|
|
exports.VERSION = '0.5.2';
|
2010-02-11 01:57:33 -05:00
|
|
|
// Compile CoffeeScript to JavaScript, using the Coffee/Jison compiler.
|
2010-02-13 10:27:18 -05:00
|
|
|
exports.compile = function compile(code, options) {
|
|
|
|
return (parser.parse(lexer.tokenize(code))).compile(options);
|
2010-02-11 02:39:57 -05:00
|
|
|
};
|
|
|
|
// Just the tokens.
|
2010-02-25 18:42:35 -05:00
|
|
|
exports.tokens = function tokens(code) {
|
2010-02-11 02:39:57 -05:00
|
|
|
return lexer.tokenize(code);
|
2010-02-11 01:57:33 -05:00
|
|
|
};
|
2010-02-11 23:11:05 -05:00
|
|
|
// Just the nodes.
|
2010-02-25 18:42:35 -05:00
|
|
|
exports.nodes = function nodes(code) {
|
2010-02-11 23:11:05 -05:00
|
|
|
return parser.parse(lexer.tokenize(code));
|
|
|
|
};
|
2010-02-24 19:57:29 -05:00
|
|
|
// Activate CoffeeScript in the browser by having it compile and eval
|
|
|
|
// all script tags with a content-type of text/coffeescript.
|
2010-02-24 23:57:39 -05:00
|
|
|
if ((typeof document !== "undefined" && document !== null) && document.getElementsByTagName) {
|
2010-02-25 06:26:27 -05:00
|
|
|
process_scripts = function process_scripts() {
|
2010-02-25 23:39:14 -05:00
|
|
|
var _a, _b, _c, _d, tag;
|
2010-02-25 06:26:27 -05:00
|
|
|
_a = []; _b = document.getElementsByTagName('script');
|
2010-02-25 23:39:14 -05:00
|
|
|
for (_c = 0, _d = _b.length; _c < _d; _c++) {
|
2010-02-25 06:26:27 -05:00
|
|
|
tag = _b[_c];
|
|
|
|
if (tag.type === 'text/coffeescript') {
|
|
|
|
_a.push(eval(exports.compile(tag.innerHTML)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _a;
|
|
|
|
};
|
|
|
|
if (window.addEventListener) {
|
|
|
|
window.addEventListener('load', process_scripts, false);
|
|
|
|
} else if (window.attachEvent) {
|
|
|
|
window.attachEvent('onload', process_scripts);
|
2010-02-24 19:57:29 -05:00
|
|
|
}
|
2010-02-24 23:57:39 -05:00
|
|
|
}
|
2010-02-24 18:56:32 -05:00
|
|
|
})();
|