2010-07-24 11:31:43 -04:00
|
|
|
(function() {
|
2010-12-03 18:07:36 -05:00
|
|
|
var Lexer, RESERVED, compile, fs, lexer, parser, path, _ref;
|
2010-10-24 14:19:47 -04:00
|
|
|
fs = require('fs');
|
2010-09-21 05:19:49 -04:00
|
|
|
path = require('path');
|
2010-12-03 18:07:36 -05:00
|
|
|
_ref = require('./lexer'), Lexer = _ref.Lexer, RESERVED = _ref.RESERVED;
|
2010-09-28 16:47:12 -04:00
|
|
|
parser = require('./parser').parser;
|
2010-09-21 05:19:49 -04:00
|
|
|
if (require.extensions) {
|
|
|
|
require.extensions['.coffee'] = function(module, filename) {
|
|
|
|
var content;
|
|
|
|
content = compile(fs.readFileSync(filename, 'utf8'));
|
2010-09-22 19:55:05 -04:00
|
|
|
return module._compile(content, filename);
|
2010-09-21 05:19:49 -04:00
|
|
|
};
|
|
|
|
} else if (require.registerExtension) {
|
|
|
|
require.registerExtension('.coffee', function(content) {
|
|
|
|
return compile(content);
|
|
|
|
});
|
2010-02-13 15:25:04 -05:00
|
|
|
}
|
2011-01-15 10:28:26 -05:00
|
|
|
exports.VERSION = '1.1-pre';
|
2010-12-03 18:07:36 -05:00
|
|
|
exports.RESERVED = RESERVED;
|
2010-10-11 10:42:13 -04:00
|
|
|
exports.helpers = require('./helpers');
|
2010-10-20 13:29:06 -04:00
|
|
|
exports.compile = compile = function(code, options) {
|
2010-11-21 21:12:59 -05:00
|
|
|
if (options == null) {
|
|
|
|
options = {};
|
|
|
|
}
|
2010-03-07 22:08:24 -05:00
|
|
|
try {
|
|
|
|
return (parser.parse(lexer.tokenize(code))).compile(options);
|
|
|
|
} catch (err) {
|
2011-01-15 10:46:53 -05:00
|
|
|
if (options.filename) {
|
|
|
|
err.message = "In " + options.filename + ", " + err.message;
|
2010-03-07 22:08:24 -05:00
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
2010-10-20 13:29:06 -04:00
|
|
|
};
|
2010-10-08 15:27:05 -04:00
|
|
|
exports.tokens = function(code, options) {
|
|
|
|
return lexer.tokenize(code, options);
|
2010-03-06 20:30:40 -05:00
|
|
|
};
|
2010-11-17 11:34:23 -05:00
|
|
|
exports.nodes = function(source, options) {
|
|
|
|
if (typeof source === 'string') {
|
2010-11-20 16:25:22 -05:00
|
|
|
return parser.parse(lexer.tokenize(source, options));
|
2010-11-17 11:34:23 -05:00
|
|
|
} else {
|
|
|
|
return parser.parse(source);
|
|
|
|
}
|
2010-03-06 20:30:40 -05:00
|
|
|
};
|
2010-09-04 06:39:01 -04:00
|
|
|
exports.run = function(code, options) {
|
2010-09-22 19:55:05 -04:00
|
|
|
var root;
|
2010-09-20 23:32:37 -04:00
|
|
|
root = module;
|
|
|
|
while (root.parent) {
|
|
|
|
root = root.parent;
|
|
|
|
}
|
2011-01-15 10:46:53 -05:00
|
|
|
root.filename = options.filename ? fs.realpathSync(options.filename) : '.';
|
2010-09-20 23:32:37 -04:00
|
|
|
if (root.moduleCache) {
|
|
|
|
root.moduleCache = {};
|
|
|
|
}
|
2010-11-08 23:07:51 -05:00
|
|
|
if (path.extname(root.filename) !== '.coffee' || require.extensions) {
|
|
|
|
return root._compile(compile(code, options), root.filename);
|
|
|
|
} else {
|
|
|
|
return root._compile(code, root.filename);
|
|
|
|
}
|
2010-09-04 06:39:01 -04:00
|
|
|
};
|
2010-09-21 01:36:23 -04:00
|
|
|
exports.eval = function(code, options) {
|
|
|
|
var __dirname, __filename;
|
2011-01-15 10:46:53 -05:00
|
|
|
__filename = module.filename = options.filename;
|
2010-09-21 01:36:23 -04:00
|
|
|
__dirname = path.dirname(__filename);
|
2010-11-04 22:53:07 -04:00
|
|
|
return eval(compile(code, options));
|
2010-09-21 01:36:23 -04:00
|
|
|
};
|
2010-09-25 04:39:19 -04:00
|
|
|
lexer = new Lexer;
|
2010-02-11 01:57:33 -05:00
|
|
|
parser.lexer = {
|
2010-05-14 23:40:04 -04:00
|
|
|
lex: function() {
|
2010-11-02 00:05:06 -04:00
|
|
|
var tag, _ref;
|
|
|
|
_ref = this.tokens[this.pos++] || [''], tag = _ref[0], this.yytext = _ref[1], this.yylineno = _ref[2];
|
|
|
|
return tag;
|
2010-02-11 01:57:33 -05:00
|
|
|
},
|
2010-11-20 20:39:35 -05:00
|
|
|
setInput: function(tokens) {
|
|
|
|
this.tokens = tokens;
|
2010-10-20 18:19:08 -04:00
|
|
|
return this.pos = 0;
|
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-09-21 03:50:32 -04:00
|
|
|
parser.yy = require('./nodes');
|
2010-09-21 03:53:58 -04:00
|
|
|
}).call(this);
|