2010-01-29 22:51:51 -05:00
|
|
|
(function(){
|
2010-06-12 19:05:13 -04:00
|
|
|
var Lexer, compile, helpers, lexer, parser, path, processScripts;
|
2010-06-27 23:55:18 -04:00
|
|
|
if (typeof process !== "undefined" && process !== null) {
|
2010-02-13 15:25:04 -05:00
|
|
|
path = require('path');
|
2010-03-15 20:39:46 -07:00
|
|
|
Lexer = require('./lexer').Lexer;
|
|
|
|
parser = require('./parser').parser;
|
2010-03-15 22:53:25 -07:00
|
|
|
helpers = require('./helpers').helpers;
|
|
|
|
helpers.extend(global, require('./nodes'));
|
2010-03-15 23:10:14 -07:00
|
|
|
require.registerExtension ? require.registerExtension('.coffee', function(content) {
|
|
|
|
return compile(content);
|
|
|
|
}) : null;
|
2010-02-13 15:25:04 -05:00
|
|
|
} else {
|
2010-02-17 23:25:17 -05:00
|
|
|
this.exports = (this.CoffeeScript = {});
|
2010-03-10 09:47:02 -05:00
|
|
|
Lexer = this.Lexer;
|
|
|
|
parser = this.parser;
|
2010-03-16 23:18:54 -04:00
|
|
|
helpers = this.helpers;
|
2010-02-13 15:25:04 -05:00
|
|
|
}
|
2010-07-12 08:17:26 -04:00
|
|
|
exports.VERSION = '0.7.2';
|
2010-03-09 23:04:16 -05:00
|
|
|
lexer = new Lexer();
|
2010-05-14 23:40:04 -04:00
|
|
|
exports.compile = (compile = function(code, options) {
|
2010-03-09 23:04:16 -05:00
|
|
|
options = options || {};
|
2010-03-07 22:08:24 -05:00
|
|
|
try {
|
|
|
|
return (parser.parse(lexer.tokenize(code))).compile(options);
|
|
|
|
} catch (err) {
|
|
|
|
if (options.source) {
|
2010-04-11 16:57:53 -04:00
|
|
|
err.message = ("In " + options.source + ", " + err.message);
|
2010-03-07 22:08:24 -05:00
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
2010-03-15 23:03:30 -07:00
|
|
|
});
|
2010-05-14 23:40:04 -04:00
|
|
|
exports.tokens = function(code) {
|
2010-03-06 20:30:40 -05:00
|
|
|
return lexer.tokenize(code);
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
exports.nodes = function(code) {
|
2010-03-06 20:30:40 -05:00
|
|
|
return parser.parse(lexer.tokenize(code));
|
|
|
|
};
|
2010-03-19 23:15:42 -04:00
|
|
|
exports.run = (function(code, options) {
|
2010-03-07 21:49:08 -05:00
|
|
|
var __dirname, __filename;
|
2010-03-07 22:17:45 -05:00
|
|
|
module.filename = (__filename = options.source);
|
2010-03-31 21:30:14 -04:00
|
|
|
__dirname = path.dirname(__filename);
|
|
|
|
return eval(exports.compile(code, options));
|
2010-03-19 23:15:42 -04:00
|
|
|
});
|
2010-02-11 01:57:33 -05:00
|
|
|
parser.lexer = {
|
2010-05-14 23:40:04 -04:00
|
|
|
lex: function() {
|
2010-02-11 01:57:33 -05:00
|
|
|
var token;
|
|
|
|
token = this.tokens[this.pos] || [""];
|
|
|
|
this.pos += 1;
|
|
|
|
this.yylineno = token[2];
|
|
|
|
this.yytext = token[1];
|
|
|
|
return token[0];
|
|
|
|
},
|
2010-05-14 23:40:04 -04:00
|
|
|
setInput: function(tokens) {
|
2010-02-11 01:57:33 -05:00
|
|
|
this.tokens = tokens;
|
2010-03-21 22:17:58 +11:00
|
|
|
this.pos = 0;
|
|
|
|
return this.pos;
|
2010-02-11 01:57:33 -05:00
|
|
|
},
|
2010-05-14 23:40:04 -04:00
|
|
|
upcomingInput: function() {
|
2010-02-11 01:57:33 -05:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
};
|
2010-02-24 23:57:39 -05:00
|
|
|
if ((typeof document !== "undefined" && document !== null) && document.getElementsByTagName) {
|
2010-06-12 19:05:13 -04:00
|
|
|
processScripts = function() {
|
2010-02-25 23:39:14 -05:00
|
|
|
var _a, _b, _c, _d, tag;
|
2010-03-27 16:04:47 -04:00
|
|
|
_a = []; _c = document.getElementsByTagName('script');
|
|
|
|
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
|
|
|
tag = _c[_b];
|
2010-03-21 22:17:58 +11:00
|
|
|
tag.type === 'text/coffeescript' ? _a.push(eval(exports.compile(tag.innerHTML))) : null;
|
2010-02-25 06:26:27 -05:00
|
|
|
}
|
|
|
|
return _a;
|
|
|
|
};
|
|
|
|
if (window.addEventListener) {
|
2010-06-12 19:05:13 -04:00
|
|
|
window.addEventListener('load', processScripts, false);
|
2010-02-25 06:26:27 -05:00
|
|
|
} else if (window.attachEvent) {
|
2010-06-12 19:05:13 -04:00
|
|
|
window.attachEvent('onload', processScripts);
|
2010-02-24 19:57:29 -05:00
|
|
|
}
|
2010-02-24 23:57:39 -05:00
|
|
|
}
|
2010-02-24 18:56:32 -05:00
|
|
|
})();
|