generator: add keyword method, remove __ident method and implement better position tracking
This commit is contained in:
parent
bbffde374e
commit
e77382582f
@ -20,6 +20,9 @@ function CodeGenerator(code, ast, opts) {
|
|||||||
this.ast = ast;
|
this.ast = ast;
|
||||||
this.buf = "";
|
this.buf = "";
|
||||||
|
|
||||||
|
this.line = 1;
|
||||||
|
this.column = 0;
|
||||||
|
|
||||||
if (opts.sourceMap) {
|
if (opts.sourceMap) {
|
||||||
this.map = new sourceMap.SourceMapGenerator({
|
this.map = new sourceMap.SourceMapGenerator({
|
||||||
file: opts.sourceMapName
|
file: opts.sourceMapName
|
||||||
@ -38,16 +41,11 @@ CodeGenerator.prototype.mark = function (node, locType) {
|
|||||||
var map = this.map;
|
var map = this.map;
|
||||||
if (!map) return; // no source map
|
if (!map) return; // no source map
|
||||||
|
|
||||||
var lines = this.buf.split("\n");
|
|
||||||
|
|
||||||
var line = lines.length;
|
|
||||||
var col = _.last(lines).length;
|
|
||||||
|
|
||||||
map.addMapping({
|
map.addMapping({
|
||||||
source: this.opts.sourceFileName,
|
source: this.opts.sourceFileName,
|
||||||
generated: {
|
generated: {
|
||||||
line: line,
|
line: this.line,
|
||||||
column: col
|
column: this.column
|
||||||
},
|
},
|
||||||
original: loc[locType]
|
original: loc[locType]
|
||||||
});
|
});
|
||||||
@ -55,10 +53,12 @@ CodeGenerator.prototype.mark = function (node, locType) {
|
|||||||
|
|
||||||
CodeGenerator.prototype.newline = function () {
|
CodeGenerator.prototype.newline = function () {
|
||||||
this.buf += "\n";
|
this.buf += "\n";
|
||||||
|
this.line++;
|
||||||
};
|
};
|
||||||
|
|
||||||
CodeGenerator.prototype.semicolon = function () {
|
CodeGenerator.prototype.semicolon = function () {
|
||||||
this.buf += ";";
|
this.buf += ";";
|
||||||
|
this.column++;
|
||||||
};
|
};
|
||||||
|
|
||||||
CodeGenerator.prototype.push = function (str) {
|
CodeGenerator.prototype.push = function (str) {
|
||||||
@ -73,6 +73,17 @@ CodeGenerator.prototype.push = function (str) {
|
|||||||
if (_.last(this.buf) === "\n") str = indent + 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;
|
this.buf += str;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -88,12 +99,6 @@ CodeGenerator.prototype.dedent = function () {
|
|||||||
this._indent--;
|
this._indent--;
|
||||||
};
|
};
|
||||||
|
|
||||||
CodeGenerator.prototype.__indent = function (str) {
|
|
||||||
return str.split("\n").map(function (line) {
|
|
||||||
return " " + line;
|
|
||||||
}).join("\n");
|
|
||||||
};
|
|
||||||
|
|
||||||
CodeGenerator.prototype.generate = function () {
|
CodeGenerator.prototype.generate = function () {
|
||||||
var ast = this.ast;
|
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) {
|
CodeGenerator.prototype.generateComment = function (comment) {
|
||||||
var val = comment.value;
|
var val = comment.value;
|
||||||
if (comment.type === "Line") {
|
if (comment.type === "Line") {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user