first working version of literate coffeescript

This commit is contained in:
Jeremy Ashkenas
2012-09-25 19:15:40 -05:00
parent bb194dc6c6
commit 82fadea1ed
4 changed files with 53 additions and 22 deletions

View File

@@ -170,9 +170,9 @@
};
CoffeeScript.emit('compile', task);
if (o.tokens) {
return printTokens(CoffeeScript.tokens(t.input));
return printTokens(CoffeeScript.tokens(t.input, t.options));
} else if (o.nodes) {
return printLine(CoffeeScript.nodes(t.input).toString().trim());
return printLine(CoffeeScript.nodes(t.input, t.options).toString().trim());
} else if (o.run) {
return CoffeeScript.run(t.input, t.options);
} else if (o.join && t.file !== o.join) {
@@ -472,9 +472,11 @@
};
compileOptions = function(filename) {
var literate;
literate = path.extname(filename) === '.litcoffee';
return {
filename: filename,
extension: path.extname(filename),
literate: literate,
bare: opts.bare,
header: opts.compile
};

View File

@@ -16,12 +16,8 @@
if (opts == null) {
opts = {};
}
this.literate = opts.extension === '.litcoffee';
if (WHITESPACE.test(code)) {
code = "\n" + code;
}
code = code.replace(/\r/g, '').replace(TRAILING_SPACES, '');
this.code = code;
this.literate = opts.literate;
code = this.clean(code);
this.line = opts.line || 0;
this.indent = 0;
this.indebt = 0;
@@ -31,7 +27,7 @@
this.tokens = [];
i = 0;
while (this.chunk = code.slice(i)) {
i += this.commentToken() || this.identifierToken() || this.whitespaceToken() || this.lineToken() || this.heredocToken() || this.stringToken() || this.numberToken() || this.regexToken() || this.jsToken() || this.literalToken();
i += this.identifierToken() || this.commentToken() || this.whitespaceToken() || this.lineToken() || this.heredocToken() || this.stringToken() || this.numberToken() || this.regexToken() || this.jsToken() || this.literalToken();
}
this.closeIndentation();
if (tag = this.ends.pop()) {
@@ -43,6 +39,30 @@
return (new Rewriter).rewrite(this.tokens);
};
Lexer.prototype.clean = function(code) {
var line, lines;
if (WHITESPACE.test(code)) {
code = "\n" + code;
}
code = code.replace(/\r/g, '').replace(TRAILING_SPACES, '');
if (this.literate) {
lines = (function() {
var _i, _len, _ref2, _results;
_ref2 = code.split('\n');
_results = [];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
line = _ref2[_i];
if (line.substr(0, 4) === ' ') {
_results.push(line.substr(4));
}
}
return _results;
})();
code = lines.join('\n');
}
return code;
};
Lexer.prototype.identifierToken = function() {
var colon, forcedIdentifier, id, input, match, prev, tag, _ref2, _ref3;
if (!(match = IDENTIFIER.exec(this.chunk))) {