generator: add keyword method, remove __ident method and implement better position tracking

This commit is contained in:
Sebastian McKenzie 2014-11-01 15:56:10 +11:00
parent bbffde374e
commit e77382582f

View File

@ -20,6 +20,9 @@ function CodeGenerator(code, ast, opts) {
this.ast = ast;
this.buf = "";
this.line = 1;
this.column = 0;
if (opts.sourceMap) {
this.map = new sourceMap.SourceMapGenerator({
file: opts.sourceMapName
@ -38,16 +41,11 @@ CodeGenerator.prototype.mark = function (node, locType) {
var map = this.map;
if (!map) return; // no source map
var lines = this.buf.split("\n");
var line = lines.length;
var col = _.last(lines).length;
map.addMapping({
source: this.opts.sourceFileName,
generated: {
line: line,
column: col
line: this.line,
column: this.column
},
original: loc[locType]
});
@ -55,10 +53,12 @@ CodeGenerator.prototype.mark = function (node, locType) {
CodeGenerator.prototype.newline = function () {
this.buf += "\n";
this.line++;
};
CodeGenerator.prototype.semicolon = function () {
this.buf += ";";
this.column++;
};
CodeGenerator.prototype.push = function (str) {
@ -73,6 +73,17 @@ CodeGenerator.prototype.push = function (str) {
if (_.last(this.buf) === "\n") str = indent + str;
}
var self = this;
_.each(str, function (cha) {
if (cha === "\n") {
self.line++;
self.column = 0;
} else {
self.column++;
}
});
this.buf += str;
};
@ -88,12 +99,6 @@ CodeGenerator.prototype.dedent = function () {
this._indent--;
};
CodeGenerator.prototype.__indent = function (str) {
return str.split("\n").map(function (line) {
return " " + line;
}).join("\n");
};
CodeGenerator.prototype.generate = function () {
var ast = this.ast;
@ -142,6 +147,11 @@ CodeGenerator.prototype.print = function (node, parent) {
}
};
CodeGenerator.prototype.keyword = function (name) {
this.push(name);
this.push(" ");
};
CodeGenerator.prototype.generateComment = function (comment) {
var val = comment.value;
if (comment.type === "Line") {